blob: c5d9ddf328ef7507a797116fdd8d6168d17b6e2f [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000080 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#include "vim.h"
307
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000308#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000309
Bram Moolenaar4770d092006-01-12 23:22:24 +0000310#ifndef UNIX /* it's in os_unix.h for Unix */
311# include <time.h> /* for time_t */
312#endif
313
Bram Moolenaar98f52502015-02-10 20:03:45 +0100314#define MAXWLEN 254 /* Assume max. word len is this many bytes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000315 Some places assume a word length fits in a
Bram Moolenaar98f52502015-02-10 20:03:45 +0100316 byte, thus it can't be above 255.
317 Must be >= PFD_NOTSPECIAL. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000318
Bram Moolenaare52325c2005-08-22 22:54:29 +0000319/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000320 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100321#if VIM_SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000322typedef int idx_T;
323#else
324typedef long idx_T;
325#endif
326
Bram Moolenaar56f78042010-12-08 17:09:32 +0100327#ifdef VMS
328# define SPL_FNAME_TMPL "%s_%s.spl"
329# define SPL_FNAME_ADD "_add."
330# define SPL_FNAME_ASCII "_ascii."
331#else
332# define SPL_FNAME_TMPL "%s.%s.spl"
333# define SPL_FNAME_ADD ".add."
334# define SPL_FNAME_ASCII ".ascii."
335#endif
336
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000337/* Flags used for a word. Only the lowest byte can be used, the region byte
338 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000339#define WF_REGION 0x01 /* region byte follows */
340#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
341#define WF_ALLCAP 0x04 /* word must be all capitals */
342#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000343#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000344#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000345#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000346#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000347
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000348/* for <flags2>, shifted up one byte to be used in wn_flags */
349#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000350#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000351#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000352#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000353#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
354#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000355
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000356/* only used for su_badflags */
357#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
358
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000359#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000360
Bram Moolenaar53805d12005-08-01 07:08:33 +0000361/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000362#define WFP_RARE 0x01 /* rare prefix */
363#define WFP_NC 0x02 /* prefix is not combining */
364#define WFP_UP 0x04 /* to-upper prefix */
365#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
366#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000367
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000368/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
369 * byte) and prefcondnr (two bytes). */
370#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
371#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
372#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
373#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
374 * COMPOUNDPERMITFLAG */
375#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
376 * COMPOUNDFORBIDFLAG */
377
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000378
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000379/* flags for <compoptions> */
380#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
381#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
382#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
383#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
384
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000385/* Special byte values for <byte>. Some are only used in the tree for
386 * postponed prefixes, some only in the other trees. This is a bit messy... */
387#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000388 * postponed prefix: no <pflags> */
389#define BY_INDEX 1 /* child is shared, index follows */
390#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
391 * postponed prefix: <pflags> follows */
392#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
393 * follow; never used in prefix tree */
394#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000395
Bram Moolenaar4770d092006-01-12 23:22:24 +0000396/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
397 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398 * One replacement: from "ft_from" to "ft_to". */
399typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000400{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000401 char_u *ft_from;
402 char_u *ft_to;
403} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000404
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000405/* Info from "SAL" entries in ".aff" file used in sl_sal.
406 * The info is split for quick processing by spell_soundfold().
407 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
408typedef struct salitem_S
409{
410 char_u *sm_lead; /* leading letters */
411 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000412 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000413 char_u *sm_rules; /* rules like ^, $, priority */
414 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000415#ifdef FEAT_MBYTE
416 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000417 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000418 int *sm_to_w; /* wide character copy of "sm_to" */
419#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000420} salitem_T;
421
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000422#ifdef FEAT_MBYTE
423typedef int salfirst_T;
424#else
425typedef short salfirst_T;
426#endif
427
Bram Moolenaar5195e452005-08-19 20:32:47 +0000428/* Values for SP_*ERROR are negative, positive values are used by
429 * read_cnt_string(). */
430#define SP_TRUNCERROR -1 /* spell file truncated error */
431#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000432#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000433
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000434/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000435 * Structure used to store words and other info for one language, loaded from
436 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000437 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
438 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
439 *
440 * The "byts" array stores the possible bytes in each tree node, preceded by
441 * the number of possible bytes, sorted on byte value:
442 * <len> <byte1> <byte2> ...
443 * The "idxs" array stores the index of the child node corresponding to the
444 * byte in "byts".
445 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000446 * the flags, region mask and affixID for the word. There may be several
447 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000448 */
449typedef struct slang_S slang_T;
450struct slang_S
451{
452 slang_T *sl_next; /* next language */
453 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000454 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000455 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000456
Bram Moolenaar51485f02005-06-04 21:55:20 +0000457 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000458 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000459 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000460 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000461 char_u *sl_pbyts; /* prefix tree word bytes */
462 idx_T *sl_pidxs; /* prefix tree word indexes */
463
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000464 char_u *sl_info; /* infotext string or NULL */
465
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000466 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000467
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000468 char_u *sl_midword; /* MIDWORD string or NULL */
469
Bram Moolenaar4770d092006-01-12 23:22:24 +0000470 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
471
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000472 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000473 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000474 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000475 int sl_compoptions; /* COMP_* flags */
476 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000477 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000478 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000479 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000480 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000481 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000482 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000483 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
484 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000485
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000486 int sl_prefixcnt; /* number of items in "sl_prefprog" */
487 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000489 garray_T sl_rep; /* list of fromto_T entries from REP lines */
490 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
491 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000493 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000494 there is none */
495 int sl_followup; /* SAL followup */
496 int sl_collapse; /* SAL collapse_result */
497 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000498 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
499 * "sl_sal_first" maps chars, when has_mbyte
500 * "sl_sal" is a list of wide char lists. */
501 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
502 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000503 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000504
505 /* Info from the .sug file. Loaded on demand. */
506 time_t sl_sugtime; /* timestamp for .sug file */
507 char_u *sl_sbyts; /* soundfolded word bytes */
508 idx_T *sl_sidxs; /* soundfolded word indexes */
509 buf_T *sl_sugbuf; /* buffer with word number table */
510 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
511 load */
512
Bram Moolenaarea424162005-06-16 21:51:00 +0000513 int sl_has_map; /* TRUE if there is a MAP line */
514#ifdef FEAT_MBYTE
515 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
516 int sl_map_array[256]; /* MAP for first 256 chars */
517#else
518 char_u sl_map_array[256]; /* MAP for first 256 chars */
519#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000520 hashtab_T sl_sounddone; /* table with soundfolded words that have
521 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000522};
523
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000524/* First language that is loaded, start of the linked list of loaded
525 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000526static slang_T *first_lang = NULL;
527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000528/* Flags used in .spl file for soundsalike flags. */
529#define SAL_F0LLOWUP 1
530#define SAL_COLLAPSE 2
531#define SAL_REM_ACCENTS 4
532
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000533/*
534 * Structure used in "b_langp", filled from 'spelllang'.
535 */
536typedef struct langp_S
537{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000538 slang_T *lp_slang; /* info for this language */
539 slang_T *lp_sallang; /* language used for sound folding or NULL */
540 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000541 int lp_region; /* bitmask for region or REGION_ALL */
542} langp_T;
543
544#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
545
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000546#define REGION_ALL 0xff /* word valid in all regions */
547
Bram Moolenaar5195e452005-08-19 20:32:47 +0000548#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
549#define VIMSPELLMAGICL 8
550#define VIMSPELLVERSION 50
551
Bram Moolenaar4770d092006-01-12 23:22:24 +0000552#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
553#define VIMSUGMAGICL 6
554#define VIMSUGVERSION 1
555
Bram Moolenaar5195e452005-08-19 20:32:47 +0000556/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
557#define SN_REGION 0 /* <regionname> section */
558#define SN_CHARFLAGS 1 /* charflags section */
559#define SN_MIDWORD 2 /* <midword> section */
560#define SN_PREFCOND 3 /* <prefcond> section */
561#define SN_REP 4 /* REP items section */
562#define SN_SAL 5 /* SAL items section */
563#define SN_SOFO 6 /* soundfolding section */
564#define SN_MAP 7 /* MAP items section */
565#define SN_COMPOUND 8 /* compound words section */
566#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000567#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000568#define SN_SUGFILE 11 /* timestamp for .sug file */
569#define SN_REPSAL 12 /* REPSAL items section */
570#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000571#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000572#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000573#define SN_END 255 /* end of sections */
574
575#define SNF_REQUIRED 1 /* <sectionflags>: required section */
576
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000577/* Result values. Lower number is accepted over higher one. */
578#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000579#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000580#define SP_RARE 1
581#define SP_LOCAL 2
582#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000583
Bram Moolenaar7887d882005-07-01 22:33:52 +0000584/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000585static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000586
Bram Moolenaar4770d092006-01-12 23:22:24 +0000587typedef struct wordcount_S
588{
589 short_u wc_count; /* nr of times word was seen */
590 char_u wc_word[1]; /* word, actually longer */
591} wordcount_T;
592
593static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000594#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000595#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
596#define MAXWORDCOUNT 0xffff
597
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000598/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000599 * Information used when looking for suggestions.
600 */
601typedef struct suginfo_S
602{
603 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000604 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000605 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000606 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000607 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000608 char_u *su_badptr; /* start of bad word in line */
609 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000610 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
612 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000613 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000614 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000615 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000616} suginfo_T;
617
618/* One word suggestion. Used in "si_ga". */
619typedef struct suggest_S
620{
621 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000622 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000623 int st_orglen; /* length of replaced text */
624 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625 int st_altscore; /* used when st_score compares equal */
626 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000627 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000628 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000629} suggest_T;
630
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000631#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000632
Bram Moolenaar4770d092006-01-12 23:22:24 +0000633/* TRUE if a word appears in the list of banned words. */
634#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
635
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000636/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000637 * what is displayed, because when rescore_suggestions() is called the score
638 * may change and wrong suggestions may be removed later. */
639#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000640
641/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
642 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000643#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644
645/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000646#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000647#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000649#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000651#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000652#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000653#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654#define SCORE_SUBST 93 /* substitute a character */
655#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000656#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000657#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000658#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000659#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000660#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000661#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000662#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000663#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000665#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000666#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
667 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000668
Bram Moolenaar4770d092006-01-12 23:22:24 +0000669#define SCORE_COMMON1 30 /* subtracted for words seen before */
670#define SCORE_COMMON2 40 /* subtracted for words often seen */
671#define SCORE_COMMON3 50 /* subtracted for words very often seen */
672#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
673#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
674
675/* When trying changed soundfold words it becomes slow when trying more than
676 * two changes. With less then two changes it's slightly faster but we miss a
677 * few good suggestions. In rare cases we need to try three of four changes.
678 */
679#define SCORE_SFMAX1 200 /* maximum score for first try */
680#define SCORE_SFMAX2 300 /* maximum score for second try */
681#define SCORE_SFMAX3 400 /* maximum score for third try */
682
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000683#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000684#define SCORE_MAXMAX 999999 /* accept any score */
685#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
686
687/* for spell_edit_score_limit() we need to know the minimum value of
688 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
689#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000690
691/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000692 * Structure to store info for word matching.
693 */
694typedef struct matchinf_S
695{
696 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697
698 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000699 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000700 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000701 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000702 char_u *mi_cend; /* char after what was used for
703 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000704
705 /* case-folded text */
706 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000707 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000708
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000709 /* for when checking word after a prefix */
710 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000711 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000712 int mi_prefcnt; /* number of entries at mi_prefarridx */
713 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000714#ifdef FEAT_MBYTE
715 int mi_cprefixlen; /* byte length of prefix in original
716 case */
717#else
718# define mi_cprefixlen mi_prefixlen /* it's the same value */
719#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000720
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000721 /* for when checking a compound word */
722 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000723 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
724 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000725 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000726
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000727 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000728 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000729 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200730 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000731
732 /* for NOBREAK */
733 int mi_result2; /* "mi_resul" without following word */
734 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000735} matchinf_T;
736
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000737/*
738 * The tables used for recognizing word characters according to spelling.
739 * These are only used for the first 256 characters of 'encoding'.
740 */
741typedef struct spelltab_S
742{
743 char_u st_isw[256]; /* flags: is word char */
744 char_u st_isu[256]; /* flags: is uppercase char */
745 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000746 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000747} spelltab_T;
748
749static spelltab_T spelltab;
750static int did_set_spelltab;
751
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000752#define CF_WORD 0x01
753#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000754
755static void clear_spell_chartab __ARGS((spelltab_T *sp));
756static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200757static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100758static int spell_iswordp_nmw __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000759#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100760static int spell_mb_isword_class __ARGS((int cl, win_T *wp));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200761static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000762#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000763static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000764
765/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000766 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000767 */
768typedef enum
769{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 STATE_START = 0, /* At start of node check for NUL bytes (goodword
771 * ends); if badword ends there is a match, otherwise
772 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000773 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000774 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000775 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
776 STATE_PLAIN, /* Use each byte of the node. */
777 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000778 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000779 STATE_INS, /* Insert a byte in the bad word. */
780 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000781 STATE_UNSWAP, /* Undo swap two characters. */
782 STATE_SWAP3, /* Swap two characters over three. */
783 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000784 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000785 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000786 STATE_REP_INI, /* Prepare for using REP items. */
787 STATE_REP, /* Use matching REP items from the .aff file. */
788 STATE_REP_UNDO, /* Undo a REP item replacement. */
789 STATE_FINAL /* End of this node. */
790} state_T;
791
792/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000793 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000794 */
795typedef struct trystate_S
796{
Bram Moolenaarea424162005-06-16 21:51:00 +0000797 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000798 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000799 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000800 short ts_curi; /* index in list of child nodes */
801 char_u ts_fidx; /* index in fword[], case-folded bad word */
802 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
803 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000804 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000805 * PFD_PREFIXTREE or PFD_NOPREFIX */
806 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000807#ifdef FEAT_MBYTE
808 char_u ts_tcharlen; /* number of bytes in tword character */
809 char_u ts_tcharidx; /* current byte index in tword character */
810 char_u ts_isdiff; /* DIFF_ values */
811 char_u ts_fcharstart; /* index in fword where badword char started */
812#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000813 char_u ts_prewordlen; /* length of word in "preword[]" */
814 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000815 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000816 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000817 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000818 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000819 char_u ts_delidx; /* index in fword for char that was deleted,
820 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000821} trystate_T;
822
Bram Moolenaarea424162005-06-16 21:51:00 +0000823/* values for ts_isdiff */
824#define DIFF_NONE 0 /* no different byte (yet) */
825#define DIFF_YES 1 /* different byte found */
826#define DIFF_INSERT 2 /* inserting character */
827
Bram Moolenaard12a1322005-08-21 22:08:24 +0000828/* values for ts_flags */
829#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
830#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000831#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000832
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000833/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000834#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000835#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000836#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000837
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000838/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000839#define FIND_FOLDWORD 0 /* find word case-folded */
840#define FIND_KEEPWORD 1 /* find keep-case word */
841#define FIND_PREFIX 2 /* find word after prefix */
842#define FIND_COMPOUND 3 /* find case-folded compound word */
843#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000845static slang_T *slang_alloc __ARGS((char_u *lang));
846static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000847static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000848static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000849static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000850static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000851static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000852static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
853static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000854static 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 +0000855static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000856static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000857static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000858static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000859static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000860static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000861static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000862static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000863static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000864static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000865static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
866static int read_charflags_section __ARGS((FILE *fd));
867static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000868static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000869static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000870static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
871static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
872static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000873static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
874static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000875static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000876static int init_syl_tab __ARGS((slang_T *slang));
877static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000878static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
879static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000880#ifdef FEAT_MBYTE
881static int *mb_str2wide __ARGS((char_u *s));
882#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000883static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200884static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
885static void clear_midword __ARGS((win_T *buf));
886static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000887static int find_region __ARGS((char_u *rp, char_u *region));
888static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000889static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000890static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000891static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000892static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000893static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000894static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000895static 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 +0000896#ifdef FEAT_EVAL
897static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
898#endif
899static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000900static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
901static void suggest_load_files __ARGS((void));
902static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000903static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000904static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000905static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000906static void suggest_try_special __ARGS((suginfo_T *su));
907static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000908static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
909static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000910#ifdef FEAT_MBYTE
911static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
912#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000913static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000914static void score_comp_sal __ARGS((suginfo_T *su));
915static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000916static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000917static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000918static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000919static void suggest_try_soundalike_finish __ARGS((void));
920static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
921static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000922static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000923static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000924static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000925static 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));
926static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000927static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000928static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000929static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000931static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
932static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
933static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000934#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000935static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000936#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000937static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000938static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
939static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
940#ifdef FEAT_MBYTE
941static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
942#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000943static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
944static 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 +0000945static buf_T *open_spellbuf __ARGS((void));
946static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000947
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000948/*
949 * Use our own character-case definitions, because the current locale may
950 * differ from what the .spl file uses.
951 * These must not be called with negative number!
952 */
953#ifndef FEAT_MBYTE
954/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000955# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
956# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000957# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
958#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000959# if defined(HAVE_WCHAR_H)
960# include <wchar.h> /* for towupper() and towlower() */
961# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000962/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
963 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
964 * the "w" library function for characters above 255 if available. */
965# ifdef HAVE_TOWLOWER
966# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000967 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000968# else
969# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000970 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000971# endif
972
973# ifdef HAVE_TOWUPPER
974# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000975 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000976# else
977# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000978 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000979# endif
980
981# ifdef HAVE_ISWUPPER
982# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
983 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
984# else
985# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000986 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000987# endif
988#endif
989
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000990
991static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000992static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000993static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000994static char *e_affname = N_("Affix name too long in %s line %d: %s");
995static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
996static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000997static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000998
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000999/* Remember what "z?" replaced. */
1000static char_u *repl_from = NULL;
1001static char_u *repl_to = NULL;
1002
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003/*
1004 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001005 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001006 * "*attrp" is set to the highlight index for a badly spelled word. For a
1007 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001009 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001010 * "capcol" is used to check for a Capitalised word after the end of a
1011 * sentence. If it's zero then perform the check. Return the column where to
1012 * check next, or -1 when no sentence end was found. If it's NULL then don't
1013 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001014 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001015 * Returns the length of the word in bytes, also when it's OK, so that the
1016 * caller can skip over the word.
1017 */
1018 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001019spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001020 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001021 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001022 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001023 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001024 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001025{
1026 matchinf_T mi; /* Most things are put in "mi" so that it can
1027 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001028 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001029 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001030 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001031 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001032 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001033
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001034 /* A word never starts at a space or a control character. Return quickly
1035 * then, skipping over the character. */
1036 if (*ptr <= ' ')
1037 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001038
1039 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001040 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001041 return 1;
1042
Bram Moolenaar5195e452005-08-19 20:32:47 +00001043 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001044
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001045 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001046 * 0X99FF. But always do check spelling to find "3GPP" and "11
1047 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001048 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001050 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
1051 mi.mi_end = skipbin(ptr + 2);
1052 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +00001053 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 else
1055 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001056 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001058
Bram Moolenaar0c405862005-06-22 22:26:26 +00001059 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001060 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001061 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001062 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001063 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001064 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001065 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001066 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001067 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068
Bram Moolenaar860cae12010-06-05 23:22:07 +02001069 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 {
1071 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001072 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001073 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001074 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001075 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001076 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001077 if (capcol != NULL)
1078 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001079
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001080 /* We always use the characters up to the next non-word character,
1081 * also for bad words. */
1082 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001083
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001084 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001085 mi.mi_capflags = 0;
1086 mi.mi_cend = NULL;
1087 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001088
Bram Moolenaar5195e452005-08-19 20:32:47 +00001089 /* case-fold the word with one non-word character, so that we can check
1090 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001091 if (*mi.mi_fend != NUL)
1092 mb_ptr_adv(mi.mi_fend);
1093
1094 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1095 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001096 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001097
1098 /* The word is bad unless we recognize it. */
1099 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001100 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101
1102 /*
1103 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001104 * We check them all, because a word may be matched longer in another
1105 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001106 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001107 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001108 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001109 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001110
1111 /* If reloading fails the language is still in the list but everything
1112 * has been cleared. */
1113 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1114 continue;
1115
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001116 /* Check for a matching word in case-folded words. */
1117 find_word(&mi, FIND_FOLDWORD);
1118
1119 /* Check for a matching word in keep-case words. */
1120 find_word(&mi, FIND_KEEPWORD);
1121
1122 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001123 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001124
1125 /* For a NOBREAK language, may want to use a word without a following
1126 * word as a backup. */
1127 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1128 && mi.mi_result2 != SP_BAD)
1129 {
1130 mi.mi_result = mi.mi_result2;
1131 mi.mi_end = mi.mi_end2;
1132 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001133
1134 /* Count the word in the first language where it's found to be OK. */
1135 if (count_word && mi.mi_result == SP_OK)
1136 {
1137 count_common_word(mi.mi_lp->lp_slang, ptr,
1138 (int)(mi.mi_end - ptr), 1);
1139 count_word = FALSE;
1140 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001141 }
1142
1143 if (mi.mi_result != SP_OK)
1144 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001145 /* If we found a number skip over it. Allows for "42nd". Do flag
1146 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001147 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001148 {
1149 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1150 return nrlen;
1151 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001152
1153 /* When we are at a non-word character there is no error, just
1154 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001155 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001156 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001157 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001158 {
1159 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001160 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001161
1162 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001163 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001164 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001165 r = vim_regexec(&regmatch, ptr, 0);
1166 wp->w_s->b_cap_prog = regmatch.regprog;
1167 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001168 *capcol = (int)(regmatch.endp[0] - ptr);
1169 }
1170
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001171#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001172 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001173 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001174#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001175 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001176 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001177 else if (mi.mi_end == ptr)
1178 /* Always include at least one character. Required for when there
1179 * is a mixup in "midword". */
1180 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001182 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001183 {
1184 char_u *p, *fp;
1185 int save_result = mi.mi_result;
1186
1187 /* First language in 'spelllang' is NOBREAK. Find first position
1188 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001189 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001190 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001191 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001192 p = mi.mi_word;
1193 fp = mi.mi_fword;
1194 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001195 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001196 mb_ptr_adv(p);
1197 mb_ptr_adv(fp);
1198 if (p >= mi.mi_end)
1199 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001200 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001201 find_word(&mi, FIND_COMPOUND);
1202 if (mi.mi_result != SP_BAD)
1203 {
1204 mi.mi_end = p;
1205 break;
1206 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001207 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001208 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001209 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001210 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001211
1212 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001213 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001214 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001215 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001216 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001217 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001218 }
1219
Bram Moolenaar5195e452005-08-19 20:32:47 +00001220 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1221 {
1222 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001223 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001224 return wrongcaplen;
1225 }
1226
Bram Moolenaar51485f02005-06-04 21:55:20 +00001227 return (int)(mi.mi_end - ptr);
1228}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001229
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230/*
1231 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1233 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1234 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1235 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001236 *
1237 * For a match mip->mi_result is updated.
1238 */
1239 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001240find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001241 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001243{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001244 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001245 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001246 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 int endidxcnt = 0;
1248 int len;
1249 int wlen = 0;
1250 int flen;
1251 int c;
1252 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001253 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001254#ifdef FEAT_MBYTE
1255 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001256#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001257 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001258 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 slang_T *slang = mip->mi_lp->lp_slang;
1260 unsigned flags;
1261 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001262 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001263 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001264 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001265 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001266
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001267 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001268 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001269 /* Check for word with matching case in keep-case tree. */
1270 ptr = mip->mi_word;
1271 flen = 9999; /* no case folding, always enough bytes */
1272 byts = slang->sl_kbyts;
1273 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001274
1275 if (mode == FIND_KEEPCOMPOUND)
1276 /* Skip over the previously found word(s). */
1277 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001278 }
1279 else
1280 {
1281 /* Check for case-folded in case-folded tree. */
1282 ptr = mip->mi_fword;
1283 flen = mip->mi_fwordlen; /* available case-folded bytes */
1284 byts = slang->sl_fbyts;
1285 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001286
1287 if (mode == FIND_PREFIX)
1288 {
1289 /* Skip over the prefix. */
1290 wlen = mip->mi_prefixlen;
1291 flen -= mip->mi_prefixlen;
1292 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001293 else if (mode == FIND_COMPOUND)
1294 {
1295 /* Skip over the previously found word(s). */
1296 wlen = mip->mi_compoff;
1297 flen -= mip->mi_compoff;
1298 }
1299
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001300 }
1301
Bram Moolenaar51485f02005-06-04 21:55:20 +00001302 if (byts == NULL)
1303 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001304
Bram Moolenaar51485f02005-06-04 21:55:20 +00001305 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001306 * Repeat advancing in the tree until:
1307 * - there is a byte that doesn't match,
1308 * - we reach the end of the tree,
1309 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001310 */
1311 for (;;)
1312 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001313 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001314 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001315
1316 len = byts[arridx++];
1317
1318 /* If the first possible byte is a zero the word could end here.
1319 * Remember this index, we first check for the longest word. */
1320 if (byts[arridx] == 0)
1321 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001322 if (endidxcnt == MAXWLEN)
1323 {
1324 /* Must be a corrupted spell file. */
1325 EMSG(_(e_format));
1326 return;
1327 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001328 endlen[endidxcnt] = wlen;
1329 endidx[endidxcnt++] = arridx++;
1330 --len;
1331
1332 /* Skip over the zeros, there can be several flag/region
1333 * combinations. */
1334 while (len > 0 && byts[arridx] == 0)
1335 {
1336 ++arridx;
1337 --len;
1338 }
1339 if (len == 0)
1340 break; /* no children, word must end here */
1341 }
1342
1343 /* Stop looking at end of the line. */
1344 if (ptr[wlen] == NUL)
1345 break;
1346
1347 /* Perform a binary search in the list of accepted bytes. */
1348 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001349 if (c == TAB) /* <Tab> is handled like <Space> */
1350 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001351 lo = arridx;
1352 hi = arridx + len - 1;
1353 while (lo < hi)
1354 {
1355 m = (lo + hi) / 2;
1356 if (byts[m] > c)
1357 hi = m - 1;
1358 else if (byts[m] < c)
1359 lo = m + 1;
1360 else
1361 {
1362 lo = hi = m;
1363 break;
1364 }
1365 }
1366
1367 /* Stop if there is no matching byte. */
1368 if (hi < lo || byts[lo] != c)
1369 break;
1370
1371 /* Continue at the child (if there is one). */
1372 arridx = idxs[lo];
1373 ++wlen;
1374 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001375
1376 /* One space in the good word may stand for several spaces in the
1377 * checked word. */
1378 if (c == ' ')
1379 {
1380 for (;;)
1381 {
1382 if (flen <= 0 && *mip->mi_fend != NUL)
1383 flen = fold_more(mip);
1384 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1385 break;
1386 ++wlen;
1387 --flen;
1388 }
1389 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001390 }
1391
1392 /*
1393 * Verify that one of the possible endings is valid. Try the longest
1394 * first.
1395 */
1396 while (endidxcnt > 0)
1397 {
1398 --endidxcnt;
1399 arridx = endidx[endidxcnt];
1400 wlen = endlen[endidxcnt];
1401
1402#ifdef FEAT_MBYTE
1403 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1404 continue; /* not at first byte of character */
1405#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001406 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001407 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001408 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001409 continue; /* next char is a word character */
1410 word_ends = FALSE;
1411 }
1412 else
1413 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001414 /* The prefix flag is before compound flags. Once a valid prefix flag
1415 * has been found we try compound flags. */
1416 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001417
1418#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001419 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001420 {
1421 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001422 * when folding case. This can be slow, take a shortcut when the
1423 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001424 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001425 if (STRNCMP(ptr, p, wlen) != 0)
1426 {
1427 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1428 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001429 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 }
1432#endif
1433
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001434 /* Check flags and region. For FIND_PREFIX check the condition and
1435 * prefix ID.
1436 * Repeat this if there are more flags/region alternatives until there
1437 * is a match. */
1438 res = SP_BAD;
1439 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1440 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001441 {
1442 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001443
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001444 /* For the fold-case tree check that the case of the checked word
1445 * matches with what the word in the tree requires.
1446 * For keep-case tree the case is always right. For prefixes we
1447 * don't bother to check. */
1448 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001450 if (mip->mi_cend != mip->mi_word + wlen)
1451 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001452 /* mi_capflags was set for a different word length, need
1453 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001455 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001456 }
1457
Bram Moolenaar0c405862005-06-22 22:26:26 +00001458 if (mip->mi_capflags == WF_KEEPCAP
1459 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001461 }
1462
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001463 /* When mode is FIND_PREFIX the word must support the prefix:
1464 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001465 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001466 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001467 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001468 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001469 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001470 mip->mi_word + mip->mi_cprefixlen, slang,
1471 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001472 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001474
1475 /* Use the WF_RARE flag for a rare prefix. */
1476 if (c & WF_RAREPFX)
1477 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001478 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001479 }
1480
Bram Moolenaar78622822005-08-23 21:00:13 +00001481 if (slang->sl_nobreak)
1482 {
1483 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1484 && (flags & WF_BANNED) == 0)
1485 {
1486 /* NOBREAK: found a valid following word. That's all we
1487 * need to know, so return. */
1488 mip->mi_result = SP_OK;
1489 break;
1490 }
1491 }
1492
1493 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1494 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001495 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001496 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001497 * COMPOUNDMIN reject it quickly.
1498 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001499 * that's too short... Myspell compatibility requires this
1500 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001501 if (((unsigned)flags >> 24) == 0
1502 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001503 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001504#ifdef FEAT_MBYTE
1505 /* For multi-byte chars check character length against
1506 * COMPOUNDMIN. */
1507 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001508 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001509 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1510 wlen - mip->mi_compoff) < slang->sl_compminlen)
1511 continue;
1512#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001513
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001514 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001515 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001516 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1517 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001518 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001519 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001520
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001521 /* Don't allow compounding on a side where an affix was added,
1522 * unless COMPOUNDPERMITFLAG was used. */
1523 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1524 continue;
1525 if (!word_ends && (flags & WF_NOCOMPAFT))
1526 continue;
1527
Bram Moolenaard12a1322005-08-21 22:08:24 +00001528 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001529 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001530 ? slang->sl_compstartflags
1531 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001532 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001533 continue;
1534
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001535 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1536 * discard the compound word. */
1537 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1538 continue;
1539
Bram Moolenaare52325c2005-08-22 22:54:29 +00001540 if (mode == FIND_COMPOUND)
1541 {
1542 int capflags;
1543
1544 /* Need to check the caps type of the appended compound
1545 * word. */
1546#ifdef FEAT_MBYTE
1547 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1548 mip->mi_compoff) != 0)
1549 {
1550 /* case folding may have changed the length */
1551 p = mip->mi_word;
1552 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1553 mb_ptr_adv(p);
1554 }
1555 else
1556#endif
1557 p = mip->mi_word + mip->mi_compoff;
1558 capflags = captype(p, mip->mi_word + wlen);
1559 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1560 && (flags & WF_FIXCAP) != 0))
1561 continue;
1562
1563 if (capflags != WF_ALLCAP)
1564 {
1565 /* When the character before the word is a word
1566 * character we do not accept a Onecap word. We do
1567 * accept a no-caps word, even when the dictionary
1568 * word specifies ONECAP. */
1569 mb_ptr_back(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001570 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +00001571 ? capflags == WF_ONECAP
1572 : (flags & WF_ONECAP) != 0
1573 && capflags != WF_ONECAP)
1574 continue;
1575 }
1576 }
1577
Bram Moolenaar5195e452005-08-19 20:32:47 +00001578 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001579 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001580 * the number of syllables must not be too large. */
1581 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1582 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1583 if (word_ends)
1584 {
1585 char_u fword[MAXWLEN];
1586
1587 if (slang->sl_compsylmax < MAXWLEN)
1588 {
1589 /* "fword" is only needed for checking syllables. */
1590 if (ptr == mip->mi_word)
1591 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1592 else
1593 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1594 }
1595 if (!can_compound(slang, fword, mip->mi_compflags))
1596 continue;
1597 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001598 else if (slang->sl_comprules != NULL
1599 && !match_compoundrule(slang, mip->mi_compflags))
1600 /* The compound flags collected so far do not match any
1601 * COMPOUNDRULE, discard the compounded word. */
1602 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001603 }
1604
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001605 /* Check NEEDCOMPOUND: can't use word without compounding. */
1606 else if (flags & WF_NEEDCOMP)
1607 continue;
1608
Bram Moolenaar78622822005-08-23 21:00:13 +00001609 nobreak_result = SP_OK;
1610
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001611 if (!word_ends)
1612 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001613 int save_result = mip->mi_result;
1614 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001615 langp_T *save_lp = mip->mi_lp;
1616 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001617
1618 /* Check that a valid word follows. If there is one and we
1619 * are compounding, it will set "mi_result", thus we are
1620 * always finished here. For NOBREAK we only check that a
1621 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001622 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001623 if (slang->sl_nobreak)
1624 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001625
1626 /* Find following word in case-folded tree. */
1627 mip->mi_compoff = endlen[endidxcnt];
1628#ifdef FEAT_MBYTE
1629 if (has_mbyte && mode == FIND_KEEPWORD)
1630 {
1631 /* Compute byte length in case-folded word from "wlen":
1632 * byte length in keep-case word. Length may change when
1633 * folding case. This can be slow, take a shortcut when
1634 * the case-folded word is equal to the keep-case word. */
1635 p = mip->mi_fword;
1636 if (STRNCMP(ptr, p, wlen) != 0)
1637 {
1638 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1639 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001640 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001641 }
1642 }
1643#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001644 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001645 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001646 if (flags & WF_COMPROOT)
1647 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001648
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649 /* For NOBREAK we need to try all NOBREAK languages, at least
1650 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001651 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001652 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001653 if (slang->sl_nobreak)
1654 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001655 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001656 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1657 || !mip->mi_lp->lp_slang->sl_nobreak)
1658 continue;
1659 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001660
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001661 find_word(mip, FIND_COMPOUND);
1662
1663 /* When NOBREAK any word that matches is OK. Otherwise we
1664 * need to find the longest match, thus try with keep-case
1665 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001666 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1667 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001668 /* Find following word in keep-case tree. */
1669 mip->mi_compoff = wlen;
1670 find_word(mip, FIND_KEEPCOMPOUND);
1671
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001672#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1673 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1674 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001675 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1676 {
1677 /* Check for following word with prefix. */
1678 mip->mi_compoff = c;
1679 find_prefix(mip, FIND_COMPOUND);
1680 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001681#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001682 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001683
1684 if (!slang->sl_nobreak)
1685 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001686 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001687 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001688 if (flags & WF_COMPROOT)
1689 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001690 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001691
Bram Moolenaar78622822005-08-23 21:00:13 +00001692 if (slang->sl_nobreak)
1693 {
1694 nobreak_result = mip->mi_result;
1695 mip->mi_result = save_result;
1696 mip->mi_end = save_end;
1697 }
1698 else
1699 {
1700 if (mip->mi_result == SP_OK)
1701 break;
1702 continue;
1703 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001704 }
1705
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001706 if (flags & WF_BANNED)
1707 res = SP_BANNED;
1708 else if (flags & WF_REGION)
1709 {
1710 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001711 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001712 res = SP_OK;
1713 else
1714 res = SP_LOCAL;
1715 }
1716 else if (flags & WF_RARE)
1717 res = SP_RARE;
1718 else
1719 res = SP_OK;
1720
Bram Moolenaar78622822005-08-23 21:00:13 +00001721 /* Always use the longest match and the best result. For NOBREAK
1722 * we separately keep the longest match without a following good
1723 * word as a fall-back. */
1724 if (nobreak_result == SP_BAD)
1725 {
1726 if (mip->mi_result2 > res)
1727 {
1728 mip->mi_result2 = res;
1729 mip->mi_end2 = mip->mi_word + wlen;
1730 }
1731 else if (mip->mi_result2 == res
1732 && mip->mi_end2 < mip->mi_word + wlen)
1733 mip->mi_end2 = mip->mi_word + wlen;
1734 }
1735 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001736 {
1737 mip->mi_result = res;
1738 mip->mi_end = mip->mi_word + wlen;
1739 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001740 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001741 mip->mi_end = mip->mi_word + wlen;
1742
Bram Moolenaar78622822005-08-23 21:00:13 +00001743 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001744 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001745 }
1746
Bram Moolenaar78622822005-08-23 21:00:13 +00001747 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001748 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001749 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001750}
1751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001752/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001753 * Return TRUE if there is a match between the word ptr[wlen] and
1754 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1755 * word.
1756 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1757 * end of ptr[wlen] and the second part matches after it.
1758 */
1759 static int
1760match_checkcompoundpattern(ptr, wlen, gap)
1761 char_u *ptr;
1762 int wlen;
1763 garray_T *gap; /* &sl_comppat */
1764{
1765 int i;
1766 char_u *p;
1767 int len;
1768
1769 for (i = 0; i + 1 < gap->ga_len; i += 2)
1770 {
1771 p = ((char_u **)gap->ga_data)[i + 1];
1772 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1773 {
1774 /* Second part matches at start of following compound word, now
1775 * check if first part matches at end of previous word. */
1776 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001777 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001778 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1779 return TRUE;
1780 }
1781 }
1782 return FALSE;
1783}
1784
1785/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001786 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1787 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001788 */
1789 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001790can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001791 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001792 char_u *word;
1793 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001794{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001795#ifdef FEAT_MBYTE
1796 char_u uflags[MAXWLEN * 2];
1797 int i;
1798#endif
1799 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001800
1801 if (slang->sl_compprog == NULL)
1802 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001803#ifdef FEAT_MBYTE
1804 if (enc_utf8)
1805 {
1806 /* Need to convert the single byte flags to utf8 characters. */
1807 p = uflags;
1808 for (i = 0; flags[i] != NUL; ++i)
1809 p += mb_char2bytes(flags[i], p);
1810 *p = NUL;
1811 p = uflags;
1812 }
1813 else
1814#endif
1815 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001816 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001817 return FALSE;
1818
Bram Moolenaare52325c2005-08-22 22:54:29 +00001819 /* Count the number of syllables. This may be slow, do it last. If there
1820 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001821 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001822 if (slang->sl_compsylmax < MAXWLEN
1823 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001824 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001825 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001826}
1827
1828/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001829 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1830 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1831 * lines if they don't contain wildcards.
1832 */
1833 static int
1834can_be_compound(sp, slang, compflags, flag)
1835 trystate_T *sp;
1836 slang_T *slang;
1837 char_u *compflags;
1838 int flag;
1839{
1840 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1841 * then it can't possibly compound. */
1842 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1843 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1844 return FALSE;
1845
1846 /* If there are no wildcards, we can check if the flags collected so far
1847 * possibly can form a match with COMPOUNDRULE patterns. This only
1848 * makes sense when we have two or more words. */
1849 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1850 {
1851 int v;
1852
1853 compflags[sp->ts_complen] = flag;
1854 compflags[sp->ts_complen + 1] = NUL;
1855 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1856 compflags[sp->ts_complen] = NUL;
1857 return v;
1858 }
1859
1860 return TRUE;
1861}
1862
1863
1864/*
1865 * Return TRUE if the compound flags in compflags[] match the start of any
1866 * compound rule. This is used to stop trying a compound if the flags
1867 * collected so far can't possibly match any compound rule.
1868 * Caller must check that slang->sl_comprules is not NULL.
1869 */
1870 static int
1871match_compoundrule(slang, compflags)
1872 slang_T *slang;
1873 char_u *compflags;
1874{
1875 char_u *p;
1876 int i;
1877 int c;
1878
1879 /* loop over all the COMPOUNDRULE entries */
1880 for (p = slang->sl_comprules; *p != NUL; ++p)
1881 {
1882 /* loop over the flags in the compound word we have made, match
1883 * them against the current rule entry */
1884 for (i = 0; ; ++i)
1885 {
1886 c = compflags[i];
1887 if (c == NUL)
1888 /* found a rule that matches for the flags we have so far */
1889 return TRUE;
1890 if (*p == '/' || *p == NUL)
1891 break; /* end of rule, it's too short */
1892 if (*p == '[')
1893 {
1894 int match = FALSE;
1895
1896 /* compare against all the flags in [] */
1897 ++p;
1898 while (*p != ']' && *p != NUL)
1899 if (*p++ == c)
1900 match = TRUE;
1901 if (!match)
1902 break; /* none matches */
1903 }
1904 else if (*p != c)
1905 break; /* flag of word doesn't match flag in pattern */
1906 ++p;
1907 }
1908
1909 /* Skip to the next "/", where the next pattern starts. */
1910 p = vim_strchr(p, '/');
1911 if (p == NULL)
1912 break;
1913 }
1914
1915 /* Checked all the rules and none of them match the flags, so there
1916 * can't possibly be a compound starting with these flags. */
1917 return FALSE;
1918}
1919
1920/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001921 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1922 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001923 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001924 */
1925 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001926valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001927 int totprefcnt; /* nr of prefix IDs */
1928 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001929 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001930 char_u *word;
1931 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001932 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001933{
1934 int prefcnt;
1935 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001936 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001937 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001938
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001939 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001940 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1941 {
1942 pidx = slang->sl_pidxs[arridx + prefcnt];
1943
1944 /* Check the prefix ID. */
1945 if (prefid != (pidx & 0xff))
1946 continue;
1947
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001948 /* Check if the prefix doesn't combine and the word already has a
1949 * suffix. */
1950 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1951 continue;
1952
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001953 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001954 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001955 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1956 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001957 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001958 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001959 continue;
1960 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001961 else if (cond_req)
1962 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963
Bram Moolenaar53805d12005-08-01 07:08:33 +00001964 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001965 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001966 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001967 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001968}
1969
1970/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971 * Check if the word at "mip->mi_word" has a matching prefix.
1972 * If it does, then check the following word.
1973 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001974 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1975 * prefix in a compound word.
1976 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 * For a match mip->mi_result is updated.
1978 */
1979 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001980find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001981 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001982 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001983{
1984 idx_T arridx = 0;
1985 int len;
1986 int wlen = 0;
1987 int flen;
1988 int c;
1989 char_u *ptr;
1990 idx_T lo, hi, m;
1991 slang_T *slang = mip->mi_lp->lp_slang;
1992 char_u *byts;
1993 idx_T *idxs;
1994
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001995 byts = slang->sl_pbyts;
1996 if (byts == NULL)
1997 return; /* array is empty */
1998
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001999 /* We use the case-folded word here, since prefixes are always
2000 * case-folded. */
2001 ptr = mip->mi_fword;
2002 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002003 if (mode == FIND_COMPOUND)
2004 {
2005 /* Skip over the previously found word(s). */
2006 ptr += mip->mi_compoff;
2007 flen -= mip->mi_compoff;
2008 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002009 idxs = slang->sl_pidxs;
2010
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002011 /*
2012 * Repeat advancing in the tree until:
2013 * - there is a byte that doesn't match,
2014 * - we reach the end of the tree,
2015 * - or we reach the end of the line.
2016 */
2017 for (;;)
2018 {
2019 if (flen == 0 && *mip->mi_fend != NUL)
2020 flen = fold_more(mip);
2021
2022 len = byts[arridx++];
2023
2024 /* If the first possible byte is a zero the prefix could end here.
2025 * Check if the following word matches and supports the prefix. */
2026 if (byts[arridx] == 0)
2027 {
2028 /* There can be several prefixes with different conditions. We
2029 * try them all, since we don't know which one will give the
2030 * longest match. The word is the same each time, pass the list
2031 * of possible prefixes to find_word(). */
2032 mip->mi_prefarridx = arridx;
2033 mip->mi_prefcnt = len;
2034 while (len > 0 && byts[arridx] == 0)
2035 {
2036 ++arridx;
2037 --len;
2038 }
2039 mip->mi_prefcnt -= len;
2040
2041 /* Find the word that comes after the prefix. */
2042 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002043 if (mode == FIND_COMPOUND)
2044 /* Skip over the previously found word(s). */
2045 mip->mi_prefixlen += mip->mi_compoff;
2046
Bram Moolenaar53805d12005-08-01 07:08:33 +00002047#ifdef FEAT_MBYTE
2048 if (has_mbyte)
2049 {
2050 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002051 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2052 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002053 }
2054 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002055 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002056#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002057 find_word(mip, FIND_PREFIX);
2058
2059
2060 if (len == 0)
2061 break; /* no children, word must end here */
2062 }
2063
2064 /* Stop looking at end of the line. */
2065 if (ptr[wlen] == NUL)
2066 break;
2067
2068 /* Perform a binary search in the list of accepted bytes. */
2069 c = ptr[wlen];
2070 lo = arridx;
2071 hi = arridx + len - 1;
2072 while (lo < hi)
2073 {
2074 m = (lo + hi) / 2;
2075 if (byts[m] > c)
2076 hi = m - 1;
2077 else if (byts[m] < c)
2078 lo = m + 1;
2079 else
2080 {
2081 lo = hi = m;
2082 break;
2083 }
2084 }
2085
2086 /* Stop if there is no matching byte. */
2087 if (hi < lo || byts[lo] != c)
2088 break;
2089
2090 /* Continue at the child (if there is one). */
2091 arridx = idxs[lo];
2092 ++wlen;
2093 --flen;
2094 }
2095}
2096
2097/*
2098 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002099 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002100 * Return the length of the folded chars in bytes.
2101 */
2102 static int
2103fold_more(mip)
2104 matchinf_T *mip;
2105{
2106 int flen;
2107 char_u *p;
2108
2109 p = mip->mi_fend;
2110 do
2111 {
2112 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002113 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002114
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002115 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002116 if (*mip->mi_fend != NUL)
2117 mb_ptr_adv(mip->mi_fend);
2118
2119 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2120 mip->mi_fword + mip->mi_fwordlen,
2121 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002122 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002123 mip->mi_fwordlen += flen;
2124 return flen;
2125}
2126
2127/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 * Check case flags for a word. Return TRUE if the word has the requested
2129 * case.
2130 */
2131 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002132spell_valid_case(wordflags, treeflags)
2133 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 int treeflags; /* flags for the word in the spell tree */
2135{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002136 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002138 && ((treeflags & WF_ONECAP) == 0
2139 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140}
2141
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002142/*
2143 * Return TRUE if spell checking is not enabled.
2144 */
2145 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002146no_spell_checking(wp)
2147 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002148{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002149 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2150 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002151 {
2152 EMSG(_("E756: Spell checking is not enabled"));
2153 return TRUE;
2154 }
2155 return FALSE;
2156}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002158/*
2159 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002160 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2161 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002162 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2163 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002164 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 */
2166 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002167spell_move_to(wp, dir, allwords, curline, attrp)
2168 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002170 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002172 hlf_T *attrp; /* return: attributes of bad word or NULL
2173 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002174{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002175 linenr_T lnum;
2176 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002177 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002178 char_u *line;
2179 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002180 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002181 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002183#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002184 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002185#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002186 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002187 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002188 char_u *buf = NULL;
2189 int buflen = 0;
2190 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002191 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002192 int found_one = FALSE;
2193 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaar95529562005-08-25 21:21:38 +00002195 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002197
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002198 /*
2199 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002200 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002201 *
2202 * When searching backwards, we continue in the line to find the last
2203 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002204 *
2205 * We concatenate the start of the next line, so that wrapped words work
2206 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2207 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002208 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002209 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002210 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002211
2212 while (!got_int)
2213 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002214 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002216 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002217 if (buflen < len + MAXWLEN + 2)
2218 {
2219 vim_free(buf);
2220 buflen = len + MAXWLEN + 2;
2221 buf = alloc(buflen);
2222 if (buf == NULL)
2223 break;
2224 }
2225
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002226 /* In first line check first word for Capital. */
2227 if (lnum == 1)
2228 capcol = 0;
2229
2230 /* For checking first word with a capital skip white space. */
2231 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002232 capcol = (int)(skipwhite(line) - line);
2233 else if (curline && wp == curwin)
2234 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002235 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002236 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002237 if (check_need_cap(lnum, col))
2238 capcol = col;
2239
2240 /* Need to get the line again, may have looked at the previous
2241 * one. */
2242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2243 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002244
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 /* Copy the line into "buf" and append the start of the next line if
2246 * possible. */
2247 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002248 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002249 spell_cat_line(buf + STRLEN(buf),
2250 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251
2252 p = buf + skip;
2253 endp = buf + len;
2254 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002255 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002256 /* When searching backward don't search after the cursor. Unless
2257 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002258 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002259 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002260 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002261 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002262 break;
2263
2264 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002266 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002268 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002270 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002271 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002273 /* When searching forward only accept a bad word after
2274 * the cursor. */
2275 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002276 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002277 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002278 && (wrapped
2279 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002280 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002281 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002283#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002284 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002285 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002286 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002287 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002288 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002289 if (!can_spell)
2290 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002291 }
2292 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002293#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002294 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002295
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 if (can_spell)
2297 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002298 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002299 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002300 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002301#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002302 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002303#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002304 if (dir == FORWARD)
2305 {
2306 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002307 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002308 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002309 if (attrp != NULL)
2310 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002311 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002312 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002313 else if (curline)
2314 /* Insert mode completion: put cursor after
2315 * the bad word. */
2316 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002317 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002318 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002320 else
2321 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 }
2324
Bram Moolenaar51485f02005-06-04 21:55:20 +00002325 /* advance to character after the word */
2326 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002327 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 }
2329
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 if (dir == BACKWARD && found_pos.lnum != 0)
2331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002332 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002333 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002335 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002336 }
2337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002339 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002341 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002342 if (dir == BACKWARD)
2343 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002344 /* If we are back at the starting line and searched it again there
2345 * is no match, give up. */
2346 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002347 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002348
2349 if (lnum > 1)
2350 --lnum;
2351 else if (!p_ws)
2352 break; /* at first line and 'nowrapscan' */
2353 else
2354 {
2355 /* Wrap around to the end of the buffer. May search the
2356 * starting line again and accept the last match. */
2357 lnum = wp->w_buffer->b_ml.ml_line_count;
2358 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002359 if (!shortmess(SHM_SEARCH))
2360 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002361 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002362 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002363 }
2364 else
2365 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002366 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2367 ++lnum;
2368 else if (!p_ws)
2369 break; /* at first line and 'nowrapscan' */
2370 else
2371 {
2372 /* Wrap around to the start of the buffer. May search the
2373 * starting line again and accept the first match. */
2374 lnum = 1;
2375 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376 if (!shortmess(SHM_SEARCH))
2377 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002378 }
2379
2380 /* If we are back at the starting line and there is no match then
2381 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002382 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002383 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002384
2385 /* Skip the characters at the start of the next line that were
2386 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002387 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002389 else
2390 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002392 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002393 --capcol;
2394
2395 /* But after empty line check first word in next line */
2396 if (*skipwhite(line) == NUL)
2397 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399
2400 line_breakcheck();
2401 }
2402
Bram Moolenaar0c405862005-06-22 22:26:26 +00002403 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002404 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002405}
2406
2407/*
2408 * For spell checking: concatenate the start of the following line "line" into
2409 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002410 * Keep the blanks at the start of the next line, this is used in win_line()
2411 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002412 */
2413 void
2414spell_cat_line(buf, line, maxlen)
2415 char_u *buf;
2416 char_u *line;
2417 int maxlen;
2418{
2419 char_u *p;
2420 int n;
2421
2422 p = skipwhite(line);
2423 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2424 p = skipwhite(p + 1);
2425
2426 if (*p != NUL)
2427 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002428 /* Only worth concatenating if there is something else than spaces to
2429 * concatenate. */
2430 n = (int)(p - line) + 1;
2431 if (n < maxlen - 1)
2432 {
2433 vim_memset(buf, ' ', n);
2434 vim_strncpy(buf + n, p, maxlen - 1 - n);
2435 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002437}
2438
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002439/*
2440 * Structure used for the cookie argument of do_in_runtimepath().
2441 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002442typedef struct spelload_S
2443{
2444 char_u sl_lang[MAXWLEN + 1]; /* language name */
2445 slang_T *sl_slang; /* resulting slang_T struct */
2446 int sl_nobreak; /* NOBREAK language found */
2447} spelload_T;
2448
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002450 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002453 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454spell_load_lang(lang)
2455 char_u *lang;
2456{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002457 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002459 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002460#ifdef FEAT_AUTOCMD
2461 int round;
2462#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463
Bram Moolenaarb765d632005-06-07 21:00:02 +00002464 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002465 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002466 STRCPY(sl.sl_lang, lang);
2467 sl.sl_slang = NULL;
2468 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002470#ifdef FEAT_AUTOCMD
2471 /* We may retry when no spell file is found for the language, an
2472 * autocommand may load it then. */
2473 for (round = 1; round <= 2; ++round)
2474#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002475 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002476 /*
2477 * Find the first spell file for "lang" in 'runtimepath' and load it.
2478 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002479 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002480#ifdef VMS
2481 "spell/%s_%s.spl",
2482#else
2483 "spell/%s.%s.spl",
2484#endif
2485 lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002486 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002487
2488 if (r == FAIL && *sl.sl_lang != NUL)
2489 {
2490 /* Try loading the ASCII version. */
2491 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002492#ifdef VMS
2493 "spell/%s_ascii.spl",
2494#else
2495 "spell/%s.ascii.spl",
2496#endif
2497 lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002498 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2499
2500#ifdef FEAT_AUTOCMD
2501 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2502 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2503 curbuf->b_fname, FALSE, curbuf))
2504 continue;
2505 break;
2506#endif
2507 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002508#ifdef FEAT_AUTOCMD
2509 break;
2510#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 }
2512
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002513 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002514 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01002515 smsg((char_u *)
2516#ifdef VMS
2517 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2518#else
2519 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2520#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002522 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002523 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002524 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002525 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002526 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002527 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002528 }
2529}
2530
2531/*
2532 * Return the encoding used for spell checking: Use 'encoding', except that we
2533 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2534 */
2535 static char_u *
2536spell_enc()
2537{
2538
2539#ifdef FEAT_MBYTE
2540 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2541 return p_enc;
2542#endif
2543 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544}
2545
2546/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002547 * Get the name of the .spl file for the internal wordlist into
2548 * "fname[MAXPATHL]".
2549 */
2550 static void
2551int_wordlist_spl(fname)
2552 char_u *fname;
2553{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002554 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002555 int_wordlist, spell_enc());
2556}
2557
2558/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560 * Caller must fill "sl_next".
2561 */
2562 static slang_T *
2563slang_alloc(lang)
2564 char_u *lang;
2565{
2566 slang_T *lp;
2567
Bram Moolenaar51485f02005-06-04 21:55:20 +00002568 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 if (lp != NULL)
2570 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002571 if (lang != NULL)
2572 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002575 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002576 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002577 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 return lp;
2581}
2582
2583/*
2584 * Free the contents of an slang_T and the structure itself.
2585 */
2586 static void
2587slang_free(lp)
2588 slang_T *lp;
2589{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 vim_free(lp->sl_fname);
2592 slang_clear(lp);
2593 vim_free(lp);
2594}
2595
2596/*
2597 * Clear an slang_T so that the file can be reloaded.
2598 */
2599 static void
2600slang_clear(lp)
2601 slang_T *lp;
2602{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002603 garray_T *gap;
2604 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002605 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002606 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002607 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608
Bram Moolenaar51485f02005-06-04 21:55:20 +00002609 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002610 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002613 vim_free(lp->sl_pbyts);
2614 lp->sl_pbyts = NULL;
2615
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002617 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002620 vim_free(lp->sl_pidxs);
2621 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002624 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002625 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2626 while (gap->ga_len > 0)
2627 {
2628 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2629 vim_free(ftp->ft_from);
2630 vim_free(ftp->ft_to);
2631 }
2632 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002634
2635 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002636 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002637 {
2638 /* "ga_len" is set to 1 without adding an item for latin1 */
2639 if (gap->ga_data != NULL)
2640 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2641 for (i = 0; i < gap->ga_len; ++i)
2642 vim_free(((int **)gap->ga_data)[i]);
2643 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002644 else
2645 /* SAL items: free salitem_T items */
2646 while (gap->ga_len > 0)
2647 {
2648 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2649 vim_free(smp->sm_lead);
2650 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2651 vim_free(smp->sm_to);
2652#ifdef FEAT_MBYTE
2653 vim_free(smp->sm_lead_w);
2654 vim_free(smp->sm_oneof_w);
2655 vim_free(smp->sm_to_w);
2656#endif
2657 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002658 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002660 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002661 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002662 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002663 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002664 lp->sl_prefprog = NULL;
2665
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002666 vim_free(lp->sl_info);
2667 lp->sl_info = NULL;
2668
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002669 vim_free(lp->sl_midword);
2670 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002671
Bram Moolenaar473de612013-06-08 18:19:48 +02002672 vim_regfree(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002673 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002674 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002675 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002677 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002679 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002680
2681 vim_free(lp->sl_syllable);
2682 lp->sl_syllable = NULL;
2683 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002684
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002685 ga_clear_strings(&lp->sl_comppat);
2686
Bram Moolenaar4770d092006-01-12 23:22:24 +00002687 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2688 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002689
Bram Moolenaar4770d092006-01-12 23:22:24 +00002690#ifdef FEAT_MBYTE
2691 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002692#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693
Bram Moolenaar4770d092006-01-12 23:22:24 +00002694 /* Clear info from .sug file. */
2695 slang_clear_sug(lp);
2696
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002698 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002699 lp->sl_compsylmax = MAXWLEN;
2700 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002701}
2702
2703/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002704 * Clear the info from the .sug file in "lp".
2705 */
2706 static void
2707slang_clear_sug(lp)
2708 slang_T *lp;
2709{
2710 vim_free(lp->sl_sbyts);
2711 lp->sl_sbyts = NULL;
2712 vim_free(lp->sl_sidxs);
2713 lp->sl_sidxs = NULL;
2714 close_spellbuf(lp->sl_sugbuf);
2715 lp->sl_sugbuf = NULL;
2716 lp->sl_sugloaded = FALSE;
2717 lp->sl_sugtime = 0;
2718}
2719
2720/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002721 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002722 * Invoked through do_in_runtimepath().
2723 */
2724 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002725spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002727 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002728{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002729 spelload_T *slp = (spelload_T *)cookie;
2730 slang_T *slang;
2731
2732 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2733 if (slang != NULL)
2734 {
2735 /* When a previously loaded file has NOBREAK also use it for the
2736 * ".add" files. */
2737 if (slp->sl_nobreak && slang->sl_add)
2738 slang->sl_nobreak = TRUE;
2739 else if (slang->sl_nobreak)
2740 slp->sl_nobreak = TRUE;
2741
2742 slp->sl_slang = slang;
2743 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002744}
2745
2746/*
2747 * Load one spell file and store the info into a slang_T.
2748 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002749 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002750 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2751 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2752 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2753 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002754 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002755 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2756 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002758 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002759 static slang_T *
2760spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002761 char_u *fname;
2762 char_u *lang;
2763 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002765{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002767 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002768 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002770 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002771 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002772 char_u *save_sourcing_name = sourcing_name;
2773 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002774 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002775 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002776 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002777
Bram Moolenaarb765d632005-06-07 21:00:02 +00002778 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002779 if (fd == NULL)
2780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 if (!silent)
2782 EMSG2(_(e_notopen), fname);
2783 else if (p_verbose > 2)
2784 {
2785 verbose_enter();
2786 smsg((char_u *)e_notopen, fname);
2787 verbose_leave();
2788 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002789 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002790 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002791 if (p_verbose > 2)
2792 {
2793 verbose_enter();
2794 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2795 verbose_leave();
2796 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797
Bram Moolenaarb765d632005-06-07 21:00:02 +00002798 if (old_lp == NULL)
2799 {
2800 lp = slang_alloc(lang);
2801 if (lp == NULL)
2802 goto endFAIL;
2803
2804 /* Remember the file name, used to reload the file when it's updated. */
2805 lp->sl_fname = vim_strsave(fname);
2806 if (lp->sl_fname == NULL)
2807 goto endFAIL;
2808
Bram Moolenaar56f78042010-12-08 17:09:32 +01002809 /* Check for .add.spl (_add.spl for VMS). */
2810 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002811 }
2812 else
2813 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002814
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002815 /* Set sourcing_name, so that error messages mention the file name. */
2816 sourcing_name = fname;
2817 sourcing_lnum = 0;
2818
Bram Moolenaar4770d092006-01-12 23:22:24 +00002819 /*
2820 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002821 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002822 for (i = 0; i < VIMSPELLMAGICL; ++i)
2823 buf[i] = getc(fd); /* <fileID> */
2824 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2825 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002826 EMSG(_("E757: This does not look like a spell file"));
2827 goto endFAIL;
2828 }
2829 c = getc(fd); /* <versionnr> */
2830 if (c < VIMSPELLVERSION)
2831 {
2832 EMSG(_("E771: Old spell file, needs to be updated"));
2833 goto endFAIL;
2834 }
2835 else if (c > VIMSPELLVERSION)
2836 {
2837 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002838 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 }
2840
Bram Moolenaar5195e452005-08-19 20:32:47 +00002841
2842 /*
2843 * <SECTIONS>: <section> ... <sectionend>
2844 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2845 */
2846 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002847 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002848 n = getc(fd); /* <sectionID> or <sectionend> */
2849 if (n == SN_END)
2850 break;
2851 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002852 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002853 if (len < 0)
2854 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002855
Bram Moolenaar5195e452005-08-19 20:32:47 +00002856 res = 0;
2857 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002858 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002859 case SN_INFO:
2860 lp->sl_info = read_string(fd, len); /* <infotext> */
2861 if (lp->sl_info == NULL)
2862 goto endFAIL;
2863 break;
2864
Bram Moolenaar5195e452005-08-19 20:32:47 +00002865 case SN_REGION:
2866 res = read_region_section(fd, lp, len);
2867 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002868
Bram Moolenaar5195e452005-08-19 20:32:47 +00002869 case SN_CHARFLAGS:
2870 res = read_charflags_section(fd);
2871 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002872
Bram Moolenaar5195e452005-08-19 20:32:47 +00002873 case SN_MIDWORD:
2874 lp->sl_midword = read_string(fd, len); /* <midword> */
2875 if (lp->sl_midword == NULL)
2876 goto endFAIL;
2877 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002878
Bram Moolenaar5195e452005-08-19 20:32:47 +00002879 case SN_PREFCOND:
2880 res = read_prefcond_section(fd, lp);
2881 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002882
Bram Moolenaar5195e452005-08-19 20:32:47 +00002883 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002884 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2885 break;
2886
2887 case SN_REPSAL:
2888 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002889 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002890
Bram Moolenaar5195e452005-08-19 20:32:47 +00002891 case SN_SAL:
2892 res = read_sal_section(fd, lp);
2893 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002894
Bram Moolenaar5195e452005-08-19 20:32:47 +00002895 case SN_SOFO:
2896 res = read_sofo_section(fd, lp);
2897 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002898
Bram Moolenaar5195e452005-08-19 20:32:47 +00002899 case SN_MAP:
2900 p = read_string(fd, len); /* <mapstr> */
2901 if (p == NULL)
2902 goto endFAIL;
2903 set_map_str(lp, p);
2904 vim_free(p);
2905 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002906
Bram Moolenaar4770d092006-01-12 23:22:24 +00002907 case SN_WORDS:
2908 res = read_words_section(fd, lp, len);
2909 break;
2910
2911 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002912 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002913 break;
2914
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002915 case SN_NOSPLITSUGS:
2916 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2917 break;
2918
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 case SN_COMPOUND:
2920 res = read_compound(fd, lp, len);
2921 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002922
Bram Moolenaar78622822005-08-23 21:00:13 +00002923 case SN_NOBREAK:
2924 lp->sl_nobreak = TRUE;
2925 break;
2926
Bram Moolenaar5195e452005-08-19 20:32:47 +00002927 case SN_SYLLABLE:
2928 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2929 if (lp->sl_syllable == NULL)
2930 goto endFAIL;
2931 if (init_syl_tab(lp) == FAIL)
2932 goto endFAIL;
2933 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002934
Bram Moolenaar5195e452005-08-19 20:32:47 +00002935 default:
2936 /* Unsupported section. When it's required give an error
2937 * message. When it's not required skip the contents. */
2938 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002939 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002941 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002942 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 while (--len >= 0)
2944 if (getc(fd) < 0)
2945 goto truncerr;
2946 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002947 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002948someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002949 if (res == SP_FORMERROR)
2950 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002951 EMSG(_(e_format));
2952 goto endFAIL;
2953 }
2954 if (res == SP_TRUNCERROR)
2955 {
2956truncerr:
2957 EMSG(_(e_spell_trunc));
2958 goto endFAIL;
2959 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002960 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002961 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002962 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002963
Bram Moolenaar4770d092006-01-12 23:22:24 +00002964 /* <LWORDTREE> */
2965 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2966 if (res != 0)
2967 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002968
Bram Moolenaar4770d092006-01-12 23:22:24 +00002969 /* <KWORDTREE> */
2970 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2971 if (res != 0)
2972 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973
Bram Moolenaar4770d092006-01-12 23:22:24 +00002974 /* <PREFIXTREE> */
2975 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2976 lp->sl_prefixcnt);
2977 if (res != 0)
2978 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002979
Bram Moolenaarb765d632005-06-07 21:00:02 +00002980 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002981 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002982 {
2983 lp->sl_next = first_lang;
2984 first_lang = lp;
2985 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002986
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002987 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002988
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002989endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002990 if (lang != NULL)
2991 /* truncating the name signals the error to spell_load_lang() */
2992 *lang = NUL;
2993 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002994 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002995 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002996
2997endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002998 if (fd != NULL)
2999 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003000 sourcing_name = save_sourcing_name;
3001 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002
3003 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004}
3005
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003006/*
3007 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003008 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003009 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3011 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003012 */
3013 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003014read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003015 FILE *fd;
3016 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003017 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003018{
3019 int cnt = 0;
3020 int i;
3021 char_u *str;
3022
3023 /* read the length bytes, MSB first */
3024 for (i = 0; i < cnt_bytes; ++i)
3025 cnt = (cnt << 8) + getc(fd);
3026 if (cnt < 0)
3027 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003028 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003029 return NULL;
3030 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003031 *cntp = cnt;
3032 if (cnt == 0)
3033 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003034
Bram Moolenaar5195e452005-08-19 20:32:47 +00003035 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003036 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003037 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003038 return str;
3039}
3040
Bram Moolenaar7887d882005-07-01 22:33:52 +00003041/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003042 * Read SN_REGION: <regionname> ...
3043 * Return SP_*ERROR flags.
3044 */
3045 static int
3046read_region_section(fd, lp, len)
3047 FILE *fd;
3048 slang_T *lp;
3049 int len;
3050{
3051 int i;
3052
3053 if (len > 16)
3054 return SP_FORMERROR;
3055 for (i = 0; i < len; ++i)
3056 lp->sl_regions[i] = getc(fd); /* <regionname> */
3057 lp->sl_regions[len] = NUL;
3058 return 0;
3059}
3060
3061/*
3062 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3063 * <folcharslen> <folchars>
3064 * Return SP_*ERROR flags.
3065 */
3066 static int
3067read_charflags_section(fd)
3068 FILE *fd;
3069{
3070 char_u *flags;
3071 char_u *fol;
3072 int flagslen, follen;
3073
3074 /* <charflagslen> <charflags> */
3075 flags = read_cnt_string(fd, 1, &flagslen);
3076 if (flagslen < 0)
3077 return flagslen;
3078
3079 /* <folcharslen> <folchars> */
3080 fol = read_cnt_string(fd, 2, &follen);
3081 if (follen < 0)
3082 {
3083 vim_free(flags);
3084 return follen;
3085 }
3086
3087 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3088 if (flags != NULL && fol != NULL)
3089 set_spell_charflags(flags, flagslen, fol);
3090
3091 vim_free(flags);
3092 vim_free(fol);
3093
3094 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3095 if ((flags == NULL) != (fol == NULL))
3096 return SP_FORMERROR;
3097 return 0;
3098}
3099
3100/*
3101 * Read SN_PREFCOND section.
3102 * Return SP_*ERROR flags.
3103 */
3104 static int
3105read_prefcond_section(fd, lp)
3106 FILE *fd;
3107 slang_T *lp;
3108{
3109 int cnt;
3110 int i;
3111 int n;
3112 char_u *p;
3113 char_u buf[MAXWLEN + 1];
3114
3115 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003116 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003117 if (cnt <= 0)
3118 return SP_FORMERROR;
3119
3120 lp->sl_prefprog = (regprog_T **)alloc_clear(
3121 (unsigned)sizeof(regprog_T *) * cnt);
3122 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003123 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003124 lp->sl_prefixcnt = cnt;
3125
3126 for (i = 0; i < cnt; ++i)
3127 {
3128 /* <prefcond> : <condlen> <condstr> */
3129 n = getc(fd); /* <condlen> */
3130 if (n < 0 || n >= MAXWLEN)
3131 return SP_FORMERROR;
3132
3133 /* When <condlen> is zero we have an empty condition. Otherwise
3134 * compile the regexp program used to check for the condition. */
3135 if (n > 0)
3136 {
3137 buf[0] = '^'; /* always match at one position only */
3138 p = buf + 1;
3139 while (n-- > 0)
3140 *p++ = getc(fd); /* <condstr> */
3141 *p = NUL;
3142 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3143 }
3144 }
3145 return 0;
3146}
3147
3148/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003149 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150 * Return SP_*ERROR flags.
3151 */
3152 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003153read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003155 garray_T *gap;
3156 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003157{
3158 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003160 int i;
3161
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003162 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003163 if (cnt < 0)
3164 return SP_TRUNCERROR;
3165
Bram Moolenaar5195e452005-08-19 20:32:47 +00003166 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003167 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003168
3169 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3170 for (; gap->ga_len < cnt; ++gap->ga_len)
3171 {
3172 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3173 ftp->ft_from = read_cnt_string(fd, 1, &i);
3174 if (i < 0)
3175 return i;
3176 if (i == 0)
3177 return SP_FORMERROR;
3178 ftp->ft_to = read_cnt_string(fd, 1, &i);
3179 if (i <= 0)
3180 {
3181 vim_free(ftp->ft_from);
3182 if (i < 0)
3183 return i;
3184 return SP_FORMERROR;
3185 }
3186 }
3187
3188 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003189 for (i = 0; i < 256; ++i)
3190 first[i] = -1;
3191 for (i = 0; i < gap->ga_len; ++i)
3192 {
3193 ftp = &((fromto_T *)gap->ga_data)[i];
3194 if (first[*ftp->ft_from] == -1)
3195 first[*ftp->ft_from] = i;
3196 }
3197 return 0;
3198}
3199
3200/*
3201 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3202 * Return SP_*ERROR flags.
3203 */
3204 static int
3205read_sal_section(fd, slang)
3206 FILE *fd;
3207 slang_T *slang;
3208{
3209 int i;
3210 int cnt;
3211 garray_T *gap;
3212 salitem_T *smp;
3213 int ccnt;
3214 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003215 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003216
3217 slang->sl_sofo = FALSE;
3218
3219 i = getc(fd); /* <salflags> */
3220 if (i & SAL_F0LLOWUP)
3221 slang->sl_followup = TRUE;
3222 if (i & SAL_COLLAPSE)
3223 slang->sl_collapse = TRUE;
3224 if (i & SAL_REM_ACCENTS)
3225 slang->sl_rem_accents = TRUE;
3226
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003227 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003228 if (cnt < 0)
3229 return SP_TRUNCERROR;
3230
3231 gap = &slang->sl_sal;
3232 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003233 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003234 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003235
3236 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3237 for (; gap->ga_len < cnt; ++gap->ga_len)
3238 {
3239 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3240 ccnt = getc(fd); /* <salfromlen> */
3241 if (ccnt < 0)
3242 return SP_TRUNCERROR;
3243 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003244 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003245 smp->sm_lead = p;
3246
3247 /* Read up to the first special char into sm_lead. */
3248 for (i = 0; i < ccnt; ++i)
3249 {
3250 c = getc(fd); /* <salfrom> */
3251 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3252 break;
3253 *p++ = c;
3254 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003255 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003256 *p++ = NUL;
3257
3258 /* Put (abc) chars in sm_oneof, if any. */
3259 if (c == '(')
3260 {
3261 smp->sm_oneof = p;
3262 for (++i; i < ccnt; ++i)
3263 {
3264 c = getc(fd); /* <salfrom> */
3265 if (c == ')')
3266 break;
3267 *p++ = c;
3268 }
3269 *p++ = NUL;
3270 if (++i < ccnt)
3271 c = getc(fd);
3272 }
3273 else
3274 smp->sm_oneof = NULL;
3275
3276 /* Any following chars go in sm_rules. */
3277 smp->sm_rules = p;
3278 if (i < ccnt)
3279 /* store the char we got while checking for end of sm_lead */
3280 *p++ = c;
3281 for (++i; i < ccnt; ++i)
3282 *p++ = getc(fd); /* <salfrom> */
3283 *p++ = NUL;
3284
3285 /* <saltolen> <salto> */
3286 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3287 if (ccnt < 0)
3288 {
3289 vim_free(smp->sm_lead);
3290 return ccnt;
3291 }
3292
3293#ifdef FEAT_MBYTE
3294 if (has_mbyte)
3295 {
3296 /* convert the multi-byte strings to wide char strings */
3297 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3298 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3299 if (smp->sm_oneof == NULL)
3300 smp->sm_oneof_w = NULL;
3301 else
3302 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3303 if (smp->sm_to == NULL)
3304 smp->sm_to_w = NULL;
3305 else
3306 smp->sm_to_w = mb_str2wide(smp->sm_to);
3307 if (smp->sm_lead_w == NULL
3308 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3309 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3310 {
3311 vim_free(smp->sm_lead);
3312 vim_free(smp->sm_to);
3313 vim_free(smp->sm_lead_w);
3314 vim_free(smp->sm_oneof_w);
3315 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003316 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003317 }
3318 }
3319#endif
3320 }
3321
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003322 if (gap->ga_len > 0)
3323 {
3324 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3325 * that we need to check the index every time. */
3326 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3327 if ((p = alloc(1)) == NULL)
3328 return SP_OTHERERROR;
3329 p[0] = NUL;
3330 smp->sm_lead = p;
3331 smp->sm_leadlen = 0;
3332 smp->sm_oneof = NULL;
3333 smp->sm_rules = p;
3334 smp->sm_to = NULL;
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 {
3338 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3339 smp->sm_leadlen = 0;
3340 smp->sm_oneof_w = NULL;
3341 smp->sm_to_w = NULL;
3342 }
3343#endif
3344 ++gap->ga_len;
3345 }
3346
Bram Moolenaar5195e452005-08-19 20:32:47 +00003347 /* Fill the first-index table. */
3348 set_sal_first(slang);
3349
3350 return 0;
3351}
3352
3353/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003354 * Read SN_WORDS: <word> ...
3355 * Return SP_*ERROR flags.
3356 */
3357 static int
3358read_words_section(fd, lp, len)
3359 FILE *fd;
3360 slang_T *lp;
3361 int len;
3362{
3363 int done = 0;
3364 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003365 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003366 char_u word[MAXWLEN];
3367
3368 while (done < len)
3369 {
3370 /* Read one word at a time. */
3371 for (i = 0; ; ++i)
3372 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003373 c = getc(fd);
3374 if (c == EOF)
3375 return SP_TRUNCERROR;
3376 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003377 if (word[i] == NUL)
3378 break;
3379 if (i == MAXWLEN - 1)
3380 return SP_FORMERROR;
3381 }
3382
3383 /* Init the count to 10. */
3384 count_common_word(lp, word, -1, 10);
3385 done += i + 1;
3386 }
3387 return 0;
3388}
3389
3390/*
3391 * Add a word to the hashtable of common words.
3392 * If it's already there then the counter is increased.
3393 */
3394 static void
3395count_common_word(lp, word, len, count)
3396 slang_T *lp;
3397 char_u *word;
3398 int len; /* word length, -1 for upto NUL */
3399 int count; /* 1 to count once, 10 to init */
3400{
3401 hash_T hash;
3402 hashitem_T *hi;
3403 wordcount_T *wc;
3404 char_u buf[MAXWLEN];
3405 char_u *p;
3406
3407 if (len == -1)
3408 p = word;
3409 else
3410 {
3411 vim_strncpy(buf, word, len);
3412 p = buf;
3413 }
3414
3415 hash = hash_hash(p);
3416 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3417 if (HASHITEM_EMPTY(hi))
3418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003420 if (wc == NULL)
3421 return;
3422 STRCPY(wc->wc_word, p);
3423 wc->wc_count = count;
3424 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3425 }
3426 else
3427 {
3428 wc = HI2WC(hi);
3429 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3430 wc->wc_count = MAXWORDCOUNT;
3431 }
3432}
3433
3434/*
3435 * Adjust the score of common words.
3436 */
3437 static int
3438score_wordcount_adj(slang, score, word, split)
3439 slang_T *slang;
3440 int score;
3441 char_u *word;
3442 int split; /* word was split, less bonus */
3443{
3444 hashitem_T *hi;
3445 wordcount_T *wc;
3446 int bonus;
3447 int newscore;
3448
3449 hi = hash_find(&slang->sl_wordcount, word);
3450 if (!HASHITEM_EMPTY(hi))
3451 {
3452 wc = HI2WC(hi);
3453 if (wc->wc_count < SCORE_THRES2)
3454 bonus = SCORE_COMMON1;
3455 else if (wc->wc_count < SCORE_THRES3)
3456 bonus = SCORE_COMMON2;
3457 else
3458 bonus = SCORE_COMMON3;
3459 if (split)
3460 newscore = score - bonus / 2;
3461 else
3462 newscore = score - bonus;
3463 if (newscore < 0)
3464 return 0;
3465 return newscore;
3466 }
3467 return score;
3468}
3469
3470/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003471 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3472 * Return SP_*ERROR flags.
3473 */
3474 static int
3475read_sofo_section(fd, slang)
3476 FILE *fd;
3477 slang_T *slang;
3478{
3479 int cnt;
3480 char_u *from, *to;
3481 int res;
3482
3483 slang->sl_sofo = TRUE;
3484
3485 /* <sofofromlen> <sofofrom> */
3486 from = read_cnt_string(fd, 2, &cnt);
3487 if (cnt < 0)
3488 return cnt;
3489
3490 /* <sofotolen> <sofoto> */
3491 to = read_cnt_string(fd, 2, &cnt);
3492 if (cnt < 0)
3493 {
3494 vim_free(from);
3495 return cnt;
3496 }
3497
3498 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3499 if (from != NULL && to != NULL)
3500 res = set_sofo(slang, from, to);
3501 else if (from != NULL || to != NULL)
3502 res = SP_FORMERROR; /* only one of two strings is an error */
3503 else
3504 res = 0;
3505
3506 vim_free(from);
3507 vim_free(to);
3508 return res;
3509}
3510
3511/*
3512 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003513 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003514 * Returns SP_*ERROR flags.
3515 */
3516 static int
3517read_compound(fd, slang, len)
3518 FILE *fd;
3519 slang_T *slang;
3520 int len;
3521{
3522 int todo = len;
3523 int c;
3524 int atstart;
3525 char_u *pat;
3526 char_u *pp;
3527 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003528 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003529 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003530 int cnt;
3531 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003532
3533 if (todo < 2)
3534 return SP_FORMERROR; /* need at least two bytes */
3535
3536 --todo;
3537 c = getc(fd); /* <compmax> */
3538 if (c < 2)
3539 c = MAXWLEN;
3540 slang->sl_compmax = c;
3541
3542 --todo;
3543 c = getc(fd); /* <compminlen> */
3544 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003545 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003546 slang->sl_compminlen = c;
3547
3548 --todo;
3549 c = getc(fd); /* <compsylmax> */
3550 if (c < 1)
3551 c = MAXWLEN;
3552 slang->sl_compsylmax = c;
3553
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003554 c = getc(fd); /* <compoptions> */
3555 if (c != 0)
3556 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3557 else
3558 {
3559 --todo;
3560 c = getc(fd); /* only use the lower byte for now */
3561 --todo;
3562 slang->sl_compoptions = c;
3563
3564 gap = &slang->sl_comppat;
3565 c = get2c(fd); /* <comppatcount> */
3566 todo -= 2;
3567 ga_init2(gap, sizeof(char_u *), c);
3568 if (ga_grow(gap, c) == OK)
3569 while (--c >= 0)
3570 {
3571 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3572 read_cnt_string(fd, 1, &cnt);
3573 /* <comppatlen> <comppattext> */
3574 if (cnt < 0)
3575 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003576 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003577 }
3578 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003579 if (todo < 0)
3580 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003581
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003582 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003583 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003584 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3585 * Conversion to utf-8 may double the size. */
3586 c = todo * 2 + 7;
3587#ifdef FEAT_MBYTE
3588 if (enc_utf8)
3589 c += todo * 2;
3590#endif
3591 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003592 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003593 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003594
Bram Moolenaard12a1322005-08-21 22:08:24 +00003595 /* We also need a list of all flags that can appear at the start and one
3596 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003597 cp = alloc(todo + 1);
3598 if (cp == NULL)
3599 {
3600 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003601 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003602 }
3603 slang->sl_compstartflags = cp;
3604 *cp = NUL;
3605
Bram Moolenaard12a1322005-08-21 22:08:24 +00003606 ap = alloc(todo + 1);
3607 if (ap == NULL)
3608 {
3609 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003610 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003611 }
3612 slang->sl_compallflags = ap;
3613 *ap = NUL;
3614
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003615 /* And a list of all patterns in their original form, for checking whether
3616 * compounding may work in match_compoundrule(). This is freed when we
3617 * encounter a wildcard, the check doesn't work then. */
3618 crp = alloc(todo + 1);
3619 slang->sl_comprules = crp;
3620
Bram Moolenaar5195e452005-08-19 20:32:47 +00003621 pp = pat;
3622 *pp++ = '^';
3623 *pp++ = '\\';
3624 *pp++ = '(';
3625
3626 atstart = 1;
3627 while (todo-- > 0)
3628 {
3629 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003630 if (c == EOF)
3631 {
3632 vim_free(pat);
3633 return SP_TRUNCERROR;
3634 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003635
3636 /* Add all flags to "sl_compallflags". */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003637 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003638 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003639 {
3640 *ap++ = c;
3641 *ap = NUL;
3642 }
3643
Bram Moolenaar5195e452005-08-19 20:32:47 +00003644 if (atstart != 0)
3645 {
3646 /* At start of item: copy flags to "sl_compstartflags". For a
3647 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3648 if (c == '[')
3649 atstart = 2;
3650 else if (c == ']')
3651 atstart = 0;
3652 else
3653 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003654 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003655 {
3656 *cp++ = c;
3657 *cp = NUL;
3658 }
3659 if (atstart == 1)
3660 atstart = 0;
3661 }
3662 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003663
3664 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3665 if (crp != NULL)
3666 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003667 if (c == '?' || c == '+' || c == '*')
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003668 {
3669 vim_free(slang->sl_comprules);
3670 slang->sl_comprules = NULL;
3671 crp = NULL;
3672 }
3673 else
3674 *crp++ = c;
3675 }
3676
Bram Moolenaar5195e452005-08-19 20:32:47 +00003677 if (c == '/') /* slash separates two items */
3678 {
3679 *pp++ = '\\';
3680 *pp++ = '|';
3681 atstart = 1;
3682 }
3683 else /* normal char, "[abc]" and '*' are copied as-is */
3684 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003685 if (c == '?' || c == '+' || c == '~')
3686 *pp++ = '\\'; /* "a?" becomes "a\?", "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003687#ifdef FEAT_MBYTE
3688 if (enc_utf8)
3689 pp += mb_char2bytes(c, pp);
3690 else
3691#endif
3692 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003693 }
3694 }
3695
3696 *pp++ = '\\';
3697 *pp++ = ')';
3698 *pp++ = '$';
3699 *pp = NUL;
3700
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003701 if (crp != NULL)
3702 *crp = NUL;
3703
Bram Moolenaar5195e452005-08-19 20:32:47 +00003704 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3705 vim_free(pat);
3706 if (slang->sl_compprog == NULL)
3707 return SP_FORMERROR;
3708
3709 return 0;
3710}
3711
Bram Moolenaar6de68532005-08-24 22:08:48 +00003712/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003713 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003714 * Like strchr() but independent of locale.
3715 */
3716 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003717byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003718 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003719 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003720{
3721 char_u *p;
3722
3723 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003724 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003725 return TRUE;
3726 return FALSE;
3727}
3728
Bram Moolenaar5195e452005-08-19 20:32:47 +00003729#define SY_MAXLEN 30
3730typedef struct syl_item_S
3731{
3732 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3733 int sy_len;
3734} syl_item_T;
3735
3736/*
3737 * Truncate "slang->sl_syllable" at the first slash and put the following items
3738 * in "slang->sl_syl_items".
3739 */
3740 static int
3741init_syl_tab(slang)
3742 slang_T *slang;
3743{
3744 char_u *p;
3745 char_u *s;
3746 int l;
3747 syl_item_T *syl;
3748
3749 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3750 p = vim_strchr(slang->sl_syllable, '/');
3751 while (p != NULL)
3752 {
3753 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003754 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003755 break;
3756 s = p;
3757 p = vim_strchr(p, '/');
3758 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003759 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003760 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003761 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003762 if (l >= SY_MAXLEN)
3763 return SP_FORMERROR;
3764 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003765 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003766 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3767 + slang->sl_syl_items.ga_len++;
3768 vim_strncpy(syl->sy_chars, s, l);
3769 syl->sy_len = l;
3770 }
3771 return OK;
3772}
3773
3774/*
3775 * Count the number of syllables in "word".
3776 * When "word" contains spaces the syllables after the last space are counted.
3777 * Returns zero if syllables are not defines.
3778 */
3779 static int
3780count_syllables(slang, word)
3781 slang_T *slang;
3782 char_u *word;
3783{
3784 int cnt = 0;
3785 int skip = FALSE;
3786 char_u *p;
3787 int len;
3788 int i;
3789 syl_item_T *syl;
3790 int c;
3791
3792 if (slang->sl_syllable == NULL)
3793 return 0;
3794
3795 for (p = word; *p != NUL; p += len)
3796 {
3797 /* When running into a space reset counter. */
3798 if (*p == ' ')
3799 {
3800 len = 1;
3801 cnt = 0;
3802 continue;
3803 }
3804
3805 /* Find longest match of syllable items. */
3806 len = 0;
3807 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3808 {
3809 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3810 if (syl->sy_len > len
3811 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3812 len = syl->sy_len;
3813 }
3814 if (len != 0) /* found a match, count syllable */
3815 {
3816 ++cnt;
3817 skip = FALSE;
3818 }
3819 else
3820 {
3821 /* No recognized syllable item, at least a syllable char then? */
3822#ifdef FEAT_MBYTE
3823 c = mb_ptr2char(p);
3824 len = (*mb_ptr2len)(p);
3825#else
3826 c = *p;
3827 len = 1;
3828#endif
3829 if (vim_strchr(slang->sl_syllable, c) == NULL)
3830 skip = FALSE; /* No, search for next syllable */
3831 else if (!skip)
3832 {
3833 ++cnt; /* Yes, count it */
3834 skip = TRUE; /* don't count following syllable chars */
3835 }
3836 }
3837 }
3838 return cnt;
3839}
3840
3841/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003842 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003843 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003844 */
3845 static int
3846set_sofo(lp, from, to)
3847 slang_T *lp;
3848 char_u *from;
3849 char_u *to;
3850{
3851 int i;
3852
3853#ifdef FEAT_MBYTE
3854 garray_T *gap;
3855 char_u *s;
3856 char_u *p;
3857 int c;
3858 int *inp;
3859
3860 if (has_mbyte)
3861 {
3862 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3863 * characters. The index is the low byte of the character.
3864 * The list contains from-to pairs with a terminating NUL.
3865 * sl_sal_first[] is used for latin1 "from" characters. */
3866 gap = &lp->sl_sal;
3867 ga_init2(gap, sizeof(int *), 1);
3868 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003869 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003870 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3871 gap->ga_len = 256;
3872
3873 /* First count the number of items for each list. Temporarily use
3874 * sl_sal_first[] for this. */
3875 for (p = from, s = to; *p != NUL && *s != NUL; )
3876 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003877 c = mb_cptr2char_adv(&p);
3878 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003879 if (c >= 256)
3880 ++lp->sl_sal_first[c & 0xff];
3881 }
3882 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003883 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003884
3885 /* Allocate the lists. */
3886 for (i = 0; i < 256; ++i)
3887 if (lp->sl_sal_first[i] > 0)
3888 {
3889 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3890 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003891 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003892 ((int **)gap->ga_data)[i] = (int *)p;
3893 *(int *)p = 0;
3894 }
3895
3896 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3897 * list. */
3898 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3899 for (p = from, s = to; *p != NUL && *s != NUL; )
3900 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003901 c = mb_cptr2char_adv(&p);
3902 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003903 if (c >= 256)
3904 {
3905 /* Append the from-to chars at the end of the list with
3906 * the low byte. */
3907 inp = ((int **)gap->ga_data)[c & 0xff];
3908 while (*inp != 0)
3909 ++inp;
3910 *inp++ = c; /* from char */
3911 *inp++ = i; /* to char */
3912 *inp++ = NUL; /* NUL at the end */
3913 }
3914 else
3915 /* mapping byte to char is done in sl_sal_first[] */
3916 lp->sl_sal_first[c] = i;
3917 }
3918 }
3919 else
3920#endif
3921 {
3922 /* mapping bytes to bytes is done in sl_sal_first[] */
3923 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003924 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003925
3926 for (i = 0; to[i] != NUL; ++i)
3927 lp->sl_sal_first[from[i]] = to[i];
3928 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3929 }
3930
Bram Moolenaar5195e452005-08-19 20:32:47 +00003931 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003932}
3933
3934/*
3935 * Fill the first-index table for "lp".
3936 */
3937 static void
3938set_sal_first(lp)
3939 slang_T *lp;
3940{
3941 salfirst_T *sfirst;
3942 int i;
3943 salitem_T *smp;
3944 int c;
3945 garray_T *gap = &lp->sl_sal;
3946
3947 sfirst = lp->sl_sal_first;
3948 for (i = 0; i < 256; ++i)
3949 sfirst[i] = -1;
3950 smp = (salitem_T *)gap->ga_data;
3951 for (i = 0; i < gap->ga_len; ++i)
3952 {
3953#ifdef FEAT_MBYTE
3954 if (has_mbyte)
3955 /* Use the lowest byte of the first character. For latin1 it's
3956 * the character, for other encodings it should differ for most
3957 * characters. */
3958 c = *smp[i].sm_lead_w & 0xff;
3959 else
3960#endif
3961 c = *smp[i].sm_lead;
3962 if (sfirst[c] == -1)
3963 {
3964 sfirst[c] = i;
3965#ifdef FEAT_MBYTE
3966 if (has_mbyte)
3967 {
3968 int n;
3969
3970 /* Make sure all entries with this byte are following each
3971 * other. Move the ones that are in the wrong position. Do
3972 * keep the same ordering! */
3973 while (i + 1 < gap->ga_len
3974 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3975 /* Skip over entry with same index byte. */
3976 ++i;
3977
3978 for (n = 1; i + n < gap->ga_len; ++n)
3979 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3980 {
3981 salitem_T tsal;
3982
3983 /* Move entry with same index byte after the entries
3984 * we already found. */
3985 ++i;
3986 --n;
3987 tsal = smp[i + n];
3988 mch_memmove(smp + i + 1, smp + i,
3989 sizeof(salitem_T) * n);
3990 smp[i] = tsal;
3991 }
3992 }
3993#endif
3994 }
3995 }
3996}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003997
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003998#ifdef FEAT_MBYTE
3999/*
4000 * Turn a multi-byte string into a wide character string.
4001 * Return it in allocated memory (NULL for out-of-memory)
4002 */
4003 static int *
4004mb_str2wide(s)
4005 char_u *s;
4006{
4007 int *res;
4008 char_u *p;
4009 int i = 0;
4010
4011 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
4012 if (res != NULL)
4013 {
4014 for (p = s; *p != NUL; )
4015 res[i++] = mb_ptr2char_adv(&p);
4016 res[i] = NUL;
4017 }
4018 return res;
4019}
4020#endif
4021
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004022/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004023 * Read a tree from the .spl or .sug file.
4024 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4025 * This is skipped when the tree has zero length.
4026 * Returns zero when OK, SP_ value for an error.
4027 */
4028 static int
4029spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4030 FILE *fd;
4031 char_u **bytsp;
4032 idx_T **idxsp;
4033 int prefixtree; /* TRUE for the prefix tree */
4034 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4035{
4036 int len;
4037 int idx;
4038 char_u *bp;
4039 idx_T *ip;
4040
4041 /* The tree size was computed when writing the file, so that we can
4042 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004043 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004044 if (len < 0)
4045 return SP_TRUNCERROR;
4046 if (len > 0)
4047 {
4048 /* Allocate the byte array. */
4049 bp = lalloc((long_u)len, TRUE);
4050 if (bp == NULL)
4051 return SP_OTHERERROR;
4052 *bytsp = bp;
4053
4054 /* Allocate the index array. */
4055 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4056 if (ip == NULL)
4057 return SP_OTHERERROR;
4058 *idxsp = ip;
4059
4060 /* Recursively read the tree and store it in the array. */
4061 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4062 if (idx < 0)
4063 return idx;
4064 }
4065 return 0;
4066}
4067
4068/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 * Read one row of siblings from the spell file and store it in the byte array
4070 * "byts" and index array "idxs". Recursively read the children.
4071 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004072 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004074 * Returns the index (>= 0) following the siblings.
4075 * Returns SP_TRUNCERROR if the file is shorter than expected.
4076 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004078 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004079read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 FILE *fd;
4081 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004082 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004084 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004085 int prefixtree; /* TRUE for reading PREFIXTREE */
4086 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004088 int len;
4089 int i;
4090 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004091 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004093 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004094#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095
Bram Moolenaar51485f02005-06-04 21:55:20 +00004096 len = getc(fd); /* <siblingcount> */
4097 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004098 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004099
4100 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004101 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004102 byts[idx++] = len;
4103
4104 /* Read the byte values, flag/region bytes and shared indexes. */
4105 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004106 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004107 c = getc(fd); /* <byte> */
4108 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004109 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004110 if (c <= BY_SPECIAL)
4111 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004112 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004113 {
4114 /* No flags, all regions. */
4115 idxs[idx] = 0;
4116 c = 0;
4117 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004118 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004119 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004120 if (prefixtree)
4121 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004122 /* Read the optional pflags byte, the prefix ID and the
4123 * condition nr. In idxs[] store the prefix ID in the low
4124 * byte, the condition index shifted up 8 bits, the flags
4125 * shifted up 24 bits. */
4126 if (c == BY_FLAGS)
4127 c = getc(fd) << 24; /* <pflags> */
4128 else
4129 c = 0;
4130
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004131 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004132
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004133 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004134 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004135 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004136 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004137 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004138 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004139 {
4140 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004141 * idxs[] the flags go in the low two bytes, region above
4142 * that and prefix ID above the region. */
4143 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004144 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004145 if (c2 == BY_FLAGS2)
4146 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004147 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004148 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004149 if (c & WF_AFX)
4150 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004151 }
4152
Bram Moolenaar51485f02005-06-04 21:55:20 +00004153 idxs[idx] = c;
4154 c = 0;
4155 }
4156 else /* c == BY_INDEX */
4157 {
4158 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004159 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004160 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004161 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004162 idxs[idx] = n + SHARED_MASK;
4163 c = getc(fd); /* <xbyte> */
4164 }
4165 }
4166 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 }
4168
Bram Moolenaar51485f02005-06-04 21:55:20 +00004169 /* Recursively read the children for non-shared siblings.
4170 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4171 * remove SHARED_MASK) */
4172 for (i = 1; i <= len; ++i)
4173 if (byts[startidx + i] != 0)
4174 {
4175 if (idxs[startidx + i] & SHARED_MASK)
4176 idxs[startidx + i] &= ~SHARED_MASK;
4177 else
4178 {
4179 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004180 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004181 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004182 if (idx < 0)
4183 break;
4184 }
4185 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004186
Bram Moolenaar51485f02005-06-04 21:55:20 +00004187 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004188}
4189
4190/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004191 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004192 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193 */
4194 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004195did_set_spelllang(wp)
4196 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197{
4198 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004199 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004201 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004202 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004204 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004205 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004208 int len;
4209 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004210 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004211 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004212 char_u *use_region = NULL;
4213 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004214 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004215 int i, j;
4216 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004217 static int recursive = FALSE;
4218 char_u *ret_msg = NULL;
4219 char_u *spl_copy;
4220
4221 /* We don't want to do this recursively. May happen when a language is
4222 * not available and the SpellFileMissing autocommand opens a new buffer
4223 * in which 'spell' is set. */
4224 if (recursive)
4225 return NULL;
4226 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227
4228 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004229 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004230
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004231 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004232 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004233 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004234 if (spl_copy == NULL)
4235 goto theend;
4236
Bram Moolenaar2593e032013-11-14 03:54:07 +01004237#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004238 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004239#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004240
4241 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004242 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004244 /* Get one language name. */
4245 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004246 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004248
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004249 if (STRCMP(lang, "cjk") == 0)
4250 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01004251#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004252 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004253#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004254 continue;
4255 }
4256
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004257 /* If the name ends in ".spl" use it as the name of the spell file.
4258 * If there is a region name let "region" point to it and remove it
4259 * from the name. */
4260 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4261 {
4262 filename = TRUE;
4263
Bram Moolenaarb6356332005-07-18 21:40:44 +00004264 /* Locate a region and remove it from the file name. */
4265 p = vim_strchr(gettail(lang), '_');
4266 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4267 && !ASCII_ISALPHA(p[3]))
4268 {
4269 vim_strncpy(region_cp, p + 1, 2);
4270 mch_memmove(p, p + 3, len - (p - lang) - 2);
4271 len -= 3;
4272 region = region_cp;
4273 }
4274 else
4275 dont_use_region = TRUE;
4276
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004277 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004278 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4279 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004280 break;
4281 }
4282 else
4283 {
4284 filename = FALSE;
4285 if (len > 3 && lang[len - 3] == '_')
4286 {
4287 region = lang + len - 2;
4288 len -= 3;
4289 lang[len] = NUL;
4290 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004291 else
4292 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004293
4294 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4296 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004297 break;
4298 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004299
Bram Moolenaarb6356332005-07-18 21:40:44 +00004300 if (region != NULL)
4301 {
4302 /* If the region differs from what was used before then don't
4303 * use it for 'spellfile'. */
4304 if (use_region != NULL && STRCMP(region, use_region) != 0)
4305 dont_use_region = TRUE;
4306 use_region = region;
4307 }
4308
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004309 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004311 {
4312 if (filename)
4313 (void)spell_load_file(lang, lang, NULL, FALSE);
4314 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004315 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004316 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004317#ifdef FEAT_AUTOCMD
4318 /* SpellFileMissing autocommands may do anything, including
4319 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004320 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004321 {
4322 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4323 goto theend;
4324 }
4325#endif
4326 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004327 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004328
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004329 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004330 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004331 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4333 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4334 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004335 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004336 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004337 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004338 {
4339 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004340 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004341 if (c == REGION_ALL)
4342 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004343 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004344 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004346 /* This addition file is for other regions. */
4347 region_mask = 0;
4348 }
4349 else
4350 /* This is probably an error. Give a warning and
4351 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004352 smsg((char_u *)
4353 _("Warning: region %s not supported"),
4354 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004355 }
4356 else
4357 region_mask = 1 << c;
4358 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004359
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004360 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004361 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004362 if (ga_grow(&ga, 1) == FAIL)
4363 {
4364 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004365 ret_msg = e_outofmem;
4366 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004367 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004368 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004369 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4370 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004371 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004373 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004374 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004375 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004376 }
4377
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004378 /* round 0: load int_wordlist, if possible.
4379 * round 1: load first name in 'spellfile'.
4380 * round 2: load second name in 'spellfile.
4381 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004382 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004383 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004385 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004386 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004387 /* Internal wordlist, if there is one. */
4388 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004389 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004390 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004391 }
4392 else
4393 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004394 /* One entry in 'spellfile'. */
4395 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4396 STRCAT(spf_name, ".spl");
4397
4398 /* If it was already found above then skip it. */
4399 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004400 {
4401 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4402 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004403 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004404 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004405 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004406 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004407 }
4408
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004409 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4411 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004413 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004415 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004416 * region name, the region is ignored otherwise. for int_wordlist
4417 * use an arbitrary name. */
4418 if (round == 0)
4419 STRCPY(lang, "internal wordlist");
4420 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004421 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004422 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004423 p = vim_strchr(lang, '.');
4424 if (p != NULL)
4425 *p = NUL; /* truncate at ".encoding.add" */
4426 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004427 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004428
4429 /* If one of the languages has NOBREAK we assume the addition
4430 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 if (slang != NULL && nobreak)
4432 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004434 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004436 region_mask = REGION_ALL;
4437 if (use_region != NULL && !dont_use_region)
4438 {
4439 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004440 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004441 if (c != REGION_ALL)
4442 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004443 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004444 /* This spell file is for other regions. */
4445 region_mask = 0;
4446 }
4447
4448 if (region_mask != 0)
4449 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004450 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4451 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4452 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004453 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4454 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004455 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 }
4458 }
4459
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004460 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004461 ga_clear(&wp->w_s->b_langp);
4462 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004463
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004464 /* For each language figure out what language to use for sound folding and
4465 * REP items. If the language doesn't support it itself use another one
4466 * with the same name. E.g. for "en-math" use "en". */
4467 for (i = 0; i < ga.ga_len; ++i)
4468 {
4469 lp = LANGP_ENTRY(ga, i);
4470
4471 /* sound folding */
4472 if (lp->lp_slang->sl_sal.ga_len > 0)
4473 /* language does sound folding itself */
4474 lp->lp_sallang = lp->lp_slang;
4475 else
4476 /* find first similar language that does sound folding */
4477 for (j = 0; j < ga.ga_len; ++j)
4478 {
4479 lp2 = LANGP_ENTRY(ga, j);
4480 if (lp2->lp_slang->sl_sal.ga_len > 0
4481 && STRNCMP(lp->lp_slang->sl_name,
4482 lp2->lp_slang->sl_name, 2) == 0)
4483 {
4484 lp->lp_sallang = lp2->lp_slang;
4485 break;
4486 }
4487 }
4488
4489 /* REP items */
4490 if (lp->lp_slang->sl_rep.ga_len > 0)
4491 /* language has REP items itself */
4492 lp->lp_replang = lp->lp_slang;
4493 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004494 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004495 for (j = 0; j < ga.ga_len; ++j)
4496 {
4497 lp2 = LANGP_ENTRY(ga, j);
4498 if (lp2->lp_slang->sl_rep.ga_len > 0
4499 && STRNCMP(lp->lp_slang->sl_name,
4500 lp2->lp_slang->sl_name, 2) == 0)
4501 {
4502 lp->lp_replang = lp2->lp_slang;
4503 break;
4504 }
4505 }
4506 }
4507
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004508theend:
4509 vim_free(spl_copy);
4510 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02004511 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004512 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004513}
4514
4515/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004516 * Clear the midword characters for buffer "buf".
4517 */
4518 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519clear_midword(wp)
4520 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004521{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004522 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004523#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004524 vim_free(wp->w_s->b_spell_ismw_mb);
4525 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004526#endif
4527}
4528
4529/*
4530 * Use the "sl_midword" field of language "lp" for buffer "buf".
4531 * They add up to any currently used midword characters.
4532 */
4533 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004534use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004535 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004536 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004537{
4538 char_u *p;
4539
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004540 if (lp->sl_midword == NULL) /* there aren't any */
4541 return;
4542
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004543 for (p = lp->sl_midword; *p != NUL; )
4544#ifdef FEAT_MBYTE
4545 if (has_mbyte)
4546 {
4547 int c, l, n;
4548 char_u *bp;
4549
4550 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004551 l = (*mb_ptr2len)(p);
4552 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004553 wp->w_s->b_spell_ismw[c] = TRUE;
4554 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004555 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004556 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004557 else
4558 {
4559 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004560 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4561 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004562 if (bp != NULL)
4563 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004564 vim_free(wp->w_s->b_spell_ismw_mb);
4565 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004566 vim_strncpy(bp + n, p, l);
4567 }
4568 }
4569 p += l;
4570 }
4571 else
4572#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004573 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004574}
4575
4576/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 * Find the region "region[2]" in "rp" (points to "sl_regions").
4578 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004579 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004580 */
4581 static int
4582find_region(rp, region)
4583 char_u *rp;
4584 char_u *region;
4585{
4586 int i;
4587
4588 for (i = 0; ; i += 2)
4589 {
4590 if (rp[i] == NUL)
4591 return REGION_ALL;
4592 if (rp[i] == region[0] && rp[i + 1] == region[1])
4593 break;
4594 }
4595 return i / 2;
4596}
4597
4598/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004599 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004601 * Word WF_ONECAP
4602 * W WORD WF_ALLCAP
4603 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004604 */
4605 static int
4606captype(word, end)
4607 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004609{
4610 char_u *p;
4611 int c;
4612 int firstcap;
4613 int allcap;
4614 int past_second = FALSE; /* past second word char */
4615
4616 /* find first letter */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004617 for (p = word; !spell_iswordp_nmw(p, curwin); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004618 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004619 return 0; /* only non-word characters, illegal word */
4620#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004621 if (has_mbyte)
4622 c = mb_ptr2char_adv(&p);
4623 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004625 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004626 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004627
4628 /*
4629 * Need to check all letters to find a word with mixed upper/lower.
4630 * But a word with an upper char only at start is a ONECAP.
4631 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004633 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004635 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004636 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 {
4638 /* UUl -> KEEPCAP */
4639 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004640 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641 allcap = FALSE;
4642 }
4643 else if (!allcap)
4644 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004645 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 past_second = TRUE;
4647 }
4648
4649 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004650 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004651 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004652 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004653 return 0;
4654}
4655
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004656/*
4657 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4658 * capital. So that make_case_word() can turn WOrd into Word.
4659 * Add ALLCAP for "WOrD".
4660 */
4661 static int
4662badword_captype(word, end)
4663 char_u *word;
4664 char_u *end;
4665{
4666 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004667 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004668 int l, u;
4669 int first;
4670 char_u *p;
4671
4672 if (flags & WF_KEEPCAP)
4673 {
4674 /* Count the number of UPPER and lower case letters. */
4675 l = u = 0;
4676 first = FALSE;
4677 for (p = word; p < end; mb_ptr_adv(p))
4678 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004679 c = PTR2CHAR(p);
4680 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004681 {
4682 ++u;
4683 if (p == word)
4684 first = TRUE;
4685 }
4686 else
4687 ++l;
4688 }
4689
4690 /* If there are more UPPER than lower case letters suggest an
4691 * ALLCAP word. Otherwise, if the first letter is UPPER then
4692 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4693 * require three upper case letters. */
4694 if (u > l && u > 2)
4695 flags |= WF_ALLCAP;
4696 else if (first)
4697 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004698
4699 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4700 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004701 }
4702 return flags;
4703}
4704
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004705/*
4706 * Delete the internal wordlist and its .spl file.
4707 */
4708 void
4709spell_delete_wordlist()
4710{
4711 char_u fname[MAXPATHL];
4712
4713 if (int_wordlist != NULL)
4714 {
4715 mch_remove(int_wordlist);
4716 int_wordlist_spl(fname);
4717 mch_remove(fname);
4718 vim_free(int_wordlist);
4719 int_wordlist = NULL;
4720 }
4721}
4722
4723#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004724/*
4725 * Free all languages.
4726 */
4727 void
4728spell_free_all()
4729{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004730 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004731 buf_T *buf;
4732
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004733 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004734 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004736
4737 while (first_lang != NULL)
4738 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004739 slang = first_lang;
4740 first_lang = slang->sl_next;
4741 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004742 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004743
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004744 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00004745
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004746 vim_free(repl_to);
4747 repl_to = NULL;
4748 vim_free(repl_from);
4749 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004750}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004751#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004752
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004753#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004754/*
4755 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004756 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757 */
4758 void
4759spell_reload()
4760{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004761 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004762
Bram Moolenaarea408852005-06-25 22:49:46 +00004763 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764 init_spell_chartab();
4765
4766 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004767 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768
4769 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004770 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004771 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004772 /* Only load the wordlists when 'spelllang' is set and there is a
4773 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004774 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004775 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004776 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004777 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004778 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004779# ifdef FEAT_WINDOWS
4780 break;
4781# endif
4782 }
4783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004784 }
4785}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004786#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004787
Bram Moolenaarb765d632005-06-07 21:00:02 +00004788/*
4789 * Reload the spell file "fname" if it's loaded.
4790 */
4791 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004793 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004795{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004796 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004798
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004799 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004800 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004801 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004802 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004803 slang_clear(slang);
4804 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004805 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004806 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004807 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004809 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811
4812 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004813 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004815 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004816}
4817
4818
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004819/*
4820 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 */
4822
Bram Moolenaar51485f02005-06-04 21:55:20 +00004823#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004824 and .dic file. */
4825/*
4826 * Main structure to store the contents of a ".aff" file.
4827 */
4828typedef struct afffile_S
4829{
4830 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004831 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004832 unsigned af_rare; /* RARE ID for rare word */
4833 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004834 unsigned af_bad; /* BAD ID for banned word */
4835 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004836 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004837 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004838 unsigned af_comproot; /* COMPOUNDROOT ID */
4839 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4840 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004841 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004842 int af_pfxpostpone; /* postpone prefixes without chop string and
4843 without flags */
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02004844 int af_ignoreextra; /* IGNOREEXTRA present */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004845 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4846 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004847 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004848} afffile_T;
4849
Bram Moolenaar6de68532005-08-24 22:08:48 +00004850#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004851#define AFT_LONG 1 /* flags are two characters */
4852#define AFT_CAPLONG 2 /* flags are one or two characters */
4853#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004854
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004855typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004856/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4857struct affentry_S
4858{
4859 affentry_T *ae_next; /* next affix with same name/number */
4860 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4861 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004862 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004863 char_u *ae_cond; /* condition (NULL for ".") */
4864 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004865 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4866 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004867};
4868
Bram Moolenaar6de68532005-08-24 22:08:48 +00004869#ifdef FEAT_MBYTE
4870# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4871#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004872# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004873#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004874
Bram Moolenaar51485f02005-06-04 21:55:20 +00004875/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4876typedef struct affheader_S
4877{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004878 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4879 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4880 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004881 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004882 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004883 affentry_T *ah_first; /* first affix entry */
4884} affheader_T;
4885
4886#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4887
Bram Moolenaar6de68532005-08-24 22:08:48 +00004888/* Flag used in compound items. */
4889typedef struct compitem_S
4890{
4891 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4892 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4893 int ci_newID; /* affix ID after renumbering. */
4894} compitem_T;
4895
4896#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4897
Bram Moolenaar51485f02005-06-04 21:55:20 +00004898/*
4899 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004900 * the need to keep track of each allocated thing, everything is freed all at
4901 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004902 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4903 * "sb_data" is correct for systems where pointers must be aligned on
4904 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004905 */
4906#define SBLOCKSIZE 16000 /* size of sb_data */
4907typedef struct sblock_S sblock_T;
4908struct sblock_S
4909{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004910 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004911 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004913};
4914
4915/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004917 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004918typedef struct wordnode_S wordnode_T;
4919struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004920{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004921 union /* shared to save space */
4922 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004923 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004924 int index; /* index in written nodes (valid after first
4925 round) */
4926 } wn_u1;
4927 union /* shared to save space */
4928 {
4929 wordnode_T *next; /* next node with same hash key */
4930 wordnode_T *wnode; /* parent node that will write this node */
4931 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004932 wordnode_T *wn_child; /* child (next byte in word) */
4933 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4934 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004935 int wn_refs; /* Nr. of references to this node. Only
4936 relevant for first node in a list of
4937 siblings, in following siblings it is
4938 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004939 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004940
4941 /* Info for when "wn_byte" is NUL.
4942 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4943 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4944 * "wn_region" the LSW of the wordnr. */
4945 char_u wn_affixID; /* supported/required prefix ID or 0 */
4946 short_u wn_flags; /* WF_ flags */
4947 short wn_region; /* region mask */
4948
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004949#ifdef SPELL_PRINTTREE
4950 int wn_nr; /* sequence nr for printing */
4951#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952};
4953
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004954#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4955
Bram Moolenaar51485f02005-06-04 21:55:20 +00004956#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004957
Bram Moolenaar51485f02005-06-04 21:55:20 +00004958/*
4959 * Info used while reading the spell files.
4960 */
4961typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004962{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004963 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004964 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004965
Bram Moolenaar51485f02005-06-04 21:55:20 +00004966 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004967 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004968
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004969 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004970
Bram Moolenaar4770d092006-01-12 23:22:24 +00004971 long si_sugtree; /* creating the soundfolding trie */
4972
Bram Moolenaar51485f02005-06-04 21:55:20 +00004973 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004974 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004975 int si_did_emsg; /* TRUE when ran out of memory */
4976
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004977 long si_compress_cnt; /* words to add before lowering
4978 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004979 wordnode_T *si_first_free; /* List of nodes that have been freed during
4980 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004981 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004982#ifdef SPELL_PRINTTREE
4983 int si_wordnode_nr; /* sequence nr for nodes */
4984#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004985 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004986
Bram Moolenaar51485f02005-06-04 21:55:20 +00004987 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004988 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004989 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004990 int si_region; /* region mask */
4991 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004992 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004993 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004994 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004995 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004996 int si_region_count; /* number of regions supported (1 when there
4997 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004998 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004999 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005000
5001 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005002 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005004 char_u *si_sofofr; /* SOFOFROM text */
5005 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005006 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005007 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005008 int si_followup; /* soundsalike: ? */
5009 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005010 hashtab_T si_commonwords; /* hashtable for common words */
5011 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005012 int si_rem_accents; /* soundsalike: remove accents */
5013 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005014 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005015 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005016 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005017 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005018 int si_compoptions; /* COMP_ flags */
5019 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
5020 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005021 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00005022 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005023 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005024 garray_T si_prefcond; /* table with conditions for postponed
5025 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005026 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005027 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005028} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005029
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005030static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005031static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005032static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005033static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005034static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5035static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5036static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005037static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005038static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5039static void aff_check_number __ARGS((int spinval, int affval, char *name));
5040static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005041static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005042static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5043static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005045static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005046static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005047static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005048static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005049static 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 +00005050static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5051static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5052static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005053static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005054static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005055static 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 +00005056static 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 +00005057static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005058static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005059static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5060static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5061static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005062static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005063static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005064static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005065static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005066static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5067static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5068static int sug_maketable __ARGS((spellinfo_T *spin));
5069static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5070static int offset2bytes __ARGS((int nr, char_u *buf));
5071static int bytes2offset __ARGS((char_u **pp));
5072static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005073static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int over_write, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005074static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005075static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005076
Bram Moolenaar53805d12005-08-01 07:08:33 +00005077/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5078 * but it must be negative to indicate the prefix tree to tree_add_word().
5079 * Use a negative number with the lower 8 bits zero. */
5080#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005081
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005082/* flags for "condit" argument of store_aff_word() */
5083#define CONDIT_COMB 1 /* affix must combine */
5084#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5085#define CONDIT_SUF 4 /* add a suffix for matching flags */
5086#define CONDIT_AFF 8 /* word already has an affix */
5087
Bram Moolenaar5195e452005-08-19 20:32:47 +00005088/*
5089 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5090 */
5091static long compress_start = 30000; /* memory / SBLOCKSIZE */
5092static long compress_inc = 100; /* memory / SBLOCKSIZE */
5093static long compress_added = 500000; /* word count */
5094
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005095#ifdef SPELL_PRINTTREE
5096/*
5097 * For debugging the tree code: print the current tree in a (more or less)
5098 * readable format, so that we can see what happens when adding a word and/or
5099 * compressing the tree.
5100 * Based on code from Olaf Seibert.
5101 */
5102#define PRINTLINESIZE 1000
5103#define PRINTWIDTH 6
5104
5105#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5106 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5107
5108static char line1[PRINTLINESIZE];
5109static char line2[PRINTLINESIZE];
5110static char line3[PRINTLINESIZE];
5111
5112 static void
5113spell_clear_flags(wordnode_T *node)
5114{
5115 wordnode_T *np;
5116
5117 for (np = node; np != NULL; np = np->wn_sibling)
5118 {
5119 np->wn_u1.index = FALSE;
5120 spell_clear_flags(np->wn_child);
5121 }
5122}
5123
5124 static void
5125spell_print_node(wordnode_T *node, int depth)
5126{
5127 if (node->wn_u1.index)
5128 {
5129 /* Done this node before, print the reference. */
5130 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5131 PRINTSOME(line2, depth, " ", 0, 0);
5132 PRINTSOME(line3, depth, " ", 0, 0);
5133 msg(line1);
5134 msg(line2);
5135 msg(line3);
5136 }
5137 else
5138 {
5139 node->wn_u1.index = TRUE;
5140
5141 if (node->wn_byte != NUL)
5142 {
5143 if (node->wn_child != NULL)
5144 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5145 else
5146 /* Cannot happen? */
5147 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5148 }
5149 else
5150 PRINTSOME(line1, depth, " $ ", 0, 0);
5151
5152 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5153
5154 if (node->wn_sibling != NULL)
5155 PRINTSOME(line3, depth, " | ", 0, 0);
5156 else
5157 PRINTSOME(line3, depth, " ", 0, 0);
5158
5159 if (node->wn_byte == NUL)
5160 {
5161 msg(line1);
5162 msg(line2);
5163 msg(line3);
5164 }
5165
5166 /* do the children */
5167 if (node->wn_byte != NUL && node->wn_child != NULL)
5168 spell_print_node(node->wn_child, depth + 1);
5169
5170 /* do the siblings */
5171 if (node->wn_sibling != NULL)
5172 {
5173 /* get rid of all parent details except | */
5174 STRCPY(line1, line3);
5175 STRCPY(line2, line3);
5176 spell_print_node(node->wn_sibling, depth);
5177 }
5178 }
5179}
5180
5181 static void
5182spell_print_tree(wordnode_T *root)
5183{
5184 if (root != NULL)
5185 {
5186 /* Clear the "wn_u1.index" fields, used to remember what has been
5187 * done. */
5188 spell_clear_flags(root);
5189
5190 /* Recursively print the tree. */
5191 spell_print_node(root, 0);
5192 }
5193}
5194#endif /* SPELL_PRINTTREE */
5195
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005198 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005199 */
5200 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005201spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005202 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005203 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005204{
5205 FILE *fd;
5206 afffile_T *aff;
5207 char_u rline[MAXLINELEN];
5208 char_u *line;
5209 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005210#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005211 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005212 int itemcnt;
5213 char_u *p;
5214 int lnum = 0;
5215 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005216 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005217 int aff_todo = 0;
5218 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005219 char_u *low = NULL;
5220 char_u *fol = NULL;
5221 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005223 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005225 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005227 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005228 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005229 int compminlen = 0; /* COMPOUNDMIN value */
5230 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005231 int compoptions = 0; /* COMP_ flags */
5232 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005233 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005234 concatenated */
5235 char_u *midword = NULL; /* MIDWORD value */
5236 char_u *syllable = NULL; /* SYLLABLE value */
5237 char_u *sofofrom = NULL; /* SOFOFROM value */
5238 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005239
Bram Moolenaar51485f02005-06-04 21:55:20 +00005240 /*
5241 * Open the file.
5242 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005243 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005244 if (fd == NULL)
5245 {
5246 EMSG2(_(e_notopen), fname);
5247 return NULL;
5248 }
5249
Bram Moolenaar4770d092006-01-12 23:22:24 +00005250 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5251 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 /* Only do REP lines when not done in another .aff file already. */
5254 do_rep = spin->si_rep.ga_len == 0;
5255
Bram Moolenaar4770d092006-01-12 23:22:24 +00005256 /* Only do REPSAL lines when not done in another .aff file already. */
5257 do_repsal = spin->si_repsal.ga_len == 0;
5258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005259 /* Only do SAL lines when not done in another .aff file already. */
5260 do_sal = spin->si_sal.ga_len == 0;
5261
5262 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005263 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005264
Bram Moolenaar51485f02005-06-04 21:55:20 +00005265 /*
5266 * Allocate and init the afffile_T structure.
5267 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005268 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005269 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005270 {
5271 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005272 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005273 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005274 hash_init(&aff->af_pref);
5275 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005276 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005277
5278 /*
5279 * Read all the lines in the file one by one.
5280 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005281 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005282 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005283 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005284 ++lnum;
5285
5286 /* Skip comment lines. */
5287 if (*rline == '#')
5288 continue;
5289
5290 /* Convert from "SET" to 'encoding' when needed. */
5291 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005292#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005293 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005294 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005295 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005296 if (pc == NULL)
5297 {
5298 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5299 fname, lnum, rline);
5300 continue;
5301 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005302 line = pc;
5303 }
5304 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005305#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005306 {
5307 pc = NULL;
5308 line = rline;
5309 }
5310
5311 /* Split the line up in white separated items. Put a NUL after each
5312 * item. */
5313 itemcnt = 0;
5314 for (p = line; ; )
5315 {
5316 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5317 ++p;
5318 if (*p == NUL)
5319 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005320 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005321 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005322 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005323 /* A few items have arbitrary text argument, don't split them. */
5324 if (itemcnt == 2 && spell_info_item(items[0]))
5325 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5326 ++p;
5327 else
5328 while (*p > ' ') /* skip until white space or CR/NL */
5329 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005330 if (*p == NUL)
5331 break;
5332 *p++ = NUL;
5333 }
5334
5335 /* Handle non-empty lines. */
5336 if (itemcnt > 0)
5337 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005338 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005339 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005340#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005341 /* Setup for conversion from "ENC" to 'encoding'. */
5342 aff->af_enc = enc_canonize(items[1]);
5343 if (aff->af_enc != NULL && !spin->si_ascii
5344 && convert_setup(&spin->si_conv, aff->af_enc,
5345 p_enc) == FAIL)
5346 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5347 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005348 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005349#else
5350 smsg((char_u *)_("Conversion in %s not supported"), fname);
5351#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005352 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005353 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005354 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005355 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005356 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005357 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005358 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005359 aff->af_flagtype = AFT_NUM;
5360 else if (STRCMP(items[1], "caplong") == 0)
5361 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005362 else
5363 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5364 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005365 if (aff->af_rare != 0
5366 || aff->af_keepcase != 0
5367 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005368 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005369 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005370 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005371 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005372 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005373 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005374 || aff->af_suff.ht_used > 0
5375 || aff->af_pref.ht_used > 0)
5376 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5377 fname, lnum, items[1]);
5378 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005379 else if (spell_info_item(items[0]))
5380 {
5381 p = (char_u *)getroom(spin,
5382 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5383 + STRLEN(items[0])
5384 + STRLEN(items[1]) + 3, FALSE);
5385 if (p != NULL)
5386 {
5387 if (spin->si_info != NULL)
5388 {
5389 STRCPY(p, spin->si_info);
5390 STRCAT(p, "\n");
5391 }
5392 STRCAT(p, items[0]);
5393 STRCAT(p, " ");
5394 STRCAT(p, items[1]);
5395 spin->si_info = p;
5396 }
5397 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005398 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005399 && midword == NULL)
5400 {
5401 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005402 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005403 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005405 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005406 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005407 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005408 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5409 || is_aff_rule(items, itemcnt, "RARE", 2))
5410 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005411 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005412 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005413 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005414 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005415 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005416 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5417 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005418 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005419 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005420 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005421 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005422 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005423 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5424 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5425 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005426 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005427 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5428 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005429 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005430 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005431 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005432 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005433 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5434 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005435 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005436 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005437 && aff->af_circumfix == 0)
5438 {
5439 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5440 fname, lnum);
5441 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005442 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005443 && aff->af_nosuggest == 0)
5444 {
5445 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5446 fname, lnum);
5447 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005448 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5449 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005450 && aff->af_needcomp == 0)
5451 {
5452 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5453 fname, lnum);
5454 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005455 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005456 && aff->af_comproot == 0)
5457 {
5458 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5459 fname, lnum);
5460 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005461 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5462 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005463 {
5464 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5465 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005466 if (aff->af_pref.ht_used > 0)
5467 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5468 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005469 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005470 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5471 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005472 {
5473 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5474 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005475 if (aff->af_pref.ht_used > 0)
5476 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5477 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005478 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005479 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005480 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005481 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005482 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005483 * "Na" into "Na+", "1234" into "1234+". */
5484 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005485 if (p != NULL)
5486 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005487 STRCPY(p, items[1]);
5488 STRCAT(p, "+");
5489 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005490 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005491 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005492 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5493 {
5494 /* We don't use the count, but do check that it's a number and
5495 * not COMPOUNDRULE mistyped. */
5496 if (atoi((char *)items[1]) == 0)
5497 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5498 fname, lnum, items[1]);
5499 }
5500 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005501 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005502 /* Don't use the first rule if it is a number. */
5503 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005504 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005505 /* Concatenate this string to previously defined ones,
5506 * using a slash to separate them. */
5507 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005508 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005509 l += (int)STRLEN(compflags) + 1;
5510 p = getroom(spin, l, FALSE);
5511 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005512 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005513 if (compflags != NULL)
5514 {
5515 STRCPY(p, compflags);
5516 STRCAT(p, "/");
5517 }
5518 STRCAT(p, items[1]);
5519 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005520 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005521 }
5522 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005523 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005524 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005525 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005526 compmax = atoi((char *)items[1]);
5527 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005528 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005529 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005530 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005531 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005532 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005533 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005534 compminlen = atoi((char *)items[1]);
5535 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005536 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5537 fname, lnum, items[1]);
5538 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005539 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005540 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005541 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005542 compsylmax = atoi((char *)items[1]);
5543 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005544 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5545 fname, lnum, items[1]);
5546 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005547 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005548 {
5549 compoptions |= COMP_CHECKDUP;
5550 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005551 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005552 {
5553 compoptions |= COMP_CHECKREP;
5554 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005555 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005556 {
5557 compoptions |= COMP_CHECKCASE;
5558 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005559 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005560 {
5561 compoptions |= COMP_CHECKTRIPLE;
5562 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005563 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005564 {
5565 if (atoi((char *)items[1]) == 0)
5566 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5567 fname, lnum, items[1]);
5568 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005569 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005570 {
5571 garray_T *gap = &spin->si_comppat;
5572 int i;
5573
5574 /* Only add the couple if it isn't already there. */
5575 for (i = 0; i < gap->ga_len - 1; i += 2)
5576 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5577 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5578 items[2]) == 0)
5579 break;
5580 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5581 {
5582 ((char_u **)(gap->ga_data))[gap->ga_len++]
5583 = getroom_save(spin, items[1]);
5584 ((char_u **)(gap->ga_data))[gap->ga_len++]
5585 = getroom_save(spin, items[2]);
5586 }
5587 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005588 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005589 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005590 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005591 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005592 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005593 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005594 {
5595 spin->si_nobreak = TRUE;
5596 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005597 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005598 {
5599 spin->si_nosplitsugs = TRUE;
5600 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005601 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005602 {
5603 spin->si_nosugfile = TRUE;
5604 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005605 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005606 {
5607 aff->af_pfxpostpone = TRUE;
5608 }
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005609 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
5610 {
5611 aff->af_ignoreextra = TRUE;
5612 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005613 else if ((STRCMP(items[0], "PFX") == 0
5614 || STRCMP(items[0], "SFX") == 0)
5615 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005616 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005617 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005618 int lasti = 4;
5619 char_u key[AH_KEY_LEN];
5620
5621 if (*items[0] == 'P')
5622 tp = &aff->af_pref;
5623 else
5624 tp = &aff->af_suff;
5625
5626 /* Myspell allows the same affix name to be used multiple
5627 * times. The affix files that do this have an undocumented
5628 * "S" flag on all but the last block, thus we check for that
5629 * and store it in ah_follows. */
5630 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5631 hi = hash_find(tp, key);
5632 if (!HASHITEM_EMPTY(hi))
5633 {
5634 cur_aff = HI2AH(hi);
5635 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5636 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5637 fname, lnum, items[1]);
5638 if (!cur_aff->ah_follows)
5639 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5640 fname, lnum, items[1]);
5641 }
5642 else
5643 {
5644 /* New affix letter. */
5645 cur_aff = (affheader_T *)getroom(spin,
5646 sizeof(affheader_T), TRUE);
5647 if (cur_aff == NULL)
5648 break;
5649 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5650 fname, lnum);
5651 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5652 break;
5653 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005654 || cur_aff->ah_flag == aff->af_rare
5655 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005656 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005657 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005658 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005659 || cur_aff->ah_flag == aff->af_needcomp
5660 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005661 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 +00005662 fname, lnum, items[1]);
5663 STRCPY(cur_aff->ah_key, items[1]);
5664 hash_add(tp, cur_aff->ah_key);
5665
5666 cur_aff->ah_combine = (*items[2] == 'Y');
5667 }
5668
5669 /* Check for the "S" flag, which apparently means that another
5670 * block with the same affix name is following. */
5671 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5672 {
5673 ++lasti;
5674 cur_aff->ah_follows = TRUE;
5675 }
5676 else
5677 cur_aff->ah_follows = FALSE;
5678
Bram Moolenaar8db73182005-06-17 21:51:16 +00005679 /* Myspell allows extra text after the item, but that might
5680 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005681 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005682 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005683
Bram Moolenaar95529562005-08-25 21:21:38 +00005684 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005685 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5686 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005687
Bram Moolenaar95529562005-08-25 21:21:38 +00005688 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005689 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005690 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005691 {
5692 /* Use a new number in the .spl file later, to be able
5693 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005694 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005695 cur_aff->ah_newID = ++spin->si_newprefID;
5696
5697 /* We only really use ah_newID if the prefix is
5698 * postponed. We know that only after handling all
5699 * the items. */
5700 did_postpone_prefix = FALSE;
5701 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005702 else
5703 /* Did use the ID in a previous block. */
5704 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005705 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005706
Bram Moolenaar51485f02005-06-04 21:55:20 +00005707 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005708 }
5709 else if ((STRCMP(items[0], "PFX") == 0
5710 || STRCMP(items[0], "SFX") == 0)
5711 && aff_todo > 0
5712 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005713 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005714 {
5715 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005716 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005717 int lasti = 5;
5718
Bram Moolenaar8db73182005-06-17 21:51:16 +00005719 /* Myspell allows extra text after the item, but that might
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005720 * mean mistakes go unnoticed. Require a comment-starter,
5721 * unless IGNOREEXTRA is used. Hunspell uses a "-" item. */
5722 if (itemcnt > lasti
5723 && !aff->af_ignoreextra
5724 && *items[lasti] != '#'
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005725 && (STRCMP(items[lasti], "-") != 0
5726 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005727 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005728
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005729 /* New item for an affix letter. */
5730 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005731 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005732 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005733 if (aff_entry == NULL)
5734 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005735
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005736 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005737 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005738 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005739 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005740 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005741
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005742 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005743 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5744 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005745 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005746 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005747 aff_process_flags(aff, aff_entry);
5748 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005749 }
5750
Bram Moolenaar51485f02005-06-04 21:55:20 +00005751 /* Don't use an affix entry with non-ASCII characters when
5752 * "spin->si_ascii" is TRUE. */
5753 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005754 || has_non_ascii(aff_entry->ae_add)))
5755 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005756 aff_entry->ae_next = cur_aff->ah_first;
5757 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005758
5759 if (STRCMP(items[4], ".") != 0)
5760 {
5761 char_u buf[MAXLINELEN];
5762
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005763 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005764 if (*items[0] == 'P')
5765 sprintf((char *)buf, "^%s", items[4]);
5766 else
5767 sprintf((char *)buf, "%s$", items[4]);
5768 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005769 RE_MAGIC + RE_STRING + RE_STRICT);
5770 if (aff_entry->ae_prog == NULL)
5771 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5772 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005773 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005774
5775 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005776 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005777 * Can't be done for an affix with flags, ignoring
5778 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005779 if (*items[0] == 'P' && aff->af_pfxpostpone
5780 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005781 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005782 /* When the chop string is one lower-case letter and
5783 * the add string ends in the upper-case letter we set
5784 * the "upper" flag, clear "ae_chop" and remove the
5785 * letters from "ae_add". The condition must either
5786 * be empty or start with the same letter. */
5787 if (aff_entry->ae_chop != NULL
5788 && aff_entry->ae_add != NULL
5789#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005790 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005791 aff_entry->ae_chop)] == NUL
5792#else
5793 && aff_entry->ae_chop[1] == NUL
5794#endif
5795 )
5796 {
5797 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005798
Bram Moolenaar53805d12005-08-01 07:08:33 +00005799 c = PTR2CHAR(aff_entry->ae_chop);
5800 c_up = SPELL_TOUPPER(c);
5801 if (c_up != c
5802 && (aff_entry->ae_cond == NULL
5803 || PTR2CHAR(aff_entry->ae_cond) == c))
5804 {
5805 p = aff_entry->ae_add
5806 + STRLEN(aff_entry->ae_add);
5807 mb_ptr_back(aff_entry->ae_add, p);
5808 if (PTR2CHAR(p) == c_up)
5809 {
5810 upper = TRUE;
5811 aff_entry->ae_chop = NULL;
5812 *p = NUL;
5813
5814 /* The condition is matched with the
5815 * actual word, thus must check for the
5816 * upper-case letter. */
5817 if (aff_entry->ae_cond != NULL)
5818 {
5819 char_u buf[MAXLINELEN];
5820#ifdef FEAT_MBYTE
5821 if (has_mbyte)
5822 {
5823 onecap_copy(items[4], buf, TRUE);
5824 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005825 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005826 }
5827 else
5828#endif
5829 *aff_entry->ae_cond = c_up;
5830 if (aff_entry->ae_cond != NULL)
5831 {
5832 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005833 aff_entry->ae_cond);
Bram Moolenaar473de612013-06-08 18:19:48 +02005834 vim_regfree(aff_entry->ae_prog);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005835 aff_entry->ae_prog = vim_regcomp(
5836 buf, RE_MAGIC + RE_STRING);
5837 }
5838 }
5839 }
5840 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005841 }
5842
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005843 if (aff_entry->ae_chop == NULL
5844 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005845 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005846 int idx;
5847 char_u **pp;
5848 int n;
5849
Bram Moolenaar6de68532005-08-24 22:08:48 +00005850 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005851 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5852 --idx)
5853 {
5854 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5855 if (str_equal(p, aff_entry->ae_cond))
5856 break;
5857 }
5858 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5859 {
5860 /* Not found, add a new condition. */
5861 idx = spin->si_prefcond.ga_len++;
5862 pp = ((char_u **)spin->si_prefcond.ga_data)
5863 + idx;
5864 if (aff_entry->ae_cond == NULL)
5865 *pp = NULL;
5866 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005867 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005868 aff_entry->ae_cond);
5869 }
5870
5871 /* Add the prefix to the prefix tree. */
5872 if (aff_entry->ae_add == NULL)
5873 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005874 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005875 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005876
Bram Moolenaar53805d12005-08-01 07:08:33 +00005877 /* PFX_FLAGS is a negative number, so that
5878 * tree_add_word() knows this is the prefix tree. */
5879 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005880 if (!cur_aff->ah_combine)
5881 n |= WFP_NC;
5882 if (upper)
5883 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005884 if (aff_entry->ae_comppermit)
5885 n |= WFP_COMPPERMIT;
5886 if (aff_entry->ae_compforbid)
5887 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005888 tree_add_word(spin, p, spin->si_prefroot, n,
5889 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005890 did_postpone_prefix = TRUE;
5891 }
5892
5893 /* Didn't actually use ah_newID, backup si_newprefID. */
5894 if (aff_todo == 0 && !did_postpone_prefix)
5895 {
5896 --spin->si_newprefID;
5897 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005898 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005899 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005900 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005901 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005902 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005903 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005904 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005905 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005906 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005907 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005908 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005909 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005910 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005911 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005912 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005913 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005914 else if (is_aff_rule(items, itemcnt, "REP", 2)
5915 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005916 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005917 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005918 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005919 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005920 fname, lnum);
5921 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005922 else if ((STRCMP(items[0], "REP") == 0
5923 || STRCMP(items[0], "REPSAL") == 0)
5924 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005925 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005926 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005927 /* Myspell ignores extra arguments, we require it starts with
5928 * # to detect mistakes. */
5929 if (itemcnt > 3 && items[3][0] != '#')
5930 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005931 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005932 {
5933 /* Replace underscore with space (can't include a space
5934 * directly). */
5935 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5936 if (*p == '_')
5937 *p = ' ';
5938 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5939 if (*p == '_')
5940 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005941 add_fromto(spin, items[0][3] == 'S'
5942 ? &spin->si_repsal
5943 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005944 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005945 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005946 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005947 {
5948 /* MAP item or count */
5949 if (!found_map)
5950 {
5951 /* First line contains the count. */
5952 found_map = TRUE;
5953 if (!isdigit(*items[1]))
5954 smsg((char_u *)_("Expected MAP count in %s line %d"),
5955 fname, lnum);
5956 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005957 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005958 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005959 int c;
5960
5961 /* Check that every character appears only once. */
5962 for (p = items[1]; *p != NUL; )
5963 {
5964#ifdef FEAT_MBYTE
5965 c = mb_ptr2char_adv(&p);
5966#else
5967 c = *p++;
5968#endif
5969 if ((spin->si_map.ga_len > 0
5970 && vim_strchr(spin->si_map.ga_data, c)
5971 != NULL)
5972 || vim_strchr(p, c) != NULL)
5973 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5974 fname, lnum);
5975 }
5976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005977 /* We simply concatenate all the MAP strings, separated by
5978 * slashes. */
5979 ga_concat(&spin->si_map, items[1]);
5980 ga_append(&spin->si_map, '/');
5981 }
5982 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005983 /* Accept "SAL from to" and "SAL from to #comment". */
5984 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005985 {
5986 if (do_sal)
5987 {
5988 /* SAL item (sounds-a-like)
5989 * Either one of the known keys or a from-to pair. */
5990 if (STRCMP(items[1], "followup") == 0)
5991 spin->si_followup = sal_to_bool(items[2]);
5992 else if (STRCMP(items[1], "collapse_result") == 0)
5993 spin->si_collapse = sal_to_bool(items[2]);
5994 else if (STRCMP(items[1], "remove_accents") == 0)
5995 spin->si_rem_accents = sal_to_bool(items[2]);
5996 else
5997 /* when "to" is "_" it means empty */
5998 add_fromto(spin, &spin->si_sal, items[1],
5999 STRCMP(items[2], "_") == 0 ? (char_u *)""
6000 : items[2]);
6001 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006002 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006003 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006004 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006005 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006006 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006007 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006008 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006009 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006010 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006011 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006012 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006013 else if (STRCMP(items[0], "COMMON") == 0)
6014 {
6015 int i;
6016
6017 for (i = 1; i < itemcnt; ++i)
6018 {
6019 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
6020 items[i])))
6021 {
6022 p = vim_strsave(items[i]);
6023 if (p == NULL)
6024 break;
6025 hash_add(&spin->si_commonwords, p);
6026 }
6027 }
6028 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006029 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006030 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006031 fname, lnum, items[0]);
6032 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006033 }
6034
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006035 if (fol != NULL || low != NULL || upp != NULL)
6036 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006037 if (spin->si_clear_chartab)
6038 {
6039 /* Clear the char type tables, don't want to use any of the
6040 * currently used spell properties. */
6041 init_spell_chartab();
6042 spin->si_clear_chartab = FALSE;
6043 }
6044
Bram Moolenaar3982c542005-06-08 21:56:31 +00006045 /*
6046 * Don't write a word table for an ASCII file, so that we don't check
6047 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006048 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006049 * mb_get_class(), the list of chars in the file will be incomplete.
6050 */
6051 if (!spin->si_ascii
6052#ifdef FEAT_MBYTE
6053 && !enc_utf8
6054#endif
6055 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006056 {
6057 if (fol == NULL || low == NULL || upp == NULL)
6058 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6059 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006060 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006061 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006062
6063 vim_free(fol);
6064 vim_free(low);
6065 vim_free(upp);
6066 }
6067
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006068 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006069 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006070 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006071 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006072 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006073 }
6074
Bram Moolenaar6de68532005-08-24 22:08:48 +00006075 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006076 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006077 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6078 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006079 }
6080
Bram Moolenaar6de68532005-08-24 22:08:48 +00006081 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006082 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006083 if (syllable == NULL)
6084 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6085 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6086 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006087 }
6088
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006089 if (compoptions != 0)
6090 {
6091 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6092 spin->si_compoptions |= compoptions;
6093 }
6094
Bram Moolenaar6de68532005-08-24 22:08:48 +00006095 if (compflags != NULL)
6096 process_compflags(spin, aff, compflags);
6097
6098 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006099 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006100 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006101 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006102 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006103 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006104 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006105 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006106 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006107 }
6108
Bram Moolenaar6de68532005-08-24 22:08:48 +00006109 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006110 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006111 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6112 spin->si_syllable = syllable;
6113 }
6114
6115 if (sofofrom != NULL || sofoto != NULL)
6116 {
6117 if (sofofrom == NULL || sofoto == NULL)
6118 smsg((char_u *)_("Missing SOFO%s line in %s"),
6119 sofofrom == NULL ? "FROM" : "TO", fname);
6120 else if (spin->si_sal.ga_len > 0)
6121 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006122 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006123 {
6124 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6125 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6126 spin->si_sofofr = sofofrom;
6127 spin->si_sofoto = sofoto;
6128 }
6129 }
6130
6131 if (midword != NULL)
6132 {
6133 aff_check_string(spin->si_midword, midword, "MIDWORD");
6134 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006135 }
6136
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006137 vim_free(pc);
6138 fclose(fd);
6139 return aff;
6140}
6141
6142/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006143 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6144 * a comment is following after item "mincount".
6145 */
6146 static int
6147is_aff_rule(items, itemcnt, rulename, mincount)
6148 char_u **items;
6149 int itemcnt;
6150 char *rulename;
6151 int mincount;
6152{
6153 return (STRCMP(items[0], rulename) == 0
6154 && (itemcnt == mincount
6155 || (itemcnt > mincount && items[mincount][0] == '#')));
6156}
6157
6158/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006159 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6160 * ae_flags to ae_comppermit and ae_compforbid.
6161 */
6162 static void
6163aff_process_flags(affile, entry)
6164 afffile_T *affile;
6165 affentry_T *entry;
6166{
6167 char_u *p;
6168 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006169 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006170
6171 if (entry->ae_flags != NULL
6172 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6173 {
6174 for (p = entry->ae_flags; *p != NUL; )
6175 {
6176 prevp = p;
6177 flag = get_affitem(affile->af_flagtype, &p);
6178 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6179 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006180 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006181 p = prevp;
6182 if (flag == affile->af_comppermit)
6183 entry->ae_comppermit = TRUE;
6184 else
6185 entry->ae_compforbid = TRUE;
6186 }
6187 if (affile->af_flagtype == AFT_NUM && *p == ',')
6188 ++p;
6189 }
6190 if (*entry->ae_flags == NUL)
6191 entry->ae_flags = NULL; /* nothing left */
6192 }
6193}
6194
6195/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006196 * Return TRUE if "s" is the name of an info item in the affix file.
6197 */
6198 static int
6199spell_info_item(s)
6200 char_u *s;
6201{
6202 return STRCMP(s, "NAME") == 0
6203 || STRCMP(s, "HOME") == 0
6204 || STRCMP(s, "VERSION") == 0
6205 || STRCMP(s, "AUTHOR") == 0
6206 || STRCMP(s, "EMAIL") == 0
6207 || STRCMP(s, "COPYRIGHT") == 0;
6208}
6209
6210/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006211 * Turn an affix flag name into a number, according to the FLAG type.
6212 * returns zero for failure.
6213 */
6214 static unsigned
6215affitem2flag(flagtype, item, fname, lnum)
6216 int flagtype;
6217 char_u *item;
6218 char_u *fname;
6219 int lnum;
6220{
6221 unsigned res;
6222 char_u *p = item;
6223
6224 res = get_affitem(flagtype, &p);
6225 if (res == 0)
6226 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006227 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006228 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6229 fname, lnum, item);
6230 else
6231 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6232 fname, lnum, item);
6233 }
6234 if (*p != NUL)
6235 {
6236 smsg((char_u *)_(e_affname), fname, lnum, item);
6237 return 0;
6238 }
6239
6240 return res;
6241}
6242
6243/*
6244 * Get one affix name from "*pp" and advance the pointer.
6245 * Returns zero for an error, still advances the pointer then.
6246 */
6247 static unsigned
6248get_affitem(flagtype, pp)
6249 int flagtype;
6250 char_u **pp;
6251{
6252 int res;
6253
Bram Moolenaar95529562005-08-25 21:21:38 +00006254 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006255 {
6256 if (!VIM_ISDIGIT(**pp))
6257 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006258 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006259 return 0;
6260 }
6261 res = getdigits(pp);
6262 }
6263 else
6264 {
6265#ifdef FEAT_MBYTE
6266 res = mb_ptr2char_adv(pp);
6267#else
6268 res = *(*pp)++;
6269#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006270 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006271 && res >= 'A' && res <= 'Z'))
6272 {
6273 if (**pp == NUL)
6274 return 0;
6275#ifdef FEAT_MBYTE
6276 res = mb_ptr2char_adv(pp) + (res << 16);
6277#else
6278 res = *(*pp)++ + (res << 16);
6279#endif
6280 }
6281 }
6282 return res;
6283}
6284
6285/*
6286 * Process the "compflags" string used in an affix file and append it to
6287 * spin->si_compflags.
6288 * The processing involves changing the affix names to ID numbers, so that
6289 * they fit in one byte.
6290 */
6291 static void
6292process_compflags(spin, aff, compflags)
6293 spellinfo_T *spin;
6294 afffile_T *aff;
6295 char_u *compflags;
6296{
6297 char_u *p;
6298 char_u *prevp;
6299 unsigned flag;
6300 compitem_T *ci;
6301 int id;
6302 int len;
6303 char_u *tp;
6304 char_u key[AH_KEY_LEN];
6305 hashitem_T *hi;
6306
6307 /* Make room for the old and the new compflags, concatenated with a / in
6308 * between. Processing it makes it shorter, but we don't know by how
6309 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006310 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006311 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006312 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006313 p = getroom(spin, len, FALSE);
6314 if (p == NULL)
6315 return;
6316 if (spin->si_compflags != NULL)
6317 {
6318 STRCPY(p, spin->si_compflags);
6319 STRCAT(p, "/");
6320 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006321 spin->si_compflags = p;
6322 tp = p + STRLEN(p);
6323
6324 for (p = compflags; *p != NUL; )
6325 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006326 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006327 /* Copy non-flag characters directly. */
6328 *tp++ = *p++;
6329 else
6330 {
6331 /* First get the flag number, also checks validity. */
6332 prevp = p;
6333 flag = get_affitem(aff->af_flagtype, &p);
6334 if (flag != 0)
6335 {
6336 /* Find the flag in the hashtable. If it was used before, use
6337 * the existing ID. Otherwise add a new entry. */
6338 vim_strncpy(key, prevp, p - prevp);
6339 hi = hash_find(&aff->af_comp, key);
6340 if (!HASHITEM_EMPTY(hi))
6341 id = HI2CI(hi)->ci_newID;
6342 else
6343 {
6344 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6345 if (ci == NULL)
6346 break;
6347 STRCPY(ci->ci_key, key);
6348 ci->ci_flag = flag;
6349 /* Avoid using a flag ID that has a special meaning in a
6350 * regexp (also inside []). */
6351 do
6352 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006353 check_renumber(spin);
6354 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006355 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006356 ci->ci_newID = id;
6357 hash_add(&aff->af_comp, ci->ci_key);
6358 }
6359 *tp++ = id;
6360 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006361 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006362 ++p;
6363 }
6364 }
6365
6366 *tp = NUL;
6367}
6368
6369/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006370 * Check that the new IDs for postponed affixes and compounding don't overrun
6371 * each other. We have almost 255 available, but start at 0-127 to avoid
6372 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6373 * When that is used up an error message is given.
6374 */
6375 static void
6376check_renumber(spin)
6377 spellinfo_T *spin;
6378{
6379 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6380 {
6381 spin->si_newprefID = 127;
6382 spin->si_newcompID = 255;
6383 }
6384}
6385
6386/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006387 * Return TRUE if flag "flag" appears in affix list "afflist".
6388 */
6389 static int
6390flag_in_afflist(flagtype, afflist, flag)
6391 int flagtype;
6392 char_u *afflist;
6393 unsigned flag;
6394{
6395 char_u *p;
6396 unsigned n;
6397
6398 switch (flagtype)
6399 {
6400 case AFT_CHAR:
6401 return vim_strchr(afflist, flag) != NULL;
6402
Bram Moolenaar95529562005-08-25 21:21:38 +00006403 case AFT_CAPLONG:
6404 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006405 for (p = afflist; *p != NUL; )
6406 {
6407#ifdef FEAT_MBYTE
6408 n = mb_ptr2char_adv(&p);
6409#else
6410 n = *p++;
6411#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006412 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006413 && *p != NUL)
6414#ifdef FEAT_MBYTE
6415 n = mb_ptr2char_adv(&p) + (n << 16);
6416#else
6417 n = *p++ + (n << 16);
6418#endif
6419 if (n == flag)
6420 return TRUE;
6421 }
6422 break;
6423
Bram Moolenaar95529562005-08-25 21:21:38 +00006424 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006425 for (p = afflist; *p != NUL; )
6426 {
6427 n = getdigits(&p);
6428 if (n == flag)
6429 return TRUE;
6430 if (*p != NUL) /* skip over comma */
6431 ++p;
6432 }
6433 break;
6434 }
6435 return FALSE;
6436}
6437
6438/*
6439 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6440 */
6441 static void
6442aff_check_number(spinval, affval, name)
6443 int spinval;
6444 int affval;
6445 char *name;
6446{
6447 if (spinval != 0 && spinval != affval)
6448 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6449}
6450
6451/*
6452 * Give a warning when "spinval" and "affval" strings are set and not the same.
6453 */
6454 static void
6455aff_check_string(spinval, affval, name)
6456 char_u *spinval;
6457 char_u *affval;
6458 char *name;
6459{
6460 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6461 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6462}
6463
6464/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006465 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6466 * NULL as equal.
6467 */
6468 static int
6469str_equal(s1, s2)
6470 char_u *s1;
6471 char_u *s2;
6472{
6473 if (s1 == NULL || s2 == NULL)
6474 return s1 == s2;
6475 return STRCMP(s1, s2) == 0;
6476}
6477
6478/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006479 * Add a from-to item to "gap". Used for REP and SAL items.
6480 * They are stored case-folded.
6481 */
6482 static void
6483add_fromto(spin, gap, from, to)
6484 spellinfo_T *spin;
6485 garray_T *gap;
6486 char_u *from;
6487 char_u *to;
6488{
6489 fromto_T *ftp;
6490 char_u word[MAXWLEN];
6491
6492 if (ga_grow(gap, 1) == OK)
6493 {
6494 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006495 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006496 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006497 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006498 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006499 ++gap->ga_len;
6500 }
6501}
6502
6503/*
6504 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6505 */
6506 static int
6507sal_to_bool(s)
6508 char_u *s;
6509{
6510 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6511}
6512
6513/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006514 * Free the structure filled by spell_read_aff().
6515 */
6516 static void
6517spell_free_aff(aff)
6518 afffile_T *aff;
6519{
6520 hashtab_T *ht;
6521 hashitem_T *hi;
6522 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006523 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006524 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006525
6526 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006527
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006528 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006529 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6530 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006531 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006532 for (hi = ht->ht_array; todo > 0; ++hi)
6533 {
6534 if (!HASHITEM_EMPTY(hi))
6535 {
6536 --todo;
6537 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006538 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar473de612013-06-08 18:19:48 +02006539 vim_regfree(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006540 }
6541 }
6542 if (ht == &aff->af_suff)
6543 break;
6544 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006545
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006546 hash_clear(&aff->af_pref);
6547 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006548 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006549}
6550
6551/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006552 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006553 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006554 */
6555 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006556spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006557 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006558 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006559 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006560{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006563 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006564 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006565 char_u store_afflist[MAXWLEN];
6566 int pfxlen;
6567 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006568 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006569 char_u *pc;
6570 char_u *w;
6571 int l;
6572 hash_T hash;
6573 hashitem_T *hi;
6574 FILE *fd;
6575 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006576 int non_ascii = 0;
6577 int retval = OK;
6578 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006579 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006580 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006581
Bram Moolenaar51485f02005-06-04 21:55:20 +00006582 /*
6583 * Open the file.
6584 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006585 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006586 if (fd == NULL)
6587 {
6588 EMSG2(_(e_notopen), fname);
6589 return FAIL;
6590 }
6591
Bram Moolenaar51485f02005-06-04 21:55:20 +00006592 /* The hashtable is only used to detect duplicated words. */
6593 hash_init(&ht);
6594
Bram Moolenaar4770d092006-01-12 23:22:24 +00006595 vim_snprintf((char *)IObuff, IOSIZE,
6596 _("Reading dictionary file %s ..."), fname);
6597 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006598
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006599 /* start with a message for the first line */
6600 spin->si_msg_count = 999999;
6601
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006602 /* Read and ignore the first line: word count. */
6603 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006604 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006605 EMSG2(_("E760: No word count in %s"), fname);
6606
6607 /*
6608 * Read all the lines in the file one by one.
6609 * The words are converted to 'encoding' here, before being added to
6610 * the hashtable.
6611 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006612 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006613 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006614 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006615 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006616 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006617 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006618
Bram Moolenaar51485f02005-06-04 21:55:20 +00006619 /* Remove CR, LF and white space from the end. White space halfway
6620 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006621 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006622 while (l > 0 && line[l - 1] <= ' ')
6623 --l;
6624 if (l == 0)
6625 continue; /* empty line */
6626 line[l] = NUL;
6627
Bram Moolenaarb765d632005-06-07 21:00:02 +00006628#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006629 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006630 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006631 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006632 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006633 if (pc == NULL)
6634 {
6635 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6636 fname, lnum, line);
6637 continue;
6638 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006639 w = pc;
6640 }
6641 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006642#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006643 {
6644 pc = NULL;
6645 w = line;
6646 }
6647
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006648 /* Truncate the word at the "/", set "afflist" to what follows.
6649 * Replace "\/" by "/" and "\\" by "\". */
6650 afflist = NULL;
6651 for (p = w; *p != NUL; mb_ptr_adv(p))
6652 {
6653 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006654 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006655 else if (*p == '/')
6656 {
6657 *p = NUL;
6658 afflist = p + 1;
6659 break;
6660 }
6661 }
6662
6663 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6664 if (spin->si_ascii && has_non_ascii(w))
6665 {
6666 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006667 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006668 continue;
6669 }
6670
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006671 /* This takes time, print a message every 10000 words. */
6672 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006673 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006674 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006675 vim_snprintf((char *)message, sizeof(message),
6676 _("line %6d, word %6d - %s"),
6677 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6678 msg_start();
6679 msg_puts_long_attr(message, 0);
6680 msg_clr_eos();
6681 msg_didout = FALSE;
6682 msg_col = 0;
6683 out_flush();
6684 }
6685
Bram Moolenaar51485f02005-06-04 21:55:20 +00006686 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006687 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006688 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006689 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006690 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006691 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006692 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006693 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006694
Bram Moolenaar51485f02005-06-04 21:55:20 +00006695 hash = hash_hash(dw);
6696 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006697 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006698 {
6699 if (p_verbose > 0)
6700 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006701 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006702 else if (duplicate == 0)
6703 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6704 fname, lnum, dw);
6705 ++duplicate;
6706 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006707 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006708 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006709
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006710 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006711 store_afflist[0] = NUL;
6712 pfxlen = 0;
6713 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006714 if (afflist != NULL)
6715 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006716 /* Extract flags from the affix list. */
6717 flags |= get_affix_flags(affile, afflist);
6718
Bram Moolenaar6de68532005-08-24 22:08:48 +00006719 if (affile->af_needaffix != 0 && flag_in_afflist(
6720 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006721 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006722
6723 if (affile->af_pfxpostpone)
6724 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006725 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006726
Bram Moolenaar5195e452005-08-19 20:32:47 +00006727 if (spin->si_compflags != NULL)
6728 /* Need to store the list of compound flags with the word.
6729 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006730 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006731 }
6732
Bram Moolenaar51485f02005-06-04 21:55:20 +00006733 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006734 if (store_word(spin, dw, flags, spin->si_region,
6735 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006736 retval = FAIL;
6737
6738 if (afflist != NULL)
6739 {
6740 /* Find all matching suffixes and add the resulting words.
6741 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006742 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006743 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006744 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006745 retval = FAIL;
6746
6747 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006748 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006749 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006750 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006751 retval = FAIL;
6752 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006753
6754 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006755 }
6756
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006757 if (duplicate > 0)
6758 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006759 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006760 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6761 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006762 hash_clear(&ht);
6763
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006764 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006765 return retval;
6766}
6767
6768/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006769 * Check for affix flags in "afflist" that are turned into word flags.
6770 * Return WF_ flags.
6771 */
6772 static int
6773get_affix_flags(affile, afflist)
6774 afffile_T *affile;
6775 char_u *afflist;
6776{
6777 int flags = 0;
6778
6779 if (affile->af_keepcase != 0 && flag_in_afflist(
6780 affile->af_flagtype, afflist, affile->af_keepcase))
6781 flags |= WF_KEEPCAP | WF_FIXCAP;
6782 if (affile->af_rare != 0 && flag_in_afflist(
6783 affile->af_flagtype, afflist, affile->af_rare))
6784 flags |= WF_RARE;
6785 if (affile->af_bad != 0 && flag_in_afflist(
6786 affile->af_flagtype, afflist, affile->af_bad))
6787 flags |= WF_BANNED;
6788 if (affile->af_needcomp != 0 && flag_in_afflist(
6789 affile->af_flagtype, afflist, affile->af_needcomp))
6790 flags |= WF_NEEDCOMP;
6791 if (affile->af_comproot != 0 && flag_in_afflist(
6792 affile->af_flagtype, afflist, affile->af_comproot))
6793 flags |= WF_COMPROOT;
6794 if (affile->af_nosuggest != 0 && flag_in_afflist(
6795 affile->af_flagtype, afflist, affile->af_nosuggest))
6796 flags |= WF_NOSUGGEST;
6797 return flags;
6798}
6799
6800/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006801 * Get the list of prefix IDs from the affix list "afflist".
6802 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006803 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6804 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006805 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006806 static int
6807get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006808 afffile_T *affile;
6809 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006810 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006811{
6812 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006813 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006814 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006815 int id;
6816 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006817 hashitem_T *hi;
6818
Bram Moolenaar6de68532005-08-24 22:08:48 +00006819 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006820 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006821 prevp = p;
6822 if (get_affitem(affile->af_flagtype, &p) != 0)
6823 {
6824 /* A flag is a postponed prefix flag if it appears in "af_pref"
6825 * and it's ID is not zero. */
6826 vim_strncpy(key, prevp, p - prevp);
6827 hi = hash_find(&affile->af_pref, key);
6828 if (!HASHITEM_EMPTY(hi))
6829 {
6830 id = HI2AH(hi)->ah_newID;
6831 if (id != 0)
6832 store_afflist[cnt++] = id;
6833 }
6834 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006835 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006836 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006837 }
6838
Bram Moolenaar5195e452005-08-19 20:32:47 +00006839 store_afflist[cnt] = NUL;
6840 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006841}
6842
6843/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006844 * Get the list of compound IDs from the affix list "afflist" that are used
6845 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006846 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006847 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006848 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006849get_compflags(affile, afflist, store_afflist)
6850 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006851 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006852 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006853{
6854 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006855 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006856 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006857 char_u key[AH_KEY_LEN];
6858 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006859
Bram Moolenaar6de68532005-08-24 22:08:48 +00006860 for (p = afflist; *p != NUL; )
6861 {
6862 prevp = p;
6863 if (get_affitem(affile->af_flagtype, &p) != 0)
6864 {
6865 /* A flag is a compound flag if it appears in "af_comp". */
6866 vim_strncpy(key, prevp, p - prevp);
6867 hi = hash_find(&affile->af_comp, key);
6868 if (!HASHITEM_EMPTY(hi))
6869 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6870 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006871 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006872 ++p;
6873 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006874
Bram Moolenaar5195e452005-08-19 20:32:47 +00006875 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006876}
6877
6878/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006879 * Apply affixes to a word and store the resulting words.
6880 * "ht" is the hashtable with affentry_T that need to be applied, either
6881 * prefixes or suffixes.
6882 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6883 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006884 *
6885 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006886 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006887 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006888store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006889 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006890 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006891 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006892 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006893 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006894 hashtab_T *ht;
6895 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006896 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006897 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006898 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006899 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6900 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006901{
6902 int todo;
6903 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006904 affheader_T *ah;
6905 affentry_T *ae;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006906 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006907 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006908 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006909 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006910 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006911 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006912 int use_pfxlen;
6913 int need_affix;
6914 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006915 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006916 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006917 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006918
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006919 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006921 {
6922 if (!HASHITEM_EMPTY(hi))
6923 {
6924 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006925 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006926
Bram Moolenaar51485f02005-06-04 21:55:20 +00006927 /* Check that the affix combines, if required, and that the word
6928 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006929 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6930 && flag_in_afflist(affile->af_flagtype, afflist,
6931 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006932 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006933 /* Loop over all affix entries with this name. */
6934 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006935 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006936 /* Check the condition. It's not logical to match case
6937 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006938 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006939 * Another requirement from Myspell is that the chop
6940 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006941 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006942 * prefixes with a chop string and/or flags.
6943 * When a previously added affix had CIRCUMFIX this one
6944 * must have it too, if it had not then this one must not
6945 * have one either. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006946 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006947 || ae->ae_chop != NULL
6948 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006949 && (ae->ae_chop == NULL
6950 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006951 && (ae->ae_prog == NULL
Bram Moolenaardffa5b82014-11-19 16:38:07 +01006952 || vim_regexec_prog(&ae->ae_prog, FALSE,
6953 word, (colnr_T)0))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006954 && (((condit & CONDIT_CFIX) == 0)
6955 == ((condit & CONDIT_AFF) == 0
6956 || ae->ae_flags == NULL
6957 || !flag_in_afflist(affile->af_flagtype,
6958 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006959 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006960 /* Match. Remove the chop and add the affix. */
6961 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006962 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006963 /* prefix: chop/add at the start of the word */
6964 if (ae->ae_add == NULL)
6965 *newword = NUL;
6966 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006967 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 p = word;
6969 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006970 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006971 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006972#ifdef FEAT_MBYTE
6973 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006974 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006975 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006976 for ( ; i > 0; --i)
6977 mb_ptr_adv(p);
6978 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006979 else
6980#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006981 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006982 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006983 STRCAT(newword, p);
6984 }
6985 else
6986 {
6987 /* suffix: chop/add at the end of the word */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006988 vim_strncpy(newword, word, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006989 if (ae->ae_chop != NULL)
6990 {
6991 /* Remove chop string. */
6992 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006993 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006994 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006995 mb_ptr_back(newword, p);
6996 *p = NUL;
6997 }
6998 if (ae->ae_add != NULL)
6999 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007000 }
7001
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007002 use_flags = flags;
7003 use_pfxlist = pfxlist;
7004 use_pfxlen = pfxlen;
7005 need_affix = FALSE;
7006 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
7007 if (ae->ae_flags != NULL)
7008 {
7009 /* Extract flags from the affix list. */
7010 use_flags |= get_affix_flags(affile, ae->ae_flags);
7011
7012 if (affile->af_needaffix != 0 && flag_in_afflist(
7013 affile->af_flagtype, ae->ae_flags,
7014 affile->af_needaffix))
7015 need_affix = TRUE;
7016
7017 /* When there is a CIRCUMFIX flag the other affix
7018 * must also have it and we don't add the word
7019 * with one affix. */
7020 if (affile->af_circumfix != 0 && flag_in_afflist(
7021 affile->af_flagtype, ae->ae_flags,
7022 affile->af_circumfix))
7023 {
7024 use_condit |= CONDIT_CFIX;
7025 if ((condit & CONDIT_CFIX) == 0)
7026 need_affix = TRUE;
7027 }
7028
7029 if (affile->af_pfxpostpone
7030 || spin->si_compflags != NULL)
7031 {
7032 if (affile->af_pfxpostpone)
7033 /* Get prefix IDS from the affix list. */
7034 use_pfxlen = get_pfxlist(affile,
7035 ae->ae_flags, store_afflist);
7036 else
7037 use_pfxlen = 0;
7038 use_pfxlist = store_afflist;
7039
7040 /* Combine the prefix IDs. Avoid adding the
7041 * same ID twice. */
7042 for (i = 0; i < pfxlen; ++i)
7043 {
7044 for (j = 0; j < use_pfxlen; ++j)
7045 if (pfxlist[i] == use_pfxlist[j])
7046 break;
7047 if (j == use_pfxlen)
7048 use_pfxlist[use_pfxlen++] = pfxlist[i];
7049 }
7050
7051 if (spin->si_compflags != NULL)
7052 /* Get compound IDS from the affix list. */
7053 get_compflags(affile, ae->ae_flags,
7054 use_pfxlist + use_pfxlen);
7055
7056 /* Combine the list of compound flags.
7057 * Concatenate them to the prefix IDs list.
7058 * Avoid adding the same ID twice. */
7059 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7060 {
7061 for (j = use_pfxlen;
7062 use_pfxlist[j] != NUL; ++j)
7063 if (pfxlist[i] == use_pfxlist[j])
7064 break;
7065 if (use_pfxlist[j] == NUL)
7066 {
7067 use_pfxlist[j++] = pfxlist[i];
7068 use_pfxlist[j] = NUL;
7069 }
7070 }
7071 }
7072 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007073
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007074 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007075 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007076 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007077 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007078 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007079 use_pfxlist = pfx_pfxlist;
7080 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007081
7082 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007083 if (spin->si_prefroot != NULL
7084 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007085 {
7086 /* ... add a flag to indicate an affix was used. */
7087 use_flags |= WF_HAS_AFF;
7088
7089 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007090 * affixes is not allowed. But do use the
7091 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007092 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007093 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007094 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007095
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096 /* When compounding is supported and there is no
7097 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7098 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007099 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007100 {
7101 if (xht != NULL)
7102 use_flags |= WF_NOCOMPAFT;
7103 else
7104 use_flags |= WF_NOCOMPBEF;
7105 }
7106
Bram Moolenaar51485f02005-06-04 21:55:20 +00007107 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007108 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007109 spin->si_region, use_pfxlist,
7110 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007111 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007112
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007113 /* When added a prefix or a first suffix and the affix
7114 * has flags may add a(nother) suffix. RECURSIVE! */
7115 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7116 if (store_aff_word(spin, newword, ae->ae_flags,
7117 affile, &affile->af_suff, xht,
7118 use_condit & (xht == NULL
7119 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007120 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007121 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007122
7123 /* When added a suffix and combining is allowed also
7124 * try adding a prefix additionally. Both for the
7125 * word flags and for the affix flags. RECURSIVE! */
7126 if (xht != NULL && ah->ah_combine)
7127 {
7128 if (store_aff_word(spin, newword,
7129 afflist, affile,
7130 xht, NULL, use_condit,
7131 use_flags, use_pfxlist,
7132 pfxlen) == FAIL
7133 || (ae->ae_flags != NULL
7134 && store_aff_word(spin, newword,
7135 ae->ae_flags, affile,
7136 xht, NULL, use_condit,
7137 use_flags, use_pfxlist,
7138 pfxlen) == FAIL))
7139 retval = FAIL;
7140 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007141 }
7142 }
7143 }
7144 }
7145 }
7146
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007147 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007148}
7149
7150/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007151 * Read a file with a list of words.
7152 */
7153 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007154spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007155 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007156 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007157{
7158 FILE *fd;
7159 long lnum = 0;
7160 char_u rline[MAXLINELEN];
7161 char_u *line;
7162 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007163 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007164 int l;
7165 int retval = OK;
7166 int did_word = FALSE;
7167 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007168 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007169 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007170
7171 /*
7172 * Open the file.
7173 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007174 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007175 if (fd == NULL)
7176 {
7177 EMSG2(_(e_notopen), fname);
7178 return FAIL;
7179 }
7180
Bram Moolenaar4770d092006-01-12 23:22:24 +00007181 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7182 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007183
7184 /*
7185 * Read all the lines in the file one by one.
7186 */
7187 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7188 {
7189 line_breakcheck();
7190 ++lnum;
7191
7192 /* Skip comment lines. */
7193 if (*rline == '#')
7194 continue;
7195
7196 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198 while (l > 0 && rline[l - 1] <= ' ')
7199 --l;
7200 if (l == 0)
7201 continue; /* empty or blank line */
7202 rline[l] = NUL;
7203
Bram Moolenaar9c102382006-05-03 21:26:49 +00007204 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007205 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007206#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007207 if (spin->si_conv.vc_type != CONV_NONE)
7208 {
7209 pc = string_convert(&spin->si_conv, rline, NULL);
7210 if (pc == NULL)
7211 {
7212 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7213 fname, lnum, rline);
7214 continue;
7215 }
7216 line = pc;
7217 }
7218 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007219#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 {
7221 pc = NULL;
7222 line = rline;
7223 }
7224
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007225 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007226 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007227 ++line;
7228 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007229 {
7230 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007231 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7232 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007234 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7235 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007236 else
7237 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007238#ifdef FEAT_MBYTE
7239 char_u *enc;
7240
Bram Moolenaar51485f02005-06-04 21:55:20 +00007241 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007242 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007243 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007244 if (enc != NULL && !spin->si_ascii
7245 && convert_setup(&spin->si_conv, enc,
7246 p_enc) == FAIL)
7247 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007248 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007249 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007250 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007251#else
7252 smsg((char_u *)_("Conversion in %s not supported"), fname);
7253#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007254 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007255 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007256 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007257
Bram Moolenaar3982c542005-06-08 21:56:31 +00007258 if (STRNCMP(line, "regions=", 8) == 0)
7259 {
7260 if (spin->si_region_count > 1)
7261 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7262 fname, lnum, line);
7263 else
7264 {
7265 line += 8;
7266 if (STRLEN(line) > 16)
7267 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7268 fname, lnum, line);
7269 else
7270 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007271 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007272 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007273
7274 /* Adjust the mask for a word valid in all regions. */
7275 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007276 }
7277 }
7278 continue;
7279 }
7280
Bram Moolenaar7887d882005-07-01 22:33:52 +00007281 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7282 fname, lnum, line - 1);
7283 continue;
7284 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007285
Bram Moolenaar7887d882005-07-01 22:33:52 +00007286 flags = 0;
7287 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007288
Bram Moolenaar7887d882005-07-01 22:33:52 +00007289 /* Check for flags and region after a slash. */
7290 p = vim_strchr(line, '/');
7291 if (p != NULL)
7292 {
7293 *p++ = NUL;
7294 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007295 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007296 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007297 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007298 else if (*p == '!') /* Bad, bad, wicked word. */
7299 flags |= WF_BANNED;
7300 else if (*p == '?') /* Rare word. */
7301 flags |= WF_RARE;
7302 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007303 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007304 if ((flags & WF_REGION) == 0) /* first one */
7305 regionmask = 0;
7306 flags |= WF_REGION;
7307
7308 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007309 if (l > spin->si_region_count)
7310 {
7311 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007312 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007313 break;
7314 }
7315 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007316 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007317 else
7318 {
7319 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7320 fname, lnum, p);
7321 break;
7322 }
7323 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007324 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007325 }
7326
7327 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7328 if (spin->si_ascii && has_non_ascii(line))
7329 {
7330 ++non_ascii;
7331 continue;
7332 }
7333
7334 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007335 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007336 {
7337 retval = FAIL;
7338 break;
7339 }
7340 did_word = TRUE;
7341 }
7342
7343 vim_free(pc);
7344 fclose(fd);
7345
Bram Moolenaar4770d092006-01-12 23:22:24 +00007346 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007347 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007348 vim_snprintf((char *)IObuff, IOSIZE,
7349 _("Ignored %d words with non-ASCII characters"), non_ascii);
7350 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007351 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007352
Bram Moolenaar51485f02005-06-04 21:55:20 +00007353 return retval;
7354}
7355
7356/*
7357 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007358 * This avoids calling free() for every little struct we use (and keeping
7359 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007360 * The memory is cleared to all zeros.
7361 * Returns NULL when out of memory.
7362 */
7363 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007364getroom(spin, len, align)
7365 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007366 size_t len; /* length needed */
7367 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368{
7369 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007370 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007371
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007372 if (align && bl != NULL)
7373 /* Round size up for alignment. On some systems structures need to be
7374 * aligned to the size of a pointer (e.g., SPARC). */
7375 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7376 & ~(sizeof(char *) - 1);
7377
Bram Moolenaar51485f02005-06-04 21:55:20 +00007378 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7379 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007380 if (len >= SBLOCKSIZE)
7381 bl = NULL;
7382 else
7383 /* Allocate a block of memory. It is not freed until much later. */
7384 bl = (sblock_T *)alloc_clear(
7385 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007386 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007387 {
7388 if (!spin->si_did_emsg)
7389 {
7390 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7391 spin->si_did_emsg = TRUE;
7392 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007393 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007394 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007395 bl->sb_next = spin->si_blocks;
7396 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007397 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007398 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007399 }
7400
7401 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007402 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007403
7404 return p;
7405}
7406
7407/*
7408 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007409 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007410 */
7411 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007412getroom_save(spin, s)
7413 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007414 char_u *s;
7415{
7416 char_u *sc;
7417
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007418 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007419 if (sc != NULL)
7420 STRCPY(sc, s);
7421 return sc;
7422}
7423
7424
7425/*
7426 * Free the list of allocated sblock_T.
7427 */
7428 static void
7429free_blocks(bl)
7430 sblock_T *bl;
7431{
7432 sblock_T *next;
7433
7434 while (bl != NULL)
7435 {
7436 next = bl->sb_next;
7437 vim_free(bl);
7438 bl = next;
7439 }
7440}
7441
7442/*
7443 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007444 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007445 */
7446 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007447wordtree_alloc(spin)
7448 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007449{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007450 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007451}
7452
7453/*
7454 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007455 * Always store it in the case-folded tree. For a keep-case word this is
7456 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7457 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007458 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007459 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7460 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007461 */
7462 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007463store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007464 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007465 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007466 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007467 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007468 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007469 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007470{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007471 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007472 int ct = captype(word, word + len);
7473 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007474 int res = OK;
7475 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007477 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007478 for (p = pfxlist; res == OK; ++p)
7479 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007480 if (!need_affix || (p != NULL && *p != NUL))
7481 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007482 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007483 if (p == NULL || *p == NUL)
7484 break;
7485 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007486 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007487
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007488 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007489 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007490 for (p = pfxlist; res == OK; ++p)
7491 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007492 if (!need_affix || (p != NULL && *p != NUL))
7493 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007494 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007495 if (p == NULL || *p == NUL)
7496 break;
7497 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007498 ++spin->si_keepwcount;
7499 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007500 return res;
7501}
7502
7503/*
7504 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007505 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007506 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007507 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007508 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007509 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007510tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007511 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007512 char_u *word;
7513 wordnode_T *root;
7514 int flags;
7515 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007516 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007517{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007518 wordnode_T *node = root;
7519 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007520 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007521 wordnode_T **prev = NULL;
7522 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007523
Bram Moolenaar51485f02005-06-04 21:55:20 +00007524 /* Add each byte of the word to the tree, including the NUL at the end. */
7525 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007526 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007527 /* When there is more than one reference to this node we need to make
7528 * a copy, so that we can modify it. Copy the whole list of siblings
7529 * (we don't optimize for a partly shared list of siblings). */
7530 if (node != NULL && node->wn_refs > 1)
7531 {
7532 --node->wn_refs;
7533 copyprev = prev;
7534 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7535 {
7536 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007537 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007538 if (np == NULL)
7539 return FAIL;
7540 np->wn_child = copyp->wn_child;
7541 if (np->wn_child != NULL)
7542 ++np->wn_child->wn_refs; /* child gets extra ref */
7543 np->wn_byte = copyp->wn_byte;
7544 if (np->wn_byte == NUL)
7545 {
7546 np->wn_flags = copyp->wn_flags;
7547 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007548 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007549 }
7550
7551 /* Link the new node in the list, there will be one ref. */
7552 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007553 if (copyprev != NULL)
7554 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007555 copyprev = &np->wn_sibling;
7556
7557 /* Let "node" point to the head of the copied list. */
7558 if (copyp == node)
7559 node = np;
7560 }
7561 }
7562
Bram Moolenaar51485f02005-06-04 21:55:20 +00007563 /* Look for the sibling that has the same character. They are sorted
7564 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007565 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007566 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007567 while (node != NULL
7568 && (node->wn_byte < word[i]
7569 || (node->wn_byte == NUL
7570 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007571 ? node->wn_affixID < (unsigned)affixID
7572 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007573 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007574 && (spin->si_sugtree
7575 ? (node->wn_region & 0xffff) < region
7576 : node->wn_affixID
7577 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007578 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007579 prev = &node->wn_sibling;
7580 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007581 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007582 if (node == NULL
7583 || node->wn_byte != word[i]
7584 || (word[i] == NUL
7585 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007586 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007587 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007588 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007589 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007590 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007591 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007592 if (np == NULL)
7593 return FAIL;
7594 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007595
7596 /* If "node" is NULL this is a new child or the end of the sibling
7597 * list: ref count is one. Otherwise use ref count of sibling and
7598 * make ref count of sibling one (matters when inserting in front
7599 * of the list of siblings). */
7600 if (node == NULL)
7601 np->wn_refs = 1;
7602 else
7603 {
7604 np->wn_refs = node->wn_refs;
7605 node->wn_refs = 1;
7606 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007607 if (prev != NULL)
7608 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007609 np->wn_sibling = node;
7610 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007611 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007612
Bram Moolenaar51485f02005-06-04 21:55:20 +00007613 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007614 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007615 node->wn_flags = flags;
7616 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007617 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007618 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007619 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007620 prev = &node->wn_child;
7621 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007622 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007623#ifdef SPELL_PRINTTREE
7624 smsg("Added \"%s\"", word);
7625 spell_print_tree(root->wn_sibling);
7626#endif
7627
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007628 /* count nr of words added since last message */
7629 ++spin->si_msg_count;
7630
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007631 if (spin->si_compress_cnt > 1)
7632 {
7633 if (--spin->si_compress_cnt == 1)
7634 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007635 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007636 }
7637
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007638 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007639 * When we have allocated lots of memory we need to compress the word tree
7640 * to free up some room. But compression is slow, and we might actually
7641 * need that room, thus only compress in the following situations:
7642 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007643 * "compress_start" blocks.
7644 * 2. When compressed before and used "compress_inc" blocks before
7645 * adding "compress_added" words (si_compress_cnt > 1).
7646 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007647 * (si_compress_cnt == 1) and the number of free nodes drops below the
7648 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007649 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007650#ifndef SPELL_PRINTTREE
7651 if (spin->si_compress_cnt == 1
7652 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007653 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007654#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007655 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007656 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007657 * when the freed up room has been used and another "compress_inc"
7658 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007659 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007660 spin->si_blocks_cnt -= compress_inc;
7661 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007662
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007663 if (spin->si_verbose)
7664 {
7665 msg_start();
7666 msg_puts((char_u *)_(msg_compressing));
7667 msg_clr_eos();
7668 msg_didout = FALSE;
7669 msg_col = 0;
7670 out_flush();
7671 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007672
7673 /* Compress both trees. Either they both have many nodes, which makes
7674 * compression useful, or one of them is small, which means
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007675 * compression goes fast. But when filling the soundfold word tree
Bram Moolenaar4770d092006-01-12 23:22:24 +00007676 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007677 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007678 if (affixID >= 0)
7679 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007680 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007681
7682 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007683}
7684
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007685/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007686 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7687 * Sets "sps_flags".
7688 */
7689 int
7690spell_check_msm()
7691{
7692 char_u *p = p_msm;
7693 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007694 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007695 long added = 0;
7696
7697 if (!VIM_ISDIGIT(*p))
7698 return FAIL;
7699 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7700 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7701 if (*p != ',')
7702 return FAIL;
7703 ++p;
7704 if (!VIM_ISDIGIT(*p))
7705 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007706 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007707 if (*p != ',')
7708 return FAIL;
7709 ++p;
7710 if (!VIM_ISDIGIT(*p))
7711 return FAIL;
7712 added = getdigits(&p) * 1024;
7713 if (*p != NUL)
7714 return FAIL;
7715
Bram Moolenaar89d40322006-08-29 15:30:07 +00007716 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007717 return FAIL;
7718
7719 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007720 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007721 compress_added = added;
7722 return OK;
7723}
7724
7725
7726/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007727 * Get a wordnode_T, either from the list of previously freed nodes or
7728 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007729 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007730 */
7731 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007732get_wordnode(spin)
7733 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007734{
7735 wordnode_T *n;
7736
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007737 if (spin->si_first_free == NULL)
7738 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007739 else
7740 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007741 n = spin->si_first_free;
7742 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007743 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007744 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007745 }
7746#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007747 if (n != NULL)
7748 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007749#endif
7750 return n;
7751}
7752
7753/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007754 * Decrement the reference count on a node (which is the head of a list of
7755 * siblings). If the reference count becomes zero free the node and its
7756 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007757 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007758 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007759 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007760deref_wordnode(spin, node)
7761 spellinfo_T *spin;
7762 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007763{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007764 wordnode_T *np;
7765 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007766
7767 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007768 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007769 for (np = node; np != NULL; np = np->wn_sibling)
7770 {
7771 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007772 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007773 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007774 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007775 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007776 ++cnt; /* length field */
7777 }
7778 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007779}
7780
7781/*
7782 * Free a wordnode_T for re-use later.
7783 * Only the "wn_child" field becomes invalid.
7784 */
7785 static void
7786free_wordnode(spin, n)
7787 spellinfo_T *spin;
7788 wordnode_T *n;
7789{
7790 n->wn_child = spin->si_first_free;
7791 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007792 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007793}
7794
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007795/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007796 * Compress a tree: find tails that are identical and can be shared.
7797 */
7798 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007799wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007800 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007801 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007802{
7803 hashtab_T ht;
7804 int n;
7805 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007806 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007807
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007808 /* Skip the root itself, it's not actually used. The first sibling is the
7809 * start of the tree. */
7810 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007811 {
7812 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007813 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007814
7815#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007816 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007817#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007818 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007819 if (tot > 1000000)
7820 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007821 else if (tot == 0)
7822 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007823 else
7824 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007825 vim_snprintf((char *)IObuff, IOSIZE,
7826 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7827 n, tot, tot - n, perc);
7828 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007829 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007830#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007831 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007832#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007833 hash_clear(&ht);
7834 }
7835}
7836
7837/*
7838 * Compress a node, its siblings and its children, depth first.
7839 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007840 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007841 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007842node_compress(spin, node, ht, tot)
7843 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007844 wordnode_T *node;
7845 hashtab_T *ht;
7846 int *tot; /* total count of nodes before compressing,
7847 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007848{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007849 wordnode_T *np;
7850 wordnode_T *tp;
7851 wordnode_T *child;
7852 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007853 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007854 int len = 0;
7855 unsigned nr, n;
7856 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007857
Bram Moolenaar51485f02005-06-04 21:55:20 +00007858 /*
7859 * Go through the list of siblings. Compress each child and then try
7860 * finding an identical child to replace it.
7861 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007862 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007863 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007864 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007865 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007866 ++len;
7867 if ((child = np->wn_child) != NULL)
7868 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007869 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007870 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007871
7872 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007873 hash = hash_hash(child->wn_u1.hashkey);
7874 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007875 if (!HASHITEM_EMPTY(hi))
7876 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007877 /* There are children we encountered before with a hash value
7878 * identical to the current child. Now check if there is one
7879 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007880 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007881 if (node_equal(child, tp))
7882 {
7883 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007884 * current one. This means the current child and all
7885 * its siblings is unlinked from the tree. */
7886 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007887 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007888 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007889 break;
7890 }
7891 if (tp == NULL)
7892 {
7893 /* No other child with this hash value equals the child of
7894 * the node, add it to the linked list after the first
7895 * item. */
7896 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007897 child->wn_u2.next = tp->wn_u2.next;
7898 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007899 }
7900 }
7901 else
7902 /* No other child has this hash value, add it to the
7903 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007904 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007905 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007906 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007907 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007908
7909 /*
7910 * Make a hash key for the node and its siblings, so that we can quickly
7911 * find a lookalike node. This must be done after compressing the sibling
7912 * list, otherwise the hash key would become invalid by the compression.
7913 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007914 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007915 nr = 0;
7916 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007917 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007918 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007919 /* end node: use wn_flags, wn_region and wn_affixID */
7920 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007921 else
7922 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007923 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007924 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007925 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007926
7927 /* Avoid NUL bytes, it terminates the hash key. */
7928 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007929 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007930 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007931 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007932 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007933 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007934 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007935 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7936 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007937
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007938 /* Check for CTRL-C pressed now and then. */
7939 fast_breakcheck();
7940
Bram Moolenaar51485f02005-06-04 21:55:20 +00007941 return compressed;
7942}
7943
7944/*
7945 * Return TRUE when two nodes have identical siblings and children.
7946 */
7947 static int
7948node_equal(n1, n2)
7949 wordnode_T *n1;
7950 wordnode_T *n2;
7951{
7952 wordnode_T *p1;
7953 wordnode_T *p2;
7954
7955 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7956 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7957 if (p1->wn_byte != p2->wn_byte
7958 || (p1->wn_byte == NUL
7959 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007960 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007961 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007962 : (p1->wn_child != p2->wn_child)))
7963 break;
7964
7965 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007966}
7967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007968static int
7969#ifdef __BORLANDC__
7970_RTLENTRYF
7971#endif
7972rep_compare __ARGS((const void *s1, const void *s2));
7973
7974/*
7975 * Function given to qsort() to sort the REP items on "from" string.
7976 */
7977 static int
7978#ifdef __BORLANDC__
7979_RTLENTRYF
7980#endif
7981rep_compare(s1, s2)
7982 const void *s1;
7983 const void *s2;
7984{
7985 fromto_T *p1 = (fromto_T *)s1;
7986 fromto_T *p2 = (fromto_T *)s2;
7987
7988 return STRCMP(p1->ft_from, p2->ft_from);
7989}
7990
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007991/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007992 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007993 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007994 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007995 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007996write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007997 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007998 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007999{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008000 FILE *fd;
8001 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008002 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008003 wordnode_T *tree;
8004 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005 int i;
8006 int l;
8007 garray_T *gap;
8008 fromto_T *ftp;
8009 char_u *p;
8010 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008011 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008012 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008013 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008014
Bram Moolenaarb765d632005-06-07 21:00:02 +00008015 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00008016 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008017 {
8018 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008019 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008020 }
8021
Bram Moolenaar5195e452005-08-19 20:32:47 +00008022 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008023 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008024 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008025 if (fwv != (size_t)1)
8026 /* Catch first write error, don't try writing more. */
8027 goto theend;
8028
Bram Moolenaar5195e452005-08-19 20:32:47 +00008029 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008030
Bram Moolenaar5195e452005-08-19 20:32:47 +00008031 /*
8032 * <SECTIONS>: <section> ... <sectionend>
8033 */
8034
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008035 /* SN_INFO: <infotext> */
8036 if (spin->si_info != NULL)
8037 {
8038 putc(SN_INFO, fd); /* <sectionID> */
8039 putc(0, fd); /* <sectionflags> */
8040
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008041 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008042 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008043 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008044 }
8045
Bram Moolenaar5195e452005-08-19 20:32:47 +00008046 /* SN_REGION: <regionname> ...
8047 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008048 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008049 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008050 putc(SN_REGION, fd); /* <sectionID> */
8051 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8052 l = spin->si_region_count * 2;
8053 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008054 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008055 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008056 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008057 }
8058 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008059 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008060
Bram Moolenaar5195e452005-08-19 20:32:47 +00008061 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8062 *
8063 * The table with character flags and the table for case folding.
8064 * This makes sure the same characters are recognized as word characters
8065 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008066 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 * 'encoding'.
8068 * Also skip this for an .add.spl file, the main spell file must contain
8069 * the table (avoids that it conflicts). File is shorter too.
8070 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008071 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008072 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008073 char_u folchars[128 * 8];
8074 int flags;
8075
Bram Moolenaard12a1322005-08-21 22:08:24 +00008076 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008077 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8078
8079 /* Form the <folchars> string first, we need to know its length. */
8080 l = 0;
8081 for (i = 128; i < 256; ++i)
8082 {
8083#ifdef FEAT_MBYTE
8084 if (has_mbyte)
8085 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8086 else
8087#endif
8088 folchars[l++] = spelltab.st_fold[i];
8089 }
8090 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8091
8092 fputc(128, fd); /* <charflagslen> */
8093 for (i = 128; i < 256; ++i)
8094 {
8095 flags = 0;
8096 if (spelltab.st_isw[i])
8097 flags |= CF_WORD;
8098 if (spelltab.st_isu[i])
8099 flags |= CF_UPPER;
8100 fputc(flags, fd); /* <charflags> */
8101 }
8102
8103 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008104 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008105 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008106
Bram Moolenaar5195e452005-08-19 20:32:47 +00008107 /* SN_MIDWORD: <midword> */
8108 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008109 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008110 putc(SN_MIDWORD, fd); /* <sectionID> */
8111 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8112
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008113 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008114 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008115 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8116 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008117 }
8118
Bram Moolenaar5195e452005-08-19 20:32:47 +00008119 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8120 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008121 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008122 putc(SN_PREFCOND, fd); /* <sectionID> */
8123 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8124
8125 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8126 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8127
8128 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008129 }
8130
Bram Moolenaar5195e452005-08-19 20:32:47 +00008131 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008132 * SN_SAL: <salflags> <salcount> <sal> ...
8133 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008134
Bram Moolenaar5195e452005-08-19 20:32:47 +00008135 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008136 * round 2: SN_SAL section (unless SN_SOFO is used)
8137 * round 3: SN_REPSAL section */
8138 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008139 {
8140 if (round == 1)
8141 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008142 else if (round == 2)
8143 {
8144 /* Don't write SN_SAL when using a SN_SOFO section */
8145 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8146 continue;
8147 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008149 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008150 gap = &spin->si_repsal;
8151
8152 /* Don't write the section if there are no items. */
8153 if (gap->ga_len == 0)
8154 continue;
8155
8156 /* Sort the REP/REPSAL items. */
8157 if (round != 2)
8158 qsort(gap->ga_data, (size_t)gap->ga_len,
8159 sizeof(fromto_T), rep_compare);
8160
8161 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8162 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008163
Bram Moolenaar5195e452005-08-19 20:32:47 +00008164 /* This is for making suggestions, section is not required. */
8165 putc(0, fd); /* <sectionflags> */
8166
8167 /* Compute the length of what follows. */
8168 l = 2; /* count <repcount> or <salcount> */
8169 for (i = 0; i < gap->ga_len; ++i)
8170 {
8171 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008172 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8173 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008174 }
8175 if (round == 2)
8176 ++l; /* count <salflags> */
8177 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8178
8179 if (round == 2)
8180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008181 i = 0;
8182 if (spin->si_followup)
8183 i |= SAL_F0LLOWUP;
8184 if (spin->si_collapse)
8185 i |= SAL_COLLAPSE;
8186 if (spin->si_rem_accents)
8187 i |= SAL_REM_ACCENTS;
8188 putc(i, fd); /* <salflags> */
8189 }
8190
8191 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8192 for (i = 0; i < gap->ga_len; ++i)
8193 {
8194 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8195 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8196 ftp = &((fromto_T *)gap->ga_data)[i];
8197 for (rr = 1; rr <= 2; ++rr)
8198 {
8199 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008200 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008202 if (l > 0)
8203 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 }
8205 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008207 }
8208
Bram Moolenaar5195e452005-08-19 20:32:47 +00008209 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8210 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008211 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8212 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008213 putc(SN_SOFO, fd); /* <sectionID> */
8214 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008216 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008217 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8218 /* <sectionlen> */
8219
8220 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008221 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008222
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008223 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008224 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008225 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008226 }
8227
Bram Moolenaar4770d092006-01-12 23:22:24 +00008228 /* SN_WORDS: <word> ...
8229 * This is for making suggestions, section is not required. */
8230 if (spin->si_commonwords.ht_used > 0)
8231 {
8232 putc(SN_WORDS, fd); /* <sectionID> */
8233 putc(0, fd); /* <sectionflags> */
8234
8235 /* round 1: count the bytes
8236 * round 2: write the bytes */
8237 for (round = 1; round <= 2; ++round)
8238 {
8239 int todo;
8240 int len = 0;
8241 hashitem_T *hi;
8242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008243 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008244 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8245 if (!HASHITEM_EMPTY(hi))
8246 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008247 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008248 len += l;
8249 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008250 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008251 --todo;
8252 }
8253 if (round == 1)
8254 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8255 }
8256 }
8257
Bram Moolenaar5195e452005-08-19 20:32:47 +00008258 /* SN_MAP: <mapstr>
8259 * This is for making suggestions, section is not required. */
8260 if (spin->si_map.ga_len > 0)
8261 {
8262 putc(SN_MAP, fd); /* <sectionID> */
8263 putc(0, fd); /* <sectionflags> */
8264 l = spin->si_map.ga_len;
8265 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008266 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008267 /* <mapstr> */
8268 }
8269
Bram Moolenaar4770d092006-01-12 23:22:24 +00008270 /* SN_SUGFILE: <timestamp>
8271 * This is used to notify that a .sug file may be available and at the
8272 * same time allows for checking that a .sug file that is found matches
8273 * with this .spl file. That's because the word numbers must be exactly
8274 * right. */
8275 if (!spin->si_nosugfile
8276 && (spin->si_sal.ga_len > 0
8277 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8278 {
8279 putc(SN_SUGFILE, fd); /* <sectionID> */
8280 putc(0, fd); /* <sectionflags> */
8281 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8282
8283 /* Set si_sugtime and write it to the file. */
8284 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008285 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008286 }
8287
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008288 /* SN_NOSPLITSUGS: nothing
8289 * This is used to notify that no suggestions with word splits are to be
8290 * made. */
8291 if (spin->si_nosplitsugs)
8292 {
8293 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8294 putc(0, fd); /* <sectionflags> */
8295 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8296 }
8297
Bram Moolenaar5195e452005-08-19 20:32:47 +00008298 /* SN_COMPOUND: compound info.
8299 * We don't mark it required, when not supported all compound words will
8300 * be bad words. */
8301 if (spin->si_compflags != NULL)
8302 {
8303 putc(SN_COMPOUND, fd); /* <sectionID> */
8304 putc(0, fd); /* <sectionflags> */
8305
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008306 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008307 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008308 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008309 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8310
Bram Moolenaar5195e452005-08-19 20:32:47 +00008311 putc(spin->si_compmax, fd); /* <compmax> */
8312 putc(spin->si_compminlen, fd); /* <compminlen> */
8313 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008314 putc(0, fd); /* for Vim 7.0b compatibility */
8315 putc(spin->si_compoptions, fd); /* <compoptions> */
8316 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8317 /* <comppatcount> */
8318 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8319 {
8320 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008321 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008322 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8323 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008324 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008325 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008326 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008327 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008328 }
8329
Bram Moolenaar78622822005-08-23 21:00:13 +00008330 /* SN_NOBREAK: NOBREAK flag */
8331 if (spin->si_nobreak)
8332 {
8333 putc(SN_NOBREAK, fd); /* <sectionID> */
8334 putc(0, fd); /* <sectionflags> */
8335
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008336 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008337 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8338 }
8339
Bram Moolenaar5195e452005-08-19 20:32:47 +00008340 /* SN_SYLLABLE: syllable info.
8341 * We don't mark it required, when not supported syllables will not be
8342 * counted. */
8343 if (spin->si_syllable != NULL)
8344 {
8345 putc(SN_SYLLABLE, fd); /* <sectionID> */
8346 putc(0, fd); /* <sectionflags> */
8347
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008348 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008349 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008350 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8351 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008352 }
8353
8354 /* end of <SECTIONS> */
8355 putc(SN_END, fd); /* <sectionend> */
8356
Bram Moolenaar50cde822005-06-05 21:54:54 +00008357
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008359 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008360 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008361 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008362 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008363 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008364 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008365 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008366 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008367 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008368 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008369 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008370
Bram Moolenaar0c405862005-06-22 22:26:26 +00008371 /* Clear the index and wnode fields in the tree. */
8372 clear_node(tree);
8373
Bram Moolenaar51485f02005-06-04 21:55:20 +00008374 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008375 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008376 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008377 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008378
Bram Moolenaar51485f02005-06-04 21:55:20 +00008379 /* number of nodes in 4 bytes */
8380 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008381 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008382
Bram Moolenaar51485f02005-06-04 21:55:20 +00008383 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008384 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008385 }
8386
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008387 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008388 if (putc(0, fd) == EOF)
8389 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008390theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008391 if (fclose(fd) == EOF)
8392 retval = FAIL;
8393
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008394 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008395 retval = FAIL;
8396 if (retval == FAIL)
8397 EMSG(_(e_write));
8398
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008399 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008400}
8401
8402/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008403 * Clear the index and wnode fields of "node", it siblings and its
8404 * children. This is needed because they are a union with other items to save
8405 * space.
8406 */
8407 static void
8408clear_node(node)
8409 wordnode_T *node;
8410{
8411 wordnode_T *np;
8412
8413 if (node != NULL)
8414 for (np = node; np != NULL; np = np->wn_sibling)
8415 {
8416 np->wn_u1.index = 0;
8417 np->wn_u2.wnode = NULL;
8418
8419 if (np->wn_byte != NUL)
8420 clear_node(np->wn_child);
8421 }
8422}
8423
8424
8425/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008426 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008427 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008428 * This first writes the list of possible bytes (siblings). Then for each
8429 * byte recursively write the children.
8430 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008431 * NOTE: The code here must match the code in read_tree_node(), since
8432 * assumptions are made about the indexes (so that we don't have to write them
8433 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008434 *
8435 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008436 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008437 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008438put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008439 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008440 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008441 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008442 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008443 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008444{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008445 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008446 int siblingcount = 0;
8447 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008448 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008449
Bram Moolenaar51485f02005-06-04 21:55:20 +00008450 /* If "node" is zero the tree is empty. */
8451 if (node == NULL)
8452 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008453
Bram Moolenaar51485f02005-06-04 21:55:20 +00008454 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008455 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008456
8457 /* Count the number of siblings. */
8458 for (np = node; np != NULL; np = np->wn_sibling)
8459 ++siblingcount;
8460
8461 /* Write the sibling count. */
8462 if (fd != NULL)
8463 putc(siblingcount, fd); /* <siblingcount> */
8464
8465 /* Write each sibling byte and optionally extra info. */
8466 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008467 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008468 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008469 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008470 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008471 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008472 /* For a NUL byte (end of word) write the flags etc. */
8473 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008474 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008475 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008476 * associated condition nr (stored in wn_region). The
8477 * byte value is misused to store the "rare" and "not
8478 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008479 if (np->wn_flags == (short_u)PFX_FLAGS)
8480 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008481 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008482 {
8483 putc(BY_FLAGS, fd); /* <byte> */
8484 putc(np->wn_flags, fd); /* <pflags> */
8485 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008486 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008487 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008488 }
8489 else
8490 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008491 /* For word trees we write the flag/region items. */
8492 flags = np->wn_flags;
8493 if (regionmask != 0 && np->wn_region != regionmask)
8494 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008495 if (np->wn_affixID != 0)
8496 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008497 if (flags == 0)
8498 {
8499 /* word without flags or region */
8500 putc(BY_NOFLAGS, fd); /* <byte> */
8501 }
8502 else
8503 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008504 if (np->wn_flags >= 0x100)
8505 {
8506 putc(BY_FLAGS2, fd); /* <byte> */
8507 putc(flags, fd); /* <flags> */
8508 putc((unsigned)flags >> 8, fd); /* <flags2> */
8509 }
8510 else
8511 {
8512 putc(BY_FLAGS, fd); /* <byte> */
8513 putc(flags, fd); /* <flags> */
8514 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008515 if (flags & WF_REGION)
8516 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008517 if (flags & WF_AFX)
8518 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008519 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008520 }
8521 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008522 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008523 else
8524 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008525 if (np->wn_child->wn_u1.index != 0
8526 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008527 {
8528 /* The child is written elsewhere, write the reference. */
8529 if (fd != NULL)
8530 {
8531 putc(BY_INDEX, fd); /* <byte> */
8532 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008533 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008534 }
8535 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008536 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008537 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008538 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008539
Bram Moolenaar51485f02005-06-04 21:55:20 +00008540 if (fd != NULL)
8541 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8542 {
8543 EMSG(_(e_write));
8544 return 0;
8545 }
8546 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008547 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008548
8549 /* Space used in the array when reading: one for each sibling and one for
8550 * the count. */
8551 newindex += siblingcount + 1;
8552
8553 /* Recursively dump the children of each sibling. */
8554 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008555 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8556 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008557 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008558
8559 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008560}
8561
8562
8563/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008564 * ":mkspell [-ascii] outfile infile ..."
8565 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008566 */
8567 void
8568ex_mkspell(eap)
8569 exarg_T *eap;
8570{
8571 int fcount;
8572 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008573 char_u *arg = eap->arg;
8574 int ascii = FALSE;
8575
8576 if (STRNCMP(arg, "-ascii", 6) == 0)
8577 {
8578 ascii = TRUE;
8579 arg = skipwhite(arg + 6);
8580 }
8581
8582 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02008583 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008586 FreeWild(fcount, fnames);
8587 }
8588}
8589
8590/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008591 * Create the .sug file.
8592 * Uses the soundfold info in "spin".
8593 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8594 */
8595 static void
8596spell_make_sugfile(spin, wfname)
8597 spellinfo_T *spin;
8598 char_u *wfname;
8599{
Bram Moolenaard9462e32011-04-11 21:35:11 +02008600 char_u *fname = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008601 int len;
8602 slang_T *slang;
8603 int free_slang = FALSE;
8604
8605 /*
8606 * Read back the .spl file that was written. This fills the required
8607 * info for soundfolding. This also uses less memory than the
8608 * pointer-linked version of the trie. And it avoids having two versions
8609 * of the code for the soundfolding stuff.
8610 * It might have been done already by spell_reload_one().
8611 */
8612 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8613 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8614 break;
8615 if (slang == NULL)
8616 {
8617 spell_message(spin, (char_u *)_("Reading back spell file..."));
8618 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8619 if (slang == NULL)
8620 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008621 free_slang = TRUE;
8622 }
8623
8624 /*
8625 * Clear the info in "spin" that is used.
8626 */
8627 spin->si_blocks = NULL;
8628 spin->si_blocks_cnt = 0;
8629 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8630 spin->si_free_count = 0;
8631 spin->si_first_free = NULL;
8632 spin->si_foldwcount = 0;
8633
8634 /*
8635 * Go through the trie of good words, soundfold each word and add it to
8636 * the soundfold trie.
8637 */
8638 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8639 if (sug_filltree(spin, slang) == FAIL)
8640 goto theend;
8641
8642 /*
8643 * Create the table which links each soundfold word with a list of the
8644 * good words it may come from. Creates buffer "spin->si_spellbuf".
8645 * This also removes the wordnr from the NUL byte entries to make
8646 * compression possible.
8647 */
8648 if (sug_maketable(spin) == FAIL)
8649 goto theend;
8650
8651 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8652 (long)spin->si_spellbuf->b_ml.ml_line_count);
8653
8654 /*
8655 * Compress the soundfold trie.
8656 */
8657 spell_message(spin, (char_u *)_(msg_compressing));
8658 wordtree_compress(spin, spin->si_foldroot);
8659
8660 /*
8661 * Write the .sug file.
8662 * Make the file name by changing ".spl" to ".sug".
8663 */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008664 fname = alloc(MAXPATHL);
8665 if (fname == NULL)
8666 goto theend;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008667 vim_strncpy(fname, wfname, MAXPATHL - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008668 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008669 fname[len - 2] = 'u';
8670 fname[len - 1] = 'g';
8671 sug_write(spin, fname);
8672
8673theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02008674 vim_free(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008675 if (free_slang)
8676 slang_free(slang);
8677 free_blocks(spin->si_blocks);
8678 close_spellbuf(spin->si_spellbuf);
8679}
8680
8681/*
8682 * Build the soundfold trie for language "slang".
8683 */
8684 static int
8685sug_filltree(spin, slang)
8686 spellinfo_T *spin;
8687 slang_T *slang;
8688{
8689 char_u *byts;
8690 idx_T *idxs;
8691 int depth;
8692 idx_T arridx[MAXWLEN];
8693 int curi[MAXWLEN];
8694 char_u tword[MAXWLEN];
8695 char_u tsalword[MAXWLEN];
8696 int c;
8697 idx_T n;
8698 unsigned words_done = 0;
8699 int wordcount[MAXWLEN];
8700
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008701 /* We use si_foldroot for the soundfolded trie. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008702 spin->si_foldroot = wordtree_alloc(spin);
8703 if (spin->si_foldroot == NULL)
8704 return FAIL;
8705
8706 /* let tree_add_word() know we're adding to the soundfolded tree */
8707 spin->si_sugtree = TRUE;
8708
8709 /*
8710 * Go through the whole case-folded tree, soundfold each word and put it
8711 * in the trie.
8712 */
8713 byts = slang->sl_fbyts;
8714 idxs = slang->sl_fidxs;
8715
8716 arridx[0] = 0;
8717 curi[0] = 1;
8718 wordcount[0] = 0;
8719
8720 depth = 0;
8721 while (depth >= 0 && !got_int)
8722 {
8723 if (curi[depth] > byts[arridx[depth]])
8724 {
8725 /* Done all bytes at this node, go up one level. */
8726 idxs[arridx[depth]] = wordcount[depth];
8727 if (depth > 0)
8728 wordcount[depth - 1] += wordcount[depth];
8729
8730 --depth;
8731 line_breakcheck();
8732 }
8733 else
8734 {
8735
8736 /* Do one more byte at this node. */
8737 n = arridx[depth] + curi[depth];
8738 ++curi[depth];
8739
8740 c = byts[n];
8741 if (c == 0)
8742 {
8743 /* Sound-fold the word. */
8744 tword[depth] = NUL;
8745 spell_soundfold(slang, tword, TRUE, tsalword);
8746
8747 /* We use the "flags" field for the MSB of the wordnr,
8748 * "region" for the LSB of the wordnr. */
8749 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8750 words_done >> 16, words_done & 0xffff,
8751 0) == FAIL)
8752 return FAIL;
8753
8754 ++words_done;
8755 ++wordcount[depth];
8756
8757 /* Reset the block count each time to avoid compression
8758 * kicking in. */
8759 spin->si_blocks_cnt = 0;
8760
8761 /* Skip over any other NUL bytes (same word with different
8762 * flags). */
8763 while (byts[n + 1] == 0)
8764 {
8765 ++n;
8766 ++curi[depth];
8767 }
8768 }
8769 else
8770 {
8771 /* Normal char, go one level deeper. */
8772 tword[depth++] = c;
8773 arridx[depth] = idxs[n];
8774 curi[depth] = 1;
8775 wordcount[depth] = 0;
8776 }
8777 }
8778 }
8779
8780 smsg((char_u *)_("Total number of words: %d"), words_done);
8781
8782 return OK;
8783}
8784
8785/*
8786 * Make the table that links each word in the soundfold trie to the words it
8787 * can be produced from.
8788 * This is not unlike lines in a file, thus use a memfile to be able to access
8789 * the table efficiently.
8790 * Returns FAIL when out of memory.
8791 */
8792 static int
8793sug_maketable(spin)
8794 spellinfo_T *spin;
8795{
8796 garray_T ga;
8797 int res = OK;
8798
8799 /* Allocate a buffer, open a memline for it and create the swap file
8800 * (uses a temp file, not a .swp file). */
8801 spin->si_spellbuf = open_spellbuf();
8802 if (spin->si_spellbuf == NULL)
8803 return FAIL;
8804
8805 /* Use a buffer to store the line info, avoids allocating many small
8806 * pieces of memory. */
8807 ga_init2(&ga, 1, 100);
8808
8809 /* recursively go through the tree */
8810 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8811 res = FAIL;
8812
8813 ga_clear(&ga);
8814 return res;
8815}
8816
8817/*
8818 * Fill the table for one node and its children.
8819 * Returns the wordnr at the start of the node.
8820 * Returns -1 when out of memory.
8821 */
8822 static int
8823sug_filltable(spin, node, startwordnr, gap)
8824 spellinfo_T *spin;
8825 wordnode_T *node;
8826 int startwordnr;
8827 garray_T *gap; /* place to store line of numbers */
8828{
8829 wordnode_T *p, *np;
8830 int wordnr = startwordnr;
8831 int nr;
8832 int prev_nr;
8833
8834 for (p = node; p != NULL; p = p->wn_sibling)
8835 {
8836 if (p->wn_byte == NUL)
8837 {
8838 gap->ga_len = 0;
8839 prev_nr = 0;
8840 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8841 {
8842 if (ga_grow(gap, 10) == FAIL)
8843 return -1;
8844
8845 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8846 /* Compute the offset from the previous nr and store the
8847 * offset in a way that it takes a minimum number of bytes.
8848 * It's a bit like utf-8, but without the need to mark
8849 * following bytes. */
8850 nr -= prev_nr;
8851 prev_nr += nr;
8852 gap->ga_len += offset2bytes(nr,
8853 (char_u *)gap->ga_data + gap->ga_len);
8854 }
8855
8856 /* add the NUL byte */
8857 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8858
8859 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8860 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8861 return -1;
8862 ++wordnr;
8863
8864 /* Remove extra NUL entries, we no longer need them. We don't
8865 * bother freeing the nodes, the won't be reused anyway. */
8866 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8867 p->wn_sibling = p->wn_sibling->wn_sibling;
8868
8869 /* Clear the flags on the remaining NUL node, so that compression
8870 * works a lot better. */
8871 p->wn_flags = 0;
8872 p->wn_region = 0;
8873 }
8874 else
8875 {
8876 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8877 if (wordnr == -1)
8878 return -1;
8879 }
8880 }
8881 return wordnr;
8882}
8883
8884/*
8885 * Convert an offset into a minimal number of bytes.
8886 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8887 * bytes.
8888 */
8889 static int
8890offset2bytes(nr, buf)
8891 int nr;
8892 char_u *buf;
8893{
8894 int rem;
8895 int b1, b2, b3, b4;
8896
8897 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8898 b1 = nr % 255 + 1;
8899 rem = nr / 255;
8900 b2 = rem % 255 + 1;
8901 rem = rem / 255;
8902 b3 = rem % 255 + 1;
8903 b4 = rem / 255 + 1;
8904
8905 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8906 {
8907 buf[0] = 0xe0 + b4;
8908 buf[1] = b3;
8909 buf[2] = b2;
8910 buf[3] = b1;
8911 return 4;
8912 }
8913 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8914 {
8915 buf[0] = 0xc0 + b3;
8916 buf[1] = b2;
8917 buf[2] = b1;
8918 return 3;
8919 }
8920 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8921 {
8922 buf[0] = 0x80 + b2;
8923 buf[1] = b1;
8924 return 2;
8925 }
8926 /* 1 byte */
8927 buf[0] = b1;
8928 return 1;
8929}
8930
8931/*
8932 * Opposite of offset2bytes().
8933 * "pp" points to the bytes and is advanced over it.
8934 * Returns the offset.
8935 */
8936 static int
8937bytes2offset(pp)
8938 char_u **pp;
8939{
8940 char_u *p = *pp;
8941 int nr;
8942 int c;
8943
8944 c = *p++;
8945 if ((c & 0x80) == 0x00) /* 1 byte */
8946 {
8947 nr = c - 1;
8948 }
8949 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8950 {
8951 nr = (c & 0x3f) - 1;
8952 nr = nr * 255 + (*p++ - 1);
8953 }
8954 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8955 {
8956 nr = (c & 0x1f) - 1;
8957 nr = nr * 255 + (*p++ - 1);
8958 nr = nr * 255 + (*p++ - 1);
8959 }
8960 else /* 4 bytes */
8961 {
8962 nr = (c & 0x0f) - 1;
8963 nr = nr * 255 + (*p++ - 1);
8964 nr = nr * 255 + (*p++ - 1);
8965 nr = nr * 255 + (*p++ - 1);
8966 }
8967
8968 *pp = p;
8969 return nr;
8970}
8971
8972/*
8973 * Write the .sug file in "fname".
8974 */
8975 static void
8976sug_write(spin, fname)
8977 spellinfo_T *spin;
8978 char_u *fname;
8979{
8980 FILE *fd;
8981 wordnode_T *tree;
8982 int nodecount;
8983 int wcount;
8984 char_u *line;
8985 linenr_T lnum;
8986 int len;
8987
8988 /* Create the file. Note that an existing file is silently overwritten! */
8989 fd = mch_fopen((char *)fname, "w");
8990 if (fd == NULL)
8991 {
8992 EMSG2(_(e_notopen), fname);
8993 return;
8994 }
8995
8996 vim_snprintf((char *)IObuff, IOSIZE,
8997 _("Writing suggestion file %s ..."), fname);
8998 spell_message(spin, IObuff);
8999
9000 /*
9001 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
9002 */
9003 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
9004 {
9005 EMSG(_(e_write));
9006 goto theend;
9007 }
9008 putc(VIMSUGVERSION, fd); /* <versionnr> */
9009
9010 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02009011 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009012
9013 /*
9014 * <SUGWORDTREE>
9015 */
9016 spin->si_memtot = 0;
9017 tree = spin->si_foldroot->wn_sibling;
9018
9019 /* Clear the index and wnode fields in the tree. */
9020 clear_node(tree);
9021
9022 /* Count the number of nodes. Needed to be able to allocate the
9023 * memory when reading the nodes. Also fills in index for shared
9024 * nodes. */
9025 nodecount = put_node(NULL, tree, 0, 0, FALSE);
9026
9027 /* number of nodes in 4 bytes */
9028 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9029 spin->si_memtot += nodecount + nodecount * sizeof(int);
9030
9031 /* Write the nodes. */
9032 (void)put_node(fd, tree, 0, 0, FALSE);
9033
9034 /*
9035 * <SUGTABLE>: <sugwcount> <sugline> ...
9036 */
9037 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9038 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9039
9040 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9041 {
9042 /* <sugline>: <sugnr> ... NUL */
9043 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009044 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009045 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9046 {
9047 EMSG(_(e_write));
9048 goto theend;
9049 }
9050 spin->si_memtot += len;
9051 }
9052
9053 /* Write another byte to check for errors. */
9054 if (putc(0, fd) == EOF)
9055 EMSG(_(e_write));
9056
9057 vim_snprintf((char *)IObuff, IOSIZE,
9058 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9059 spell_message(spin, IObuff);
9060
9061theend:
9062 /* close the file */
9063 fclose(fd);
9064}
9065
9066/*
9067 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9068 * list and only contains text lines. Can use a swapfile to reduce memory
9069 * use.
9070 * Most other fields are invalid! Esp. watch out for string options being
9071 * NULL and there is no undo info.
9072 * Returns NULL when out of memory.
9073 */
9074 static buf_T *
9075open_spellbuf()
9076{
9077 buf_T *buf;
9078
9079 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9080 if (buf != NULL)
9081 {
9082 buf->b_spell = TRUE;
9083 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02009084#ifdef FEAT_CRYPT
9085 buf->b_p_key = empty_option;
9086#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00009087 ml_open(buf);
9088 ml_open_file(buf); /* create swap file now */
9089 }
9090 return buf;
9091}
9092
9093/*
9094 * Close the buffer used for spell info.
9095 */
9096 static void
9097close_spellbuf(buf)
9098 buf_T *buf;
9099{
9100 if (buf != NULL)
9101 {
9102 ml_close(buf, TRUE);
9103 vim_free(buf);
9104 }
9105}
9106
9107
9108/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009109 * Create a Vim spell file from one or more word lists.
9110 * "fnames[0]" is the output file name.
9111 * "fnames[fcount - 1]" is the last input file name.
9112 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9113 * and ".spl" is appended to make the output file name.
9114 */
9115 static void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009116mkspell(fcount, fnames, ascii, over_write, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009117 int fcount;
9118 char_u **fnames;
9119 int ascii; /* -ascii argument given */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009120 int over_write; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009122{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009123 char_u *fname = NULL;
9124 char_u *wfname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009125 char_u **innames;
9126 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009127 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009128 int i;
9129 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009130 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009131 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009132 spellinfo_T spin;
9133
9134 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009136 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 spin.si_followup = TRUE;
9138 spin.si_rem_accents = TRUE;
9139 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009140 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9142 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009143 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009144 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009145 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009146 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009147
Bram Moolenaarb765d632005-06-07 21:00:02 +00009148 /* default: fnames[0] is output file, following are input files */
9149 innames = &fnames[1];
9150 incount = fcount - 1;
9151
Bram Moolenaard9462e32011-04-11 21:35:11 +02009152 wfname = alloc(MAXPATHL);
9153 if (wfname == NULL)
9154 return;
9155
Bram Moolenaarb765d632005-06-07 21:00:02 +00009156 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009157 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009158 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009159 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9160 {
9161 /* For ":mkspell path/en.latin1.add" output file is
9162 * "path/en.latin1.add.spl". */
9163 innames = &fnames[0];
9164 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009165 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009166 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009167 else if (fcount == 1)
9168 {
9169 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9170 innames = &fnames[0];
9171 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009172 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009173 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009174 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009175 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9176 {
9177 /* Name ends in ".spl", use as the file name. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009178 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009179 }
9180 else
9181 /* Name should be language, make the file name from it. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009182 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009183 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009184
9185 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009186 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009187 spin.si_ascii = TRUE;
9188
9189 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009190 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009191 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009192 }
9193
Bram Moolenaarb765d632005-06-07 21:00:02 +00009194 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009195 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009196 else if (vim_strchr(gettail(wfname), '_') != NULL)
9197 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009198 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009199 EMSG(_("E754: Only up to 8 regions supported"));
9200 else
9201 {
9202 /* Check for overwriting before doing things that may take a lot of
9203 * time. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009204 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009205 {
9206 EMSG(_(e_exists));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009207 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009208 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009209 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009210 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009211 EMSG2(_(e_isadir2), wfname);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009212 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009213 }
9214
Bram Moolenaard9462e32011-04-11 21:35:11 +02009215 fname = alloc(MAXPATHL);
9216 if (fname == NULL)
9217 goto theend;
9218
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009219 /*
9220 * Init the aff and dic pointers.
9221 * Get the region names if there are more than 2 arguments.
9222 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009223 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009224 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009225 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009226
Bram Moolenaar3982c542005-06-08 21:56:31 +00009227 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009228 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009229 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009230 if (STRLEN(gettail(innames[i])) < 5
9231 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009232 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009233 EMSG2(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009234 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009235 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009236 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9237 spin.si_region_name[i * 2 + 1] =
9238 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009239 }
9240 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009241 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009242
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009243 spin.si_foldroot = wordtree_alloc(&spin);
9244 spin.si_keeproot = wordtree_alloc(&spin);
9245 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009246 if (spin.si_foldroot == NULL
9247 || spin.si_keeproot == NULL
9248 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009249 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009250 free_blocks(spin.si_blocks);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009251 goto theend;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009252 }
9253
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009254 /* When not producing a .add.spl file clear the character table when
9255 * we encounter one in the .aff file. This means we dump the current
9256 * one in the .spl file if the .aff file doesn't define one. That's
9257 * better than guessing the contents, the table will match a
9258 * previously loaded spell file. */
9259 if (!spin.si_add)
9260 spin.si_clear_chartab = TRUE;
9261
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009262 /*
9263 * Read all the .aff and .dic files.
9264 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009265 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009266 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009267 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009268 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009269 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009270 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009271
Bram Moolenaard9462e32011-04-11 21:35:11 +02009272 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009273 if (mch_stat((char *)fname, &st) >= 0)
9274 {
9275 /* Read the .aff file. Will init "spin->si_conv" based on the
9276 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009277 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009278 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009279 error = TRUE;
9280 else
9281 {
9282 /* Read the .dic file and store the words in the trees. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009283 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009284 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009285 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009286 error = TRUE;
9287 }
9288 }
9289 else
9290 {
9291 /* No .aff file, try reading the file as a word list. Store
9292 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009293 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009294 error = TRUE;
9295 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009296
Bram Moolenaarb765d632005-06-07 21:00:02 +00009297#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009298 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009299 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009300#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009301 }
9302
Bram Moolenaar78622822005-08-23 21:00:13 +00009303 if (spin.si_compflags != NULL && spin.si_nobreak)
9304 MSG(_("Warning: both compounding and NOBREAK specified"));
9305
Bram Moolenaar4770d092006-01-12 23:22:24 +00009306 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009307 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009308 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009309 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009310 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009311 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009312 wordtree_compress(&spin, spin.si_foldroot);
9313 wordtree_compress(&spin, spin.si_keeproot);
9314 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009315 }
9316
Bram Moolenaar4770d092006-01-12 23:22:24 +00009317 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009318 {
9319 /*
9320 * Write the info in the spell file.
9321 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009322 vim_snprintf((char *)IObuff, IOSIZE,
9323 _("Writing spell file %s ..."), wfname);
9324 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009325
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009326 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009327
Bram Moolenaar4770d092006-01-12 23:22:24 +00009328 spell_message(&spin, (char_u *)_("Done!"));
9329 vim_snprintf((char *)IObuff, IOSIZE,
9330 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9331 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009332
Bram Moolenaar4770d092006-01-12 23:22:24 +00009333 /*
9334 * If the file is loaded need to reload it.
9335 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009336 if (!error)
9337 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009338 }
9339
9340 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009342 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 ga_clear(&spin.si_sal);
9344 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009345 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009346 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009347 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009348
9349 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009350 for (i = 0; i < incount; ++i)
9351 if (afile[i] != NULL)
9352 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009353
9354 /* Free all the bits and pieces at once. */
9355 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009356
9357 /*
9358 * If there is soundfolding info and no NOSUGFILE item create the
9359 * .sug file with the soundfolded word trie.
9360 */
9361 if (spin.si_sugtime != 0 && !error && !got_int)
9362 spell_make_sugfile(&spin, wfname);
9363
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009364 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009365
9366theend:
9367 vim_free(fname);
9368 vim_free(wfname);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009369}
9370
Bram Moolenaar4770d092006-01-12 23:22:24 +00009371/*
9372 * Display a message for spell file processing when 'verbose' is set or using
9373 * ":mkspell". "str" can be IObuff.
9374 */
9375 static void
9376spell_message(spin, str)
9377 spellinfo_T *spin;
9378 char_u *str;
9379{
9380 if (spin->si_verbose || p_verbose > 2)
9381 {
9382 if (!spin->si_verbose)
9383 verbose_enter();
9384 MSG(str);
9385 out_flush();
9386 if (!spin->si_verbose)
9387 verbose_leave();
9388 }
9389}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009390
9391/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009392 * ":[count]spellgood {word}"
9393 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009394 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009395 */
9396 void
9397ex_spell(eap)
9398 exarg_T *eap;
9399{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009400 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009401 eap->forceit ? 0 : (int)eap->line2,
9402 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009403}
9404
9405/*
9406 * Add "word[len]" to 'spellfile' as a good or bad word.
9407 */
9408 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009409spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009410 char_u *word;
9411 int len;
9412 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009413 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009414 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009415 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009416{
Bram Moolenaara3917072006-09-14 08:48:14 +00009417 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009418 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009419 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009420 char_u *fname;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009421 char_u *fnamebuf = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009422 char_u line[MAXWLEN * 2];
9423 long fpos, fpos_next = 0;
9424 int i;
9425 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009426
Bram Moolenaar89d40322006-08-29 15:30:07 +00009427 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009428 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009429 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009430 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02009431 int_wordlist = vim_tempname('s', FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009432 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009433 return;
9434 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009435 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009436 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009437 else
9438 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009439 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009440 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009441 {
9442 init_spellfile();
9443 new_spf = TRUE;
9444 }
9445
Bram Moolenaar860cae12010-06-05 23:22:07 +02009446 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009447 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009448 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009449 return;
9450 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009451 fnamebuf = alloc(MAXPATHL);
9452 if (fnamebuf == NULL)
9453 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009454
Bram Moolenaar860cae12010-06-05 23:22:07 +02009455 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009456 {
9457 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009458 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009459 break;
9460 if (*spf == NUL)
9461 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009462 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009463 vim_free(fnamebuf);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009464 return;
9465 }
9466 }
9467
Bram Moolenaarb765d632005-06-07 21:00:02 +00009468 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009469 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009470 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9471 buf = NULL;
9472 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009473 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009474 EMSG(_(e_bufloaded));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009475 vim_free(fnamebuf);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009476 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009477 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009478
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009479 fname = fnamebuf;
9480 }
9481
Bram Moolenaard0131a82006-03-04 21:46:13 +00009482 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009483 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009484 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009485 * since its flags sort before the one with WF_BANNED. */
9486 fd = mch_fopen((char *)fname, "r");
9487 if (fd != NULL)
9488 {
9489 while (!vim_fgets(line, MAXWLEN * 2, fd))
9490 {
9491 fpos = fpos_next;
9492 fpos_next = ftell(fd);
9493 if (STRNCMP(word, line, len) == 0
9494 && (line[len] == '/' || line[len] < ' '))
9495 {
9496 /* Found duplicate word. Remove it by writing a '#' at
9497 * the start of the line. Mixing reading and writing
9498 * doesn't work for all systems, close the file first. */
9499 fclose(fd);
9500 fd = mch_fopen((char *)fname, "r+");
9501 if (fd == NULL)
9502 break;
9503 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009504 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009505 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009506 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009507 {
9508 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009509 smsg((char_u *)_("Word '%.*s' removed from %s"),
9510 len, word, NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009511 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009512 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009513 fseek(fd, fpos_next, SEEK_SET);
9514 }
9515 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009516 if (fd != NULL)
9517 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009518 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009519 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009520
9521 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009522 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009523 fd = mch_fopen((char *)fname, "a");
9524 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009525 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009526 char_u *p;
9527
Bram Moolenaard0131a82006-03-04 21:46:13 +00009528 /* We just initialized the 'spellfile' option and can't open the
9529 * file. We may need to create the "spell" directory first. We
9530 * already checked the runtime directory is writable in
9531 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009532 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009533 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009534 int c = *p;
9535
Bram Moolenaard0131a82006-03-04 21:46:13 +00009536 /* The directory doesn't exist. Try creating it and opening
9537 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009538 *p = NUL;
9539 vim_mkdir(fname, 0755);
9540 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009541 fd = mch_fopen((char *)fname, "a");
9542 }
9543 }
9544
9545 if (fd == NULL)
9546 EMSG2(_(e_notopen), fname);
9547 else
9548 {
9549 if (bad)
9550 fprintf(fd, "%.*s/!\n", len, word);
9551 else
9552 fprintf(fd, "%.*s\n", len, word);
9553 fclose(fd);
9554
9555 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009556 smsg((char_u *)_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009557 }
9558 }
9559
Bram Moolenaard0131a82006-03-04 21:46:13 +00009560 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009561 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009562 /* Update the .add.spl file. */
9563 mkspell(1, &fname, FALSE, TRUE, TRUE);
9564
9565 /* If the .add file is edited somewhere, reload it. */
9566 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009567 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009568
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009569 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009570 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009571 vim_free(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009572}
9573
9574/*
9575 * Initialize 'spellfile' for the current buffer.
9576 */
9577 static void
9578init_spellfile()
9579{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009580 char_u *buf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009581 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009582 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009583 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009584 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009585 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009586 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009587
Bram Moolenaar860cae12010-06-05 23:22:07 +02009588 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009589 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009590 buf = alloc(MAXPATHL);
9591 if (buf == NULL)
9592 return;
9593
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009594 /* Find the end of the language name. Exclude the region. If there
9595 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009596 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009597 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009598 if (vim_ispathsep(*lend))
9599 {
9600 aspath = TRUE;
9601 lstart = lend + 1;
9602 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009603
9604 /* Loop over all entries in 'runtimepath'. Use the first one where we
9605 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009606 rtp = p_rtp;
9607 while (*rtp != NUL)
9608 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009609 if (aspath)
9610 /* Use directory of an entry with path, e.g., for
9611 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009612 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9613 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009614 else
9615 /* Copy the path from 'runtimepath' to buf[]. */
9616 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009617 if (filewritable(buf) == 2)
9618 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009619 /* Use the first language name from 'spelllang' and the
9620 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009621 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009622 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9623 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009624 else
9625 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009626 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009627 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009628 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009629 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009630 vim_mkdir(buf, 0755);
9631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009632 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009633 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009634 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009635 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009636 l = (int)STRLEN(buf);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009637 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9638 ->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009639 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9640 fname != NULL
9641 && strstr((char *)gettail(fname), ".ascii.") != NULL
9642 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009643 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9644 break;
9645 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009646 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009647 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009648
9649 vim_free(buf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009650 }
9651}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009652
Bram Moolenaar51485f02005-06-04 21:55:20 +00009653
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009654/*
9655 * Init the chartab used for spelling for ASCII.
9656 * EBCDIC is not supported!
9657 */
9658 static void
9659clear_spell_chartab(sp)
9660 spelltab_T *sp;
9661{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009662 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009663
9664 /* Init everything to FALSE. */
9665 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9666 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9667 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009668 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009669 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009670 sp->st_upper[i] = i;
9671 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009672
9673 /* We include digits. A word shouldn't start with a digit, but handling
9674 * that is done separately. */
9675 for (i = '0'; i <= '9'; ++i)
9676 sp->st_isw[i] = TRUE;
9677 for (i = 'A'; i <= 'Z'; ++i)
9678 {
9679 sp->st_isw[i] = TRUE;
9680 sp->st_isu[i] = TRUE;
9681 sp->st_fold[i] = i + 0x20;
9682 }
9683 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009684 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009685 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009686 sp->st_upper[i] = i - 0x20;
9687 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009688}
9689
9690/*
9691 * Init the chartab used for spelling. Only depends on 'encoding'.
9692 * Called once while starting up and when 'encoding' changes.
9693 * The default is to use isalpha(), but the spell file should define the word
9694 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009695 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009696 */
9697 void
9698init_spell_chartab()
9699{
9700 int i;
9701
9702 did_set_spelltab = FALSE;
9703 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009704#ifdef FEAT_MBYTE
9705 if (enc_dbcs)
9706 {
9707 /* DBCS: assume double-wide characters are word characters. */
9708 for (i = 128; i <= 255; ++i)
9709 if (MB_BYTE2LEN(i) == 2)
9710 spelltab.st_isw[i] = TRUE;
9711 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009712 else if (enc_utf8)
9713 {
9714 for (i = 128; i < 256; ++i)
9715 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009716 int f = utf_fold(i);
9717 int u = utf_toupper(i);
9718
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009719 spelltab.st_isu[i] = utf_isupper(i);
9720 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009721 /* The folded/upper-cased value is different between latin1 and
9722 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9723 * value for utf-8 to avoid this. */
9724 spelltab.st_fold[i] = (f < 256) ? f : i;
9725 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009726 }
9727 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009728 else
9729#endif
9730 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009731 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009732 for (i = 128; i < 256; ++i)
9733 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009734 if (MB_ISUPPER(i))
9735 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009736 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009737 spelltab.st_isu[i] = TRUE;
9738 spelltab.st_fold[i] = MB_TOLOWER(i);
9739 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009740 else if (MB_ISLOWER(i))
9741 {
9742 spelltab.st_isw[i] = TRUE;
9743 spelltab.st_upper[i] = MB_TOUPPER(i);
9744 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009745 }
9746 }
9747}
9748
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009749/*
9750 * Set the spell character tables from strings in the affix file.
9751 */
9752 static int
9753set_spell_chartab(fol, low, upp)
9754 char_u *fol;
9755 char_u *low;
9756 char_u *upp;
9757{
9758 /* We build the new tables here first, so that we can compare with the
9759 * previous one. */
9760 spelltab_T new_st;
9761 char_u *pf = fol, *pl = low, *pu = upp;
9762 int f, l, u;
9763
9764 clear_spell_chartab(&new_st);
9765
9766 while (*pf != NUL)
9767 {
9768 if (*pl == NUL || *pu == NUL)
9769 {
9770 EMSG(_(e_affform));
9771 return FAIL;
9772 }
9773#ifdef FEAT_MBYTE
9774 f = mb_ptr2char_adv(&pf);
9775 l = mb_ptr2char_adv(&pl);
9776 u = mb_ptr2char_adv(&pu);
9777#else
9778 f = *pf++;
9779 l = *pl++;
9780 u = *pu++;
9781#endif
9782 /* Every character that appears is a word character. */
9783 if (f < 256)
9784 new_st.st_isw[f] = TRUE;
9785 if (l < 256)
9786 new_st.st_isw[l] = TRUE;
9787 if (u < 256)
9788 new_st.st_isw[u] = TRUE;
9789
9790 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9791 * case-folding */
9792 if (l < 256 && l != f)
9793 {
9794 if (f >= 256)
9795 {
9796 EMSG(_(e_affrange));
9797 return FAIL;
9798 }
9799 new_st.st_fold[l] = f;
9800 }
9801
9802 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009803 * case-folding, it's upper case and the "UPP" is the upper case of
9804 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009805 if (u < 256 && u != f)
9806 {
9807 if (f >= 256)
9808 {
9809 EMSG(_(e_affrange));
9810 return FAIL;
9811 }
9812 new_st.st_fold[u] = f;
9813 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009814 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009815 }
9816 }
9817
9818 if (*pl != NUL || *pu != NUL)
9819 {
9820 EMSG(_(e_affform));
9821 return FAIL;
9822 }
9823
9824 return set_spell_finish(&new_st);
9825}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009826
9827/*
9828 * Set the spell character tables from strings in the .spl file.
9829 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009830 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009831set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009832 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009833 int cnt; /* length of "flags" */
9834 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009835{
9836 /* We build the new tables here first, so that we can compare with the
9837 * previous one. */
9838 spelltab_T new_st;
9839 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009840 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009841 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009842
9843 clear_spell_chartab(&new_st);
9844
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009845 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009846 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009847 if (i < cnt)
9848 {
9849 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9850 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9851 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009852
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009853 if (*p != NUL)
9854 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009855#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009856 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009857#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009858 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009859#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009860 new_st.st_fold[i + 128] = c;
9861 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9862 new_st.st_upper[c] = i + 128;
9863 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009864 }
9865
Bram Moolenaar5195e452005-08-19 20:32:47 +00009866 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009867}
9868
9869 static int
9870set_spell_finish(new_st)
9871 spelltab_T *new_st;
9872{
9873 int i;
9874
9875 if (did_set_spelltab)
9876 {
9877 /* check that it's the same table */
9878 for (i = 0; i < 256; ++i)
9879 {
9880 if (spelltab.st_isw[i] != new_st->st_isw[i]
9881 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009882 || spelltab.st_fold[i] != new_st->st_fold[i]
9883 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009884 {
9885 EMSG(_("E763: Word characters differ between spell files"));
9886 return FAIL;
9887 }
9888 }
9889 }
9890 else
9891 {
9892 /* copy the new spelltab into the one being used */
9893 spelltab = *new_st;
9894 did_set_spelltab = TRUE;
9895 }
9896
9897 return OK;
9898}
9899
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009900/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009901 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009902 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009903 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009904 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009905 */
9906 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009907spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009908 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009909 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009910{
Bram Moolenaarea408852005-06-25 22:49:46 +00009911#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009912 char_u *s;
9913 int l;
9914 int c;
9915
9916 if (has_mbyte)
9917 {
9918 l = MB_BYTE2LEN(*p);
9919 s = p;
9920 if (l == 1)
9921 {
9922 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009923 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009924 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009925 }
9926 else
9927 {
9928 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009929 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9930 : (wp->w_s->b_spell_ismw_mb != NULL
9931 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009932 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009933 }
9934
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009935 c = mb_ptr2char(s);
9936 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009937 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009938 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009939 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009940#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009941
Bram Moolenaar860cae12010-06-05 23:22:07 +02009942 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009943}
9944
9945/*
9946 * Return TRUE if "p" points to a word character.
9947 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9948 */
9949 static int
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009950spell_iswordp_nmw(p, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009951 char_u *p;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009952 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009953{
9954#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009955 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009956
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009957 if (has_mbyte)
9958 {
9959 c = mb_ptr2char(p);
9960 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009961 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009962 return spelltab.st_isw[c];
9963 }
9964#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009965 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009966}
9967
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009968#ifdef FEAT_MBYTE
9969/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009970 * Return TRUE if word class indicates a word character.
9971 * Only for characters above 255.
9972 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009973 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009974 */
9975 static int
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009976spell_mb_isword_class(cl, wp)
9977 int cl;
9978 win_T *wp;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009979{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009980 if (wp->w_s->b_cjk)
9981 /* East Asian characters are not considered word characters. */
9982 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009983 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9984}
9985
9986/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009987 * Return TRUE if "p" points to a word character.
9988 * Wide version of spell_iswordp().
9989 */
9990 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009991spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009992 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009993 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009994{
9995 int *s;
9996
Bram Moolenaar860cae12010-06-05 23:22:07 +02009997 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9998 : (wp->w_s->b_spell_ismw_mb != NULL
9999 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010000 s = p + 1;
10001 else
10002 s = p;
10003
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010004 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010005 {
10006 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010007 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010008 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010009 return spell_mb_isword_class(
10010 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010011 return 0;
10012 }
10013 return spelltab.st_isw[*s];
10014}
10015#endif
10016
Bram Moolenaarea408852005-06-25 22:49:46 +000010017/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010018 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010019 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010020 */
Bram Moolenaar5195e452005-08-19 20:32:47 +000010021 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010022write_spell_prefcond(fd, gap)
10023 FILE *fd;
10024 garray_T *gap;
10025{
10026 int i;
10027 char_u *p;
10028 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010029 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +000010030 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010031
Bram Moolenaar5195e452005-08-19 20:32:47 +000010032 if (fd != NULL)
10033 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
10034
10035 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010036
10037 for (i = 0; i < gap->ga_len; ++i)
10038 {
10039 /* <prefcond> : <condlen> <condstr> */
10040 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +000010041 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010042 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010043 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010044 if (fd != NULL)
10045 {
10046 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +000010047 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010048 }
10049 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010050 }
Bram Moolenaar5195e452005-08-19 20:32:47 +000010051 else if (fd != NULL)
10052 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010053 }
10054
Bram Moolenaar5195e452005-08-19 20:32:47 +000010055 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010056}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010057
10058/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010059 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
10060 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010061 * When using a multi-byte 'encoding' the length may change!
10062 * Returns FAIL when something wrong.
10063 */
10064 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010065spell_casefold(str, len, buf, buflen)
10066 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010067 int len;
10068 char_u *buf;
10069 int buflen;
10070{
10071 int i;
10072
10073 if (len >= buflen)
10074 {
10075 buf[0] = NUL;
10076 return FAIL; /* result will not fit */
10077 }
10078
10079#ifdef FEAT_MBYTE
10080 if (has_mbyte)
10081 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010082 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010083 char_u *p;
10084 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010085
10086 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010087 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010088 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010089 if (outi + MB_MAXBYTES > buflen)
10090 {
10091 buf[outi] = NUL;
10092 return FAIL;
10093 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010094 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010095 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010096 }
10097 buf[outi] = NUL;
10098 }
10099 else
10100#endif
10101 {
10102 /* Be quick for non-multibyte encodings. */
10103 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010104 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010105 buf[i] = NUL;
10106 }
10107
10108 return OK;
10109}
10110
Bram Moolenaar4770d092006-01-12 23:22:24 +000010111/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010112#define SPS_BEST 1
10113#define SPS_FAST 2
10114#define SPS_DOUBLE 4
10115
Bram Moolenaar4770d092006-01-12 23:22:24 +000010116static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10117static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010118
10119/*
10120 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010121 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010122 */
10123 int
10124spell_check_sps()
10125{
10126 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010127 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010128 char_u buf[MAXPATHL];
10129 int f;
10130
10131 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010132 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010133
10134 for (p = p_sps; *p != NUL; )
10135 {
10136 copy_option_part(&p, buf, MAXPATHL, ",");
10137
10138 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010139 if (VIM_ISDIGIT(*buf))
10140 {
10141 s = buf;
10142 sps_limit = getdigits(&s);
10143 if (*s != NUL && !VIM_ISDIGIT(*s))
10144 f = -1;
10145 }
10146 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010147 f = SPS_BEST;
10148 else if (STRCMP(buf, "fast") == 0)
10149 f = SPS_FAST;
10150 else if (STRCMP(buf, "double") == 0)
10151 f = SPS_DOUBLE;
10152 else if (STRNCMP(buf, "expr:", 5) != 0
10153 && STRNCMP(buf, "file:", 5) != 0)
10154 f = -1;
10155
10156 if (f == -1 || (sps_flags != 0 && f != 0))
10157 {
10158 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010159 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010160 return FAIL;
10161 }
10162 if (f != 0)
10163 sps_flags = f;
10164 }
10165
10166 if (sps_flags == 0)
10167 sps_flags = SPS_BEST;
10168
10169 return OK;
10170}
10171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172/*
Bram Moolenaar134bf072013-09-25 18:54:24 +020010173 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010174 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010175 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010176 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 */
10178 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010179spell_suggest(count)
10180 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181{
10182 char_u *line;
10183 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 char_u wcopy[MAXWLEN + 2];
10185 char_u *p;
10186 int i;
10187 int c;
10188 suginfo_T sug;
10189 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010190 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010191 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010192 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010193 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010194 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010195 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010197 if (no_spell_checking(curwin))
10198 return;
10199
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010200 if (VIsual_active)
10201 {
10202 /* Use the Visually selected text as the bad word. But reject
10203 * a multi-line selection. */
10204 if (curwin->w_cursor.lnum != VIsual.lnum)
10205 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010206 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010207 return;
10208 }
10209 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10210 if (badlen < 0)
10211 badlen = -badlen;
10212 else
10213 curwin->w_cursor.col = VIsual.col;
10214 ++badlen;
10215 end_visual_mode();
10216 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010217 /* Find the start of the badly spelled word. */
10218 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010219 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010221 /* No bad word or it starts after the cursor: use the word under the
10222 * cursor. */
10223 curwin->w_cursor = prev_cursor;
10224 line = ml_get_curline();
10225 p = line + curwin->w_cursor.col;
10226 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010227 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010228 mb_ptr_back(line, p);
10229 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010230 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010231 mb_ptr_adv(p);
10232
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010233 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010234 {
10235 beep_flush();
10236 return;
10237 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010238 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239 }
10240
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010241 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010243 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010244 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010245
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010246 /* Make a copy of current line since autocommands may free the line. */
10247 line = vim_strsave(ml_get_curline());
10248 if (line == NULL)
10249 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010250
Bram Moolenaar5195e452005-08-19 20:32:47 +000010251 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10252 * 'spellsuggest', whatever is smaller. */
10253 if (sps_limit > (int)Rows - 2)
10254 limit = (int)Rows - 2;
10255 else
10256 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010257 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010258 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259
10260 if (sug.su_ga.ga_len == 0)
10261 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010262 else if (count > 0)
10263 {
10264 if (count > sug.su_ga.ga_len)
10265 smsg((char_u *)_("Sorry, only %ld suggestions"),
10266 (long)sug.su_ga.ga_len);
10267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 else
10269 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010270 vim_free(repl_from);
10271 repl_from = NULL;
10272 vim_free(repl_to);
10273 repl_to = NULL;
10274
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010275#ifdef FEAT_RIGHTLEFT
10276 /* When 'rightleft' is set the list is drawn right-left. */
10277 cmdmsg_rl = curwin->w_p_rl;
10278 if (cmdmsg_rl)
10279 msg_col = Columns - 1;
10280#endif
10281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010282 /* List the suggestions. */
10283 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010284 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010285 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10287 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010288#ifdef FEAT_RIGHTLEFT
10289 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10290 {
10291 /* And now the rabbit from the high hat: Avoid showing the
10292 * untranslated message rightleft. */
10293 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10294 sug.su_badlen, sug.su_badptr);
10295 }
10296#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 msg_puts(IObuff);
10298 msg_clr_eos();
10299 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010301 msg_scroll = TRUE;
10302 for (i = 0; i < sug.su_ga.ga_len; ++i)
10303 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010304 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305
10306 /* The suggested word may replace only part of the bad word, add
10307 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020010308 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010310 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 sug.su_badptr + stp->st_orglen,
10312 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010313 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10314#ifdef FEAT_RIGHTLEFT
10315 if (cmdmsg_rl)
10316 rl_mirror(IObuff);
10317#endif
10318 msg_puts(IObuff);
10319
10320 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010321 msg_puts(IObuff);
10322
10323 /* The word may replace more than "su_badlen". */
10324 if (sug.su_badlen < stp->st_orglen)
10325 {
10326 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10327 stp->st_orglen, sug.su_badptr);
10328 msg_puts(IObuff);
10329 }
10330
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010331 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010332 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010333 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010334 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010335 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010336 stp->st_salscore ? "s " : "",
10337 stp->st_score, stp->st_altscore);
10338 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010339 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010340 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010341#ifdef FEAT_RIGHTLEFT
10342 if (cmdmsg_rl)
10343 /* Mirror the numbers, but keep the leading space. */
10344 rl_mirror(IObuff + 1);
10345#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010346 msg_advance(30);
10347 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010349 msg_putchar('\n');
10350 }
10351
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010352#ifdef FEAT_RIGHTLEFT
10353 cmdmsg_rl = FALSE;
10354 msg_col = 0;
10355#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010357 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010358 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010359 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010360 lines_left = Rows; /* avoid more prompt */
10361 /* don't delay for 'smd' in normal_cmd() */
10362 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010363 }
10364
Bram Moolenaard12a1322005-08-21 22:08:24 +000010365 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10366 {
10367 /* Save the from and to text for :spellrepall. */
10368 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010369 if (sug.su_badlen > stp->st_orglen)
10370 {
10371 /* Replacing less than "su_badlen", append the remainder to
10372 * repl_to. */
10373 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10374 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10375 sug.su_badlen - stp->st_orglen,
10376 sug.su_badptr + stp->st_orglen);
10377 repl_to = vim_strsave(IObuff);
10378 }
10379 else
10380 {
10381 /* Replacing su_badlen or more, use the whole word. */
10382 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10383 repl_to = vim_strsave(stp->st_word);
10384 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010385
10386 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010387 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10388 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010389 if (p != NULL)
10390 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010391 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010392 mch_memmove(p, line, c);
10393 STRCPY(p + c, stp->st_word);
10394 STRCAT(p, sug.su_badptr + stp->st_orglen);
10395 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10396 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010397
10398 /* For redo we use a change-word command. */
10399 ResetRedobuff();
10400 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010401 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010402 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010403 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010404
10405 /* After this "p" may be invalid. */
10406 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010407 }
10408 }
10409 else
10410 curwin->w_cursor = prev_cursor;
10411
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010412 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010413skip:
10414 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010415}
10416
10417/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010418 * Check if the word at line "lnum" column "col" is required to start with a
10419 * capital. This uses 'spellcapcheck' of the current buffer.
10420 */
10421 static int
10422check_need_cap(lnum, col)
10423 linenr_T lnum;
10424 colnr_T col;
10425{
10426 int need_cap = FALSE;
10427 char_u *line;
10428 char_u *line_copy = NULL;
10429 char_u *p;
10430 colnr_T endcol;
10431 regmatch_T regmatch;
10432
Bram Moolenaar860cae12010-06-05 23:22:07 +020010433 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010434 return FALSE;
10435
10436 line = ml_get_curline();
10437 endcol = 0;
10438 if ((int)(skipwhite(line) - line) >= (int)col)
10439 {
10440 /* At start of line, check if previous line is empty or sentence
10441 * ends there. */
10442 if (lnum == 1)
10443 need_cap = TRUE;
10444 else
10445 {
10446 line = ml_get(lnum - 1);
10447 if (*skipwhite(line) == NUL)
10448 need_cap = TRUE;
10449 else
10450 {
10451 /* Append a space in place of the line break. */
10452 line_copy = concat_str(line, (char_u *)" ");
10453 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010454 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010455 }
10456 }
10457 }
10458 else
10459 endcol = col;
10460
10461 if (endcol > 0)
10462 {
10463 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010464 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010465 regmatch.rm_ic = FALSE;
10466 p = line + endcol;
10467 for (;;)
10468 {
10469 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010470 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010471 break;
10472 if (vim_regexec(&regmatch, p, 0)
10473 && regmatch.endp[0] == line + endcol)
10474 {
10475 need_cap = TRUE;
10476 break;
10477 }
10478 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +010010479 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010480 }
10481
10482 vim_free(line_copy);
10483
10484 return need_cap;
10485}
10486
10487
10488/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010489 * ":spellrepall"
10490 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010491 void
10492ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010493 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010494{
10495 pos_T pos = curwin->w_cursor;
10496 char_u *frompat;
10497 int addlen;
10498 char_u *line;
10499 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010500 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010501 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010502
10503 if (repl_from == NULL || repl_to == NULL)
10504 {
10505 EMSG(_("E752: No previous spell replacement"));
10506 return;
10507 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010508 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010509
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010510 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010511 if (frompat == NULL)
10512 return;
10513 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10514 p_ws = FALSE;
10515
Bram Moolenaar5195e452005-08-19 20:32:47 +000010516 sub_nsubs = 0;
10517 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010518 curwin->w_cursor.lnum = 0;
10519 while (!got_int)
10520 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010521 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010522 || u_save_cursor() == FAIL)
10523 break;
10524
10525 /* Only replace when the right word isn't there yet. This happens
10526 * when changing "etc" to "etc.". */
10527 line = ml_get_curline();
10528 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10529 repl_to, STRLEN(repl_to)) != 0)
10530 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010531 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010532 if (p == NULL)
10533 break;
10534 mch_memmove(p, line, curwin->w_cursor.col);
10535 STRCPY(p + curwin->w_cursor.col, repl_to);
10536 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10537 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10538 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010539
10540 if (curwin->w_cursor.lnum != prev_lnum)
10541 {
10542 ++sub_nlines;
10543 prev_lnum = curwin->w_cursor.lnum;
10544 }
10545 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010546 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010547 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010548 }
10549
10550 p_ws = save_ws;
10551 curwin->w_cursor = pos;
10552 vim_free(frompat);
10553
Bram Moolenaar5195e452005-08-19 20:32:47 +000010554 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010555 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010556 else
10557 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010558}
10559
10560/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010561 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10562 * a list of allocated strings.
10563 */
10564 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010565spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010566 garray_T *gap;
10567 char_u *word;
10568 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010569 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010570 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010571{
10572 suginfo_T sug;
10573 int i;
10574 suggest_T *stp;
10575 char_u *wcopy;
10576
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010577 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010578
10579 /* Make room in "gap". */
10580 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010581 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010582 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010583 for (i = 0; i < sug.su_ga.ga_len; ++i)
10584 {
10585 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010586
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010587 /* The suggested word may replace only part of "word", add the not
10588 * replaced part. */
10589 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010590 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010591 if (wcopy == NULL)
10592 break;
10593 STRCPY(wcopy, stp->st_word);
10594 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10595 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10596 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010597 }
10598
10599 spell_find_cleanup(&sug);
10600}
10601
10602/*
10603 * Find spell suggestions for the word at the start of "badptr".
10604 * Return the suggestions in "su->su_ga".
10605 * The maximum number of suggestions is "maxcount".
10606 * Note: does use info for the current window.
10607 * This is based on the mechanisms of Aspell, but completely reimplemented.
10608 */
10609 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010610spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010611 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010612 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010613 suginfo_T *su;
10614 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010615 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010616 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010617 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010618{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010619 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010620 char_u buf[MAXPATHL];
10621 char_u *p;
10622 int do_combine = FALSE;
10623 char_u *sps_copy;
10624#ifdef FEAT_EVAL
10625 static int expr_busy = FALSE;
10626#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010627 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010628 int i;
10629 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010630
10631 /*
10632 * Set the info in "*su".
10633 */
10634 vim_memset(su, 0, sizeof(suginfo_T));
10635 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10636 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010637 if (*badptr == NUL)
10638 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010639 hash_init(&su->su_banned);
10640
10641 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010642 if (badlen != 0)
10643 su->su_badlen = badlen;
10644 else
10645 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010646 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010647 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010648
10649 if (su->su_badlen >= MAXWLEN)
10650 su->su_badlen = MAXWLEN - 1; /* just in case */
10651 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10652 (void)spell_casefold(su->su_badptr, su->su_badlen,
10653 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010654 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010655 su->su_badflags = badword_captype(su->su_badptr,
10656 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010657 if (need_cap)
10658 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010659
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010660 /* Find the default language for sound folding. We simply use the first
10661 * one in 'spelllang' that supports sound folding. That's good for when
10662 * using multiple files for one language, it's not that bad when mixing
10663 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010664 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010665 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010666 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010667 if (lp->lp_sallang != NULL)
10668 {
10669 su->su_sallang = lp->lp_sallang;
10670 break;
10671 }
10672 }
10673
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010674 /* Soundfold the bad word with the default sound folding, so that we don't
10675 * have to do this many times. */
10676 if (su->su_sallang != NULL)
10677 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10678 su->su_sal_badword);
10679
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010680 /* If the word is not capitalised and spell_check() doesn't consider the
10681 * word to be bad then it might need to be capitalised. Add a suggestion
10682 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010683 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010684 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010685 {
10686 make_case_word(su->su_badword, buf, WF_ONECAP);
10687 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010688 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010689 }
10690
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010691 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010692 if (banbadword)
10693 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010694
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010695 /* Make a copy of 'spellsuggest', because the expression may change it. */
10696 sps_copy = vim_strsave(p_sps);
10697 if (sps_copy == NULL)
10698 return;
10699
10700 /* Loop over the items in 'spellsuggest'. */
10701 for (p = sps_copy; *p != NUL; )
10702 {
10703 copy_option_part(&p, buf, MAXPATHL, ",");
10704
10705 if (STRNCMP(buf, "expr:", 5) == 0)
10706 {
10707#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010708 /* Evaluate an expression. Skip this when called recursively,
10709 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010710 if (!expr_busy)
10711 {
10712 expr_busy = TRUE;
10713 spell_suggest_expr(su, buf + 5);
10714 expr_busy = FALSE;
10715 }
10716#endif
10717 }
10718 else if (STRNCMP(buf, "file:", 5) == 0)
10719 /* Use list of suggestions in a file. */
10720 spell_suggest_file(su, buf + 5);
10721 else
10722 {
10723 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010724 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010725 if (sps_flags & SPS_DOUBLE)
10726 do_combine = TRUE;
10727 }
10728 }
10729
10730 vim_free(sps_copy);
10731
10732 if (do_combine)
10733 /* Combine the two list of suggestions. This must be done last,
10734 * because sorting changes the order again. */
10735 score_combine(su);
10736}
10737
10738#ifdef FEAT_EVAL
10739/*
10740 * Find suggestions by evaluating expression "expr".
10741 */
10742 static void
10743spell_suggest_expr(su, expr)
10744 suginfo_T *su;
10745 char_u *expr;
10746{
10747 list_T *list;
10748 listitem_T *li;
10749 int score;
10750 char_u *p;
10751
10752 /* The work is split up in a few parts to avoid having to export
10753 * suginfo_T.
10754 * First evaluate the expression and get the resulting list. */
10755 list = eval_spell_expr(su->su_badword, expr);
10756 if (list != NULL)
10757 {
10758 /* Loop over the items in the list. */
10759 for (li = list->lv_first; li != NULL; li = li->li_next)
10760 if (li->li_tv.v_type == VAR_LIST)
10761 {
10762 /* Get the word and the score from the items. */
10763 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010764 if (score >= 0 && score <= su->su_maxscore)
10765 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10766 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010767 }
10768 list_unref(list);
10769 }
10770
Bram Moolenaar4770d092006-01-12 23:22:24 +000010771 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10772 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010773 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10774}
10775#endif
10776
10777/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010778 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010779 */
10780 static void
10781spell_suggest_file(su, fname)
10782 suginfo_T *su;
10783 char_u *fname;
10784{
10785 FILE *fd;
10786 char_u line[MAXWLEN * 2];
10787 char_u *p;
10788 int len;
10789 char_u cword[MAXWLEN];
10790
10791 /* Open the file. */
10792 fd = mch_fopen((char *)fname, "r");
10793 if (fd == NULL)
10794 {
10795 EMSG2(_(e_notopen), fname);
10796 return;
10797 }
10798
10799 /* Read it line by line. */
10800 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10801 {
10802 line_breakcheck();
10803
10804 p = vim_strchr(line, '/');
10805 if (p == NULL)
10806 continue; /* No Tab found, just skip the line. */
10807 *p++ = NUL;
10808 if (STRICMP(su->su_badword, line) == 0)
10809 {
10810 /* Match! Isolate the good word, until CR or NL. */
10811 for (len = 0; p[len] >= ' '; ++len)
10812 ;
10813 p[len] = NUL;
10814
10815 /* If the suggestion doesn't have specific case duplicate the case
10816 * of the bad word. */
10817 if (captype(p, NULL) == 0)
10818 {
10819 make_case_word(p, cword, su->su_badflags);
10820 p = cword;
10821 }
10822
10823 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010824 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010825 }
10826 }
10827
10828 fclose(fd);
10829
Bram Moolenaar4770d092006-01-12 23:22:24 +000010830 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10831 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010832 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10833}
10834
10835/*
10836 * Find suggestions for the internal method indicated by "sps_flags".
10837 */
10838 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010839spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010840 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010841 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010842{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010843 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010844 * Load the .sug file(s) that are available and not done yet.
10845 */
10846 suggest_load_files();
10847
10848 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010849 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010850 *
10851 * Set a maximum score to limit the combination of operations that is
10852 * tried.
10853 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010854 suggest_try_special(su);
10855
10856 /*
10857 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10858 * from the .aff file and inserting a space (split the word).
10859 */
10860 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010861
10862 /* For the resulting top-scorers compute the sound-a-like score. */
10863 if (sps_flags & SPS_DOUBLE)
10864 score_comp_sal(su);
10865
10866 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010867 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010868 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010869 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010870 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010871 if (sps_flags & SPS_BEST)
10872 /* Adjust the word score for the suggestions found so far for how
10873 * they sounds like. */
10874 rescore_suggestions(su);
10875
10876 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010877 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010878 * for the soundfold word, limits the changes that are being tried,
10879 * and "su_sfmaxscore" the rescored score, which is set by
10880 * cleanup_suggestions().
10881 * First find words with a small edit distance, because this is much
10882 * faster and often already finds the top-N suggestions. If we didn't
10883 * find many suggestions try again with a higher edit distance.
10884 * "sl_sounddone" is used to avoid doing the same word twice.
10885 */
10886 suggest_try_soundalike_prep();
10887 su->su_maxscore = SCORE_SFMAX1;
10888 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010889 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010890 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10891 {
10892 /* We didn't find enough matches, try again, allowing more
10893 * changes to the soundfold word. */
10894 su->su_maxscore = SCORE_SFMAX2;
10895 suggest_try_soundalike(su);
10896 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10897 {
10898 /* Still didn't find enough matches, try again, allowing even
10899 * more changes to the soundfold word. */
10900 su->su_maxscore = SCORE_SFMAX3;
10901 suggest_try_soundalike(su);
10902 }
10903 }
10904 su->su_maxscore = su->su_sfmaxscore;
10905 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010906 }
10907
Bram Moolenaar4770d092006-01-12 23:22:24 +000010908 /* When CTRL-C was hit while searching do show the results. Only clear
10909 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010910 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010911 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010912 {
10913 (void)vgetc();
10914 got_int = FALSE;
10915 }
10916
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010917 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010918 {
10919 if (sps_flags & SPS_BEST)
10920 /* Adjust the word score for how it sounds like. */
10921 rescore_suggestions(su);
10922
Bram Moolenaar4770d092006-01-12 23:22:24 +000010923 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10924 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010925 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010926 }
10927}
10928
10929/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010930 * Load the .sug files for languages that have one and weren't loaded yet.
10931 */
10932 static void
10933suggest_load_files()
10934{
10935 langp_T *lp;
10936 int lpi;
10937 slang_T *slang;
10938 char_u *dotp;
10939 FILE *fd;
10940 char_u buf[MAXWLEN];
10941 int i;
10942 time_t timestamp;
10943 int wcount;
10944 int wordnr;
10945 garray_T ga;
10946 int c;
10947
10948 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010949 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010950 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010951 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010952 slang = lp->lp_slang;
10953 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10954 {
10955 /* Change ".spl" to ".sug" and open the file. When the file isn't
10956 * found silently skip it. Do set "sl_sugloaded" so that we
10957 * don't try again and again. */
10958 slang->sl_sugloaded = TRUE;
10959
10960 dotp = vim_strrchr(slang->sl_fname, '.');
10961 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10962 continue;
10963 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010964 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010965 if (fd == NULL)
10966 goto nextone;
10967
10968 /*
10969 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10970 */
10971 for (i = 0; i < VIMSUGMAGICL; ++i)
10972 buf[i] = getc(fd); /* <fileID> */
10973 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10974 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010975 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010976 slang->sl_fname);
10977 goto nextone;
10978 }
10979 c = getc(fd); /* <versionnr> */
10980 if (c < VIMSUGVERSION)
10981 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010982 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010983 slang->sl_fname);
10984 goto nextone;
10985 }
10986 else if (c > VIMSUGVERSION)
10987 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010988 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010989 slang->sl_fname);
10990 goto nextone;
10991 }
10992
10993 /* Check the timestamp, it must be exactly the same as the one in
10994 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010995 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010996 if (timestamp != slang->sl_sugtime)
10997 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010998 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010999 slang->sl_fname);
11000 goto nextone;
11001 }
11002
11003 /*
11004 * <SUGWORDTREE>: <wordtree>
11005 * Read the trie with the soundfolded words.
11006 */
11007 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
11008 FALSE, 0) != 0)
11009 {
11010someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000011011 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011012 slang->sl_fname);
11013 slang_clear_sug(slang);
11014 goto nextone;
11015 }
11016
11017 /*
11018 * <SUGTABLE>: <sugwcount> <sugline> ...
11019 *
11020 * Read the table with word numbers. We use a file buffer for
11021 * this, because it's so much like a file with lines. Makes it
11022 * possible to swap the info and save on memory use.
11023 */
11024 slang->sl_sugbuf = open_spellbuf();
11025 if (slang->sl_sugbuf == NULL)
11026 goto someerror;
11027 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000011028 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011029 if (wcount < 0)
11030 goto someerror;
11031
11032 /* Read all the wordnr lists into the buffer, one NUL terminated
11033 * list per line. */
11034 ga_init2(&ga, 1, 100);
11035 for (wordnr = 0; wordnr < wcount; ++wordnr)
11036 {
11037 ga.ga_len = 0;
11038 for (;;)
11039 {
11040 c = getc(fd); /* <sugline> */
11041 if (c < 0 || ga_grow(&ga, 1) == FAIL)
11042 goto someerror;
11043 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
11044 if (c == NUL)
11045 break;
11046 }
11047 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
11048 ga.ga_data, ga.ga_len, TRUE) == FAIL)
11049 goto someerror;
11050 }
11051 ga_clear(&ga);
11052
11053 /*
11054 * Need to put word counts in the word tries, so that we can find
11055 * a word by its number.
11056 */
11057 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
11058 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
11059
11060nextone:
11061 if (fd != NULL)
11062 fclose(fd);
11063 STRCPY(dotp, ".spl");
11064 }
11065 }
11066}
11067
11068
11069/*
11070 * Fill in the wordcount fields for a trie.
11071 * Returns the total number of words.
11072 */
11073 static void
11074tree_count_words(byts, idxs)
11075 char_u *byts;
11076 idx_T *idxs;
11077{
11078 int depth;
11079 idx_T arridx[MAXWLEN];
11080 int curi[MAXWLEN];
11081 int c;
11082 idx_T n;
11083 int wordcount[MAXWLEN];
11084
11085 arridx[0] = 0;
11086 curi[0] = 1;
11087 wordcount[0] = 0;
11088 depth = 0;
11089 while (depth >= 0 && !got_int)
11090 {
11091 if (curi[depth] > byts[arridx[depth]])
11092 {
11093 /* Done all bytes at this node, go up one level. */
11094 idxs[arridx[depth]] = wordcount[depth];
11095 if (depth > 0)
11096 wordcount[depth - 1] += wordcount[depth];
11097
11098 --depth;
11099 fast_breakcheck();
11100 }
11101 else
11102 {
11103 /* Do one more byte at this node. */
11104 n = arridx[depth] + curi[depth];
11105 ++curi[depth];
11106
11107 c = byts[n];
11108 if (c == 0)
11109 {
11110 /* End of word, count it. */
11111 ++wordcount[depth];
11112
11113 /* Skip over any other NUL bytes (same word with different
11114 * flags). */
11115 while (byts[n + 1] == 0)
11116 {
11117 ++n;
11118 ++curi[depth];
11119 }
11120 }
11121 else
11122 {
11123 /* Normal char, go one level deeper to count the words. */
11124 ++depth;
11125 arridx[depth] = idxs[n];
11126 curi[depth] = 1;
11127 wordcount[depth] = 0;
11128 }
11129 }
11130 }
11131}
11132
11133/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011134 * Free the info put in "*su" by spell_find_suggest().
11135 */
11136 static void
11137spell_find_cleanup(su)
11138 suginfo_T *su;
11139{
11140 int i;
11141
11142 /* Free the suggestions. */
11143 for (i = 0; i < su->su_ga.ga_len; ++i)
11144 vim_free(SUG(su->su_ga, i).st_word);
11145 ga_clear(&su->su_ga);
11146 for (i = 0; i < su->su_sga.ga_len; ++i)
11147 vim_free(SUG(su->su_sga, i).st_word);
11148 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149
11150 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011151 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152}
11153
11154/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011155 * Make a copy of "word", with the first letter upper or lower cased, to
11156 * "wcopy[MAXWLEN]". "word" must not be empty.
11157 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 */
11159 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011160onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011161 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 char_u *wcopy;
11163 int upper; /* TRUE: first letter made upper case */
11164{
11165 char_u *p;
11166 int c;
11167 int l;
11168
11169 p = word;
11170#ifdef FEAT_MBYTE
11171 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011172 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011173 else
11174#endif
11175 c = *p++;
11176 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011177 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011179 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180#ifdef FEAT_MBYTE
11181 if (has_mbyte)
11182 l = mb_char2bytes(c, wcopy);
11183 else
11184#endif
11185 {
11186 l = 1;
11187 wcopy[0] = c;
11188 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011189 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190}
11191
11192/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011193 * Make a copy of "word" with all the letters upper cased into
11194 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 */
11196 static void
11197allcap_copy(word, wcopy)
11198 char_u *word;
11199 char_u *wcopy;
11200{
11201 char_u *s;
11202 char_u *d;
11203 int c;
11204
11205 d = wcopy;
11206 for (s = word; *s != NUL; )
11207 {
11208#ifdef FEAT_MBYTE
11209 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011210 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 else
11212#endif
11213 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011214
11215#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +020011216 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +000011217 * would cause weird errors in other 8-bit encodings. */
11218 if (enc_latin1like && c == 0xdf)
11219 {
11220 c = 'S';
11221 if (d - wcopy >= MAXWLEN - 1)
11222 break;
11223 *d++ = c;
11224 }
11225 else
11226#endif
11227 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228
11229#ifdef FEAT_MBYTE
11230 if (has_mbyte)
11231 {
11232 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11233 break;
11234 d += mb_char2bytes(c, d);
11235 }
11236 else
11237#endif
11238 {
11239 if (d - wcopy >= MAXWLEN - 1)
11240 break;
11241 *d++ = c;
11242 }
11243 }
11244 *d = NUL;
11245}
11246
11247/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011248 * Try finding suggestions by recognizing specific situations.
11249 */
11250 static void
11251suggest_try_special(su)
11252 suginfo_T *su;
11253{
11254 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011255 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011256 int c;
11257 char_u word[MAXWLEN];
11258
11259 /*
11260 * Recognize a word that is repeated: "the the".
11261 */
11262 p = skiptowhite(su->su_fbadword);
11263 len = p - su->su_fbadword;
11264 p = skipwhite(p);
11265 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11266 {
11267 /* Include badflags: if the badword is onecap or allcap
11268 * use that for the goodword too: "The the" -> "The". */
11269 c = su->su_fbadword[len];
11270 su->su_fbadword[len] = NUL;
11271 make_case_word(su->su_fbadword, word, su->su_badflags);
11272 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011273
11274 /* Give a soundalike score of 0, compute the score as if deleting one
11275 * character. */
11276 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011277 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011278 }
11279}
11280
11281/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011282 * Change the 0 to 1 to measure how much time is spent in each state.
11283 * Output is dumped in "suggestprof".
11284 */
11285#if 0
11286# define SUGGEST_PROFILE
11287proftime_T current;
11288proftime_T total;
11289proftime_T times[STATE_FINAL + 1];
11290long counts[STATE_FINAL + 1];
11291
11292 static void
11293prof_init(void)
11294{
11295 for (int i = 0; i <= STATE_FINAL; ++i)
11296 {
11297 profile_zero(&times[i]);
11298 counts[i] = 0;
11299 }
11300 profile_start(&current);
11301 profile_start(&total);
11302}
11303
11304/* call before changing state */
11305 static void
11306prof_store(state_T state)
11307{
11308 profile_end(&current);
11309 profile_add(&times[state], &current);
11310 ++counts[state];
11311 profile_start(&current);
11312}
11313# define PROF_STORE(state) prof_store(state);
11314
11315 static void
11316prof_report(char *name)
11317{
11318 FILE *fd = fopen("suggestprof", "a");
11319
11320 profile_end(&total);
11321 fprintf(fd, "-----------------------\n");
11322 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
11323 for (int i = 0; i <= STATE_FINAL; ++i)
11324 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
11325 fclose(fd);
11326}
11327#else
11328# define PROF_STORE(state)
11329#endif
11330
11331/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 * Try finding suggestions by adding/removing/swapping letters.
11333 */
11334 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011335suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 suginfo_T *su;
11337{
11338 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011339 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011341 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011342 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343
11344 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011345 * to find matches (esp. REP items). Append some more text, changing
11346 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011348 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011349 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011350 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011351
Bram Moolenaar860cae12010-06-05 23:22:07 +020011352 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011353 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011354 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011355
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011356 /* If reloading a spell file fails it's still in the list but
11357 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011358 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011359 continue;
11360
Bram Moolenaar4770d092006-01-12 23:22:24 +000011361 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011362#ifdef SUGGEST_PROFILE
11363 prof_init();
11364#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011365 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011366#ifdef SUGGEST_PROFILE
11367 prof_report("try_change");
11368#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011369 }
11370}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371
Bram Moolenaar4770d092006-01-12 23:22:24 +000011372/* Check the maximum score, if we go over it we won't try this change. */
11373#define TRY_DEEPER(su, stack, depth, add) \
11374 (stack[depth].ts_score + (add) < su->su_maxscore)
11375
11376/*
11377 * Try finding suggestions by adding/removing/swapping letters.
11378 *
11379 * This uses a state machine. At each node in the tree we try various
11380 * operations. When trying if an operation works "depth" is increased and the
11381 * stack[] is used to store info. This allows combinations, thus insert one
11382 * character, replace one and delete another. The number of changes is
11383 * limited by su->su_maxscore.
11384 *
11385 * After implementing this I noticed an article by Kemal Oflazer that
11386 * describes something similar: "Error-tolerant Finite State Recognition with
11387 * Applications to Morphological Analysis and Spelling Correction" (1996).
11388 * The implementation in the article is simplified and requires a stack of
11389 * unknown depth. The implementation here only needs a stack depth equal to
11390 * the length of the word.
11391 *
11392 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11393 * The mechanism is the same, but we find a match with a sound-folded word
11394 * that comes from one or more original words. Each of these words may be
11395 * added, this is done by add_sound_suggest().
11396 * Don't use:
11397 * the prefix tree or the keep-case tree
11398 * "su->su_badlen"
11399 * anything to do with upper and lower case
11400 * anything to do with word or non-word characters ("spell_iswordp()")
11401 * banned words
11402 * word flags (rare, region, compounding)
11403 * word splitting for now
11404 * "similar_chars()"
11405 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11406 */
11407 static void
11408suggest_trie_walk(su, lp, fword, soundfold)
11409 suginfo_T *su;
11410 langp_T *lp;
11411 char_u *fword;
11412 int soundfold;
11413{
11414 char_u tword[MAXWLEN]; /* good word collected so far */
11415 trystate_T stack[MAXWLEN];
11416 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011417 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011418 * words and split word. NUL terminated
11419 * when going deeper but not when coming
11420 * back. */
11421 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11422 trystate_T *sp;
11423 int newscore;
11424 int score;
11425 char_u *byts, *fbyts, *pbyts;
11426 idx_T *idxs, *fidxs, *pidxs;
11427 int depth;
11428 int c, c2, c3;
11429 int n = 0;
11430 int flags;
11431 garray_T *gap;
11432 idx_T arridx;
11433 int len;
11434 char_u *p;
11435 fromto_T *ftp;
11436 int fl = 0, tl;
11437 int repextra = 0; /* extra bytes in fword[] from REP item */
11438 slang_T *slang = lp->lp_slang;
11439 int fword_ends;
11440 int goodword_ends;
11441#ifdef DEBUG_TRIEWALK
11442 /* Stores the name of the change made at each level. */
11443 char_u changename[MAXWLEN][80];
11444#endif
11445 int breakcheckcount = 1000;
11446 int compound_ok;
11447
11448 /*
11449 * Go through the whole case-fold tree, try changes at each node.
11450 * "tword[]" contains the word collected from nodes in the tree.
11451 * "fword[]" the word we are trying to match with (initially the bad
11452 * word).
11453 */
11454 depth = 0;
11455 sp = &stack[0];
11456 vim_memset(sp, 0, sizeof(trystate_T));
11457 sp->ts_curi = 1;
11458
11459 if (soundfold)
11460 {
11461 /* Going through the soundfold tree. */
11462 byts = fbyts = slang->sl_sbyts;
11463 idxs = fidxs = slang->sl_sidxs;
11464 pbyts = NULL;
11465 pidxs = NULL;
11466 sp->ts_prefixdepth = PFD_NOPREFIX;
11467 sp->ts_state = STATE_START;
11468 }
11469 else
11470 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011471 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011472 * When there are postponed prefixes we need to use these first. At
11473 * the end of the prefix we continue in the case-fold tree.
11474 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011475 fbyts = slang->sl_fbyts;
11476 fidxs = slang->sl_fidxs;
11477 pbyts = slang->sl_pbyts;
11478 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011479 if (pbyts != NULL)
11480 {
11481 byts = pbyts;
11482 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011483 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011484 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11485 }
11486 else
11487 {
11488 byts = fbyts;
11489 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011490 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011491 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011492 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011493 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011494
Bram Moolenaar4770d092006-01-12 23:22:24 +000011495 /*
11496 * Loop to find all suggestions. At each round we either:
11497 * - For the current state try one operation, advance "ts_curi",
11498 * increase "depth".
11499 * - When a state is done go to the next, set "ts_state".
11500 * - When all states are tried decrease "depth".
11501 */
11502 while (depth >= 0 && !got_int)
11503 {
11504 sp = &stack[depth];
11505 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011507 case STATE_START:
11508 case STATE_NOPREFIX:
11509 /*
11510 * Start of node: Deal with NUL bytes, which means
11511 * tword[] may end here.
11512 */
11513 arridx = sp->ts_arridx; /* current node in the tree */
11514 len = byts[arridx]; /* bytes in this node */
11515 arridx += sp->ts_curi; /* index of current byte */
11516
11517 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011519 /* Skip over the NUL bytes, we use them later. */
11520 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11521 ;
11522 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523
Bram Moolenaar4770d092006-01-12 23:22:24 +000011524 /* Always past NUL bytes now. */
11525 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011526 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011527 sp->ts_state = STATE_ENDNUL;
11528 sp->ts_save_badflags = su->su_badflags;
11529
11530 /* At end of a prefix or at start of prefixtree: check for
11531 * following word. */
11532 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011533 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011534 /* Set su->su_badflags to the caps type at this position.
11535 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011536#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011537 if (has_mbyte)
11538 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11539 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011540#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011541 n = sp->ts_fidx;
11542 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11543 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011544 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011545#ifdef DEBUG_TRIEWALK
11546 sprintf(changename[depth], "prefix");
11547#endif
11548 go_deeper(stack, depth, 0);
11549 ++depth;
11550 sp = &stack[depth];
11551 sp->ts_prefixdepth = depth - 1;
11552 byts = fbyts;
11553 idxs = fidxs;
11554 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011555
Bram Moolenaar4770d092006-01-12 23:22:24 +000011556 /* Move the prefix to preword[] with the right case
11557 * and make find_keepcap_word() works. */
11558 tword[sp->ts_twordlen] = NUL;
11559 make_case_word(tword + sp->ts_splitoff,
11560 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011561 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011562 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011563 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011564 break;
11565 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011566
Bram Moolenaar4770d092006-01-12 23:22:24 +000011567 if (sp->ts_curi > len || byts[arridx] != 0)
11568 {
11569 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011570 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011571 sp->ts_state = STATE_ENDNUL;
11572 sp->ts_save_badflags = su->su_badflags;
11573 break;
11574 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575
Bram Moolenaar4770d092006-01-12 23:22:24 +000011576 /*
11577 * End of word in tree.
11578 */
11579 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580
Bram Moolenaar4770d092006-01-12 23:22:24 +000011581 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011582
11583 /* Skip words with the NOSUGGEST flag. */
11584 if (flags & WF_NOSUGGEST)
11585 break;
11586
Bram Moolenaar4770d092006-01-12 23:22:24 +000011587 fword_ends = (fword[sp->ts_fidx] == NUL
11588 || (soundfold
11589 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011590 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592
Bram Moolenaar4770d092006-01-12 23:22:24 +000011593 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011594 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011595 {
11596 /* There was a prefix before the word. Check that the prefix
11597 * can be used with this word. */
11598 /* Count the length of the NULs in the prefix. If there are
11599 * none this must be the first try without a prefix. */
11600 n = stack[sp->ts_prefixdepth].ts_arridx;
11601 len = pbyts[n++];
11602 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11603 ;
11604 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011605 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011606 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011607 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011608 if (c == 0)
11609 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011610
Bram Moolenaar4770d092006-01-12 23:22:24 +000011611 /* Use the WF_RARE flag for a rare prefix. */
11612 if (c & WF_RAREPFX)
11613 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011614
Bram Moolenaar4770d092006-01-12 23:22:24 +000011615 /* Tricky: when checking for both prefix and compounding
11616 * we run into the prefix flag first.
11617 * Remember that it's OK, so that we accept the prefix
11618 * when arriving at a compound flag. */
11619 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011620 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011621 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011622
Bram Moolenaar4770d092006-01-12 23:22:24 +000011623 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11624 * appending another compound word below. */
11625 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011626 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011627 goodword_ends = FALSE;
11628 else
11629 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011630
Bram Moolenaar4770d092006-01-12 23:22:24 +000011631 p = NULL;
11632 compound_ok = TRUE;
11633 if (sp->ts_complen > sp->ts_compsplit)
11634 {
11635 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011636 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011637 /* There was a word before this word. When there was no
11638 * change in this word (it was correct) add the first word
11639 * as a suggestion. If this word was corrected too, we
11640 * need to check if a correct word follows. */
11641 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011642 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011643 && STRNCMP(fword + sp->ts_splitfidx,
11644 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011645 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011646 {
11647 preword[sp->ts_prewordlen] = NUL;
11648 newscore = score_wordcount_adj(slang, sp->ts_score,
11649 preword + sp->ts_prewordlen,
11650 sp->ts_prewordlen > 0);
11651 /* Add the suggestion if the score isn't too bad. */
11652 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011653 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011654 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011655 newscore, 0, FALSE,
11656 lp->lp_sallang, FALSE);
11657 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011658 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011659 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011660 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011661 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011662 /* There was a compound word before this word. If this
11663 * word does not support compounding then give up
11664 * (splitting is tried for the word without compound
11665 * flag). */
11666 if (((unsigned)flags >> 24) == 0
11667 || sp->ts_twordlen - sp->ts_splitoff
11668 < slang->sl_compminlen)
11669 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011670#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011671 /* For multi-byte chars check character length against
11672 * COMPOUNDMIN. */
11673 if (has_mbyte
11674 && slang->sl_compminlen > 0
11675 && mb_charlen(tword + sp->ts_splitoff)
11676 < slang->sl_compminlen)
11677 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011678#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011679
Bram Moolenaar4770d092006-01-12 23:22:24 +000011680 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11681 compflags[sp->ts_complen + 1] = NUL;
11682 vim_strncpy(preword + sp->ts_prewordlen,
11683 tword + sp->ts_splitoff,
11684 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011685
11686 /* Verify CHECKCOMPOUNDPATTERN rules. */
11687 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11688 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011689 compound_ok = FALSE;
11690
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011691 if (compound_ok)
11692 {
11693 p = preword;
11694 while (*skiptowhite(p) != NUL)
11695 p = skipwhite(skiptowhite(p));
11696 if (fword_ends && !can_compound(slang, p,
11697 compflags + sp->ts_compsplit))
11698 /* Compound is not allowed. But it may still be
11699 * possible if we add another (short) word. */
11700 compound_ok = FALSE;
11701 }
11702
Bram Moolenaar4770d092006-01-12 23:22:24 +000011703 /* Get pointer to last char of previous word. */
11704 p = preword + sp->ts_prewordlen;
11705 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011706 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011707 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708
Bram Moolenaar4770d092006-01-12 23:22:24 +000011709 /*
11710 * Form the word with proper case in preword.
11711 * If there is a word from a previous split, append.
11712 * For the soundfold tree don't change the case, simply append.
11713 */
11714 if (soundfold)
11715 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11716 else if (flags & WF_KEEPCAP)
11717 /* Must find the word in the keep-case tree. */
11718 find_keepcap_word(slang, tword + sp->ts_splitoff,
11719 preword + sp->ts_prewordlen);
11720 else
11721 {
11722 /* Include badflags: If the badword is onecap or allcap
11723 * use that for the goodword too. But if the badword is
11724 * allcap and it's only one char long use onecap. */
11725 c = su->su_badflags;
11726 if ((c & WF_ALLCAP)
11727#ifdef FEAT_MBYTE
11728 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11729#else
11730 && su->su_badlen == 1
11731#endif
11732 )
11733 c = WF_ONECAP;
11734 c |= flags;
11735
11736 /* When appending a compound word after a word character don't
11737 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011738 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011739 c &= ~WF_ONECAP;
11740 make_case_word(tword + sp->ts_splitoff,
11741 preword + sp->ts_prewordlen, c);
11742 }
11743
11744 if (!soundfold)
11745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011746 /* Don't use a banned word. It may appear again as a good
11747 * word, thus remember it. */
11748 if (flags & WF_BANNED)
11749 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011750 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011751 break;
11752 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011753 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011754 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11755 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011756 {
11757 if (slang->sl_compprog == NULL)
11758 break;
11759 /* the word so far was banned but we may try compounding */
11760 goodword_ends = FALSE;
11761 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763
Bram Moolenaar4770d092006-01-12 23:22:24 +000011764 newscore = 0;
11765 if (!soundfold) /* soundfold words don't have flags */
11766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011768 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 newscore += SCORE_REGION;
11770 if (flags & WF_RARE)
11771 newscore += SCORE_RARE;
11772
Bram Moolenaar0c405862005-06-22 22:26:26 +000011773 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011774 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011776 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777
Bram Moolenaar4770d092006-01-12 23:22:24 +000011778 /* TODO: how about splitting in the soundfold tree? */
11779 if (fword_ends
11780 && goodword_ends
11781 && sp->ts_fidx >= sp->ts_fidxtry
11782 && compound_ok)
11783 {
11784 /* The badword also ends: add suggestions. */
11785#ifdef DEBUG_TRIEWALK
11786 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011788 int j;
11789
11790 /* print the stack of changes that brought us here */
11791 smsg("------ %s -------", fword);
11792 for (j = 0; j < depth; ++j)
11793 smsg("%s", changename[j]);
11794 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011795#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011796 if (soundfold)
11797 {
11798 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011799 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011800 add_sound_suggest(su, preword, sp->ts_score, lp);
11801 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011802 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011803 {
11804 /* Give a penalty when changing non-word char to word
11805 * char, e.g., "thes," -> "these". */
11806 p = fword + sp->ts_fidx;
11807 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011808 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011809 {
11810 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011811 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011812 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011813 newscore += SCORE_NONWORD;
11814 }
11815
Bram Moolenaar4770d092006-01-12 23:22:24 +000011816 /* Give a bonus to words seen before. */
11817 score = score_wordcount_adj(slang,
11818 sp->ts_score + newscore,
11819 preword + sp->ts_prewordlen,
11820 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011821
Bram Moolenaar4770d092006-01-12 23:22:24 +000011822 /* Add the suggestion if the score isn't too bad. */
11823 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011824 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011825 add_suggestion(su, &su->su_ga, preword,
11826 sp->ts_fidx - repextra,
11827 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011828
11829 if (su->su_badflags & WF_MIXCAP)
11830 {
11831 /* We really don't know if the word should be
11832 * upper or lower case, add both. */
11833 c = captype(preword, NULL);
11834 if (c == 0 || c == WF_ALLCAP)
11835 {
11836 make_case_word(tword + sp->ts_splitoff,
11837 preword + sp->ts_prewordlen,
11838 c == 0 ? WF_ALLCAP : 0);
11839
11840 add_suggestion(su, &su->su_ga, preword,
11841 sp->ts_fidx - repextra,
11842 score + SCORE_ICASE, 0, FALSE,
11843 lp->lp_sallang, FALSE);
11844 }
11845 }
11846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011848 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011849
Bram Moolenaar4770d092006-01-12 23:22:24 +000011850 /*
11851 * Try word split and/or compounding.
11852 */
11853 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011854#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011855 /* Don't split halfway a character. */
11856 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011857#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011858 )
11859 {
11860 int try_compound;
11861 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011862
Bram Moolenaar4770d092006-01-12 23:22:24 +000011863 /* If past the end of the bad word don't try a split.
11864 * Otherwise try changing the next word. E.g., find
11865 * suggestions for "the the" where the second "the" is
11866 * different. It's done like a split.
11867 * TODO: word split for soundfold words */
11868 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11869 && !soundfold;
11870
11871 /* Get here in several situations:
11872 * 1. The word in the tree ends:
11873 * If the word allows compounding try that. Otherwise try
11874 * a split by inserting a space. For both check that a
11875 * valid words starts at fword[sp->ts_fidx].
11876 * For NOBREAK do like compounding to be able to check if
11877 * the next word is valid.
11878 * 2. The badword does end, but it was due to a change (e.g.,
11879 * a swap). No need to split, but do check that the
11880 * following word is valid.
11881 * 3. The badword and the word in the tree end. It may still
11882 * be possible to compound another (short) word.
11883 */
11884 try_compound = FALSE;
11885 if (!soundfold
11886 && slang->sl_compprog != NULL
11887 && ((unsigned)flags >> 24) != 0
11888 && sp->ts_twordlen - sp->ts_splitoff
11889 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011890#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011891 && (!has_mbyte
11892 || slang->sl_compminlen == 0
11893 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011894 >= slang->sl_compminlen)
11895#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011896 && (slang->sl_compsylmax < MAXWLEN
11897 || sp->ts_complen + 1 - sp->ts_compsplit
11898 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011899 && (can_be_compound(sp, slang,
11900 compflags, ((unsigned)flags >> 24))))
11901
Bram Moolenaar4770d092006-01-12 23:22:24 +000011902 {
11903 try_compound = TRUE;
11904 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11905 compflags[sp->ts_complen + 1] = NUL;
11906 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011907
Bram Moolenaar4770d092006-01-12 23:22:24 +000011908 /* For NOBREAK we never try splitting, it won't make any word
11909 * valid. */
11910 if (slang->sl_nobreak)
11911 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011912
Bram Moolenaar4770d092006-01-12 23:22:24 +000011913 /* If we could add a compound word, and it's also possible to
11914 * split at this point, do the split first and set
11915 * TSF_DIDSPLIT to avoid doing it again. */
11916 else if (!fword_ends
11917 && try_compound
11918 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11919 {
11920 try_compound = FALSE;
11921 sp->ts_flags |= TSF_DIDSPLIT;
11922 --sp->ts_curi; /* do the same NUL again */
11923 compflags[sp->ts_complen] = NUL;
11924 }
11925 else
11926 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011927
Bram Moolenaar4770d092006-01-12 23:22:24 +000011928 if (try_split || try_compound)
11929 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011930 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011931 {
11932 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011933 * words so far are valid for compounding. If there
11934 * is only one word it must not have the NEEDCOMPOUND
11935 * flag. */
11936 if (sp->ts_complen == sp->ts_compsplit
11937 && (flags & WF_NEEDCOMP))
11938 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011939 p = preword;
11940 while (*skiptowhite(p) != NUL)
11941 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011942 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011943 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011944 compflags + sp->ts_compsplit))
11945 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011946
11947 if (slang->sl_nosplitsugs)
11948 newscore += SCORE_SPLIT_NO;
11949 else
11950 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011951
11952 /* Give a bonus to words seen before. */
11953 newscore = score_wordcount_adj(slang, newscore,
11954 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011955 }
11956
Bram Moolenaar4770d092006-01-12 23:22:24 +000011957 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011959 go_deeper(stack, depth, newscore);
11960#ifdef DEBUG_TRIEWALK
11961 if (!try_compound && !fword_ends)
11962 sprintf(changename[depth], "%.*s-%s: split",
11963 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11964 else
11965 sprintf(changename[depth], "%.*s-%s: compound",
11966 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11967#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011969 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011970 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011971 sp->ts_state = STATE_SPLITUNDO;
11972
11973 ++depth;
11974 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011976 /* Append a space to preword when splitting. */
11977 if (!try_compound && !fword_ends)
11978 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011979 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011980 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011981 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011982
11983 /* If the badword has a non-word character at this
11984 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011985 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011986 * character when the word ends. But only when the
11987 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011988 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011989 + sp->ts_fidx,
11990 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011991 || fword_ends)
11992 && fword[sp->ts_fidx] != NUL
11993 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011994 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011995 int l;
11996
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011997#ifdef FEAT_MBYTE
11998 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011999 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012000 else
12001#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012002 l = 1;
12003 if (fword_ends)
12004 {
12005 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000012006 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012007 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000012008 sp->ts_prewordlen += l;
12009 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012010 }
12011 else
12012 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
12013 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012014 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012015
Bram Moolenaard12a1322005-08-21 22:08:24 +000012016 /* When compounding include compound flag in
12017 * compflags[] (already set above). When splitting we
12018 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012019 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000012020 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012021 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000012022 sp->ts_compsplit = sp->ts_complen;
12023 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012024
Bram Moolenaar53805d12005-08-01 07:08:33 +000012025 /* set su->su_badflags to the caps type at this
12026 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012027#ifdef FEAT_MBYTE
12028 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000012029 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012030 else
12031#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000012032 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012033 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012034 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012037 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000012038
12039 /* If there are postponed prefixes, try these too. */
12040 if (pbyts != NULL)
12041 {
12042 byts = pbyts;
12043 idxs = pidxs;
12044 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012045 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +000012046 sp->ts_state = STATE_NOPREFIX;
12047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 }
12049 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012050 }
12051 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052
Bram Moolenaar4770d092006-01-12 23:22:24 +000012053 case STATE_SPLITUNDO:
12054 /* Undo the changes done for word split or compound word. */
12055 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012056
Bram Moolenaar4770d092006-01-12 23:22:24 +000012057 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012058 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012059 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000012060
Bram Moolenaar4770d092006-01-12 23:22:24 +000012061 /* In case we went into the prefix tree. */
12062 byts = fbyts;
12063 idxs = fidxs;
12064 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012065
Bram Moolenaar4770d092006-01-12 23:22:24 +000012066 case STATE_ENDNUL:
12067 /* Past the NUL bytes in the node. */
12068 su->su_badflags = sp->ts_save_badflags;
12069 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000012070#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012071 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000012072#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012073 )
12074 {
12075 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012076 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012077 sp->ts_state = STATE_DEL;
12078 break;
12079 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012080 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012081 sp->ts_state = STATE_PLAIN;
12082 /*FALLTHROUGH*/
12083
12084 case STATE_PLAIN:
12085 /*
12086 * Go over all possible bytes at this node, add each to tword[]
12087 * and use child node. "ts_curi" is the index.
12088 */
12089 arridx = sp->ts_arridx;
12090 if (sp->ts_curi > byts[arridx])
12091 {
12092 /* Done all bytes at this node, do next state. When still at
12093 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012094 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012095 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012096 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012097 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012098 sp->ts_state = STATE_FINAL;
12099 }
12100 else
12101 {
12102 arridx += sp->ts_curi++;
12103 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104
Bram Moolenaar4770d092006-01-12 23:22:24 +000012105 /* Normal byte, go one level deeper. If it's not equal to the
12106 * byte in the bad word adjust the score. But don't even try
12107 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +010012108 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +000012109 * delete + substitute. */
12110 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000012111#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012112 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012113#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012114 )
12115 newscore = 0;
12116 else
12117 newscore = SCORE_SUBST;
12118 if ((newscore == 0
12119 || (sp->ts_fidx >= sp->ts_fidxtry
12120 && ((sp->ts_flags & TSF_DIDDEL) == 0
12121 || c != fword[sp->ts_delidx])))
12122 && TRY_DEEPER(su, stack, depth, newscore))
12123 {
12124 go_deeper(stack, depth, newscore);
12125#ifdef DEBUG_TRIEWALK
12126 if (newscore > 0)
12127 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12128 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12129 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012130 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012131 sprintf(changename[depth], "%.*s-%s: accept %c",
12132 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12133 fword[sp->ts_fidx]);
12134#endif
12135 ++depth;
12136 sp = &stack[depth];
12137 ++sp->ts_fidx;
12138 tword[sp->ts_twordlen++] = c;
12139 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012140#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012141 if (newscore == SCORE_SUBST)
12142 sp->ts_isdiff = DIFF_YES;
12143 if (has_mbyte)
12144 {
12145 /* Multi-byte characters are a bit complicated to
12146 * handle: They differ when any of the bytes differ
12147 * and then their length may also differ. */
12148 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012149 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012150 /* First byte. */
12151 sp->ts_tcharidx = 0;
12152 sp->ts_tcharlen = MB_BYTE2LEN(c);
12153 sp->ts_fcharstart = sp->ts_fidx - 1;
12154 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012155 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012156 }
12157 else if (sp->ts_isdiff == DIFF_INSERT)
12158 /* When inserting trail bytes don't advance in the
12159 * bad word. */
12160 --sp->ts_fidx;
12161 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12162 {
12163 /* Last byte of character. */
12164 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012165 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012166 /* Correct ts_fidx for the byte length of the
12167 * character (we didn't check that before). */
12168 sp->ts_fidx = sp->ts_fcharstart
12169 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012170 fword[sp->ts_fcharstart]);
12171
Bram Moolenaar4770d092006-01-12 23:22:24 +000012172 /* For changing a composing character adjust
12173 * the score from SCORE_SUBST to
12174 * SCORE_SUBCOMP. */
12175 if (enc_utf8
12176 && utf_iscomposing(
12177 mb_ptr2char(tword
12178 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012179 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012180 && utf_iscomposing(
12181 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012182 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012183 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012184 SCORE_SUBST - SCORE_SUBCOMP;
12185
Bram Moolenaar4770d092006-01-12 23:22:24 +000012186 /* For a similar character adjust score from
12187 * SCORE_SUBST to SCORE_SIMILAR. */
12188 else if (!soundfold
12189 && slang->sl_has_map
12190 && similar_chars(slang,
12191 mb_ptr2char(tword
12192 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012193 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012194 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012195 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012196 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012197 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012198 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012199 else if (sp->ts_isdiff == DIFF_INSERT
12200 && sp->ts_twordlen > sp->ts_tcharlen)
12201 {
12202 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12203 c = mb_ptr2char(p);
12204 if (enc_utf8 && utf_iscomposing(c))
12205 {
12206 /* Inserting a composing char doesn't
12207 * count that much. */
12208 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12209 }
12210 else
12211 {
12212 /* If the previous character was the same,
12213 * thus doubling a character, give a bonus
12214 * to the score. Also for the soundfold
12215 * tree (might seem illogical but does
12216 * give better scores). */
12217 mb_ptr_back(tword, p);
12218 if (c == mb_ptr2char(p))
12219 sp->ts_score -= SCORE_INS
12220 - SCORE_INSDUP;
12221 }
12222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223
Bram Moolenaar4770d092006-01-12 23:22:24 +000012224 /* Starting a new char, reset the length. */
12225 sp->ts_tcharlen = 0;
12226 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012227 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012228 else
12229#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012230 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012231 /* If we found a similar char adjust the score.
12232 * We do this after calling go_deeper() because
12233 * it's slow. */
12234 if (newscore != 0
12235 && !soundfold
12236 && slang->sl_has_map
12237 && similar_chars(slang,
12238 c, fword[sp->ts_fidx - 1]))
12239 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012241 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012242 }
12243 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012244
Bram Moolenaar4770d092006-01-12 23:22:24 +000012245 case STATE_DEL:
12246#ifdef FEAT_MBYTE
12247 /* When past the first byte of a multi-byte char don't try
12248 * delete/insert/swap a character. */
12249 if (has_mbyte && sp->ts_tcharlen > 0)
12250 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012251 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012252 sp->ts_state = STATE_FINAL;
12253 break;
12254 }
12255#endif
12256 /*
12257 * Try skipping one character in the bad word (delete it).
12258 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012259 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012260 sp->ts_state = STATE_INS_PREP;
12261 sp->ts_curi = 1;
12262 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12263 /* Deleting a vowel at the start of a word counts less, see
12264 * soundalike_score(). */
12265 newscore = 2 * SCORE_DEL / 3;
12266 else
12267 newscore = SCORE_DEL;
12268 if (fword[sp->ts_fidx] != NUL
12269 && TRY_DEEPER(su, stack, depth, newscore))
12270 {
12271 go_deeper(stack, depth, newscore);
12272#ifdef DEBUG_TRIEWALK
12273 sprintf(changename[depth], "%.*s-%s: delete %c",
12274 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12275 fword[sp->ts_fidx]);
12276#endif
12277 ++depth;
12278
12279 /* Remember what character we deleted, so that we can avoid
12280 * inserting it again. */
12281 stack[depth].ts_flags |= TSF_DIDDEL;
12282 stack[depth].ts_delidx = sp->ts_fidx;
12283
12284 /* Advance over the character in fword[]. Give a bonus to the
12285 * score if the same character is following "nn" -> "n". It's
12286 * a bit illogical for soundfold tree but it does give better
12287 * results. */
12288#ifdef FEAT_MBYTE
12289 if (has_mbyte)
12290 {
12291 c = mb_ptr2char(fword + sp->ts_fidx);
12292 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12293 if (enc_utf8 && utf_iscomposing(c))
12294 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12295 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12296 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12297 }
12298 else
12299#endif
12300 {
12301 ++stack[depth].ts_fidx;
12302 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12303 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12304 }
12305 break;
12306 }
12307 /*FALLTHROUGH*/
12308
12309 case STATE_INS_PREP:
12310 if (sp->ts_flags & TSF_DIDDEL)
12311 {
12312 /* If we just deleted a byte then inserting won't make sense,
12313 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012314 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012315 sp->ts_state = STATE_SWAP;
12316 break;
12317 }
12318
12319 /* skip over NUL bytes */
12320 n = sp->ts_arridx;
12321 for (;;)
12322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012323 if (sp->ts_curi > byts[n])
12324 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012325 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012326 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012327 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012328 break;
12329 }
12330 if (byts[n + sp->ts_curi] != NUL)
12331 {
12332 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012333 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012334 sp->ts_state = STATE_INS;
12335 break;
12336 }
12337 ++sp->ts_curi;
12338 }
12339 break;
12340
12341 /*FALLTHROUGH*/
12342
12343 case STATE_INS:
12344 /* Insert one byte. Repeat this for each possible byte at this
12345 * node. */
12346 n = sp->ts_arridx;
12347 if (sp->ts_curi > byts[n])
12348 {
12349 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012350 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012351 sp->ts_state = STATE_SWAP;
12352 break;
12353 }
12354
12355 /* Do one more byte at this node, but:
12356 * - Skip NUL bytes.
12357 * - Skip the byte if it's equal to the byte in the word,
12358 * accepting that byte is always better.
12359 */
12360 n += sp->ts_curi++;
12361 c = byts[n];
12362 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12363 /* Inserting a vowel at the start of a word counts less,
12364 * see soundalike_score(). */
12365 newscore = 2 * SCORE_INS / 3;
12366 else
12367 newscore = SCORE_INS;
12368 if (c != fword[sp->ts_fidx]
12369 && TRY_DEEPER(su, stack, depth, newscore))
12370 {
12371 go_deeper(stack, depth, newscore);
12372#ifdef DEBUG_TRIEWALK
12373 sprintf(changename[depth], "%.*s-%s: insert %c",
12374 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12375 c);
12376#endif
12377 ++depth;
12378 sp = &stack[depth];
12379 tword[sp->ts_twordlen++] = c;
12380 sp->ts_arridx = idxs[n];
12381#ifdef FEAT_MBYTE
12382 if (has_mbyte)
12383 {
12384 fl = MB_BYTE2LEN(c);
12385 if (fl > 1)
12386 {
12387 /* There are following bytes for the same character.
12388 * We must find all bytes before trying
12389 * delete/insert/swap/etc. */
12390 sp->ts_tcharlen = fl;
12391 sp->ts_tcharidx = 1;
12392 sp->ts_isdiff = DIFF_INSERT;
12393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012394 }
12395 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012396 fl = 1;
12397 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012398#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012399 {
12400 /* If the previous character was the same, thus doubling a
12401 * character, give a bonus to the score. Also for
12402 * soundfold words (illogical but does give a better
12403 * score). */
12404 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012405 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012406 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012408 }
12409 break;
12410
12411 case STATE_SWAP:
12412 /*
12413 * Swap two bytes in the bad word: "12" -> "21".
12414 * We change "fword" here, it's changed back afterwards at
12415 * STATE_UNSWAP.
12416 */
12417 p = fword + sp->ts_fidx;
12418 c = *p;
12419 if (c == NUL)
12420 {
12421 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012422 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012423 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012426
Bram Moolenaar4770d092006-01-12 23:22:24 +000012427 /* Don't swap if the first character is not a word character.
12428 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012429 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012430 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012431 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012432 sp->ts_state = STATE_REP_INI;
12433 break;
12434 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012435
Bram Moolenaar4770d092006-01-12 23:22:24 +000012436#ifdef FEAT_MBYTE
12437 if (has_mbyte)
12438 {
12439 n = mb_cptr2len(p);
12440 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012441 if (p[n] == NUL)
12442 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012443 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012444 c2 = c; /* don't swap non-word char */
12445 else
12446 c2 = mb_ptr2char(p + n);
12447 }
12448 else
12449#endif
12450 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012451 if (p[1] == NUL)
12452 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012453 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012454 c2 = c; /* don't swap non-word char */
12455 else
12456 c2 = p[1];
12457 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012458
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012459 /* When the second character is NUL we can't swap. */
12460 if (c2 == NUL)
12461 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012462 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012463 sp->ts_state = STATE_REP_INI;
12464 break;
12465 }
12466
Bram Moolenaar4770d092006-01-12 23:22:24 +000012467 /* When characters are identical, swap won't do anything.
12468 * Also get here if the second char is not a word character. */
12469 if (c == c2)
12470 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012471 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012472 sp->ts_state = STATE_SWAP3;
12473 break;
12474 }
12475 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12476 {
12477 go_deeper(stack, depth, SCORE_SWAP);
12478#ifdef DEBUG_TRIEWALK
12479 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12480 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12481 c, c2);
12482#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012483 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012484 sp->ts_state = STATE_UNSWAP;
12485 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012486#ifdef FEAT_MBYTE
12487 if (has_mbyte)
12488 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012489 fl = mb_char2len(c2);
12490 mch_memmove(p, p + n, fl);
12491 mb_char2bytes(c, p + fl);
12492 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012493 }
12494 else
12495#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012496 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012497 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012498 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012499 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012500 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012501 }
12502 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012503 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012504 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012505 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012506 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012507 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012508 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012509
Bram Moolenaar4770d092006-01-12 23:22:24 +000012510 case STATE_UNSWAP:
12511 /* Undo the STATE_SWAP swap: "21" -> "12". */
12512 p = fword + sp->ts_fidx;
12513#ifdef FEAT_MBYTE
12514 if (has_mbyte)
12515 {
12516 n = MB_BYTE2LEN(*p);
12517 c = mb_ptr2char(p + n);
12518 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12519 mb_char2bytes(c, p);
12520 }
12521 else
12522#endif
12523 {
12524 c = *p;
12525 *p = p[1];
12526 p[1] = c;
12527 }
12528 /*FALLTHROUGH*/
12529
12530 case STATE_SWAP3:
12531 /* Swap two bytes, skipping one: "123" -> "321". We change
12532 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12533 p = fword + sp->ts_fidx;
12534#ifdef FEAT_MBYTE
12535 if (has_mbyte)
12536 {
12537 n = mb_cptr2len(p);
12538 c = mb_ptr2char(p);
12539 fl = mb_cptr2len(p + n);
12540 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012541 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012542 c3 = c; /* don't swap non-word char */
12543 else
12544 c3 = mb_ptr2char(p + n + fl);
12545 }
12546 else
12547#endif
12548 {
12549 c = *p;
12550 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012551 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012552 c3 = c; /* don't swap non-word char */
12553 else
12554 c3 = p[2];
12555 }
12556
12557 /* When characters are identical: "121" then SWAP3 result is
12558 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12559 * same as SWAP on next char: "112". Thus skip all swapping.
12560 * Also skip when c3 is NUL.
12561 * Also get here when the third character is not a word character.
12562 * Second character may any char: "a.b" -> "b.a" */
12563 if (c == c3 || c3 == NUL)
12564 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012565 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012566 sp->ts_state = STATE_REP_INI;
12567 break;
12568 }
12569 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12570 {
12571 go_deeper(stack, depth, SCORE_SWAP3);
12572#ifdef DEBUG_TRIEWALK
12573 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12574 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12575 c, c3);
12576#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012577 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012578 sp->ts_state = STATE_UNSWAP3;
12579 ++depth;
12580#ifdef FEAT_MBYTE
12581 if (has_mbyte)
12582 {
12583 tl = mb_char2len(c3);
12584 mch_memmove(p, p + n + fl, tl);
12585 mb_char2bytes(c2, p + tl);
12586 mb_char2bytes(c, p + fl + tl);
12587 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12588 }
12589 else
12590#endif
12591 {
12592 p[0] = p[2];
12593 p[2] = c;
12594 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12595 }
12596 }
12597 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012598 {
12599 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012600 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012601 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012602 break;
12603
12604 case STATE_UNSWAP3:
12605 /* Undo STATE_SWAP3: "321" -> "123" */
12606 p = fword + sp->ts_fidx;
12607#ifdef FEAT_MBYTE
12608 if (has_mbyte)
12609 {
12610 n = MB_BYTE2LEN(*p);
12611 c2 = mb_ptr2char(p + n);
12612 fl = MB_BYTE2LEN(p[n]);
12613 c = mb_ptr2char(p + n + fl);
12614 tl = MB_BYTE2LEN(p[n + fl]);
12615 mch_memmove(p + fl + tl, p, n);
12616 mb_char2bytes(c, p);
12617 mb_char2bytes(c2, p + tl);
12618 p = p + tl;
12619 }
12620 else
12621#endif
12622 {
12623 c = *p;
12624 *p = p[2];
12625 p[2] = c;
12626 ++p;
12627 }
12628
Bram Moolenaar860cae12010-06-05 23:22:07 +020012629 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012630 {
12631 /* Middle char is not a word char, skip the rotate. First and
12632 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012633 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012634 sp->ts_state = STATE_REP_INI;
12635 break;
12636 }
12637
12638 /* Rotate three characters left: "123" -> "231". We change
12639 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12640 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12641 {
12642 go_deeper(stack, depth, SCORE_SWAP3);
12643#ifdef DEBUG_TRIEWALK
12644 p = fword + sp->ts_fidx;
12645 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12646 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12647 p[0], p[1], p[2]);
12648#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012649 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012650 sp->ts_state = STATE_UNROT3L;
12651 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012652 p = fword + sp->ts_fidx;
12653#ifdef FEAT_MBYTE
12654 if (has_mbyte)
12655 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012656 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012657 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012658 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012659 fl += mb_cptr2len(p + n + fl);
12660 mch_memmove(p, p + n, fl);
12661 mb_char2bytes(c, p + fl);
12662 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012663 }
12664 else
12665#endif
12666 {
12667 c = *p;
12668 *p = p[1];
12669 p[1] = p[2];
12670 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012671 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012672 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012673 }
12674 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012675 {
12676 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012677 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012678 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012679 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012680
Bram Moolenaar4770d092006-01-12 23:22:24 +000012681 case STATE_UNROT3L:
12682 /* Undo ROT3L: "231" -> "123" */
12683 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012684#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012685 if (has_mbyte)
12686 {
12687 n = MB_BYTE2LEN(*p);
12688 n += MB_BYTE2LEN(p[n]);
12689 c = mb_ptr2char(p + n);
12690 tl = MB_BYTE2LEN(p[n]);
12691 mch_memmove(p + tl, p, n);
12692 mb_char2bytes(c, p);
12693 }
12694 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012695#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012696 {
12697 c = p[2];
12698 p[2] = p[1];
12699 p[1] = *p;
12700 *p = c;
12701 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702
Bram Moolenaar4770d092006-01-12 23:22:24 +000012703 /* Rotate three bytes right: "123" -> "312". We change "fword"
12704 * here, it's changed back afterwards at STATE_UNROT3R. */
12705 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12706 {
12707 go_deeper(stack, depth, SCORE_SWAP3);
12708#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012710 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12711 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12712 p[0], p[1], p[2]);
12713#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012714 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012715 sp->ts_state = STATE_UNROT3R;
12716 ++depth;
12717 p = fword + sp->ts_fidx;
12718#ifdef FEAT_MBYTE
12719 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012720 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012721 n = mb_cptr2len(p);
12722 n += mb_cptr2len(p + n);
12723 c = mb_ptr2char(p + n);
12724 tl = mb_cptr2len(p + n);
12725 mch_memmove(p + tl, p, n);
12726 mb_char2bytes(c, p);
12727 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012728 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012729 else
12730#endif
12731 {
12732 c = p[2];
12733 p[2] = p[1];
12734 p[1] = *p;
12735 *p = c;
12736 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12737 }
12738 }
12739 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012740 {
12741 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012742 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012743 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012744 break;
12745
12746 case STATE_UNROT3R:
12747 /* Undo ROT3R: "312" -> "123" */
12748 p = fword + sp->ts_fidx;
12749#ifdef FEAT_MBYTE
12750 if (has_mbyte)
12751 {
12752 c = mb_ptr2char(p);
12753 tl = MB_BYTE2LEN(*p);
12754 n = MB_BYTE2LEN(p[tl]);
12755 n += MB_BYTE2LEN(p[tl + n]);
12756 mch_memmove(p, p + tl, n);
12757 mb_char2bytes(c, p + n);
12758 }
12759 else
12760#endif
12761 {
12762 c = *p;
12763 *p = p[1];
12764 p[1] = p[2];
12765 p[2] = c;
12766 }
12767 /*FALLTHROUGH*/
12768
12769 case STATE_REP_INI:
12770 /* Check if matching with REP items from the .aff file would work.
12771 * Quickly skip if:
12772 * - there are no REP items and we are not in the soundfold trie
12773 * - the score is going to be too high anyway
12774 * - already applied a REP item or swapped here */
12775 if ((lp->lp_replang == NULL && !soundfold)
12776 || sp->ts_score + SCORE_REP >= su->su_maxscore
12777 || sp->ts_fidx < sp->ts_fidxtry)
12778 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012779 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012780 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012781 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783
Bram Moolenaar4770d092006-01-12 23:22:24 +000012784 /* Use the first byte to quickly find the first entry that may
12785 * match. If the index is -1 there is none. */
12786 if (soundfold)
12787 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12788 else
12789 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790
Bram Moolenaar4770d092006-01-12 23:22:24 +000012791 if (sp->ts_curi < 0)
12792 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012793 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012794 sp->ts_state = STATE_FINAL;
12795 break;
12796 }
12797
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012798 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012799 sp->ts_state = STATE_REP;
12800 /*FALLTHROUGH*/
12801
12802 case STATE_REP:
12803 /* Try matching with REP items from the .aff file. For each match
12804 * replace the characters and check if the resulting word is
12805 * valid. */
12806 p = fword + sp->ts_fidx;
12807
12808 if (soundfold)
12809 gap = &slang->sl_repsal;
12810 else
12811 gap = &lp->lp_replang->sl_rep;
12812 while (sp->ts_curi < gap->ga_len)
12813 {
12814 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12815 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012816 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012817 /* past possible matching entries */
12818 sp->ts_curi = gap->ga_len;
12819 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012820 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012821 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12822 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12823 {
12824 go_deeper(stack, depth, SCORE_REP);
12825#ifdef DEBUG_TRIEWALK
12826 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12827 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12828 ftp->ft_from, ftp->ft_to);
12829#endif
12830 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012831 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012832 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012833
Bram Moolenaar4770d092006-01-12 23:22:24 +000012834 /* Change the "from" to the "to" string. */
12835 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012836 fl = (int)STRLEN(ftp->ft_from);
12837 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012838 if (fl != tl)
12839 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012840 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012841 repextra += tl - fl;
12842 }
12843 mch_memmove(p, ftp->ft_to, tl);
12844 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12845#ifdef FEAT_MBYTE
12846 stack[depth].ts_tcharlen = 0;
12847#endif
12848 break;
12849 }
12850 }
12851
12852 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012853 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012854 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012855 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012856 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012857 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012858
12859 break;
12860
12861 case STATE_REP_UNDO:
12862 /* Undo a REP replacement and continue with the next one. */
12863 if (soundfold)
12864 gap = &slang->sl_repsal;
12865 else
12866 gap = &lp->lp_replang->sl_rep;
12867 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012868 fl = (int)STRLEN(ftp->ft_from);
12869 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012870 p = fword + sp->ts_fidx;
12871 if (fl != tl)
12872 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012873 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012874 repextra -= tl - fl;
12875 }
12876 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012877 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012878 sp->ts_state = STATE_REP;
12879 break;
12880
12881 default:
12882 /* Did all possible states at this level, go up one level. */
12883 --depth;
12884
12885 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12886 {
12887 /* Continue in or go back to the prefix tree. */
12888 byts = pbyts;
12889 idxs = pidxs;
12890 }
12891
12892 /* Don't check for CTRL-C too often, it takes time. */
12893 if (--breakcheckcount == 0)
12894 {
12895 ui_breakcheck();
12896 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012897 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 }
12899 }
12900}
12901
Bram Moolenaar4770d092006-01-12 23:22:24 +000012902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012904 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012905 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012906 static void
12907go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012908 trystate_T *stack;
12909 int depth;
12910 int score_add;
12911{
Bram Moolenaarea424162005-06-16 21:51:00 +000012912 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012914 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012916 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012917}
12918
Bram Moolenaar53805d12005-08-01 07:08:33 +000012919#ifdef FEAT_MBYTE
12920/*
12921 * Case-folding may change the number of bytes: Count nr of chars in
12922 * fword[flen] and return the byte length of that many chars in "word".
12923 */
12924 static int
12925nofold_len(fword, flen, word)
12926 char_u *fword;
12927 int flen;
12928 char_u *word;
12929{
12930 char_u *p;
12931 int i = 0;
12932
12933 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12934 ++i;
12935 for (p = word; i > 0; mb_ptr_adv(p))
12936 --i;
12937 return (int)(p - word);
12938}
12939#endif
12940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012941/*
12942 * "fword" is a good word with case folded. Find the matching keep-case
12943 * words and put it in "kword".
12944 * Theoretically there could be several keep-case words that result in the
12945 * same case-folded word, but we only find one...
12946 */
12947 static void
12948find_keepcap_word(slang, fword, kword)
12949 slang_T *slang;
12950 char_u *fword;
12951 char_u *kword;
12952{
12953 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12954 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012955 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956
12957 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012958 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012959 int round[MAXWLEN];
12960 int fwordidx[MAXWLEN];
12961 int uwordidx[MAXWLEN];
12962 int kwordlen[MAXWLEN];
12963
12964 int flen, ulen;
12965 int l;
12966 int len;
12967 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012968 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012969 char_u *p;
12970 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012971 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972
12973 if (byts == NULL)
12974 {
12975 /* array is empty: "cannot happen" */
12976 *kword = NUL;
12977 return;
12978 }
12979
12980 /* Make an all-cap version of "fword". */
12981 allcap_copy(fword, uword);
12982
12983 /*
12984 * Each character needs to be tried both case-folded and upper-case.
12985 * All this gets very complicated if we keep in mind that changing case
12986 * may change the byte length of a multi-byte character...
12987 */
12988 depth = 0;
12989 arridx[0] = 0;
12990 round[0] = 0;
12991 fwordidx[0] = 0;
12992 uwordidx[0] = 0;
12993 kwordlen[0] = 0;
12994 while (depth >= 0)
12995 {
12996 if (fword[fwordidx[depth]] == NUL)
12997 {
12998 /* We are at the end of "fword". If the tree allows a word to end
12999 * here we have found a match. */
13000 if (byts[arridx[depth] + 1] == 0)
13001 {
13002 kword[kwordlen[depth]] = NUL;
13003 return;
13004 }
13005
13006 /* kword is getting too long, continue one level up */
13007 --depth;
13008 }
13009 else if (++round[depth] > 2)
13010 {
13011 /* tried both fold-case and upper-case character, continue one
13012 * level up */
13013 --depth;
13014 }
13015 else
13016 {
13017 /*
13018 * round[depth] == 1: Try using the folded-case character.
13019 * round[depth] == 2: Try using the upper-case character.
13020 */
13021#ifdef FEAT_MBYTE
13022 if (has_mbyte)
13023 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013024 flen = mb_cptr2len(fword + fwordidx[depth]);
13025 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 }
13027 else
13028#endif
13029 ulen = flen = 1;
13030 if (round[depth] == 1)
13031 {
13032 p = fword + fwordidx[depth];
13033 l = flen;
13034 }
13035 else
13036 {
13037 p = uword + uwordidx[depth];
13038 l = ulen;
13039 }
13040
13041 for (tryidx = arridx[depth]; l > 0; --l)
13042 {
13043 /* Perform a binary search in the list of accepted bytes. */
13044 len = byts[tryidx++];
13045 c = *p++;
13046 lo = tryidx;
13047 hi = tryidx + len - 1;
13048 while (lo < hi)
13049 {
13050 m = (lo + hi) / 2;
13051 if (byts[m] > c)
13052 hi = m - 1;
13053 else if (byts[m] < c)
13054 lo = m + 1;
13055 else
13056 {
13057 lo = hi = m;
13058 break;
13059 }
13060 }
13061
13062 /* Stop if there is no matching byte. */
13063 if (hi < lo || byts[lo] != c)
13064 break;
13065
13066 /* Continue at the child (if there is one). */
13067 tryidx = idxs[lo];
13068 }
13069
13070 if (l == 0)
13071 {
13072 /*
13073 * Found the matching char. Copy it to "kword" and go a
13074 * level deeper.
13075 */
13076 if (round[depth] == 1)
13077 {
13078 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
13079 flen);
13080 kwordlen[depth + 1] = kwordlen[depth] + flen;
13081 }
13082 else
13083 {
13084 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
13085 ulen);
13086 kwordlen[depth + 1] = kwordlen[depth] + ulen;
13087 }
13088 fwordidx[depth + 1] = fwordidx[depth] + flen;
13089 uwordidx[depth + 1] = uwordidx[depth] + ulen;
13090
13091 ++depth;
13092 arridx[depth] = tryidx;
13093 round[depth] = 0;
13094 }
13095 }
13096 }
13097
13098 /* Didn't find it: "cannot happen". */
13099 *kword = NUL;
13100}
13101
13102/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013103 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
13104 * su->su_sga.
13105 */
13106 static void
13107score_comp_sal(su)
13108 suginfo_T *su;
13109{
13110 langp_T *lp;
13111 char_u badsound[MAXWLEN];
13112 int i;
13113 suggest_T *stp;
13114 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013115 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013116 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013117
13118 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
13119 return;
13120
13121 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013122 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013123 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013124 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013125 if (lp->lp_slang->sl_sal.ga_len > 0)
13126 {
13127 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013128 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013129
13130 for (i = 0; i < su->su_ga.ga_len; ++i)
13131 {
13132 stp = &SUG(su->su_ga, i);
13133
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013134 /* Case-fold the suggested word, sound-fold it and compute the
13135 * sound-a-like score. */
13136 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013137 if (score < SCORE_MAXMAX)
13138 {
13139 /* Add the suggestion. */
13140 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13141 sstp->st_word = vim_strsave(stp->st_word);
13142 if (sstp->st_word != NULL)
13143 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013144 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013145 sstp->st_score = score;
13146 sstp->st_altscore = 0;
13147 sstp->st_orglen = stp->st_orglen;
13148 ++su->su_sga.ga_len;
13149 }
13150 }
13151 }
13152 break;
13153 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013154 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013155}
13156
13157/*
13158 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013159 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013160 */
13161 static void
13162score_combine(su)
13163 suginfo_T *su;
13164{
13165 int i;
13166 int j;
13167 garray_T ga;
13168 garray_T *gap;
13169 langp_T *lp;
13170 suggest_T *stp;
13171 char_u *p;
13172 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013173 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013174 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013175 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013176
13177 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013178 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013179 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013180 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013181 if (lp->lp_slang->sl_sal.ga_len > 0)
13182 {
13183 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013184 slang = lp->lp_slang;
13185 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013186
13187 for (i = 0; i < su->su_ga.ga_len; ++i)
13188 {
13189 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013190 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013191 if (stp->st_altscore == SCORE_MAXMAX)
13192 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13193 else
13194 stp->st_score = (stp->st_score * 3
13195 + stp->st_altscore) / 4;
13196 stp->st_salscore = FALSE;
13197 }
13198 break;
13199 }
13200 }
13201
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013202 if (slang == NULL) /* Using "double" without sound folding. */
13203 {
13204 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13205 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013206 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013207 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013208
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013209 /* Add the alternate score to su_sga. */
13210 for (i = 0; i < su->su_sga.ga_len; ++i)
13211 {
13212 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013213 stp->st_altscore = spell_edit_score(slang,
13214 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013215 if (stp->st_score == SCORE_MAXMAX)
13216 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13217 else
13218 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13219 stp->st_salscore = TRUE;
13220 }
13221
Bram Moolenaar4770d092006-01-12 23:22:24 +000013222 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13223 * for both lists. */
13224 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013225 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013226 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013227 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13228
13229 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13230 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13231 return;
13232
13233 stp = &SUG(ga, 0);
13234 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13235 {
13236 /* round 1: get a suggestion from su_ga
13237 * round 2: get a suggestion from su_sga */
13238 for (round = 1; round <= 2; ++round)
13239 {
13240 gap = round == 1 ? &su->su_ga : &su->su_sga;
13241 if (i < gap->ga_len)
13242 {
13243 /* Don't add a word if it's already there. */
13244 p = SUG(*gap, i).st_word;
13245 for (j = 0; j < ga.ga_len; ++j)
13246 if (STRCMP(stp[j].st_word, p) == 0)
13247 break;
13248 if (j == ga.ga_len)
13249 stp[ga.ga_len++] = SUG(*gap, i);
13250 else
13251 vim_free(p);
13252 }
13253 }
13254 }
13255
13256 ga_clear(&su->su_ga);
13257 ga_clear(&su->su_sga);
13258
13259 /* Truncate the list to the number of suggestions that will be displayed. */
13260 if (ga.ga_len > su->su_maxcount)
13261 {
13262 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13263 vim_free(stp[i].st_word);
13264 ga.ga_len = su->su_maxcount;
13265 }
13266
13267 su->su_ga = ga;
13268}
13269
13270/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013271 * For the goodword in "stp" compute the soundalike score compared to the
13272 * badword.
13273 */
13274 static int
13275stp_sal_score(stp, su, slang, badsound)
13276 suggest_T *stp;
13277 suginfo_T *su;
13278 slang_T *slang;
13279 char_u *badsound; /* sound-folded badword */
13280{
13281 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013282 char_u *pbad;
13283 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013284 char_u badsound2[MAXWLEN];
13285 char_u fword[MAXWLEN];
13286 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013287 char_u goodword[MAXWLEN];
13288 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013289
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013290 lendiff = (int)(su->su_badlen - stp->st_orglen);
13291 if (lendiff >= 0)
13292 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013293 else
13294 {
13295 /* soundfold the bad word with more characters following */
13296 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13297
13298 /* When joining two words the sound often changes a lot. E.g., "t he"
13299 * sounds like "t h" while "the" sounds like "@". Avoid that by
13300 * removing the space. Don't do it when the good word also contains a
13301 * space. */
13302 if (vim_iswhite(su->su_badptr[su->su_badlen])
13303 && *skiptowhite(stp->st_word) == NUL)
13304 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013305 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013306
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013307 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013308 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013309 }
13310
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013311 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013312 {
13313 /* Add part of the bad word to the good word, so that we soundfold
13314 * what replaces the bad word. */
13315 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013316 vim_strncpy(goodword + stp->st_wordlen,
13317 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013318 pgood = goodword;
13319 }
13320 else
13321 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013322
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013323 /* Sound-fold the word and compute the score for the difference. */
13324 spell_soundfold(slang, pgood, FALSE, goodsound);
13325
13326 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013327}
13328
Bram Moolenaar4770d092006-01-12 23:22:24 +000013329/* structure used to store soundfolded words that add_sound_suggest() has
13330 * handled already. */
13331typedef struct
13332{
13333 short sft_score; /* lowest score used */
13334 char_u sft_word[1]; /* soundfolded word, actually longer */
13335} sftword_T;
13336
13337static sftword_T dumsft;
13338#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13339#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13340
13341/*
13342 * Prepare for calling suggest_try_soundalike().
13343 */
13344 static void
13345suggest_try_soundalike_prep()
13346{
13347 langp_T *lp;
13348 int lpi;
13349 slang_T *slang;
13350
13351 /* Do this for all languages that support sound folding and for which a
13352 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013353 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013354 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013355 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013356 slang = lp->lp_slang;
13357 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13358 /* prepare the hashtable used by add_sound_suggest() */
13359 hash_init(&slang->sl_sounddone);
13360 }
13361}
13362
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013363/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013365 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 */
13367 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013368suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013369 suginfo_T *su;
13370{
13371 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013372 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013373 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013374 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375
Bram Moolenaar4770d092006-01-12 23:22:24 +000013376 /* Do this for all languages that support sound folding and for which a
13377 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013378 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013380 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013381 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013382 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013383 {
13384 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013385 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013386
Bram Moolenaar4770d092006-01-12 23:22:24 +000013387 /* try all kinds of inserts/deletes/swaps/etc. */
13388 /* TODO: also soundfold the next words, so that we can try joining
13389 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010013390#ifdef SUGGEST_PROFILE
13391 prof_init();
13392#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000013393 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010013394#ifdef SUGGEST_PROFILE
13395 prof_report("soundalike");
13396#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000013397 }
13398 }
13399}
13400
13401/*
13402 * Finish up after calling suggest_try_soundalike().
13403 */
13404 static void
13405suggest_try_soundalike_finish()
13406{
13407 langp_T *lp;
13408 int lpi;
13409 slang_T *slang;
13410 int todo;
13411 hashitem_T *hi;
13412
13413 /* Do this for all languages that support sound folding and for which a
13414 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013415 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013416 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013417 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013418 slang = lp->lp_slang;
13419 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13420 {
13421 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013422 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013423 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013426 vim_free(HI2SFT(hi));
13427 --todo;
13428 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013429
13430 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013431 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013432 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013433 }
13434 }
13435}
13436
13437/*
13438 * A match with a soundfolded word is found. Add the good word(s) that
13439 * produce this soundfolded word.
13440 */
13441 static void
13442add_sound_suggest(su, goodword, score, lp)
13443 suginfo_T *su;
13444 char_u *goodword;
13445 int score; /* soundfold score */
13446 langp_T *lp;
13447{
13448 slang_T *slang = lp->lp_slang; /* language for sound folding */
13449 int sfwordnr;
13450 char_u *nrline;
13451 int orgnr;
13452 char_u theword[MAXWLEN];
13453 int i;
13454 int wlen;
13455 char_u *byts;
13456 idx_T *idxs;
13457 int n;
13458 int wordcount;
13459 int wc;
13460 int goodscore;
13461 hash_T hash;
13462 hashitem_T *hi;
13463 sftword_T *sft;
13464 int bc, gc;
13465 int limit;
13466
13467 /*
13468 * It's very well possible that the same soundfold word is found several
13469 * times with different scores. Since the following is quite slow only do
13470 * the words that have a better score than before. Use a hashtable to
13471 * remember the words that have been done.
13472 */
13473 hash = hash_hash(goodword);
13474 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13475 if (HASHITEM_EMPTY(hi))
13476 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013477 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13478 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013479 if (sft != NULL)
13480 {
13481 sft->sft_score = score;
13482 STRCPY(sft->sft_word, goodword);
13483 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13484 }
13485 }
13486 else
13487 {
13488 sft = HI2SFT(hi);
13489 if (score >= sft->sft_score)
13490 return;
13491 sft->sft_score = score;
13492 }
13493
13494 /*
13495 * Find the word nr in the soundfold tree.
13496 */
13497 sfwordnr = soundfold_find(slang, goodword);
13498 if (sfwordnr < 0)
13499 {
13500 EMSG2(_(e_intern2), "add_sound_suggest()");
13501 return;
13502 }
13503
13504 /*
13505 * go over the list of good words that produce this soundfold word
13506 */
13507 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13508 orgnr = 0;
13509 while (*nrline != NUL)
13510 {
13511 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13512 * previous wordnr. */
13513 orgnr += bytes2offset(&nrline);
13514
13515 byts = slang->sl_fbyts;
13516 idxs = slang->sl_fidxs;
13517
13518 /* Lookup the word "orgnr" one of the two tries. */
13519 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013520 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013521 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013522 {
13523 i = 1;
13524 if (wordcount == orgnr && byts[n + 1] == NUL)
13525 break; /* found end of word */
13526
13527 if (byts[n + 1] == NUL)
13528 ++wordcount;
13529
13530 /* skip over the NUL bytes */
13531 for ( ; byts[n + i] == NUL; ++i)
13532 if (i > byts[n]) /* safety check */
13533 {
13534 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013535 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013536 goto badword;
13537 }
13538
13539 /* One of the siblings must have the word. */
13540 for ( ; i < byts[n]; ++i)
13541 {
13542 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13543 if (wordcount + wc > orgnr)
13544 break;
13545 wordcount += wc;
13546 }
13547
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013548 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +000013549 n = idxs[n + i];
13550 }
13551badword:
13552 theword[wlen] = NUL;
13553
13554 /* Go over the possible flags and regions. */
13555 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13556 {
13557 char_u cword[MAXWLEN];
13558 char_u *p;
13559 int flags = (int)idxs[n + i];
13560
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013561 /* Skip words with the NOSUGGEST flag */
13562 if (flags & WF_NOSUGGEST)
13563 continue;
13564
Bram Moolenaar4770d092006-01-12 23:22:24 +000013565 if (flags & WF_KEEPCAP)
13566 {
13567 /* Must find the word in the keep-case tree. */
13568 find_keepcap_word(slang, theword, cword);
13569 p = cword;
13570 }
13571 else
13572 {
13573 flags |= su->su_badflags;
13574 if ((flags & WF_CAPMASK) != 0)
13575 {
13576 /* Need to fix case according to "flags". */
13577 make_case_word(theword, cword, flags);
13578 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 }
13580 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013581 p = theword;
13582 }
13583
13584 /* Add the suggestion. */
13585 if (sps_flags & SPS_DOUBLE)
13586 {
13587 /* Add the suggestion if the score isn't too bad. */
13588 if (score <= su->su_maxscore)
13589 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13590 score, 0, FALSE, slang, FALSE);
13591 }
13592 else
13593 {
13594 /* Add a penalty for words in another region. */
13595 if ((flags & WF_REGION)
13596 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13597 goodscore = SCORE_REGION;
13598 else
13599 goodscore = 0;
13600
13601 /* Add a small penalty for changing the first letter from
13602 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013603 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +000013604 * letter is the same, that has already been counted. */
13605 gc = PTR2CHAR(p);
13606 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013608 bc = PTR2CHAR(su->su_badword);
13609 if (!SPELL_ISUPPER(bc)
13610 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13611 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 }
13613
Bram Moolenaar4770d092006-01-12 23:22:24 +000013614 /* Compute the score for the good word. This only does letter
13615 * insert/delete/swap/replace. REP items are not considered,
13616 * which may make the score a bit higher.
13617 * Use a limit for the score to make it work faster. Use
13618 * MAXSCORE(), because RESCORE() will change the score.
13619 * If the limit is very high then the iterative method is
13620 * inefficient, using an array is quicker. */
13621 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13622 if (limit > SCORE_LIMITMAX)
13623 goodscore += spell_edit_score(slang, su->su_badword, p);
13624 else
13625 goodscore += spell_edit_score_limit(slang, su->su_badword,
13626 p, limit);
13627
13628 /* When going over the limit don't bother to do the rest. */
13629 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013631 /* Give a bonus to words seen before. */
13632 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013633
Bram Moolenaar4770d092006-01-12 23:22:24 +000013634 /* Add the suggestion if the score isn't too bad. */
13635 goodscore = RESCORE(goodscore, score);
13636 if (goodscore <= su->su_sfmaxscore)
13637 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13638 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013639 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 }
13641 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013642 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013643 }
13644}
13645
13646/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013647 * Find word "word" in fold-case tree for "slang" and return the word number.
13648 */
13649 static int
13650soundfold_find(slang, word)
13651 slang_T *slang;
13652 char_u *word;
13653{
13654 idx_T arridx = 0;
13655 int len;
13656 int wlen = 0;
13657 int c;
13658 char_u *ptr = word;
13659 char_u *byts;
13660 idx_T *idxs;
13661 int wordnr = 0;
13662
13663 byts = slang->sl_sbyts;
13664 idxs = slang->sl_sidxs;
13665
13666 for (;;)
13667 {
13668 /* First byte is the number of possible bytes. */
13669 len = byts[arridx++];
13670
13671 /* If the first possible byte is a zero the word could end here.
13672 * If the word ends we found the word. If not skip the NUL bytes. */
13673 c = ptr[wlen];
13674 if (byts[arridx] == NUL)
13675 {
13676 if (c == NUL)
13677 break;
13678
13679 /* Skip over the zeros, there can be several. */
13680 while (len > 0 && byts[arridx] == NUL)
13681 {
13682 ++arridx;
13683 --len;
13684 }
13685 if (len == 0)
13686 return -1; /* no children, word should have ended here */
13687 ++wordnr;
13688 }
13689
13690 /* If the word ends we didn't find it. */
13691 if (c == NUL)
13692 return -1;
13693
13694 /* Perform a binary search in the list of accepted bytes. */
13695 if (c == TAB) /* <Tab> is handled like <Space> */
13696 c = ' ';
13697 while (byts[arridx] < c)
13698 {
13699 /* The word count is in the first idxs[] entry of the child. */
13700 wordnr += idxs[idxs[arridx]];
13701 ++arridx;
13702 if (--len == 0) /* end of the bytes, didn't find it */
13703 return -1;
13704 }
13705 if (byts[arridx] != c) /* didn't find the byte */
13706 return -1;
13707
13708 /* Continue at the child (if there is one). */
13709 arridx = idxs[arridx];
13710 ++wlen;
13711
13712 /* One space in the good word may stand for several spaces in the
13713 * checked word. */
13714 if (c == ' ')
13715 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13716 ++wlen;
13717 }
13718
13719 return wordnr;
13720}
13721
13722/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013723 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013724 */
13725 static void
13726make_case_word(fword, cword, flags)
13727 char_u *fword;
13728 char_u *cword;
13729 int flags;
13730{
13731 if (flags & WF_ALLCAP)
13732 /* Make it all upper-case */
13733 allcap_copy(fword, cword);
13734 else if (flags & WF_ONECAP)
13735 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013736 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737 else
13738 /* Use goodword as-is. */
13739 STRCPY(cword, fword);
13740}
13741
Bram Moolenaarea424162005-06-16 21:51:00 +000013742/*
13743 * Use map string "map" for languages "lp".
13744 */
13745 static void
13746set_map_str(lp, map)
13747 slang_T *lp;
13748 char_u *map;
13749{
13750 char_u *p;
13751 int headc = 0;
13752 int c;
13753 int i;
13754
13755 if (*map == NUL)
13756 {
13757 lp->sl_has_map = FALSE;
13758 return;
13759 }
13760 lp->sl_has_map = TRUE;
13761
Bram Moolenaar4770d092006-01-12 23:22:24 +000013762 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013763 for (i = 0; i < 256; ++i)
13764 lp->sl_map_array[i] = 0;
13765#ifdef FEAT_MBYTE
13766 hash_init(&lp->sl_map_hash);
13767#endif
13768
13769 /*
13770 * The similar characters are stored separated with slashes:
13771 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13772 * before the same slash. For characters above 255 sl_map_hash is used.
13773 */
13774 for (p = map; *p != NUL; )
13775 {
13776#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013777 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013778#else
13779 c = *p++;
13780#endif
13781 if (c == '/')
13782 headc = 0;
13783 else
13784 {
13785 if (headc == 0)
13786 headc = c;
13787
13788#ifdef FEAT_MBYTE
13789 /* Characters above 255 don't fit in sl_map_array[], put them in
13790 * the hash table. Each entry is the char, a NUL the headchar and
13791 * a NUL. */
13792 if (c >= 256)
13793 {
13794 int cl = mb_char2len(c);
13795 int headcl = mb_char2len(headc);
13796 char_u *b;
13797 hash_T hash;
13798 hashitem_T *hi;
13799
13800 b = alloc((unsigned)(cl + headcl + 2));
13801 if (b == NULL)
13802 return;
13803 mb_char2bytes(c, b);
13804 b[cl] = NUL;
13805 mb_char2bytes(headc, b + cl + 1);
13806 b[cl + 1 + headcl] = NUL;
13807 hash = hash_hash(b);
13808 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13809 if (HASHITEM_EMPTY(hi))
13810 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13811 else
13812 {
13813 /* This should have been checked when generating the .spl
13814 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013815 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013816 vim_free(b);
13817 }
13818 }
13819 else
13820#endif
13821 lp->sl_map_array[c] = headc;
13822 }
13823 }
13824}
13825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013826/*
13827 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13828 * lines in the .aff file.
13829 */
13830 static int
13831similar_chars(slang, c1, c2)
13832 slang_T *slang;
13833 int c1;
13834 int c2;
13835{
Bram Moolenaarea424162005-06-16 21:51:00 +000013836 int m1, m2;
13837#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +020013838 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +000013839 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840
Bram Moolenaarea424162005-06-16 21:51:00 +000013841 if (c1 >= 256)
13842 {
13843 buf[mb_char2bytes(c1, buf)] = 0;
13844 hi = hash_find(&slang->sl_map_hash, buf);
13845 if (HASHITEM_EMPTY(hi))
13846 m1 = 0;
13847 else
13848 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13849 }
13850 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013851#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013852 m1 = slang->sl_map_array[c1];
13853 if (m1 == 0)
13854 return FALSE;
13855
13856
13857#ifdef FEAT_MBYTE
13858 if (c2 >= 256)
13859 {
13860 buf[mb_char2bytes(c2, buf)] = 0;
13861 hi = hash_find(&slang->sl_map_hash, buf);
13862 if (HASHITEM_EMPTY(hi))
13863 m2 = 0;
13864 else
13865 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13866 }
13867 else
13868#endif
13869 m2 = slang->sl_map_array[c2];
13870
13871 return m1 == m2;
13872}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873
13874/*
13875 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013876 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 */
13878 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013879add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13880 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013882 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013884 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013885 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013886 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013887 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013888 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013889 int maxsf; /* su_maxscore applies to soundfold score,
13890 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013892 int goodlen; /* len of goodword changed */
13893 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013895 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013897 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013899 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13900 * "thee the" is added next to changing the first "the" the "thee". */
13901 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013902 pbad = su->su_badptr + badlenarg;
13903 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013904 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013905 goodlen = (int)(pgood - goodword);
13906 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013907 if (goodlen <= 0 || badlen <= 0)
13908 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013909 mb_ptr_back(goodword, pgood);
13910 mb_ptr_back(su->su_badptr, pbad);
13911#ifdef FEAT_MBYTE
13912 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013913 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013914 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13915 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013916 }
13917 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013918#endif
13919 if (*pgood != *pbad)
13920 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013921 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013922
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013923 if (badlen == 0 && goodlen == 0)
13924 /* goodword doesn't change anything; may happen for "the the" changing
13925 * the first "the" to itself. */
13926 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013927
Bram Moolenaar89d40322006-08-29 15:30:07 +000013928 if (gap->ga_len == 0)
13929 i = -1;
13930 else
13931 {
13932 /* Check if the word is already there. Also check the length that is
13933 * being replaced "thes," -> "these" is a different suggestion from
13934 * "thes" -> "these". */
13935 stp = &SUG(*gap, 0);
13936 for (i = gap->ga_len; --i >= 0; ++stp)
13937 if (stp->st_wordlen == goodlen
13938 && stp->st_orglen == badlen
13939 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013941 /*
13942 * Found it. Remember the word with the lowest score.
13943 */
13944 if (stp->st_slang == NULL)
13945 stp->st_slang = slang;
13946
13947 new_sug.st_score = score;
13948 new_sug.st_altscore = altscore;
13949 new_sug.st_had_bonus = had_bonus;
13950
13951 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013952 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013953 /* Only one of the two had the soundalike score computed.
13954 * Need to do that for the other one now, otherwise the
13955 * scores can't be compared. This happens because
13956 * suggest_try_change() doesn't compute the soundalike
13957 * word to keep it fast, while some special methods set
13958 * the soundalike score to zero. */
13959 if (had_bonus)
13960 rescore_one(su, stp);
13961 else
13962 {
13963 new_sug.st_word = stp->st_word;
13964 new_sug.st_wordlen = stp->st_wordlen;
13965 new_sug.st_slang = stp->st_slang;
13966 new_sug.st_orglen = badlen;
13967 rescore_one(su, &new_sug);
13968 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970
Bram Moolenaar89d40322006-08-29 15:30:07 +000013971 if (stp->st_score > new_sug.st_score)
13972 {
13973 stp->st_score = new_sug.st_score;
13974 stp->st_altscore = new_sug.st_altscore;
13975 stp->st_had_bonus = new_sug.st_had_bonus;
13976 }
13977 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013978 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013979 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980
Bram Moolenaar4770d092006-01-12 23:22:24 +000013981 if (i < 0 && ga_grow(gap, 1) == OK)
13982 {
13983 /* Add a suggestion. */
13984 stp = &SUG(*gap, gap->ga_len);
13985 stp->st_word = vim_strnsave(goodword, goodlen);
13986 if (stp->st_word != NULL)
13987 {
13988 stp->st_wordlen = goodlen;
13989 stp->st_score = score;
13990 stp->st_altscore = altscore;
13991 stp->st_had_bonus = had_bonus;
13992 stp->st_orglen = badlen;
13993 stp->st_slang = slang;
13994 ++gap->ga_len;
13995
13996 /* If we have too many suggestions now, sort the list and keep
13997 * the best suggestions. */
13998 if (gap->ga_len > SUG_MAX_COUNT(su))
13999 {
14000 if (maxsf)
14001 su->su_sfmaxscore = cleanup_suggestions(gap,
14002 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
14003 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014004 su->su_maxscore = cleanup_suggestions(gap,
14005 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006 }
14007 }
14008 }
14009}
14010
14011/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000014012 * Suggestions may in fact be flagged as errors. Esp. for banned words and
14013 * for split words, such as "the the". Remove these from the list here.
14014 */
14015 static void
14016check_suggestions(su, gap)
14017 suginfo_T *su;
14018 garray_T *gap; /* either su_ga or su_sga */
14019{
14020 suggest_T *stp;
14021 int i;
14022 char_u longword[MAXWLEN + 1];
14023 int len;
14024 hlf_T attr;
14025
14026 stp = &SUG(*gap, 0);
14027 for (i = gap->ga_len - 1; i >= 0; --i)
14028 {
14029 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014030 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +000014031 len = stp[i].st_wordlen;
14032 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
14033 MAXWLEN - len);
14034 attr = HLF_COUNT;
14035 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
14036 if (attr != HLF_COUNT)
14037 {
14038 /* Remove this entry. */
14039 vim_free(stp[i].st_word);
14040 --gap->ga_len;
14041 if (i < gap->ga_len)
14042 mch_memmove(stp + i, stp + i + 1,
14043 sizeof(suggest_T) * (gap->ga_len - i));
14044 }
14045 }
14046}
14047
14048
14049/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014050 * Add a word to be banned.
14051 */
14052 static void
14053add_banned(su, word)
14054 suginfo_T *su;
14055 char_u *word;
14056{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000014057 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058 hash_T hash;
14059 hashitem_T *hi;
14060
Bram Moolenaar4770d092006-01-12 23:22:24 +000014061 hash = hash_hash(word);
14062 hi = hash_lookup(&su->su_banned, word, hash);
14063 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014064 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014065 s = vim_strsave(word);
14066 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014067 hash_add_item(&su->su_banned, hi, s, hash);
14068 }
14069}
14070
14071/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014072 * Recompute the score for all suggestions if sound-folding is possible. This
14073 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014074 */
14075 static void
14076rescore_suggestions(su)
14077 suginfo_T *su;
14078{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014079 int i;
14080
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014081 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000014082 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014083 rescore_one(su, &SUG(su->su_ga, i));
14084}
14085
14086/*
14087 * Recompute the score for one suggestion if sound-folding is possible.
14088 */
14089 static void
14090rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000014091 suginfo_T *su;
14092 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014093{
14094 slang_T *slang = stp->st_slang;
14095 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000014096 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014097
14098 /* Only rescore suggestions that have no sal score yet and do have a
14099 * language. */
14100 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
14101 {
14102 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000014103 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014104 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000014105 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014106 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000014107 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014108 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000014109
14110 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014111 if (stp->st_altscore == SCORE_MAXMAX)
14112 stp->st_altscore = SCORE_BIG;
14113 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
14114 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014115 }
14116}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118static int
14119#ifdef __BORLANDC__
14120_RTLENTRYF
14121#endif
14122sug_compare __ARGS((const void *s1, const void *s2));
14123
14124/*
14125 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014126 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014127 */
14128 static int
14129#ifdef __BORLANDC__
14130_RTLENTRYF
14131#endif
14132sug_compare(s1, s2)
14133 const void *s1;
14134 const void *s2;
14135{
14136 suggest_T *p1 = (suggest_T *)s1;
14137 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014138 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014139
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014140 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014141 {
14142 n = p1->st_altscore - p2->st_altscore;
14143 if (n == 0)
14144 n = STRICMP(p1->st_word, p2->st_word);
14145 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014146 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147}
14148
14149/*
14150 * Cleanup the suggestions:
14151 * - Sort on score.
14152 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014153 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014155 static int
14156cleanup_suggestions(gap, maxscore, keep)
14157 garray_T *gap;
14158 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014159 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014161 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014162 int i;
14163
14164 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014165 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166
14167 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014168 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014170 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014172 gap->ga_len = keep;
14173 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014175 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176}
14177
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014178#if defined(FEAT_EVAL) || defined(PROTO)
14179/*
14180 * Soundfold a string, for soundfold().
14181 * Result is in allocated memory, NULL for an error.
14182 */
14183 char_u *
14184eval_soundfold(word)
14185 char_u *word;
14186{
14187 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014188 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014189 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014190
Bram Moolenaar860cae12010-06-05 23:22:07 +020014191 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014192 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014193 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014194 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014195 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014196 if (lp->lp_slang->sl_sal.ga_len > 0)
14197 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014198 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014199 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014200 return vim_strsave(sound);
14201 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014202 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014203
14204 /* No language with sound folding, return word as-is. */
14205 return vim_strsave(word);
14206}
14207#endif
14208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209/*
14210 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014211 *
14212 * There are many ways to turn a word into a sound-a-like representation. The
14213 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14214 * swedish name matching - survey and test of different algorithms" by Klas
14215 * Erikson.
14216 *
14217 * We support two methods:
14218 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14219 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 */
14221 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014222spell_soundfold(slang, inword, folded, res)
14223 slang_T *slang;
14224 char_u *inword;
14225 int folded; /* "inword" is already case-folded */
14226 char_u *res;
14227{
14228 char_u fword[MAXWLEN];
14229 char_u *word;
14230
14231 if (slang->sl_sofo)
14232 /* SOFOFROM and SOFOTO used */
14233 spell_soundfold_sofo(slang, inword, res);
14234 else
14235 {
14236 /* SAL items used. Requires the word to be case-folded. */
14237 if (folded)
14238 word = inword;
14239 else
14240 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014241 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014242 word = fword;
14243 }
14244
14245#ifdef FEAT_MBYTE
14246 if (has_mbyte)
14247 spell_soundfold_wsal(slang, word, res);
14248 else
14249#endif
14250 spell_soundfold_sal(slang, word, res);
14251 }
14252}
14253
14254/*
14255 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14256 * SOFOTO lines.
14257 */
14258 static void
14259spell_soundfold_sofo(slang, inword, res)
14260 slang_T *slang;
14261 char_u *inword;
14262 char_u *res;
14263{
14264 char_u *s;
14265 int ri = 0;
14266 int c;
14267
14268#ifdef FEAT_MBYTE
14269 if (has_mbyte)
14270 {
14271 int prevc = 0;
14272 int *ip;
14273
14274 /* The sl_sal_first[] table contains the translation for chars up to
14275 * 255, sl_sal the rest. */
14276 for (s = inword; *s != NUL; )
14277 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014278 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014279 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14280 c = ' ';
14281 else if (c < 256)
14282 c = slang->sl_sal_first[c];
14283 else
14284 {
14285 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14286 if (ip == NULL) /* empty list, can't match */
14287 c = NUL;
14288 else
14289 for (;;) /* find "c" in the list */
14290 {
14291 if (*ip == 0) /* not found */
14292 {
14293 c = NUL;
14294 break;
14295 }
14296 if (*ip == c) /* match! */
14297 {
14298 c = ip[1];
14299 break;
14300 }
14301 ip += 2;
14302 }
14303 }
14304
14305 if (c != NUL && c != prevc)
14306 {
14307 ri += mb_char2bytes(c, res + ri);
14308 if (ri + MB_MAXBYTES > MAXWLEN)
14309 break;
14310 prevc = c;
14311 }
14312 }
14313 }
14314 else
14315#endif
14316 {
14317 /* The sl_sal_first[] table contains the translation. */
14318 for (s = inword; (c = *s) != NUL; ++s)
14319 {
14320 if (vim_iswhite(c))
14321 c = ' ';
14322 else
14323 c = slang->sl_sal_first[c];
14324 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14325 res[ri++] = c;
14326 }
14327 }
14328
14329 res[ri] = NUL;
14330}
14331
14332 static void
14333spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014334 slang_T *slang;
14335 char_u *inword;
14336 char_u *res;
14337{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014338 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014340 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014342 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014344 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 int n, k = 0;
14346 int z0;
14347 int k0;
14348 int n0;
14349 int c;
14350 int pri;
14351 int p0 = -333;
14352 int c0;
14353
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014354 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014355 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 if (slang->sl_rem_accents)
14357 {
14358 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014359 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014361 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014362 {
14363 *t++ = ' ';
14364 s = skipwhite(s);
14365 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014366 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014368 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 *t++ = *s;
14370 ++s;
14371 }
14372 }
14373 *t = NUL;
14374 }
14375 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014376 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014378 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379
14380 /*
14381 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014382 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014384 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 while ((c = word[i]) != NUL)
14386 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014387 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 n = slang->sl_sal_first[c];
14389 z0 = 0;
14390
14391 if (n >= 0)
14392 {
14393 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014394 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014396 /* Quickly skip entries that don't match the word. Most
14397 * entries are less then three chars, optimize for that. */
14398 k = smp[n].sm_leadlen;
14399 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014401 if (word[i + 1] != s[1])
14402 continue;
14403 if (k > 2)
14404 {
14405 for (j = 2; j < k; ++j)
14406 if (word[i + j] != s[j])
14407 break;
14408 if (j < k)
14409 continue;
14410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 }
14412
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014413 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014415 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014416 while (*pf != NUL && *pf != word[i + k])
14417 ++pf;
14418 if (*pf == NUL)
14419 continue;
14420 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014422 s = smp[n].sm_rules;
14423 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424
14425 p0 = *s;
14426 k0 = k;
14427 while (*s == '-' && k > 1)
14428 {
14429 k--;
14430 s++;
14431 }
14432 if (*s == '<')
14433 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014434 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 {
14436 /* determine priority */
14437 pri = *s - '0';
14438 s++;
14439 }
14440 if (*s == '^' && *(s + 1) == '^')
14441 s++;
14442
14443 if (*s == NUL
14444 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014445 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014446 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014447 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014448 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014450 && spell_iswordp(word + i - 1, curwin)
14451 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014452 {
14453 /* search for followup rules, if: */
14454 /* followup and k > 1 and NO '-' in searchstring */
14455 c0 = word[i + k - 1];
14456 n0 = slang->sl_sal_first[c0];
14457
14458 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014459 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014460 {
14461 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014462 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014463 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014464 /* Quickly skip entries that don't match the word.
14465 * */
14466 k0 = smp[n0].sm_leadlen;
14467 if (k0 > 1)
14468 {
14469 if (word[i + k] != s[1])
14470 continue;
14471 if (k0 > 2)
14472 {
14473 pf = word + i + k + 1;
14474 for (j = 2; j < k0; ++j)
14475 if (*pf++ != s[j])
14476 break;
14477 if (j < k0)
14478 continue;
14479 }
14480 }
14481 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014483 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014484 {
14485 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014486 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014487 while (*pf != NUL && *pf != word[i + k0])
14488 ++pf;
14489 if (*pf == NUL)
14490 continue;
14491 ++k0;
14492 }
14493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014495 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 while (*s == '-')
14497 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014498 /* "k0" gets NOT reduced because
14499 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 s++;
14501 }
14502 if (*s == '<')
14503 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014504 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 {
14506 p0 = *s - '0';
14507 s++;
14508 }
14509
14510 if (*s == NUL
14511 /* *s == '^' cuts */
14512 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014513 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014514 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 {
14516 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014517 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014518 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519
14520 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014523 /* rule fits; stop search */
14524 break;
14525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 }
14527
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014528 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014529 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 }
14531
14532 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014533 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014534 if (s == NULL)
14535 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014536 pf = smp[n].sm_rules;
14537 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014538 if (p0 == 1 && z == 0)
14539 {
14540 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014541 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14542 || res[reslen - 1] == *s))
14543 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 z0 = 1;
14545 z = 1;
14546 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014547 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 {
14549 word[i + k0] = *s;
14550 k0++;
14551 s++;
14552 }
14553 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014554 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555
14556 /* new "actual letter" */
14557 c = word[i];
14558 }
14559 else
14560 {
14561 /* no '<' rule used */
14562 i += k - 1;
14563 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014564 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014566 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014567 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 s++;
14569 }
14570 /* new "actual letter" */
14571 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014572 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 {
14574 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014575 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014576 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 i = 0;
14578 z0 = 1;
14579 }
14580 }
14581 break;
14582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014583 }
14584 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014585 else if (vim_iswhite(c))
14586 {
14587 c = ' ';
14588 k = 1;
14589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014590
14591 if (z0 == 0)
14592 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014593 if (k && !p0 && reslen < MAXWLEN && c != NUL
14594 && (!slang->sl_collapse || reslen == 0
14595 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014596 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014597 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598
14599 i++;
14600 z = 0;
14601 k = 0;
14602 }
14603 }
14604
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014605 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606}
14607
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014608#ifdef FEAT_MBYTE
14609/*
14610 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14611 * Multi-byte version of spell_soundfold().
14612 */
14613 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014614spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014615 slang_T *slang;
14616 char_u *inword;
14617 char_u *res;
14618{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014619 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014620 int word[MAXWLEN];
14621 int wres[MAXWLEN];
14622 int l;
14623 char_u *s;
14624 int *ws;
14625 char_u *t;
14626 int *pf;
14627 int i, j, z;
14628 int reslen;
14629 int n, k = 0;
14630 int z0;
14631 int k0;
14632 int n0;
14633 int c;
14634 int pri;
14635 int p0 = -333;
14636 int c0;
14637 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014638 int wordlen;
14639
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014640
14641 /*
14642 * Convert the multi-byte string to a wide-character string.
14643 * Remove accents, if wanted. We actually remove all non-word characters.
14644 * But keep white space.
14645 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014646 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014647 for (s = inword; *s != NUL; )
14648 {
14649 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014650 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014651 if (slang->sl_rem_accents)
14652 {
14653 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14654 {
14655 if (did_white)
14656 continue;
14657 c = ' ';
14658 did_white = TRUE;
14659 }
14660 else
14661 {
14662 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014663 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014664 continue;
14665 }
14666 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014667 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014668 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014669 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014670
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014671 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014672 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014673 * Converted from C++ to C. Added support for multi-byte chars.
14674 * Changed to keep spaces.
14675 */
14676 i = reslen = z = 0;
14677 while ((c = word[i]) != NUL)
14678 {
14679 /* Start with the first rule that has the character in the word. */
14680 n = slang->sl_sal_first[c & 0xff];
14681 z0 = 0;
14682
14683 if (n >= 0)
14684 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014685 /* Check all rules for the same index byte.
14686 * If c is 0x300 need extra check for the end of the array, as
14687 * (c & 0xff) is NUL. */
14688 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14689 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014690 {
14691 /* Quickly skip entries that don't match the word. Most
14692 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014693 if (c != ws[0])
14694 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014695 k = smp[n].sm_leadlen;
14696 if (k > 1)
14697 {
14698 if (word[i + 1] != ws[1])
14699 continue;
14700 if (k > 2)
14701 {
14702 for (j = 2; j < k; ++j)
14703 if (word[i + j] != ws[j])
14704 break;
14705 if (j < k)
14706 continue;
14707 }
14708 }
14709
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014710 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014711 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014712 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014713 while (*pf != NUL && *pf != word[i + k])
14714 ++pf;
14715 if (*pf == NUL)
14716 continue;
14717 ++k;
14718 }
14719 s = smp[n].sm_rules;
14720 pri = 5; /* default priority */
14721
14722 p0 = *s;
14723 k0 = k;
14724 while (*s == '-' && k > 1)
14725 {
14726 k--;
14727 s++;
14728 }
14729 if (*s == '<')
14730 s++;
14731 if (VIM_ISDIGIT(*s))
14732 {
14733 /* determine priority */
14734 pri = *s - '0';
14735 s++;
14736 }
14737 if (*s == '^' && *(s + 1) == '^')
14738 s++;
14739
14740 if (*s == NUL
14741 || (*s == '^'
14742 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014743 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014744 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014745 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014746 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014747 && spell_iswordp_w(word + i - 1, curwin)
14748 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014749 {
14750 /* search for followup rules, if: */
14751 /* followup and k > 1 and NO '-' in searchstring */
14752 c0 = word[i + k - 1];
14753 n0 = slang->sl_sal_first[c0 & 0xff];
14754
14755 if (slang->sl_followup && k > 1 && n0 >= 0
14756 && p0 != '-' && word[i + k] != NUL)
14757 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014758 /* Test follow-up rule for "word[i + k]"; loop over
14759 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014760 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14761 == (c0 & 0xff); ++n0)
14762 {
14763 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014764 */
14765 if (c0 != ws[0])
14766 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014767 k0 = smp[n0].sm_leadlen;
14768 if (k0 > 1)
14769 {
14770 if (word[i + k] != ws[1])
14771 continue;
14772 if (k0 > 2)
14773 {
14774 pf = word + i + k + 1;
14775 for (j = 2; j < k0; ++j)
14776 if (*pf++ != ws[j])
14777 break;
14778 if (j < k0)
14779 continue;
14780 }
14781 }
14782 k0 += k - 1;
14783
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014784 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014785 {
14786 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014787 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014788 while (*pf != NUL && *pf != word[i + k0])
14789 ++pf;
14790 if (*pf == NUL)
14791 continue;
14792 ++k0;
14793 }
14794
14795 p0 = 5;
14796 s = smp[n0].sm_rules;
14797 while (*s == '-')
14798 {
14799 /* "k0" gets NOT reduced because
14800 * "if (k0 == k)" */
14801 s++;
14802 }
14803 if (*s == '<')
14804 s++;
14805 if (VIM_ISDIGIT(*s))
14806 {
14807 p0 = *s - '0';
14808 s++;
14809 }
14810
14811 if (*s == NUL
14812 /* *s == '^' cuts */
14813 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014814 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014815 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014816 {
14817 if (k0 == k)
14818 /* this is just a piece of the string */
14819 continue;
14820
14821 if (p0 < pri)
14822 /* priority too low */
14823 continue;
14824 /* rule fits; stop search */
14825 break;
14826 }
14827 }
14828
14829 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14830 == (c0 & 0xff))
14831 continue;
14832 }
14833
14834 /* replace string */
14835 ws = smp[n].sm_to_w;
14836 s = smp[n].sm_rules;
14837 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14838 if (p0 == 1 && z == 0)
14839 {
14840 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014841 if (reslen > 0 && ws != NULL && *ws != NUL
14842 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014843 || wres[reslen - 1] == *ws))
14844 reslen--;
14845 z0 = 1;
14846 z = 1;
14847 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014848 if (ws != NULL)
14849 while (*ws != NUL && word[i + k0] != NUL)
14850 {
14851 word[i + k0] = *ws;
14852 k0++;
14853 ws++;
14854 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014855 if (k > k0)
14856 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014857 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014858
14859 /* new "actual letter" */
14860 c = word[i];
14861 }
14862 else
14863 {
14864 /* no '<' rule used */
14865 i += k - 1;
14866 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014867 if (ws != NULL)
14868 while (*ws != NUL && ws[1] != NUL
14869 && reslen < MAXWLEN)
14870 {
14871 if (reslen == 0 || wres[reslen - 1] != *ws)
14872 wres[reslen++] = *ws;
14873 ws++;
14874 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014875 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014876 if (ws == NULL)
14877 c = NUL;
14878 else
14879 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014880 if (strstr((char *)s, "^^") != NULL)
14881 {
14882 if (c != NUL)
14883 wres[reslen++] = c;
14884 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014885 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014886 i = 0;
14887 z0 = 1;
14888 }
14889 }
14890 break;
14891 }
14892 }
14893 }
14894 else if (vim_iswhite(c))
14895 {
14896 c = ' ';
14897 k = 1;
14898 }
14899
14900 if (z0 == 0)
14901 {
14902 if (k && !p0 && reslen < MAXWLEN && c != NUL
14903 && (!slang->sl_collapse || reslen == 0
14904 || wres[reslen - 1] != c))
14905 /* condense only double letters */
14906 wres[reslen++] = c;
14907
14908 i++;
14909 z = 0;
14910 k = 0;
14911 }
14912 }
14913
14914 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14915 l = 0;
14916 for (n = 0; n < reslen; ++n)
14917 {
14918 l += mb_char2bytes(wres[n], res + l);
14919 if (l + MB_MAXBYTES > MAXWLEN)
14920 break;
14921 }
14922 res[l] = NUL;
14923}
14924#endif
14925
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014926/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014927 * Compute a score for two sound-a-like words.
14928 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14929 * Instead of a generic loop we write out the code. That keeps it fast by
14930 * avoiding checks that will not be possible.
14931 */
14932 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014933soundalike_score(goodstart, badstart)
14934 char_u *goodstart; /* sound-folded good word */
14935 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014936{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014937 char_u *goodsound = goodstart;
14938 char_u *badsound = badstart;
14939 int goodlen;
14940 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014941 int n;
14942 char_u *pl, *ps;
14943 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014944 int score = 0;
14945
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014946 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014947 * counted so much, vowels halfway the word aren't counted at all. */
14948 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14949 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014950 if ((badsound[0] == NUL && goodsound[1] == NUL)
14951 || (goodsound[0] == NUL && badsound[1] == NUL))
14952 /* changing word with vowel to word without a sound */
14953 return SCORE_DEL;
14954 if (badsound[0] == NUL || goodsound[0] == NUL)
14955 /* more than two changes */
14956 return SCORE_MAXMAX;
14957
Bram Moolenaar4770d092006-01-12 23:22:24 +000014958 if (badsound[1] == goodsound[1]
14959 || (badsound[1] != NUL
14960 && goodsound[1] != NUL
14961 && badsound[2] == goodsound[2]))
14962 {
14963 /* handle like a substitute */
14964 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014965 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014966 {
14967 score = 2 * SCORE_DEL / 3;
14968 if (*badsound == '*')
14969 ++badsound;
14970 else
14971 ++goodsound;
14972 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014973 }
14974
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014975 goodlen = (int)STRLEN(goodsound);
14976 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014977
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014978 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014979 * changes. */
14980 n = goodlen - badlen;
14981 if (n < -2 || n > 2)
14982 return SCORE_MAXMAX;
14983
14984 if (n > 0)
14985 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014986 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014987 ps = badsound;
14988 }
14989 else
14990 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014991 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014992 ps = goodsound;
14993 }
14994
14995 /* Skip over the identical part. */
14996 while (*pl == *ps && *pl != NUL)
14997 {
14998 ++pl;
14999 ++ps;
15000 }
15001
15002 switch (n)
15003 {
15004 case -2:
15005 case 2:
15006 /*
15007 * Must delete two characters from "pl".
15008 */
15009 ++pl; /* first delete */
15010 while (*pl == *ps)
15011 {
15012 ++pl;
15013 ++ps;
15014 }
15015 /* strings must be equal after second delete */
15016 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015017 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015018
15019 /* Failed to compare. */
15020 break;
15021
15022 case -1:
15023 case 1:
15024 /*
15025 * Minimal one delete from "pl" required.
15026 */
15027
15028 /* 1: delete */
15029 pl2 = pl + 1;
15030 ps2 = ps;
15031 while (*pl2 == *ps2)
15032 {
15033 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015034 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015035 ++pl2;
15036 ++ps2;
15037 }
15038
15039 /* 2: delete then swap, then rest must be equal */
15040 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
15041 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015042 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015043
15044 /* 3: delete then substitute, then the rest must be equal */
15045 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015046 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015047
15048 /* 4: first swap then delete */
15049 if (pl[0] == ps[1] && pl[1] == ps[0])
15050 {
15051 pl2 = pl + 2; /* swap, skip two chars */
15052 ps2 = ps + 2;
15053 while (*pl2 == *ps2)
15054 {
15055 ++pl2;
15056 ++ps2;
15057 }
15058 /* delete a char and then strings must be equal */
15059 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015060 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015061 }
15062
15063 /* 5: first substitute then delete */
15064 pl2 = pl + 1; /* substitute, skip one char */
15065 ps2 = ps + 1;
15066 while (*pl2 == *ps2)
15067 {
15068 ++pl2;
15069 ++ps2;
15070 }
15071 /* delete a char and then strings must be equal */
15072 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015073 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015074
15075 /* Failed to compare. */
15076 break;
15077
15078 case 0:
15079 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000015080 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015081 * insert is only possible in combination with a delete.
15082 * 1: check if for identical strings
15083 */
15084 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015085 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015086
15087 /* 2: swap */
15088 if (pl[0] == ps[1] && pl[1] == ps[0])
15089 {
15090 pl2 = pl + 2; /* swap, skip two chars */
15091 ps2 = ps + 2;
15092 while (*pl2 == *ps2)
15093 {
15094 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015095 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015096 ++pl2;
15097 ++ps2;
15098 }
15099 /* 3: swap and swap again */
15100 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
15101 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015102 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015103
15104 /* 4: swap and substitute */
15105 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015106 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015107 }
15108
15109 /* 5: substitute */
15110 pl2 = pl + 1;
15111 ps2 = ps + 1;
15112 while (*pl2 == *ps2)
15113 {
15114 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015115 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015116 ++pl2;
15117 ++ps2;
15118 }
15119
15120 /* 6: substitute and swap */
15121 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
15122 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015123 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015124
15125 /* 7: substitute and substitute */
15126 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015127 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015128
15129 /* 8: insert then delete */
15130 pl2 = pl;
15131 ps2 = ps + 1;
15132 while (*pl2 == *ps2)
15133 {
15134 ++pl2;
15135 ++ps2;
15136 }
15137 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015138 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015139
15140 /* 9: delete then insert */
15141 pl2 = pl + 1;
15142 ps2 = ps;
15143 while (*pl2 == *ps2)
15144 {
15145 ++pl2;
15146 ++ps2;
15147 }
15148 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015149 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015150
15151 /* Failed to compare. */
15152 break;
15153 }
15154
15155 return SCORE_MAXMAX;
15156}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015158/*
15159 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015160 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015161 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015162 * The algorithm is described by Du and Chang, 1992.
15163 * The implementation of the algorithm comes from Aspell editdist.cpp,
15164 * edit_distance(). It has been converted from C++ to C and modified to
15165 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 */
15167 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000015168spell_edit_score(slang, badword, goodword)
15169 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 char_u *badword;
15171 char_u *goodword;
15172{
15173 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015174 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015175 int j, i;
15176 int t;
15177 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015178 int pbc, pgc;
15179#ifdef FEAT_MBYTE
15180 char_u *p;
15181 int wbadword[MAXWLEN];
15182 int wgoodword[MAXWLEN];
15183
15184 if (has_mbyte)
15185 {
15186 /* Get the characters from the multi-byte strings and put them in an
15187 * int array for easy access. */
15188 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015189 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015190 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015191 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015192 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015193 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015194 }
15195 else
15196#endif
15197 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015198 badlen = (int)STRLEN(badword) + 1;
15199 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015201
15202 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15203#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015204 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15205 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015206 if (cnt == NULL)
15207 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015208
15209 CNT(0, 0) = 0;
15210 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015211 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212
15213 for (i = 1; i <= badlen; ++i)
15214 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015215 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 for (j = 1; j <= goodlen; ++j)
15217 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015218#ifdef FEAT_MBYTE
15219 if (has_mbyte)
15220 {
15221 bc = wbadword[i - 1];
15222 gc = wgoodword[j - 1];
15223 }
15224 else
15225#endif
15226 {
15227 bc = badword[i - 1];
15228 gc = goodword[j - 1];
15229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 if (bc == gc)
15231 CNT(i, j) = CNT(i - 1, j - 1);
15232 else
15233 {
15234 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015235 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015236 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15237 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015238 {
15239 /* For a similar character use SCORE_SIMILAR. */
15240 if (slang != NULL
15241 && slang->sl_has_map
15242 && similar_chars(slang, gc, bc))
15243 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15244 else
15245 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015248 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015250#ifdef FEAT_MBYTE
15251 if (has_mbyte)
15252 {
15253 pbc = wbadword[i - 2];
15254 pgc = wgoodword[j - 2];
15255 }
15256 else
15257#endif
15258 {
15259 pbc = badword[i - 2];
15260 pgc = goodword[j - 2];
15261 }
15262 if (bc == pgc && pbc == gc)
15263 {
15264 t = SCORE_SWAP + CNT(i - 2, j - 2);
15265 if (t < CNT(i, j))
15266 CNT(i, j) = t;
15267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015268 }
15269 t = SCORE_DEL + CNT(i - 1, j);
15270 if (t < CNT(i, j))
15271 CNT(i, j) = t;
15272 t = SCORE_INS + CNT(i, j - 1);
15273 if (t < CNT(i, j))
15274 CNT(i, j) = t;
15275 }
15276 }
15277 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015278
15279 i = CNT(badlen - 1, goodlen - 1);
15280 vim_free(cnt);
15281 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015283
Bram Moolenaar4770d092006-01-12 23:22:24 +000015284typedef struct
15285{
15286 int badi;
15287 int goodi;
15288 int score;
15289} limitscore_T;
15290
15291/*
15292 * Like spell_edit_score(), but with a limit on the score to make it faster.
15293 * May return SCORE_MAXMAX when the score is higher than "limit".
15294 *
15295 * This uses a stack for the edits still to be tried.
15296 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15297 * for multi-byte characters.
15298 */
15299 static int
15300spell_edit_score_limit(slang, badword, goodword, limit)
15301 slang_T *slang;
15302 char_u *badword;
15303 char_u *goodword;
15304 int limit;
15305{
15306 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15307 int stackidx;
15308 int bi, gi;
15309 int bi2, gi2;
15310 int bc, gc;
15311 int score;
15312 int score_off;
15313 int minscore;
15314 int round;
15315
15316#ifdef FEAT_MBYTE
15317 /* Multi-byte characters require a bit more work, use a different function
15318 * to avoid testing "has_mbyte" quite often. */
15319 if (has_mbyte)
15320 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15321#endif
15322
15323 /*
15324 * The idea is to go from start to end over the words. So long as
15325 * characters are equal just continue, this always gives the lowest score.
15326 * When there is a difference try several alternatives. Each alternative
15327 * increases "score" for the edit distance. Some of the alternatives are
15328 * pushed unto a stack and tried later, some are tried right away. At the
15329 * end of the word the score for one alternative is known. The lowest
15330 * possible score is stored in "minscore".
15331 */
15332 stackidx = 0;
15333 bi = 0;
15334 gi = 0;
15335 score = 0;
15336 minscore = limit + 1;
15337
15338 for (;;)
15339 {
15340 /* Skip over an equal part, score remains the same. */
15341 for (;;)
15342 {
15343 bc = badword[bi];
15344 gc = goodword[gi];
15345 if (bc != gc) /* stop at a char that's different */
15346 break;
15347 if (bc == NUL) /* both words end */
15348 {
15349 if (score < minscore)
15350 minscore = score;
15351 goto pop; /* do next alternative */
15352 }
15353 ++bi;
15354 ++gi;
15355 }
15356
15357 if (gc == NUL) /* goodword ends, delete badword chars */
15358 {
15359 do
15360 {
15361 if ((score += SCORE_DEL) >= minscore)
15362 goto pop; /* do next alternative */
15363 } while (badword[++bi] != NUL);
15364 minscore = score;
15365 }
15366 else if (bc == NUL) /* badword ends, insert badword chars */
15367 {
15368 do
15369 {
15370 if ((score += SCORE_INS) >= minscore)
15371 goto pop; /* do next alternative */
15372 } while (goodword[++gi] != NUL);
15373 minscore = score;
15374 }
15375 else /* both words continue */
15376 {
15377 /* If not close to the limit, perform a change. Only try changes
15378 * that may lead to a lower score than "minscore".
15379 * round 0: try deleting a char from badword
15380 * round 1: try inserting a char in badword */
15381 for (round = 0; round <= 1; ++round)
15382 {
15383 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15384 if (score_off < minscore)
15385 {
15386 if (score_off + SCORE_EDIT_MIN >= minscore)
15387 {
15388 /* Near the limit, rest of the words must match. We
15389 * can check that right now, no need to push an item
15390 * onto the stack. */
15391 bi2 = bi + 1 - round;
15392 gi2 = gi + round;
15393 while (goodword[gi2] == badword[bi2])
15394 {
15395 if (goodword[gi2] == NUL)
15396 {
15397 minscore = score_off;
15398 break;
15399 }
15400 ++bi2;
15401 ++gi2;
15402 }
15403 }
15404 else
15405 {
15406 /* try deleting/inserting a character later */
15407 stack[stackidx].badi = bi + 1 - round;
15408 stack[stackidx].goodi = gi + round;
15409 stack[stackidx].score = score_off;
15410 ++stackidx;
15411 }
15412 }
15413 }
15414
15415 if (score + SCORE_SWAP < minscore)
15416 {
15417 /* If swapping two characters makes a match then the
15418 * substitution is more expensive, thus there is no need to
15419 * try both. */
15420 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15421 {
15422 /* Swap two characters, that is: skip them. */
15423 gi += 2;
15424 bi += 2;
15425 score += SCORE_SWAP;
15426 continue;
15427 }
15428 }
15429
15430 /* Substitute one character for another which is the same
15431 * thing as deleting a character from both goodword and badword.
15432 * Use a better score when there is only a case difference. */
15433 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15434 score += SCORE_ICASE;
15435 else
15436 {
15437 /* For a similar character use SCORE_SIMILAR. */
15438 if (slang != NULL
15439 && slang->sl_has_map
15440 && similar_chars(slang, gc, bc))
15441 score += SCORE_SIMILAR;
15442 else
15443 score += SCORE_SUBST;
15444 }
15445
15446 if (score < minscore)
15447 {
15448 /* Do the substitution. */
15449 ++gi;
15450 ++bi;
15451 continue;
15452 }
15453 }
15454pop:
15455 /*
15456 * Get here to try the next alternative, pop it from the stack.
15457 */
15458 if (stackidx == 0) /* stack is empty, finished */
15459 break;
15460
15461 /* pop an item from the stack */
15462 --stackidx;
15463 gi = stack[stackidx].goodi;
15464 bi = stack[stackidx].badi;
15465 score = stack[stackidx].score;
15466 }
15467
15468 /* When the score goes over "limit" it may actually be much higher.
15469 * Return a very large number to avoid going below the limit when giving a
15470 * bonus. */
15471 if (minscore > limit)
15472 return SCORE_MAXMAX;
15473 return minscore;
15474}
15475
15476#ifdef FEAT_MBYTE
15477/*
15478 * Multi-byte version of spell_edit_score_limit().
15479 * Keep it in sync with the above!
15480 */
15481 static int
15482spell_edit_score_limit_w(slang, badword, goodword, limit)
15483 slang_T *slang;
15484 char_u *badword;
15485 char_u *goodword;
15486 int limit;
15487{
15488 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15489 int stackidx;
15490 int bi, gi;
15491 int bi2, gi2;
15492 int bc, gc;
15493 int score;
15494 int score_off;
15495 int minscore;
15496 int round;
15497 char_u *p;
15498 int wbadword[MAXWLEN];
15499 int wgoodword[MAXWLEN];
15500
15501 /* Get the characters from the multi-byte strings and put them in an
15502 * int array for easy access. */
15503 bi = 0;
15504 for (p = badword; *p != NUL; )
15505 wbadword[bi++] = mb_cptr2char_adv(&p);
15506 wbadword[bi++] = 0;
15507 gi = 0;
15508 for (p = goodword; *p != NUL; )
15509 wgoodword[gi++] = mb_cptr2char_adv(&p);
15510 wgoodword[gi++] = 0;
15511
15512 /*
15513 * The idea is to go from start to end over the words. So long as
15514 * characters are equal just continue, this always gives the lowest score.
15515 * When there is a difference try several alternatives. Each alternative
15516 * increases "score" for the edit distance. Some of the alternatives are
15517 * pushed unto a stack and tried later, some are tried right away. At the
15518 * end of the word the score for one alternative is known. The lowest
15519 * possible score is stored in "minscore".
15520 */
15521 stackidx = 0;
15522 bi = 0;
15523 gi = 0;
15524 score = 0;
15525 minscore = limit + 1;
15526
15527 for (;;)
15528 {
15529 /* Skip over an equal part, score remains the same. */
15530 for (;;)
15531 {
15532 bc = wbadword[bi];
15533 gc = wgoodword[gi];
15534
15535 if (bc != gc) /* stop at a char that's different */
15536 break;
15537 if (bc == NUL) /* both words end */
15538 {
15539 if (score < minscore)
15540 minscore = score;
15541 goto pop; /* do next alternative */
15542 }
15543 ++bi;
15544 ++gi;
15545 }
15546
15547 if (gc == NUL) /* goodword ends, delete badword chars */
15548 {
15549 do
15550 {
15551 if ((score += SCORE_DEL) >= minscore)
15552 goto pop; /* do next alternative */
15553 } while (wbadword[++bi] != NUL);
15554 minscore = score;
15555 }
15556 else if (bc == NUL) /* badword ends, insert badword chars */
15557 {
15558 do
15559 {
15560 if ((score += SCORE_INS) >= minscore)
15561 goto pop; /* do next alternative */
15562 } while (wgoodword[++gi] != NUL);
15563 minscore = score;
15564 }
15565 else /* both words continue */
15566 {
15567 /* If not close to the limit, perform a change. Only try changes
15568 * that may lead to a lower score than "minscore".
15569 * round 0: try deleting a char from badword
15570 * round 1: try inserting a char in badword */
15571 for (round = 0; round <= 1; ++round)
15572 {
15573 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15574 if (score_off < minscore)
15575 {
15576 if (score_off + SCORE_EDIT_MIN >= minscore)
15577 {
15578 /* Near the limit, rest of the words must match. We
15579 * can check that right now, no need to push an item
15580 * onto the stack. */
15581 bi2 = bi + 1 - round;
15582 gi2 = gi + round;
15583 while (wgoodword[gi2] == wbadword[bi2])
15584 {
15585 if (wgoodword[gi2] == NUL)
15586 {
15587 minscore = score_off;
15588 break;
15589 }
15590 ++bi2;
15591 ++gi2;
15592 }
15593 }
15594 else
15595 {
15596 /* try deleting a character from badword later */
15597 stack[stackidx].badi = bi + 1 - round;
15598 stack[stackidx].goodi = gi + round;
15599 stack[stackidx].score = score_off;
15600 ++stackidx;
15601 }
15602 }
15603 }
15604
15605 if (score + SCORE_SWAP < minscore)
15606 {
15607 /* If swapping two characters makes a match then the
15608 * substitution is more expensive, thus there is no need to
15609 * try both. */
15610 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15611 {
15612 /* Swap two characters, that is: skip them. */
15613 gi += 2;
15614 bi += 2;
15615 score += SCORE_SWAP;
15616 continue;
15617 }
15618 }
15619
15620 /* Substitute one character for another which is the same
15621 * thing as deleting a character from both goodword and badword.
15622 * Use a better score when there is only a case difference. */
15623 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15624 score += SCORE_ICASE;
15625 else
15626 {
15627 /* For a similar character use SCORE_SIMILAR. */
15628 if (slang != NULL
15629 && slang->sl_has_map
15630 && similar_chars(slang, gc, bc))
15631 score += SCORE_SIMILAR;
15632 else
15633 score += SCORE_SUBST;
15634 }
15635
15636 if (score < minscore)
15637 {
15638 /* Do the substitution. */
15639 ++gi;
15640 ++bi;
15641 continue;
15642 }
15643 }
15644pop:
15645 /*
15646 * Get here to try the next alternative, pop it from the stack.
15647 */
15648 if (stackidx == 0) /* stack is empty, finished */
15649 break;
15650
15651 /* pop an item from the stack */
15652 --stackidx;
15653 gi = stack[stackidx].goodi;
15654 bi = stack[stackidx].badi;
15655 score = stack[stackidx].score;
15656 }
15657
15658 /* When the score goes over "limit" it may actually be much higher.
15659 * Return a very large number to avoid going below the limit when giving a
15660 * bonus. */
15661 if (minscore > limit)
15662 return SCORE_MAXMAX;
15663 return minscore;
15664}
15665#endif
15666
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015667/*
15668 * ":spellinfo"
15669 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015670 void
15671ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015672 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015673{
15674 int lpi;
15675 langp_T *lp;
15676 char_u *p;
15677
15678 if (no_spell_checking(curwin))
15679 return;
15680
15681 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015682 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015684 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015685 msg_puts((char_u *)"file: ");
15686 msg_puts(lp->lp_slang->sl_fname);
15687 msg_putchar('\n');
15688 p = lp->lp_slang->sl_info;
15689 if (p != NULL)
15690 {
15691 msg_puts(p);
15692 msg_putchar('\n');
15693 }
15694 }
15695 msg_end();
15696}
15697
Bram Moolenaar4770d092006-01-12 23:22:24 +000015698#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15699#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015700#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015701#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15702#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015703
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015704/*
15705 * ":spelldump"
15706 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015707 void
15708ex_spelldump(eap)
15709 exarg_T *eap;
15710{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015711 char_u *spl;
15712 long dummy;
15713
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015714 if (no_spell_checking(curwin))
15715 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015716 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015717
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015718 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015719 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015720
15721 /* enable spelling locally in the new window */
15722 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010015723 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015724 vim_free(spl);
15725
Bram Moolenaar860cae12010-06-05 23:22:07 +020015726 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015727 return;
15728
Bram Moolenaar860cae12010-06-05 23:22:07 +020015729 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015730
15731 /* Delete the empty line that we started with. */
15732 if (curbuf->b_ml.ml_line_count > 1)
15733 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15734
15735 redraw_later(NOT_VALID);
15736}
15737
15738/*
15739 * Go through all possible words and:
15740 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15741 * "ic" and "dir" are not used.
15742 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15743 */
15744 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015745spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015746 char_u *pat; /* leading part of the word */
15747 int ic; /* ignore case */
15748 int *dir; /* direction for adding matches */
15749 int dumpflags_arg; /* DUMPFLAG_* */
15750{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015751 langp_T *lp;
15752 slang_T *slang;
15753 idx_T arridx[MAXWLEN];
15754 int curi[MAXWLEN];
15755 char_u word[MAXWLEN];
15756 int c;
15757 char_u *byts;
15758 idx_T *idxs;
15759 linenr_T lnum = 0;
15760 int round;
15761 int depth;
15762 int n;
15763 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015764 char_u *region_names = NULL; /* region names being used */
15765 int do_region = TRUE; /* dump region names and numbers */
15766 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015767 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015768 int dumpflags = dumpflags_arg;
15769 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015770
Bram Moolenaard0131a82006-03-04 21:46:13 +000015771 /* When ignoring case or when the pattern starts with capital pass this on
15772 * to dump_word(). */
15773 if (pat != NULL)
15774 {
15775 if (ic)
15776 dumpflags |= DUMPFLAG_ICASE;
15777 else
15778 {
15779 n = captype(pat, NULL);
15780 if (n == WF_ONECAP)
15781 dumpflags |= DUMPFLAG_ONECAP;
15782 else if (n == WF_ALLCAP
15783#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015784 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015785#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015786 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015787#endif
15788 )
15789 dumpflags |= DUMPFLAG_ALLCAP;
15790 }
15791 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015792
Bram Moolenaar7887d882005-07-01 22:33:52 +000015793 /* Find out if we can support regions: All languages must support the same
15794 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015795 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015796 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015797 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015798 p = lp->lp_slang->sl_regions;
15799 if (p[0] != 0)
15800 {
15801 if (region_names == NULL) /* first language with regions */
15802 region_names = p;
15803 else if (STRCMP(region_names, p) != 0)
15804 {
15805 do_region = FALSE; /* region names are different */
15806 break;
15807 }
15808 }
15809 }
15810
15811 if (do_region && region_names != NULL)
15812 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015813 if (pat == NULL)
15814 {
15815 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15816 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15817 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015818 }
15819 else
15820 do_region = FALSE;
15821
15822 /*
15823 * Loop over all files loaded for the entries in 'spelllang'.
15824 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015825 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015826 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015827 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015828 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015829 if (slang->sl_fbyts == NULL) /* reloading failed */
15830 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015831
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015832 if (pat == NULL)
15833 {
15834 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15835 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15836 }
15837
15838 /* When matching with a pattern and there are no prefixes only use
15839 * parts of the tree that match "pat". */
15840 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015841 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015842 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015843 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015844
15845 /* round 1: case-folded tree
15846 * round 2: keep-case tree */
15847 for (round = 1; round <= 2; ++round)
15848 {
15849 if (round == 1)
15850 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015851 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015852 byts = slang->sl_fbyts;
15853 idxs = slang->sl_fidxs;
15854 }
15855 else
15856 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015857 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015858 byts = slang->sl_kbyts;
15859 idxs = slang->sl_kidxs;
15860 }
15861 if (byts == NULL)
15862 continue; /* array is empty */
15863
15864 depth = 0;
15865 arridx[0] = 0;
15866 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015867 while (depth >= 0 && !got_int
15868 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015869 {
15870 if (curi[depth] > byts[arridx[depth]])
15871 {
15872 /* Done all bytes at this node, go up one level. */
15873 --depth;
15874 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015875 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015876 }
15877 else
15878 {
15879 /* Do one more byte at this node. */
15880 n = arridx[depth] + curi[depth];
15881 ++curi[depth];
15882 c = byts[n];
15883 if (c == 0)
15884 {
15885 /* End of word, deal with the word.
15886 * Don't use keep-case words in the fold-case tree,
15887 * they will appear in the keep-case tree.
15888 * Only use the word when the region matches. */
15889 flags = (int)idxs[n];
15890 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015891 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015892 && (do_region
15893 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015894 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015895 & lp->lp_region) != 0))
15896 {
15897 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015898 if (!do_region)
15899 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015900
15901 /* Dump the basic word if there is no prefix or
15902 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015903 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015904 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015905 {
15906 dump_word(slang, word, pat, dir,
15907 dumpflags, flags, lnum);
15908 if (pat == NULL)
15909 ++lnum;
15910 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015911
15912 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015913 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015914 lnum = dump_prefixes(slang, word, pat, dir,
15915 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015916 }
15917 }
15918 else
15919 {
15920 /* Normal char, go one level deeper. */
15921 word[depth++] = c;
15922 arridx[depth] = idxs[n];
15923 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015924
15925 /* Check if this characters matches with the pattern.
15926 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015927 * Always ignore case here, dump_word() will check
15928 * proper case later. This isn't exactly right when
15929 * length changes for multi-byte characters with
15930 * ignore case... */
15931 if (depth <= patlen
15932 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015933 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015934 }
15935 }
15936 }
15937 }
15938 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015939}
15940
15941/*
15942 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015943 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015944 */
15945 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015946dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015947 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015948 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015949 char_u *pat;
15950 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015951 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015952 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015953 linenr_T lnum;
15954{
15955 int keepcap = FALSE;
15956 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015957 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015958 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015959 char_u badword[MAXWLEN + 10];
15960 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015961 int flags = wordflags;
15962
15963 if (dumpflags & DUMPFLAG_ONECAP)
15964 flags |= WF_ONECAP;
15965 if (dumpflags & DUMPFLAG_ALLCAP)
15966 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015967
Bram Moolenaar4770d092006-01-12 23:22:24 +000015968 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015969 {
15970 /* Need to fix case according to "flags". */
15971 make_case_word(word, cword, flags);
15972 p = cword;
15973 }
15974 else
15975 {
15976 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015977 if ((dumpflags & DUMPFLAG_KEEPCASE)
15978 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015979 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015980 keepcap = TRUE;
15981 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015982 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015983
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015984 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015985 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015986 /* Add flags and regions after a slash. */
15987 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015988 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015989 STRCPY(badword, p);
15990 STRCAT(badword, "/");
15991 if (keepcap)
15992 STRCAT(badword, "=");
15993 if (flags & WF_BANNED)
15994 STRCAT(badword, "!");
15995 else if (flags & WF_RARE)
15996 STRCAT(badword, "?");
15997 if (flags & WF_REGION)
15998 for (i = 0; i < 7; ++i)
15999 if (flags & (0x10000 << i))
16000 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
16001 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000016002 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000016003
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016004 if (dumpflags & DUMPFLAG_COUNT)
16005 {
16006 hashitem_T *hi;
16007
16008 /* Include the word count for ":spelldump!". */
16009 hi = hash_find(&slang->sl_wordcount, tw);
16010 if (!HASHITEM_EMPTY(hi))
16011 {
16012 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
16013 tw, HI2WC(hi)->wc_count);
16014 p = IObuff;
16015 }
16016 }
16017
16018 ml_append(lnum, p, (colnr_T)0, FALSE);
16019 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000016020 else if (((dumpflags & DUMPFLAG_ICASE)
16021 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
16022 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016023 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000016024 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000016025 /* if dir was BACKWARD then honor it just once */
16026 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016027}
16028
16029/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000016030 * For ":spelldump": Find matching prefixes for "word". Prepend each to
16031 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016032 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016033 * Return the updated line number.
16034 */
16035 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016036dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016037 slang_T *slang;
16038 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016039 char_u *pat;
16040 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000016041 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016042 int flags; /* flags with prefix ID */
16043 linenr_T startlnum;
16044{
16045 idx_T arridx[MAXWLEN];
16046 int curi[MAXWLEN];
16047 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000016048 char_u word_up[MAXWLEN];
16049 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016050 int c;
16051 char_u *byts;
16052 idx_T *idxs;
16053 linenr_T lnum = startlnum;
16054 int depth;
16055 int n;
16056 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016057 int i;
16058
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016059 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000016060 * upper-case letter in word_up[]. */
16061 c = PTR2CHAR(word);
16062 if (SPELL_TOUPPER(c) != c)
16063 {
16064 onecap_copy(word, word_up, TRUE);
16065 has_word_up = TRUE;
16066 }
16067
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016068 byts = slang->sl_pbyts;
16069 idxs = slang->sl_pidxs;
16070 if (byts != NULL) /* array not is empty */
16071 {
16072 /*
16073 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000016074 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016075 */
16076 depth = 0;
16077 arridx[0] = 0;
16078 curi[0] = 1;
16079 while (depth >= 0 && !got_int)
16080 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000016081 n = arridx[depth];
16082 len = byts[n];
16083 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016084 {
16085 /* Done all bytes at this node, go up one level. */
16086 --depth;
16087 line_breakcheck();
16088 }
16089 else
16090 {
16091 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000016092 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016093 ++curi[depth];
16094 c = byts[n];
16095 if (c == 0)
16096 {
16097 /* End of prefix, find out how many IDs there are. */
16098 for (i = 1; i < len; ++i)
16099 if (byts[n + i] != 0)
16100 break;
16101 curi[depth] += i - 1;
16102
Bram Moolenaar53805d12005-08-01 07:08:33 +000016103 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
16104 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016105 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000016106 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016107 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000016108 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016109 : flags, lnum);
16110 if (lnum != 0)
16111 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016112 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000016113
16114 /* Check for prefix that matches the word when the
16115 * first letter is upper-case, but only if the prefix has
16116 * a condition. */
16117 if (has_word_up)
16118 {
16119 c = valid_word_prefix(i, n, flags, word_up, slang,
16120 TRUE);
16121 if (c != 0)
16122 {
16123 vim_strncpy(prefix + depth, word_up,
16124 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016125 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000016126 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016127 : flags, lnum);
16128 if (lnum != 0)
16129 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000016130 }
16131 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016132 }
16133 else
16134 {
16135 /* Normal char, go one level deeper. */
16136 prefix[depth++] = c;
16137 arridx[depth] = idxs[n];
16138 curi[depth] = 1;
16139 }
16140 }
16141 }
16142 }
16143
16144 return lnum;
16145}
16146
Bram Moolenaar95529562005-08-25 21:21:38 +000016147/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016148 * Move "p" to the end of word "start".
16149 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000016150 */
16151 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020016152spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000016153 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020016154 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000016155{
16156 char_u *p = start;
16157
Bram Moolenaar860cae12010-06-05 23:22:07 +020016158 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000016159 mb_ptr_adv(p);
16160 return p;
16161}
16162
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016163#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016164/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016165 * For Insert mode completion CTRL-X s:
16166 * Find start of the word in front of column "startcol".
16167 * We don't check if it is badly spelled, with completion we can only change
16168 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016169 * Returns the column number of the word.
16170 */
16171 int
16172spell_word_start(startcol)
16173 int startcol;
16174{
16175 char_u *line;
16176 char_u *p;
16177 int col = 0;
16178
Bram Moolenaar95529562005-08-25 21:21:38 +000016179 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016180 return startcol;
16181
16182 /* Find a word character before "startcol". */
16183 line = ml_get_curline();
16184 for (p = line + startcol; p > line; )
16185 {
16186 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010016187 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016188 break;
16189 }
16190
16191 /* Go back to start of the word. */
16192 while (p > line)
16193 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016194 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016195 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016196 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016197 break;
16198 col = 0;
16199 }
16200
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016201 return col;
16202}
16203
16204/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016205 * Need to check for 'spellcapcheck' now, the word is removed before
16206 * expand_spelling() is called. Therefore the ugly global variable.
16207 */
16208static int spell_expand_need_cap;
16209
16210 void
16211spell_expand_check_cap(col)
16212 colnr_T col;
16213{
16214 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16215}
16216
16217/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016218 * Get list of spelling suggestions.
16219 * Used for Insert mode completion CTRL-X ?.
16220 * Returns the number of matches. The matches are in "matchp[]", array of
16221 * allocated strings.
16222 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016223 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016224expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016225 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016226 char_u *pat;
16227 char_u ***matchp;
16228{
16229 garray_T ga;
16230
Bram Moolenaar4770d092006-01-12 23:22:24 +000016231 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016232 *matchp = ga.ga_data;
16233 return ga.ga_len;
16234}
16235#endif
16236
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016237#endif /* FEAT_SPELL */