blob: af96891076da0269738354f8ea39094345e33ae3 [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 Moolenaar7b877b32016-01-09 13:51:34 +010062/* Use SPELL_COMPRESS_ALLWAYS for debugging: compress the word tree after
63 * adding a word. Only use it for small word lists! */
64#if 0
65# define SPELL_COMPRESS_ALLWAYS
66#endif
67
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000068/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
69 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000070#if 0
71# define DEBUG_TRIEWALK
72#endif
73
Bram Moolenaar51485f02005-06-04 21:55:20 +000074/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000075 * Use this to adjust the score after finding suggestions, based on the
76 * suggested word sounding like the bad word. This is much faster than doing
77 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000078 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
79 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000080 * Used when 'spellsuggest' is set to "best".
81 */
82#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
83
84/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000085 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000086 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000087 */
88#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
89
90/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000091 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000092 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000093 * <LWORDTREE>
94 * <KWORDTREE>
95 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000096 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000097 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000098 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000099 * <fileID> 8 bytes "VIMspell"
100 * <versionnr> 1 byte VIMSPELLVERSION
101 *
102 *
103 * Sections make it possible to add information to the .spl file without
104 * making it incompatible with previous versions. There are two kinds of
105 * sections:
106 * 1. Not essential for correct spell checking. E.g. for making suggestions.
107 * These are skipped when not supported.
108 * 2. Optional information, but essential for spell checking when present.
109 * E.g. conditions for affixes. When this section is present but not
110 * supported an error message is given.
111 *
112 * <SECTIONS>: <section> ... <sectionend>
113 *
114 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
115 *
116 * <sectionID> 1 byte number from 0 to 254 identifying the section
117 *
118 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
119 * spell checking
120 *
121 * <sectionlen> 4 bytes length of section contents, MSB first
122 *
123 * <sectionend> 1 byte SN_END
124 *
125 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000126 * sectionID == SN_INFO: <infotext>
127 * <infotext> N bytes free format text with spell file info (version,
128 * website, etc)
129 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000130 * sectionID == SN_REGION: <regionname> ...
131 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000132 * First <regionname> is region 1.
133 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
135 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
137 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000138 * 0x01 word character CF_WORD
139 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000140 * <folcharslen> 2 bytes Number of bytes in <folchars>.
141 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000142 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000143 * sectionID == SN_MIDWORD: <midword>
144 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000145 * in the middle of a word.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000148 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000149 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000150 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000151 * <condstr> N bytes Condition for the prefix.
152 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000153 * sectionID == SN_REP: <repcount> <rep> ...
154 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000155 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000156 * <repfromlen> 1 byte length of <repfrom>
157 * <repfrom> N bytes "from" part of replacement
158 * <reptolen> 1 byte length of <repto>
159 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000161 * sectionID == SN_REPSAL: <repcount> <rep> ...
162 * just like SN_REP but for soundfolded words
163 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000164 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
165 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000166 * SAL_F0LLOWUP
167 * SAL_COLLAPSE
168 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000169 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000170 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000171 * <salfromlen> 1 byte length of <salfrom>
172 * <salfrom> N bytes "from" part of soundsalike
173 * <saltolen> 1 byte length of <salto>
174 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000176 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
177 * <sofofromlen> 2 bytes length of <sofofrom>
178 * <sofofrom> N bytes "from" part of soundfold
179 * <sofotolen> 2 bytes length of <sofoto>
180 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000181 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000182 * sectionID == SN_SUGFILE: <timestamp>
183 * <timestamp> 8 bytes time in seconds that must match with .sug file
184 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000185 * sectionID == SN_NOSPLITSUGS: nothing
Bram Moolenaar7b877b32016-01-09 13:51:34 +0100186 *
187 * sectionID == SN_NOCOMPOUNDSUGS: nothing
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000188 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000189 * sectionID == SN_WORDS: <word> ...
190 * <word> N bytes NUL terminated common word
191 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000192 * sectionID == SN_MAP: <mapstr>
193 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000194 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000195 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000196 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
197 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000198 * <compmax> 1 byte Maximum nr of words in compound word.
199 * <compminlen> 1 byte Minimal word length for compounding.
200 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000201 * <compoptions> 2 bytes COMP_ flags.
202 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000203 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * slashes.
205 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000206 * <comppattern>: <comppatlen> <comppattext>
207 * <comppatlen> 1 byte length of <comppattext>
208 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
209 *
210 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000211 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000212 * sectionID == SN_SYLLABLE: <syllable>
213 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 *
215 * <LWORDTREE>: <wordtree>
216 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000217 * <KWORDTREE>: <wordtree>
218 *
219 * <PREFIXTREE>: <wordtree>
220 *
221 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000222 * <wordtree>: <nodecount> <nodedata> ...
223 *
224 * <nodecount> 4 bytes Number of nodes following. MSB first.
225 *
226 * <nodedata>: <siblingcount> <sibling> ...
227 *
228 * <siblingcount> 1 byte Number of siblings in this node. The siblings
229 * follow in sorted order.
230 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000231 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000232 * | <flags> [<flags2>] [<region>] [<affixID>]
233 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000234 *
235 * <byte> 1 byte Byte value of the sibling. Special cases:
236 * BY_NOFLAGS: End of word without flags and for all
237 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000238 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000239 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000240 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000241 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000242 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000243 * BY_FLAGS2: End of word, <flags> and <flags2>
244 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000245 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000246 * and <xbyte> follow.
247 *
248 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
249 *
250 * <xbyte> 1 byte byte value of the sibling.
251 *
252 * <flags> 1 byte bitmask of:
253 * WF_ALLCAP word must have only capitals
254 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000255 * WF_KEEPCAP keep-case word
256 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000257 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000258 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000259 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000260 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000261 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000262 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000263 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000264 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000265 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000266 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000267 * WF_NOCOMPBEF >> 8 no compounding before this word
268 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000269 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000270 * <pflags> 1 byte bitmask of:
271 * WFP_RARE rare prefix
272 * WFP_NC non-combining prefix
273 * WFP_UP letter after prefix made upper case
274 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000275 * <region> 1 byte Bitmask for regions in which word is valid. When
276 * omitted it's valid in all regions.
277 * Lowest bit is for region 1.
278 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000279 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000280 * PREFIXTREE used for the required prefix ID.
281 *
282 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
283 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000284 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000285 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000286 */
287
Bram Moolenaar4770d092006-01-12 23:22:24 +0000288/*
289 * Vim .sug file format: <SUGHEADER>
290 * <SUGWORDTREE>
291 * <SUGTABLE>
292 *
293 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
294 *
295 * <fileID> 6 bytes "VIMsug"
296 * <versionnr> 1 byte VIMSUGVERSION
297 * <timestamp> 8 bytes timestamp that must match with .spl file
298 *
299 *
300 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
301 *
302 *
303 * <SUGTABLE>: <sugwcount> <sugline> ...
304 *
305 * <sugwcount> 4 bytes number of <sugline> following
306 *
307 * <sugline>: <sugnr> ... NUL
308 *
309 * <sugnr>: X bytes word number that results in this soundfolded word,
310 * stored as an offset to the previous number in as
311 * few bytes as possible, see offset2bytes())
312 */
313
Bram Moolenaare19defe2005-03-21 08:23:33 +0000314#include "vim.h"
315
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000316#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000317
Bram Moolenaar4770d092006-01-12 23:22:24 +0000318#ifndef UNIX /* it's in os_unix.h for Unix */
319# include <time.h> /* for time_t */
320#endif
321
Bram Moolenaar98f52502015-02-10 20:03:45 +0100322#define MAXWLEN 254 /* Assume max. word len is this many bytes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000323 Some places assume a word length fits in a
Bram Moolenaar98f52502015-02-10 20:03:45 +0100324 byte, thus it can't be above 255.
325 Must be >= PFD_NOTSPECIAL. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000326
Bram Moolenaare52325c2005-08-22 22:54:29 +0000327/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000328 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100329#if VIM_SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000330typedef int idx_T;
331#else
332typedef long idx_T;
333#endif
334
Bram Moolenaar56f78042010-12-08 17:09:32 +0100335#ifdef VMS
336# define SPL_FNAME_TMPL "%s_%s.spl"
337# define SPL_FNAME_ADD "_add."
338# define SPL_FNAME_ASCII "_ascii."
339#else
340# define SPL_FNAME_TMPL "%s.%s.spl"
341# define SPL_FNAME_ADD ".add."
342# define SPL_FNAME_ASCII ".ascii."
343#endif
344
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000345/* Flags used for a word. Only the lowest byte can be used, the region byte
346 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000347#define WF_REGION 0x01 /* region byte follows */
348#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
349#define WF_ALLCAP 0x04 /* word must be all capitals */
350#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000351#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000352#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000353#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000354#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000355
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000356/* for <flags2>, shifted up one byte to be used in wn_flags */
357#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000358#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000359#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000360#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000361#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
362#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000363
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000364/* only used for su_badflags */
365#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
366
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000367#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000368
Bram Moolenaar53805d12005-08-01 07:08:33 +0000369/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000370#define WFP_RARE 0x01 /* rare prefix */
371#define WFP_NC 0x02 /* prefix is not combining */
372#define WFP_UP 0x04 /* to-upper prefix */
373#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
374#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000375
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000376/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
377 * byte) and prefcondnr (two bytes). */
378#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
379#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
380#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
381#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
382 * COMPOUNDPERMITFLAG */
383#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
384 * COMPOUNDFORBIDFLAG */
385
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000386
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000387/* flags for <compoptions> */
388#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
389#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
390#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
391#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
392
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000393/* Special byte values for <byte>. Some are only used in the tree for
394 * postponed prefixes, some only in the other trees. This is a bit messy... */
395#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000396 * postponed prefix: no <pflags> */
397#define BY_INDEX 1 /* child is shared, index follows */
398#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
399 * postponed prefix: <pflags> follows */
400#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
401 * follow; never used in prefix tree */
402#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000403
Bram Moolenaar4770d092006-01-12 23:22:24 +0000404/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
405 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000406 * One replacement: from "ft_from" to "ft_to". */
407typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000409 char_u *ft_from;
410 char_u *ft_to;
411} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000412
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000413/* Info from "SAL" entries in ".aff" file used in sl_sal.
414 * The info is split for quick processing by spell_soundfold().
415 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
416typedef struct salitem_S
417{
418 char_u *sm_lead; /* leading letters */
419 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000420 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000421 char_u *sm_rules; /* rules like ^, $, priority */
422 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000423#ifdef FEAT_MBYTE
424 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000425 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000426 int *sm_to_w; /* wide character copy of "sm_to" */
427#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000428} salitem_T;
429
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000430#ifdef FEAT_MBYTE
431typedef int salfirst_T;
432#else
433typedef short salfirst_T;
434#endif
435
Bram Moolenaar5195e452005-08-19 20:32:47 +0000436/* Values for SP_*ERROR are negative, positive values are used by
437 * read_cnt_string(). */
438#define SP_TRUNCERROR -1 /* spell file truncated error */
439#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000440#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000441
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000442/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000443 * Structure used to store words and other info for one language, loaded from
444 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000445 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
446 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
447 *
448 * The "byts" array stores the possible bytes in each tree node, preceded by
449 * the number of possible bytes, sorted on byte value:
450 * <len> <byte1> <byte2> ...
451 * The "idxs" array stores the index of the child node corresponding to the
452 * byte in "byts".
453 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000454 * the flags, region mask and affixID for the word. There may be several
455 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000456 */
457typedef struct slang_S slang_T;
458struct slang_S
459{
460 slang_T *sl_next; /* next language */
461 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000462 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000463 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000464
Bram Moolenaar51485f02005-06-04 21:55:20 +0000465 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000466 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000467 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000468 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000469 char_u *sl_pbyts; /* prefix tree word bytes */
470 idx_T *sl_pidxs; /* prefix tree word indexes */
471
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000472 char_u *sl_info; /* infotext string or NULL */
473
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000474 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000476 char_u *sl_midword; /* MIDWORD string or NULL */
477
Bram Moolenaar4770d092006-01-12 23:22:24 +0000478 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
479
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000480 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000481 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000482 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000483 int sl_compoptions; /* COMP_* flags */
484 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000485 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000486 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000487 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000488 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000489 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000490 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000491 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
492 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000493
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000494 int sl_prefixcnt; /* number of items in "sl_prefprog" */
495 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497 garray_T sl_rep; /* list of fromto_T entries from REP lines */
498 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
499 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000500 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000501 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502 there is none */
503 int sl_followup; /* SAL followup */
504 int sl_collapse; /* SAL collapse_result */
505 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000506 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
507 * "sl_sal_first" maps chars, when has_mbyte
508 * "sl_sal" is a list of wide char lists. */
509 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
510 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000511 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar7b877b32016-01-09 13:51:34 +0100512 int sl_nocompoundsugs; /* don't suggest compounding */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000513
514 /* Info from the .sug file. Loaded on demand. */
515 time_t sl_sugtime; /* timestamp for .sug file */
516 char_u *sl_sbyts; /* soundfolded word bytes */
517 idx_T *sl_sidxs; /* soundfolded word indexes */
518 buf_T *sl_sugbuf; /* buffer with word number table */
519 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
520 load */
521
Bram Moolenaarea424162005-06-16 21:51:00 +0000522 int sl_has_map; /* TRUE if there is a MAP line */
523#ifdef FEAT_MBYTE
524 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
525 int sl_map_array[256]; /* MAP for first 256 chars */
526#else
527 char_u sl_map_array[256]; /* MAP for first 256 chars */
528#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000529 hashtab_T sl_sounddone; /* table with soundfolded words that have
530 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000531};
532
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000533/* First language that is loaded, start of the linked list of loaded
534 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000535static slang_T *first_lang = NULL;
536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000537/* Flags used in .spl file for soundsalike flags. */
538#define SAL_F0LLOWUP 1
539#define SAL_COLLAPSE 2
540#define SAL_REM_ACCENTS 4
541
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000542/*
543 * Structure used in "b_langp", filled from 'spelllang'.
544 */
545typedef struct langp_S
546{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000547 slang_T *lp_slang; /* info for this language */
548 slang_T *lp_sallang; /* language used for sound folding or NULL */
549 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000550 int lp_region; /* bitmask for region or REGION_ALL */
551} langp_T;
552
553#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
554
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000555#define REGION_ALL 0xff /* word valid in all regions */
556
Bram Moolenaar5195e452005-08-19 20:32:47 +0000557#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
558#define VIMSPELLMAGICL 8
559#define VIMSPELLVERSION 50
560
Bram Moolenaar4770d092006-01-12 23:22:24 +0000561#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
562#define VIMSUGMAGICL 6
563#define VIMSUGVERSION 1
564
Bram Moolenaar5195e452005-08-19 20:32:47 +0000565/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
566#define SN_REGION 0 /* <regionname> section */
567#define SN_CHARFLAGS 1 /* charflags section */
568#define SN_MIDWORD 2 /* <midword> section */
569#define SN_PREFCOND 3 /* <prefcond> section */
570#define SN_REP 4 /* REP items section */
571#define SN_SAL 5 /* SAL items section */
572#define SN_SOFO 6 /* soundfolding section */
573#define SN_MAP 7 /* MAP items section */
574#define SN_COMPOUND 8 /* compound words section */
575#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000576#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000577#define SN_SUGFILE 11 /* timestamp for .sug file */
578#define SN_REPSAL 12 /* REPSAL items section */
579#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000580#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000581#define SN_INFO 15 /* info section */
Bram Moolenaar7b877b32016-01-09 13:51:34 +0100582#define SN_NOCOMPOUNDSUGS 16 /* don't compound for suggestions */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000583#define SN_END 255 /* end of sections */
584
585#define SNF_REQUIRED 1 /* <sectionflags>: required section */
586
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000587/* Result values. Lower number is accepted over higher one. */
588#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000589#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590#define SP_RARE 1
591#define SP_LOCAL 2
592#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000593
Bram Moolenaar7887d882005-07-01 22:33:52 +0000594/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000595static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000596
Bram Moolenaar4770d092006-01-12 23:22:24 +0000597typedef struct wordcount_S
598{
599 short_u wc_count; /* nr of times word was seen */
600 char_u wc_word[1]; /* word, actually longer */
601} wordcount_T;
602
603static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000604#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000605#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
606#define MAXWORDCOUNT 0xffff
607
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000608/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609 * Information used when looking for suggestions.
610 */
611typedef struct suginfo_S
612{
613 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000614 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000616 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000617 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000618 char_u *su_badptr; /* start of bad word in line */
619 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000620 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
622 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000623 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000624 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000625 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000626} suginfo_T;
627
628/* One word suggestion. Used in "si_ga". */
629typedef struct suggest_S
630{
631 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000632 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000633 int st_orglen; /* length of replaced text */
634 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000635 int st_altscore; /* used when st_score compares equal */
636 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000637 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000638 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000639} suggest_T;
640
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000641#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642
Bram Moolenaar4770d092006-01-12 23:22:24 +0000643/* TRUE if a word appears in the list of banned words. */
644#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
645
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000646/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000647 * what is displayed, because when rescore_suggestions() is called the score
648 * may change and wrong suggestions may be removed later. */
649#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000650
651/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
652 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000653#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654
655/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000656#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000657#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000659#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000663#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664#define SCORE_SUBST 93 /* substitute a character */
665#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000666#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000667#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000668#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000669#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000670#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000671#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000672#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000673#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000674
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000675#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000676#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
677 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000678
Bram Moolenaar4770d092006-01-12 23:22:24 +0000679#define SCORE_COMMON1 30 /* subtracted for words seen before */
680#define SCORE_COMMON2 40 /* subtracted for words often seen */
681#define SCORE_COMMON3 50 /* subtracted for words very often seen */
682#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
683#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
684
685/* When trying changed soundfold words it becomes slow when trying more than
686 * two changes. With less then two changes it's slightly faster but we miss a
687 * few good suggestions. In rare cases we need to try three of four changes.
688 */
689#define SCORE_SFMAX1 200 /* maximum score for first try */
690#define SCORE_SFMAX2 300 /* maximum score for second try */
691#define SCORE_SFMAX3 400 /* maximum score for third try */
692
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000693#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000694#define SCORE_MAXMAX 999999 /* accept any score */
695#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
696
697/* for spell_edit_score_limit() we need to know the minimum value of
698 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
699#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000700
701/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000702 * Structure to store info for word matching.
703 */
704typedef struct matchinf_S
705{
706 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000707
708 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000709 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000710 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000711 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000712 char_u *mi_cend; /* char after what was used for
713 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000714
715 /* case-folded text */
716 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000717 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000718
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000719 /* for when checking word after a prefix */
720 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000721 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000722 int mi_prefcnt; /* number of entries at mi_prefarridx */
723 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000724#ifdef FEAT_MBYTE
725 int mi_cprefixlen; /* byte length of prefix in original
726 case */
727#else
728# define mi_cprefixlen mi_prefixlen /* it's the same value */
729#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000730
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000731 /* for when checking a compound word */
732 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000733 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
734 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000735 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000736
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000737 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000738 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000739 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200740 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000741
742 /* for NOBREAK */
743 int mi_result2; /* "mi_resul" without following word */
744 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000745} matchinf_T;
746
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000747/*
748 * The tables used for recognizing word characters according to spelling.
749 * These are only used for the first 256 characters of 'encoding'.
750 */
751typedef struct spelltab_S
752{
753 char_u st_isw[256]; /* flags: is word char */
754 char_u st_isu[256]; /* flags: is uppercase char */
755 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000756 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000757} spelltab_T;
758
759static spelltab_T spelltab;
760static int did_set_spelltab;
761
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000762#define CF_WORD 0x01
763#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000764
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100765static void clear_spell_chartab(spelltab_T *sp);
766static int set_spell_finish(spelltab_T *new_st);
767static int spell_iswordp(char_u *p, win_T *wp);
768static int spell_iswordp_nmw(char_u *p, win_T *wp);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000769#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100770static int spell_mb_isword_class(int cl, win_T *wp);
771static int spell_iswordp_w(int *p, win_T *wp);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000772#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100773static int write_spell_prefcond(FILE *fd, garray_T *gap);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000774
775/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000777 */
778typedef enum
779{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_START = 0, /* At start of node check for NUL bytes (goodword
781 * ends); if badword ends there is a match, otherwise
782 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000783 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000784 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000785 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
786 STATE_PLAIN, /* Use each byte of the node. */
787 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000788 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000789 STATE_INS, /* Insert a byte in the bad word. */
790 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 STATE_UNSWAP, /* Undo swap two characters. */
792 STATE_SWAP3, /* Swap two characters over three. */
793 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000795 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000796 STATE_REP_INI, /* Prepare for using REP items. */
797 STATE_REP, /* Use matching REP items from the .aff file. */
798 STATE_REP_UNDO, /* Undo a REP item replacement. */
799 STATE_FINAL /* End of this node. */
800} state_T;
801
802/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000803 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000804 */
805typedef struct trystate_S
806{
Bram Moolenaarea424162005-06-16 21:51:00 +0000807 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000808 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000809 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000810 short ts_curi; /* index in list of child nodes */
811 char_u ts_fidx; /* index in fword[], case-folded bad word */
812 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
813 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000814 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000815 * PFD_PREFIXTREE or PFD_NOPREFIX */
816 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000817#ifdef FEAT_MBYTE
818 char_u ts_tcharlen; /* number of bytes in tword character */
819 char_u ts_tcharidx; /* current byte index in tword character */
820 char_u ts_isdiff; /* DIFF_ values */
821 char_u ts_fcharstart; /* index in fword where badword char started */
822#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000823 char_u ts_prewordlen; /* length of word in "preword[]" */
824 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000825 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000826 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000828 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000829 char_u ts_delidx; /* index in fword for char that was deleted,
830 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000831} trystate_T;
832
Bram Moolenaarea424162005-06-16 21:51:00 +0000833/* values for ts_isdiff */
834#define DIFF_NONE 0 /* no different byte (yet) */
835#define DIFF_YES 1 /* different byte found */
836#define DIFF_INSERT 2 /* inserting character */
837
Bram Moolenaard12a1322005-08-21 22:08:24 +0000838/* values for ts_flags */
839#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
840#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000841#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000842
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000843/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000844#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000845#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000846#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000847
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000849#define FIND_FOLDWORD 0 /* find word case-folded */
850#define FIND_KEEPWORD 1 /* find keep-case word */
851#define FIND_PREFIX 2 /* find word after prefix */
852#define FIND_COMPOUND 3 /* find case-folded compound word */
853#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000854
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100855static slang_T *slang_alloc(char_u *lang);
856static void slang_free(slang_T *lp);
857static void slang_clear(slang_T *lp);
858static void slang_clear_sug(slang_T *lp);
859static void find_word(matchinf_T *mip, int mode);
860static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap);
861static int can_compound(slang_T *slang, char_u *word, char_u *flags);
862static int can_be_compound(trystate_T *sp, slang_T *slang, char_u *compflags, int flag);
863static int match_compoundrule(slang_T *slang, char_u *compflags);
864static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req);
865static void find_prefix(matchinf_T *mip, int mode);
866static int fold_more(matchinf_T *mip);
867static int spell_valid_case(int wordflags, int treeflags);
868static int no_spell_checking(win_T *wp);
869static void spell_load_lang(char_u *lang);
870static char_u *spell_enc(void);
871static void int_wordlist_spl(char_u *fname);
872static void spell_load_cb(char_u *fname, void *cookie);
873static slang_T *spell_load_file(char_u *fname, char_u *lang, slang_T *old_lp, int silent);
874static char_u *read_cnt_string(FILE *fd, int cnt_bytes, int *lenp);
875static int read_region_section(FILE *fd, slang_T *slang, int len);
876static int read_charflags_section(FILE *fd);
877static int read_prefcond_section(FILE *fd, slang_T *lp);
878static int read_rep_section(FILE *fd, garray_T *gap, short *first);
879static int read_sal_section(FILE *fd, slang_T *slang);
880static int read_words_section(FILE *fd, slang_T *lp, int len);
881static void count_common_word(slang_T *lp, char_u *word, int len, int count);
882static int score_wordcount_adj(slang_T *slang, int score, char_u *word, int split);
883static int read_sofo_section(FILE *fd, slang_T *slang);
884static int read_compound(FILE *fd, slang_T *slang, int len);
885static int byte_in_str(char_u *str, int byte);
886static int init_syl_tab(slang_T *slang);
887static int count_syllables(slang_T *slang, char_u *word);
888static int set_sofo(slang_T *lp, char_u *from, char_u *to);
889static void set_sal_first(slang_T *lp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000890#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100891static int *mb_str2wide(char_u *s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000892#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100893static int spell_read_tree(FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt);
894static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
895static void clear_midword(win_T *buf);
896static void use_midword(slang_T *lp, win_T *buf);
897static int find_region(char_u *rp, char_u *region);
898static int captype(char_u *word, char_u *end);
899static int badword_captype(char_u *word, char_u *end);
900static void spell_reload_one(char_u *fname, int added_word);
901static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
902static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
903static int spell_casefold(char_u *p, int len, char_u *buf, int buflen);
904static int check_need_cap(linenr_T lnum, colnr_T col);
905static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000906#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100907static void spell_suggest_expr(suginfo_T *su, char_u *expr);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000908#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100909static void spell_suggest_file(suginfo_T *su, char_u *fname);
910static void spell_suggest_intern(suginfo_T *su, int interactive);
911static void suggest_load_files(void);
912static void tree_count_words(char_u *byts, idx_T *idxs);
913static void spell_find_cleanup(suginfo_T *su);
914static void onecap_copy(char_u *word, char_u *wcopy, int upper);
915static void allcap_copy(char_u *word, char_u *wcopy);
916static void suggest_try_special(suginfo_T *su);
917static void suggest_try_change(suginfo_T *su);
918static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
919static void go_deeper(trystate_T *stack, int depth, int score_add);
Bram Moolenaar53805d12005-08-01 07:08:33 +0000920#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100921static int nofold_len(char_u *fword, int flen, char_u *word);
Bram Moolenaar53805d12005-08-01 07:08:33 +0000922#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100923static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
924static void score_comp_sal(suginfo_T *su);
925static void score_combine(suginfo_T *su);
926static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
927static void suggest_try_soundalike_prep(void);
928static void suggest_try_soundalike(suginfo_T *su);
929static void suggest_try_soundalike_finish(void);
930static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
931static int soundfold_find(slang_T *slang, char_u *word);
932static void make_case_word(char_u *fword, char_u *cword, int flags);
933static void set_map_str(slang_T *lp, char_u *map);
934static int similar_chars(slang_T *slang, int c1, int c2);
935static void add_suggestion(suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf);
936static void check_suggestions(suginfo_T *su, garray_T *gap);
937static void add_banned(suginfo_T *su, char_u *word);
938static void rescore_suggestions(suginfo_T *su);
939static void rescore_one(suginfo_T *su, suggest_T *stp);
940static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
941static void spell_soundfold(slang_T *slang, char_u *inword, int folded, char_u *res);
942static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
943static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000944#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100945static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000946#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100947static int soundalike_score(char_u *goodsound, char_u *badsound);
948static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
949static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000950#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100951static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000952#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100953static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
954static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
955static buf_T *open_spellbuf(void);
956static void close_spellbuf(buf_T *buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000957
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000958/*
959 * Use our own character-case definitions, because the current locale may
960 * differ from what the .spl file uses.
961 * These must not be called with negative number!
962 */
963#ifndef FEAT_MBYTE
964/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000965# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
966# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000967# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
968#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000969# if defined(HAVE_WCHAR_H)
970# include <wchar.h> /* for towupper() and towlower() */
971# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000972/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
973 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
974 * the "w" library function for characters above 255 if available. */
975# ifdef HAVE_TOWLOWER
976# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000977 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000978# else
979# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000980 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000981# endif
982
983# ifdef HAVE_TOWUPPER
984# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000985 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000986# else
987# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000988 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000989# endif
990
991# ifdef HAVE_ISWUPPER
992# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
993 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
994# else
995# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000996 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000997# endif
998#endif
999
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001000
1001static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +00001002static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001003static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +00001004static char *e_affname = N_("Affix name too long in %s line %d: %s");
1005static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
1006static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00001007static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001009/* Remember what "z?" replaced. */
1010static char_u *repl_from = NULL;
1011static char_u *repl_to = NULL;
1012
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013/*
1014 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001015 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001016 * "*attrp" is set to the highlight index for a badly spelled word. For a
1017 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001019 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001020 * "capcol" is used to check for a Capitalised word after the end of a
1021 * sentence. If it's zero then perform the check. Return the column where to
1022 * check next, or -1 when no sentence end was found. If it's NULL then don't
1023 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001024 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001025 * Returns the length of the word in bytes, also when it's OK, so that the
1026 * caller can skip over the word.
1027 */
1028 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001029spell_check(
1030 win_T *wp, /* current window */
1031 char_u *ptr,
1032 hlf_T *attrp,
1033 int *capcol, /* column to check for Capital */
1034 int docount) /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001035{
1036 matchinf_T mi; /* Most things are put in "mi" so that it can
1037 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001038 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001039 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001040 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001041 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001042 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001043
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001044 /* A word never starts at a space or a control character. Return quickly
1045 * then, skipping over the character. */
1046 if (*ptr <= ' ')
1047 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001048
1049 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001050 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001051 return 1;
1052
Bram Moolenaar5195e452005-08-19 20:32:47 +00001053 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001054
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001056 * 0X99FF. But always do check spelling to find "3GPP" and "11
1057 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001058 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001060 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
1061 mi.mi_end = skipbin(ptr + 2);
1062 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +00001063 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001064 else
1065 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001066 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001067 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001068
Bram Moolenaar0c405862005-06-22 22:26:26 +00001069 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001070 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001071 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001072 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001073 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001074 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001075 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001076 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001077 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001078
Bram Moolenaar860cae12010-06-05 23:22:07 +02001079 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001080 {
1081 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001082 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001083 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001084 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001085 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001086 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001087 if (capcol != NULL)
1088 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001089
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090 /* We always use the characters up to the next non-word character,
1091 * also for bad words. */
1092 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001093
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001095 mi.mi_capflags = 0;
1096 mi.mi_cend = NULL;
1097 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001098
Bram Moolenaar5195e452005-08-19 20:32:47 +00001099 /* case-fold the word with one non-word character, so that we can check
1100 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101 if (*mi.mi_fend != NUL)
1102 mb_ptr_adv(mi.mi_fend);
1103
1104 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1105 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001106 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001107
1108 /* The word is bad unless we recognize it. */
1109 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001110 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001111
1112 /*
1113 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001114 * We check them all, because a word may be matched longer in another
1115 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001116 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001117 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001118 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001119 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001120
1121 /* If reloading fails the language is still in the list but everything
1122 * has been cleared. */
1123 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1124 continue;
1125
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001126 /* Check for a matching word in case-folded words. */
1127 find_word(&mi, FIND_FOLDWORD);
1128
1129 /* Check for a matching word in keep-case words. */
1130 find_word(&mi, FIND_KEEPWORD);
1131
1132 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001133 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001134
1135 /* For a NOBREAK language, may want to use a word without a following
1136 * word as a backup. */
1137 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1138 && mi.mi_result2 != SP_BAD)
1139 {
1140 mi.mi_result = mi.mi_result2;
1141 mi.mi_end = mi.mi_end2;
1142 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001143
1144 /* Count the word in the first language where it's found to be OK. */
1145 if (count_word && mi.mi_result == SP_OK)
1146 {
1147 count_common_word(mi.mi_lp->lp_slang, ptr,
1148 (int)(mi.mi_end - ptr), 1);
1149 count_word = FALSE;
1150 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001151 }
1152
1153 if (mi.mi_result != SP_OK)
1154 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001155 /* If we found a number skip over it. Allows for "42nd". Do flag
1156 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001157 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001158 {
1159 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1160 return nrlen;
1161 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162
1163 /* When we are at a non-word character there is no error, just
1164 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001165 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001166 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001167 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001168 {
1169 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001170 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001171
1172 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001173 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001174 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001175 r = vim_regexec(&regmatch, ptr, 0);
1176 wp->w_s->b_cap_prog = regmatch.regprog;
1177 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001178 *capcol = (int)(regmatch.endp[0] - ptr);
1179 }
1180
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001181#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001182 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001183 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001184#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001185 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001186 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001187 else if (mi.mi_end == ptr)
1188 /* Always include at least one character. Required for when there
1189 * is a mixup in "midword". */
1190 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001191 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001192 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001193 {
1194 char_u *p, *fp;
1195 int save_result = mi.mi_result;
1196
1197 /* First language in 'spelllang' is NOBREAK. Find first position
1198 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001199 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001200 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001201 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001202 p = mi.mi_word;
1203 fp = mi.mi_fword;
1204 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001205 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001206 mb_ptr_adv(p);
1207 mb_ptr_adv(fp);
1208 if (p >= mi.mi_end)
1209 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001210 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001211 find_word(&mi, FIND_COMPOUND);
1212 if (mi.mi_result != SP_BAD)
1213 {
1214 mi.mi_end = p;
1215 break;
1216 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001217 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001218 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001219 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001220 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001221
1222 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001223 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001224 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001225 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001226 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001227 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001228 }
1229
Bram Moolenaar5195e452005-08-19 20:32:47 +00001230 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1231 {
1232 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001233 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001234 return wrongcaplen;
1235 }
1236
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 return (int)(mi.mi_end - ptr);
1238}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001239
Bram Moolenaar51485f02005-06-04 21:55:20 +00001240/*
1241 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1243 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1244 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1245 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001246 *
1247 * For a match mip->mi_result is updated.
1248 */
1249 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001250find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001251{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001252 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001253 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001254 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 int endidxcnt = 0;
1256 int len;
1257 int wlen = 0;
1258 int flen;
1259 int c;
1260 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001261 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001262#ifdef FEAT_MBYTE
1263 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001264#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001265 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001266 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001267 slang_T *slang = mip->mi_lp->lp_slang;
1268 unsigned flags;
1269 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001270 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001271 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001272 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001273 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001274
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001275 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001276 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001277 /* Check for word with matching case in keep-case tree. */
1278 ptr = mip->mi_word;
1279 flen = 9999; /* no case folding, always enough bytes */
1280 byts = slang->sl_kbyts;
1281 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001282
1283 if (mode == FIND_KEEPCOMPOUND)
1284 /* Skip over the previously found word(s). */
1285 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001286 }
1287 else
1288 {
1289 /* Check for case-folded in case-folded tree. */
1290 ptr = mip->mi_fword;
1291 flen = mip->mi_fwordlen; /* available case-folded bytes */
1292 byts = slang->sl_fbyts;
1293 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001294
1295 if (mode == FIND_PREFIX)
1296 {
1297 /* Skip over the prefix. */
1298 wlen = mip->mi_prefixlen;
1299 flen -= mip->mi_prefixlen;
1300 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001301 else if (mode == FIND_COMPOUND)
1302 {
1303 /* Skip over the previously found word(s). */
1304 wlen = mip->mi_compoff;
1305 flen -= mip->mi_compoff;
1306 }
1307
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001308 }
1309
Bram Moolenaar51485f02005-06-04 21:55:20 +00001310 if (byts == NULL)
1311 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001312
Bram Moolenaar51485f02005-06-04 21:55:20 +00001313 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001314 * Repeat advancing in the tree until:
1315 * - there is a byte that doesn't match,
1316 * - we reach the end of the tree,
1317 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 */
1319 for (;;)
1320 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001321 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001322 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001323
1324 len = byts[arridx++];
1325
1326 /* If the first possible byte is a zero the word could end here.
1327 * Remember this index, we first check for the longest word. */
1328 if (byts[arridx] == 0)
1329 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001330 if (endidxcnt == MAXWLEN)
1331 {
1332 /* Must be a corrupted spell file. */
1333 EMSG(_(e_format));
1334 return;
1335 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001336 endlen[endidxcnt] = wlen;
1337 endidx[endidxcnt++] = arridx++;
1338 --len;
1339
1340 /* Skip over the zeros, there can be several flag/region
1341 * combinations. */
1342 while (len > 0 && byts[arridx] == 0)
1343 {
1344 ++arridx;
1345 --len;
1346 }
1347 if (len == 0)
1348 break; /* no children, word must end here */
1349 }
1350
1351 /* Stop looking at end of the line. */
1352 if (ptr[wlen] == NUL)
1353 break;
1354
1355 /* Perform a binary search in the list of accepted bytes. */
1356 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001357 if (c == TAB) /* <Tab> is handled like <Space> */
1358 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001359 lo = arridx;
1360 hi = arridx + len - 1;
1361 while (lo < hi)
1362 {
1363 m = (lo + hi) / 2;
1364 if (byts[m] > c)
1365 hi = m - 1;
1366 else if (byts[m] < c)
1367 lo = m + 1;
1368 else
1369 {
1370 lo = hi = m;
1371 break;
1372 }
1373 }
1374
1375 /* Stop if there is no matching byte. */
1376 if (hi < lo || byts[lo] != c)
1377 break;
1378
1379 /* Continue at the child (if there is one). */
1380 arridx = idxs[lo];
1381 ++wlen;
1382 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001383
1384 /* One space in the good word may stand for several spaces in the
1385 * checked word. */
1386 if (c == ' ')
1387 {
1388 for (;;)
1389 {
1390 if (flen <= 0 && *mip->mi_fend != NUL)
1391 flen = fold_more(mip);
1392 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1393 break;
1394 ++wlen;
1395 --flen;
1396 }
1397 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001398 }
1399
1400 /*
1401 * Verify that one of the possible endings is valid. Try the longest
1402 * first.
1403 */
1404 while (endidxcnt > 0)
1405 {
1406 --endidxcnt;
1407 arridx = endidx[endidxcnt];
1408 wlen = endlen[endidxcnt];
1409
1410#ifdef FEAT_MBYTE
1411 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1412 continue; /* not at first byte of character */
1413#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001414 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001415 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001416 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001417 continue; /* next char is a word character */
1418 word_ends = FALSE;
1419 }
1420 else
1421 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001422 /* The prefix flag is before compound flags. Once a valid prefix flag
1423 * has been found we try compound flags. */
1424 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425
1426#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001427 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428 {
1429 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 * when folding case. This can be slow, take a shortcut when the
1431 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001432 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001433 if (STRNCMP(ptr, p, wlen) != 0)
1434 {
1435 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1436 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001437 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001438 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 }
1440#endif
1441
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001442 /* Check flags and region. For FIND_PREFIX check the condition and
1443 * prefix ID.
1444 * Repeat this if there are more flags/region alternatives until there
1445 * is a match. */
1446 res = SP_BAD;
1447 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1448 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449 {
1450 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001451
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001452 /* For the fold-case tree check that the case of the checked word
1453 * matches with what the word in the tree requires.
1454 * For keep-case tree the case is always right. For prefixes we
1455 * don't bother to check. */
1456 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458 if (mip->mi_cend != mip->mi_word + wlen)
1459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001460 /* mi_capflags was set for a different word length, need
1461 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001462 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001463 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001464 }
1465
Bram Moolenaar0c405862005-06-22 22:26:26 +00001466 if (mip->mi_capflags == WF_KEEPCAP
1467 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001468 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469 }
1470
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001471 /* When mode is FIND_PREFIX the word must support the prefix:
1472 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001473 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001474 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001475 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001476 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001477 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001478 mip->mi_word + mip->mi_cprefixlen, slang,
1479 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001480 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001481 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001482
1483 /* Use the WF_RARE flag for a rare prefix. */
1484 if (c & WF_RAREPFX)
1485 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001486 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001487 }
1488
Bram Moolenaar78622822005-08-23 21:00:13 +00001489 if (slang->sl_nobreak)
1490 {
1491 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1492 && (flags & WF_BANNED) == 0)
1493 {
1494 /* NOBREAK: found a valid following word. That's all we
1495 * need to know, so return. */
1496 mip->mi_result = SP_OK;
1497 break;
1498 }
1499 }
1500
1501 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1502 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001503 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001504 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001505 * COMPOUNDMIN reject it quickly.
1506 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001507 * that's too short... Myspell compatibility requires this
1508 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001509 if (((unsigned)flags >> 24) == 0
1510 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001511 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001512#ifdef FEAT_MBYTE
1513 /* For multi-byte chars check character length against
1514 * COMPOUNDMIN. */
1515 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001516 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001517 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1518 wlen - mip->mi_compoff) < slang->sl_compminlen)
1519 continue;
1520#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001521
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001522 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001523 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001524 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1525 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001526 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001527 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001528
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001529 /* Don't allow compounding on a side where an affix was added,
1530 * unless COMPOUNDPERMITFLAG was used. */
1531 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1532 continue;
1533 if (!word_ends && (flags & WF_NOCOMPAFT))
1534 continue;
1535
Bram Moolenaard12a1322005-08-21 22:08:24 +00001536 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001537 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001538 ? slang->sl_compstartflags
1539 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001540 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001541 continue;
1542
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001543 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1544 * discard the compound word. */
1545 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1546 continue;
1547
Bram Moolenaare52325c2005-08-22 22:54:29 +00001548 if (mode == FIND_COMPOUND)
1549 {
1550 int capflags;
1551
1552 /* Need to check the caps type of the appended compound
1553 * word. */
1554#ifdef FEAT_MBYTE
1555 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1556 mip->mi_compoff) != 0)
1557 {
1558 /* case folding may have changed the length */
1559 p = mip->mi_word;
1560 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1561 mb_ptr_adv(p);
1562 }
1563 else
1564#endif
1565 p = mip->mi_word + mip->mi_compoff;
1566 capflags = captype(p, mip->mi_word + wlen);
1567 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1568 && (flags & WF_FIXCAP) != 0))
1569 continue;
1570
1571 if (capflags != WF_ALLCAP)
1572 {
1573 /* When the character before the word is a word
1574 * character we do not accept a Onecap word. We do
1575 * accept a no-caps word, even when the dictionary
1576 * word specifies ONECAP. */
1577 mb_ptr_back(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001578 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +00001579 ? capflags == WF_ONECAP
1580 : (flags & WF_ONECAP) != 0
1581 && capflags != WF_ONECAP)
1582 continue;
1583 }
1584 }
1585
Bram Moolenaar5195e452005-08-19 20:32:47 +00001586 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001587 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001588 * the number of syllables must not be too large. */
1589 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1590 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1591 if (word_ends)
1592 {
1593 char_u fword[MAXWLEN];
1594
1595 if (slang->sl_compsylmax < MAXWLEN)
1596 {
1597 /* "fword" is only needed for checking syllables. */
1598 if (ptr == mip->mi_word)
1599 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1600 else
1601 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1602 }
1603 if (!can_compound(slang, fword, mip->mi_compflags))
1604 continue;
1605 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001606 else if (slang->sl_comprules != NULL
1607 && !match_compoundrule(slang, mip->mi_compflags))
1608 /* The compound flags collected so far do not match any
1609 * COMPOUNDRULE, discard the compounded word. */
1610 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001611 }
1612
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001613 /* Check NEEDCOMPOUND: can't use word without compounding. */
1614 else if (flags & WF_NEEDCOMP)
1615 continue;
1616
Bram Moolenaar78622822005-08-23 21:00:13 +00001617 nobreak_result = SP_OK;
1618
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001619 if (!word_ends)
1620 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001621 int save_result = mip->mi_result;
1622 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001623 langp_T *save_lp = mip->mi_lp;
1624 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001625
1626 /* Check that a valid word follows. If there is one and we
1627 * are compounding, it will set "mi_result", thus we are
1628 * always finished here. For NOBREAK we only check that a
1629 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001630 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001631 if (slang->sl_nobreak)
1632 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001633
1634 /* Find following word in case-folded tree. */
1635 mip->mi_compoff = endlen[endidxcnt];
1636#ifdef FEAT_MBYTE
1637 if (has_mbyte && mode == FIND_KEEPWORD)
1638 {
1639 /* Compute byte length in case-folded word from "wlen":
1640 * byte length in keep-case word. Length may change when
1641 * folding case. This can be slow, take a shortcut when
1642 * the case-folded word is equal to the keep-case word. */
1643 p = mip->mi_fword;
1644 if (STRNCMP(ptr, p, wlen) != 0)
1645 {
1646 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1647 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001648 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001649 }
1650 }
1651#endif
Bram Moolenaarba534352016-04-21 09:20:26 +02001652#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001653 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001654#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001655 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001656 if (flags & WF_COMPROOT)
1657 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001658
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001659 /* For NOBREAK we need to try all NOBREAK languages, at least
1660 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001661 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663 if (slang->sl_nobreak)
1664 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001665 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001666 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1667 || !mip->mi_lp->lp_slang->sl_nobreak)
1668 continue;
1669 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001670
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001671 find_word(mip, FIND_COMPOUND);
1672
1673 /* When NOBREAK any word that matches is OK. Otherwise we
1674 * need to find the longest match, thus try with keep-case
1675 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001676 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1677 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001678 /* Find following word in keep-case tree. */
1679 mip->mi_compoff = wlen;
1680 find_word(mip, FIND_KEEPCOMPOUND);
1681
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001682#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1683 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1684 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001685 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1686 {
1687 /* Check for following word with prefix. */
1688 mip->mi_compoff = c;
1689 find_prefix(mip, FIND_COMPOUND);
1690 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001691#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001692 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001693
1694 if (!slang->sl_nobreak)
1695 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001696 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001697 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001698 if (flags & WF_COMPROOT)
1699 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001700 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001701
Bram Moolenaar78622822005-08-23 21:00:13 +00001702 if (slang->sl_nobreak)
1703 {
1704 nobreak_result = mip->mi_result;
1705 mip->mi_result = save_result;
1706 mip->mi_end = save_end;
1707 }
1708 else
1709 {
1710 if (mip->mi_result == SP_OK)
1711 break;
1712 continue;
1713 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001714 }
1715
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716 if (flags & WF_BANNED)
1717 res = SP_BANNED;
1718 else if (flags & WF_REGION)
1719 {
1720 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001721 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001722 res = SP_OK;
1723 else
1724 res = SP_LOCAL;
1725 }
1726 else if (flags & WF_RARE)
1727 res = SP_RARE;
1728 else
1729 res = SP_OK;
1730
Bram Moolenaar78622822005-08-23 21:00:13 +00001731 /* Always use the longest match and the best result. For NOBREAK
1732 * we separately keep the longest match without a following good
1733 * word as a fall-back. */
1734 if (nobreak_result == SP_BAD)
1735 {
1736 if (mip->mi_result2 > res)
1737 {
1738 mip->mi_result2 = res;
1739 mip->mi_end2 = mip->mi_word + wlen;
1740 }
1741 else if (mip->mi_result2 == res
1742 && mip->mi_end2 < mip->mi_word + wlen)
1743 mip->mi_end2 = mip->mi_word + wlen;
1744 }
1745 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001746 {
1747 mip->mi_result = res;
1748 mip->mi_end = mip->mi_word + wlen;
1749 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001750 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001751 mip->mi_end = mip->mi_word + wlen;
1752
Bram Moolenaar78622822005-08-23 21:00:13 +00001753 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001754 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001755 }
1756
Bram Moolenaar78622822005-08-23 21:00:13 +00001757 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001758 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001759 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001760}
1761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001762/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001763 * Return TRUE if there is a match between the word ptr[wlen] and
1764 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1765 * word.
1766 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1767 * end of ptr[wlen] and the second part matches after it.
1768 */
1769 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001770match_checkcompoundpattern(
1771 char_u *ptr,
1772 int wlen,
1773 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001774{
1775 int i;
1776 char_u *p;
1777 int len;
1778
1779 for (i = 0; i + 1 < gap->ga_len; i += 2)
1780 {
1781 p = ((char_u **)gap->ga_data)[i + 1];
1782 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1783 {
1784 /* Second part matches at start of following compound word, now
1785 * check if first part matches at end of previous word. */
1786 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001787 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001788 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1789 return TRUE;
1790 }
1791 }
1792 return FALSE;
1793}
1794
1795/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001796 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1797 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001798 */
1799 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001800can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001801{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001802#ifdef FEAT_MBYTE
1803 char_u uflags[MAXWLEN * 2];
1804 int i;
1805#endif
1806 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001807
1808 if (slang->sl_compprog == NULL)
1809 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001810#ifdef FEAT_MBYTE
1811 if (enc_utf8)
1812 {
1813 /* Need to convert the single byte flags to utf8 characters. */
1814 p = uflags;
1815 for (i = 0; flags[i] != NUL; ++i)
1816 p += mb_char2bytes(flags[i], p);
1817 *p = NUL;
1818 p = uflags;
1819 }
1820 else
1821#endif
1822 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001823 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001824 return FALSE;
1825
Bram Moolenaare52325c2005-08-22 22:54:29 +00001826 /* Count the number of syllables. This may be slow, do it last. If there
1827 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001828 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001829 if (slang->sl_compsylmax < MAXWLEN
1830 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001831 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001832 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001833}
1834
1835/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001836 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1837 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1838 * lines if they don't contain wildcards.
1839 */
1840 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001841can_be_compound(
1842 trystate_T *sp,
1843 slang_T *slang,
1844 char_u *compflags,
1845 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001846{
1847 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1848 * then it can't possibly compound. */
1849 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1850 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1851 return FALSE;
1852
1853 /* If there are no wildcards, we can check if the flags collected so far
1854 * possibly can form a match with COMPOUNDRULE patterns. This only
1855 * makes sense when we have two or more words. */
1856 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1857 {
1858 int v;
1859
1860 compflags[sp->ts_complen] = flag;
1861 compflags[sp->ts_complen + 1] = NUL;
1862 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1863 compflags[sp->ts_complen] = NUL;
1864 return v;
1865 }
1866
1867 return TRUE;
1868}
1869
1870
1871/*
1872 * Return TRUE if the compound flags in compflags[] match the start of any
1873 * compound rule. This is used to stop trying a compound if the flags
1874 * collected so far can't possibly match any compound rule.
1875 * Caller must check that slang->sl_comprules is not NULL.
1876 */
1877 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001878match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001879{
1880 char_u *p;
1881 int i;
1882 int c;
1883
1884 /* loop over all the COMPOUNDRULE entries */
1885 for (p = slang->sl_comprules; *p != NUL; ++p)
1886 {
1887 /* loop over the flags in the compound word we have made, match
1888 * them against the current rule entry */
1889 for (i = 0; ; ++i)
1890 {
1891 c = compflags[i];
1892 if (c == NUL)
1893 /* found a rule that matches for the flags we have so far */
1894 return TRUE;
1895 if (*p == '/' || *p == NUL)
1896 break; /* end of rule, it's too short */
1897 if (*p == '[')
1898 {
1899 int match = FALSE;
1900
1901 /* compare against all the flags in [] */
1902 ++p;
1903 while (*p != ']' && *p != NUL)
1904 if (*p++ == c)
1905 match = TRUE;
1906 if (!match)
1907 break; /* none matches */
1908 }
1909 else if (*p != c)
1910 break; /* flag of word doesn't match flag in pattern */
1911 ++p;
1912 }
1913
1914 /* Skip to the next "/", where the next pattern starts. */
1915 p = vim_strchr(p, '/');
1916 if (p == NULL)
1917 break;
1918 }
1919
1920 /* Checked all the rules and none of them match the flags, so there
1921 * can't possibly be a compound starting with these flags. */
1922 return FALSE;
1923}
1924
1925/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001926 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1927 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001928 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001929 */
1930 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001931valid_word_prefix(
1932 int totprefcnt, /* nr of prefix IDs */
1933 int arridx, /* idx in sl_pidxs[] */
1934 int flags,
1935 char_u *word,
1936 slang_T *slang,
1937 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001938{
1939 int prefcnt;
1940 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001941 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001942 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001943
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001944 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001945 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1946 {
1947 pidx = slang->sl_pidxs[arridx + prefcnt];
1948
1949 /* Check the prefix ID. */
1950 if (prefid != (pidx & 0xff))
1951 continue;
1952
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001953 /* Check if the prefix doesn't combine and the word already has a
1954 * suffix. */
1955 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1956 continue;
1957
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001958 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001959 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001960 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1961 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001962 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001963 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001964 continue;
1965 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001966 else if (cond_req)
1967 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001968
Bram Moolenaar53805d12005-08-01 07:08:33 +00001969 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001970 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001971 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001972 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001973}
1974
1975/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001976 * Check if the word at "mip->mi_word" has a matching prefix.
1977 * If it does, then check the following word.
1978 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001979 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1980 * prefix in a compound word.
1981 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001982 * For a match mip->mi_result is updated.
1983 */
1984 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001985find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001986{
1987 idx_T arridx = 0;
1988 int len;
1989 int wlen = 0;
1990 int flen;
1991 int c;
1992 char_u *ptr;
1993 idx_T lo, hi, m;
1994 slang_T *slang = mip->mi_lp->lp_slang;
1995 char_u *byts;
1996 idx_T *idxs;
1997
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001998 byts = slang->sl_pbyts;
1999 if (byts == NULL)
2000 return; /* array is empty */
2001
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002002 /* We use the case-folded word here, since prefixes are always
2003 * case-folded. */
2004 ptr = mip->mi_fword;
2005 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002006 if (mode == FIND_COMPOUND)
2007 {
2008 /* Skip over the previously found word(s). */
2009 ptr += mip->mi_compoff;
2010 flen -= mip->mi_compoff;
2011 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002012 idxs = slang->sl_pidxs;
2013
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002014 /*
2015 * Repeat advancing in the tree until:
2016 * - there is a byte that doesn't match,
2017 * - we reach the end of the tree,
2018 * - or we reach the end of the line.
2019 */
2020 for (;;)
2021 {
2022 if (flen == 0 && *mip->mi_fend != NUL)
2023 flen = fold_more(mip);
2024
2025 len = byts[arridx++];
2026
2027 /* If the first possible byte is a zero the prefix could end here.
2028 * Check if the following word matches and supports the prefix. */
2029 if (byts[arridx] == 0)
2030 {
2031 /* There can be several prefixes with different conditions. We
2032 * try them all, since we don't know which one will give the
2033 * longest match. The word is the same each time, pass the list
2034 * of possible prefixes to find_word(). */
2035 mip->mi_prefarridx = arridx;
2036 mip->mi_prefcnt = len;
2037 while (len > 0 && byts[arridx] == 0)
2038 {
2039 ++arridx;
2040 --len;
2041 }
2042 mip->mi_prefcnt -= len;
2043
2044 /* Find the word that comes after the prefix. */
2045 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002046 if (mode == FIND_COMPOUND)
2047 /* Skip over the previously found word(s). */
2048 mip->mi_prefixlen += mip->mi_compoff;
2049
Bram Moolenaar53805d12005-08-01 07:08:33 +00002050#ifdef FEAT_MBYTE
2051 if (has_mbyte)
2052 {
2053 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002054 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2055 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002056 }
2057 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002058 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002059#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002060 find_word(mip, FIND_PREFIX);
2061
2062
2063 if (len == 0)
2064 break; /* no children, word must end here */
2065 }
2066
2067 /* Stop looking at end of the line. */
2068 if (ptr[wlen] == NUL)
2069 break;
2070
2071 /* Perform a binary search in the list of accepted bytes. */
2072 c = ptr[wlen];
2073 lo = arridx;
2074 hi = arridx + len - 1;
2075 while (lo < hi)
2076 {
2077 m = (lo + hi) / 2;
2078 if (byts[m] > c)
2079 hi = m - 1;
2080 else if (byts[m] < c)
2081 lo = m + 1;
2082 else
2083 {
2084 lo = hi = m;
2085 break;
2086 }
2087 }
2088
2089 /* Stop if there is no matching byte. */
2090 if (hi < lo || byts[lo] != c)
2091 break;
2092
2093 /* Continue at the child (if there is one). */
2094 arridx = idxs[lo];
2095 ++wlen;
2096 --flen;
2097 }
2098}
2099
2100/*
2101 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002102 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002103 * Return the length of the folded chars in bytes.
2104 */
2105 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002106fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002107{
2108 int flen;
2109 char_u *p;
2110
2111 p = mip->mi_fend;
2112 do
2113 {
2114 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002115 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002116
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002117 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002118 if (*mip->mi_fend != NUL)
2119 mb_ptr_adv(mip->mi_fend);
2120
2121 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2122 mip->mi_fword + mip->mi_fwordlen,
2123 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002124 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002125 mip->mi_fwordlen += flen;
2126 return flen;
2127}
2128
2129/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002130 * Check case flags for a word. Return TRUE if the word has the requested
2131 * case.
2132 */
2133 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002134spell_valid_case(
2135 int wordflags, /* flags for the checked word. */
2136 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002138 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002139 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002140 && ((treeflags & WF_ONECAP) == 0
2141 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002142}
2143
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002144/*
2145 * Return TRUE if spell checking is not enabled.
2146 */
2147 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002148no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002149{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002150 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2151 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002152 {
2153 EMSG(_("E756: Spell checking is not enabled"));
2154 return TRUE;
2155 }
2156 return FALSE;
2157}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002158
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159/*
2160 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002161 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2162 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002163 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2164 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002165 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002166 */
2167 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002168spell_move_to(
2169 win_T *wp,
2170 int dir, /* FORWARD or BACKWARD */
2171 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
2172 int curline,
2173 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002174 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002175{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002176 linenr_T lnum;
2177 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002178 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002179 char_u *line;
2180 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002181 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002182 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002184#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002185 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002186#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002187 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002188 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002189 char_u *buf = NULL;
2190 int buflen = 0;
2191 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002192 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002193 int found_one = FALSE;
2194 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002195
Bram Moolenaar95529562005-08-25 21:21:38 +00002196 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002197 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002198
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002199 /*
2200 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002201 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002202 *
2203 * When searching backwards, we continue in the line to find the last
2204 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002205 *
2206 * We concatenate the start of the next line, so that wrapped words work
2207 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2208 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002209 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002210 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002211 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002212
2213 while (!got_int)
2214 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002215 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002216
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002217 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002218 if (buflen < len + MAXWLEN + 2)
2219 {
2220 vim_free(buf);
2221 buflen = len + MAXWLEN + 2;
2222 buf = alloc(buflen);
2223 if (buf == NULL)
2224 break;
2225 }
2226
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002227 /* In first line check first word for Capital. */
2228 if (lnum == 1)
2229 capcol = 0;
2230
2231 /* For checking first word with a capital skip white space. */
2232 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002233 capcol = (int)(skipwhite(line) - line);
2234 else if (curline && wp == curwin)
2235 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002236 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002237 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002238 if (check_need_cap(lnum, col))
2239 capcol = col;
2240
2241 /* Need to get the line again, may have looked at the previous
2242 * one. */
2243 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2244 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002245
Bram Moolenaar0c405862005-06-22 22:26:26 +00002246 /* Copy the line into "buf" and append the start of the next line if
2247 * possible. */
2248 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002249 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002250 spell_cat_line(buf + STRLEN(buf),
2251 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002252
2253 p = buf + skip;
2254 endp = buf + len;
2255 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002256 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002257 /* When searching backward don't search after the cursor. Unless
2258 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002259 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002260 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002261 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002262 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002263 break;
2264
2265 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002266 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002267 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002268
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002269 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002270 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002271 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002272 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002273 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002274 /* When searching forward only accept a bad word after
2275 * the cursor. */
2276 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002277 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002278 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002279 && (wrapped
2280 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002281 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002282 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002284#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002285 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002286 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002287 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002288 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002289 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002290 if (!can_spell)
2291 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002292 }
2293 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002294#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002295 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002296
Bram Moolenaar51485f02005-06-04 21:55:20 +00002297 if (can_spell)
2298 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002299 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002300 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002301 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002302#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002303 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002304#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002305 if (dir == FORWARD)
2306 {
2307 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002308 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002309 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002310 if (attrp != NULL)
2311 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002312 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002313 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002314 else if (curline)
2315 /* Insert mode completion: put cursor after
2316 * the bad word. */
2317 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002318 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002319 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002321 else
2322 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324 }
2325
Bram Moolenaar51485f02005-06-04 21:55:20 +00002326 /* advance to character after the word */
2327 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002328 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002329 }
2330
Bram Moolenaar5195e452005-08-19 20:32:47 +00002331 if (dir == BACKWARD && found_pos.lnum != 0)
2332 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002333 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002334 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002335 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002336 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002337 }
2338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002340 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002341
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002342 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002343 if (dir == BACKWARD)
2344 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002345 /* If we are back at the starting line and searched it again there
2346 * is no match, give up. */
2347 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002348 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002349
2350 if (lnum > 1)
2351 --lnum;
2352 else if (!p_ws)
2353 break; /* at first line and 'nowrapscan' */
2354 else
2355 {
2356 /* Wrap around to the end of the buffer. May search the
2357 * starting line again and accept the last match. */
2358 lnum = wp->w_buffer->b_ml.ml_line_count;
2359 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002360 if (!shortmess(SHM_SEARCH))
2361 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002362 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002363 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002364 }
2365 else
2366 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002367 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2368 ++lnum;
2369 else if (!p_ws)
2370 break; /* at first line and 'nowrapscan' */
2371 else
2372 {
2373 /* Wrap around to the start of the buffer. May search the
2374 * starting line again and accept the first match. */
2375 lnum = 1;
2376 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002377 if (!shortmess(SHM_SEARCH))
2378 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002379 }
2380
2381 /* If we are back at the starting line and there is no match then
2382 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002383 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002384 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002385
2386 /* Skip the characters at the start of the next line that were
2387 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002388 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002389 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002390 else
2391 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002393 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002394 --capcol;
2395
2396 /* But after empty line check first word in next line */
2397 if (*skipwhite(line) == NUL)
2398 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002399 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400
2401 line_breakcheck();
2402 }
2403
Bram Moolenaar0c405862005-06-22 22:26:26 +00002404 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002405 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002406}
2407
2408/*
2409 * For spell checking: concatenate the start of the following line "line" into
2410 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002411 * Keep the blanks at the start of the next line, this is used in win_line()
2412 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002413 */
2414 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002415spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002416{
2417 char_u *p;
2418 int n;
2419
2420 p = skipwhite(line);
2421 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2422 p = skipwhite(p + 1);
2423
2424 if (*p != NUL)
2425 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002426 /* Only worth concatenating if there is something else than spaces to
2427 * concatenate. */
2428 n = (int)(p - line) + 1;
2429 if (n < maxlen - 1)
2430 {
2431 vim_memset(buf, ' ', n);
2432 vim_strncpy(buf + n, p, maxlen - 1 - n);
2433 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002434 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002435}
2436
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002437/*
2438 * Structure used for the cookie argument of do_in_runtimepath().
2439 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002440typedef struct spelload_S
2441{
2442 char_u sl_lang[MAXWLEN + 1]; /* language name */
2443 slang_T *sl_slang; /* resulting slang_T struct */
2444 int sl_nobreak; /* NOBREAK language found */
2445} spelload_T;
2446
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002447/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002448 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002449 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002450 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002451 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002452spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002454 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002455 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002456 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002457#ifdef FEAT_AUTOCMD
2458 int round;
2459#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002460
Bram Moolenaarb765d632005-06-07 21:00:02 +00002461 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002462 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002463 STRCPY(sl.sl_lang, lang);
2464 sl.sl_slang = NULL;
2465 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002466
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002467#ifdef FEAT_AUTOCMD
2468 /* We may retry when no spell file is found for the language, an
2469 * autocommand may load it then. */
2470 for (round = 1; round <= 2; ++round)
2471#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002472 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002473 /*
2474 * Find the first spell file for "lang" in 'runtimepath' and load it.
2475 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002476 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002477#ifdef VMS
2478 "spell/%s_%s.spl",
2479#else
2480 "spell/%s.%s.spl",
2481#endif
2482 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01002483 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002484
2485 if (r == FAIL && *sl.sl_lang != NUL)
2486 {
2487 /* Try loading the ASCII version. */
2488 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002489#ifdef VMS
2490 "spell/%s_ascii.spl",
2491#else
2492 "spell/%s.ascii.spl",
2493#endif
2494 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01002495 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002496
2497#ifdef FEAT_AUTOCMD
2498 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2499 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2500 curbuf->b_fname, FALSE, curbuf))
2501 continue;
2502 break;
2503#endif
2504 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002505#ifdef FEAT_AUTOCMD
2506 break;
2507#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002508 }
2509
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002510 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002511 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01002512 smsg((char_u *)
2513#ifdef VMS
2514 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2515#else
2516 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2517#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002519 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002520 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002521 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002522 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002523 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01002524 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002525 }
2526}
2527
2528/*
2529 * Return the encoding used for spell checking: Use 'encoding', except that we
2530 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2531 */
2532 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002533spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002534{
2535
2536#ifdef FEAT_MBYTE
2537 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2538 return p_enc;
2539#endif
2540 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541}
2542
2543/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002544 * Get the name of the .spl file for the internal wordlist into
2545 * "fname[MAXPATHL]".
2546 */
2547 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002548int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002549{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002550 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002551 int_wordlist, spell_enc());
2552}
2553
2554/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002555 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002556 * Caller must fill "sl_next".
2557 */
2558 static slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560{
2561 slang_T *lp;
2562
Bram Moolenaar51485f02005-06-04 21:55:20 +00002563 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002564 if (lp != NULL)
2565 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002566 if (lang != NULL)
2567 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002568 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002569 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002570 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002571 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002572 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 return lp;
2576}
2577
2578/*
2579 * Free the contents of an slang_T and the structure itself.
2580 */
2581 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002582slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002583{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002584 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002585 vim_free(lp->sl_fname);
2586 slang_clear(lp);
2587 vim_free(lp);
2588}
2589
2590/*
2591 * Clear an slang_T so that the file can be reloaded.
2592 */
2593 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002594slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002595{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002596 garray_T *gap;
2597 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002598 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002599 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002600 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601
Bram Moolenaar51485f02005-06-04 21:55:20 +00002602 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002603 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002604 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002605 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002606 vim_free(lp->sl_pbyts);
2607 lp->sl_pbyts = NULL;
2608
Bram Moolenaar51485f02005-06-04 21:55:20 +00002609 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002610 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002613 vim_free(lp->sl_pidxs);
2614 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002615
Bram Moolenaar4770d092006-01-12 23:22:24 +00002616 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002617 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002618 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2619 while (gap->ga_len > 0)
2620 {
2621 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2622 vim_free(ftp->ft_from);
2623 vim_free(ftp->ft_to);
2624 }
2625 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002627
2628 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002629 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002630 {
2631 /* "ga_len" is set to 1 without adding an item for latin1 */
2632 if (gap->ga_data != NULL)
2633 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2634 for (i = 0; i < gap->ga_len; ++i)
2635 vim_free(((int **)gap->ga_data)[i]);
2636 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002637 else
2638 /* SAL items: free salitem_T items */
2639 while (gap->ga_len > 0)
2640 {
2641 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2642 vim_free(smp->sm_lead);
2643 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2644 vim_free(smp->sm_to);
2645#ifdef FEAT_MBYTE
2646 vim_free(smp->sm_lead_w);
2647 vim_free(smp->sm_oneof_w);
2648 vim_free(smp->sm_to_w);
2649#endif
2650 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002651 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002652
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002653 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002654 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002655 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002656 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002657 lp->sl_prefprog = NULL;
2658
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002659 vim_free(lp->sl_info);
2660 lp->sl_info = NULL;
2661
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002662 vim_free(lp->sl_midword);
2663 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002664
Bram Moolenaar473de612013-06-08 18:19:48 +02002665 vim_regfree(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002666 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002667 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002668 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002669 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002670 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002671 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002672 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002673
2674 vim_free(lp->sl_syllable);
2675 lp->sl_syllable = NULL;
2676 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002677
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002678 ga_clear_strings(&lp->sl_comppat);
2679
Bram Moolenaar4770d092006-01-12 23:22:24 +00002680 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2681 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002682
Bram Moolenaar4770d092006-01-12 23:22:24 +00002683#ifdef FEAT_MBYTE
2684 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002685#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002686
Bram Moolenaar4770d092006-01-12 23:22:24 +00002687 /* Clear info from .sug file. */
2688 slang_clear_sug(lp);
2689
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002691 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002692 lp->sl_compsylmax = MAXWLEN;
2693 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002694}
2695
2696/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002697 * Clear the info from the .sug file in "lp".
2698 */
2699 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002700slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002701{
2702 vim_free(lp->sl_sbyts);
2703 lp->sl_sbyts = NULL;
2704 vim_free(lp->sl_sidxs);
2705 lp->sl_sidxs = NULL;
2706 close_spellbuf(lp->sl_sugbuf);
2707 lp->sl_sugbuf = NULL;
2708 lp->sl_sugloaded = FALSE;
2709 lp->sl_sugtime = 0;
2710}
2711
2712/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002713 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002714 * Invoked through do_in_runtimepath().
2715 */
2716 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002717spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002718{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002719 spelload_T *slp = (spelload_T *)cookie;
2720 slang_T *slang;
2721
2722 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2723 if (slang != NULL)
2724 {
2725 /* When a previously loaded file has NOBREAK also use it for the
2726 * ".add" files. */
2727 if (slp->sl_nobreak && slang->sl_add)
2728 slang->sl_nobreak = TRUE;
2729 else if (slang->sl_nobreak)
2730 slp->sl_nobreak = TRUE;
2731
2732 slp->sl_slang = slang;
2733 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002734}
2735
2736/*
2737 * Load one spell file and store the info into a slang_T.
2738 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002739 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002740 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2741 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2742 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2743 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002744 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002745 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2746 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002748 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002749 static slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002750spell_load_file(
2751 char_u *fname,
2752 char_u *lang,
2753 slang_T *old_lp,
2754 int silent) /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002755{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002757 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002758 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002759 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002760 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002761 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002762 char_u *save_sourcing_name = sourcing_name;
2763 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002764 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002765 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002766 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002767
Bram Moolenaarb765d632005-06-07 21:00:02 +00002768 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 if (fd == NULL)
2770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 if (!silent)
2772 EMSG2(_(e_notopen), fname);
2773 else if (p_verbose > 2)
2774 {
2775 verbose_enter();
2776 smsg((char_u *)e_notopen, fname);
2777 verbose_leave();
2778 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002779 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002780 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002781 if (p_verbose > 2)
2782 {
2783 verbose_enter();
2784 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2785 verbose_leave();
2786 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002787
Bram Moolenaarb765d632005-06-07 21:00:02 +00002788 if (old_lp == NULL)
2789 {
2790 lp = slang_alloc(lang);
2791 if (lp == NULL)
2792 goto endFAIL;
2793
2794 /* Remember the file name, used to reload the file when it's updated. */
2795 lp->sl_fname = vim_strsave(fname);
2796 if (lp->sl_fname == NULL)
2797 goto endFAIL;
2798
Bram Moolenaar56f78042010-12-08 17:09:32 +01002799 /* Check for .add.spl (_add.spl for VMS). */
2800 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002801 }
2802 else
2803 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002804
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002805 /* Set sourcing_name, so that error messages mention the file name. */
2806 sourcing_name = fname;
2807 sourcing_lnum = 0;
2808
Bram Moolenaar4770d092006-01-12 23:22:24 +00002809 /*
2810 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002811 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002812 for (i = 0; i < VIMSPELLMAGICL; ++i)
2813 buf[i] = getc(fd); /* <fileID> */
2814 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2815 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002816 EMSG(_("E757: This does not look like a spell file"));
2817 goto endFAIL;
2818 }
2819 c = getc(fd); /* <versionnr> */
2820 if (c < VIMSPELLVERSION)
2821 {
2822 EMSG(_("E771: Old spell file, needs to be updated"));
2823 goto endFAIL;
2824 }
2825 else if (c > VIMSPELLVERSION)
2826 {
2827 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002828 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002829 }
2830
Bram Moolenaar5195e452005-08-19 20:32:47 +00002831
2832 /*
2833 * <SECTIONS>: <section> ... <sectionend>
2834 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2835 */
2836 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002837 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002838 n = getc(fd); /* <sectionID> or <sectionend> */
2839 if (n == SN_END)
2840 break;
2841 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002842 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002843 if (len < 0)
2844 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002845
Bram Moolenaar5195e452005-08-19 20:32:47 +00002846 res = 0;
2847 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002848 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002849 case SN_INFO:
2850 lp->sl_info = read_string(fd, len); /* <infotext> */
2851 if (lp->sl_info == NULL)
2852 goto endFAIL;
2853 break;
2854
Bram Moolenaar5195e452005-08-19 20:32:47 +00002855 case SN_REGION:
2856 res = read_region_section(fd, lp, len);
2857 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002858
Bram Moolenaar5195e452005-08-19 20:32:47 +00002859 case SN_CHARFLAGS:
2860 res = read_charflags_section(fd);
2861 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002862
Bram Moolenaar5195e452005-08-19 20:32:47 +00002863 case SN_MIDWORD:
2864 lp->sl_midword = read_string(fd, len); /* <midword> */
2865 if (lp->sl_midword == NULL)
2866 goto endFAIL;
2867 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002868
Bram Moolenaar5195e452005-08-19 20:32:47 +00002869 case SN_PREFCOND:
2870 res = read_prefcond_section(fd, lp);
2871 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002872
Bram Moolenaar5195e452005-08-19 20:32:47 +00002873 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002874 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2875 break;
2876
2877 case SN_REPSAL:
2878 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002879 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002880
Bram Moolenaar5195e452005-08-19 20:32:47 +00002881 case SN_SAL:
2882 res = read_sal_section(fd, lp);
2883 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002884
Bram Moolenaar5195e452005-08-19 20:32:47 +00002885 case SN_SOFO:
2886 res = read_sofo_section(fd, lp);
2887 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002888
Bram Moolenaar5195e452005-08-19 20:32:47 +00002889 case SN_MAP:
2890 p = read_string(fd, len); /* <mapstr> */
2891 if (p == NULL)
2892 goto endFAIL;
2893 set_map_str(lp, p);
2894 vim_free(p);
2895 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002896
Bram Moolenaar4770d092006-01-12 23:22:24 +00002897 case SN_WORDS:
2898 res = read_words_section(fd, lp, len);
2899 break;
2900
2901 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002902 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002903 break;
2904
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002905 case SN_NOSPLITSUGS:
Bram Moolenaar7b877b32016-01-09 13:51:34 +01002906 lp->sl_nosplitsugs = TRUE;
2907 break;
2908
2909 case SN_NOCOMPOUNDSUGS:
2910 lp->sl_nocompoundsugs = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002911 break;
2912
Bram Moolenaar5195e452005-08-19 20:32:47 +00002913 case SN_COMPOUND:
2914 res = read_compound(fd, lp, len);
2915 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002916
Bram Moolenaar78622822005-08-23 21:00:13 +00002917 case SN_NOBREAK:
2918 lp->sl_nobreak = TRUE;
2919 break;
2920
Bram Moolenaar5195e452005-08-19 20:32:47 +00002921 case SN_SYLLABLE:
2922 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2923 if (lp->sl_syllable == NULL)
2924 goto endFAIL;
2925 if (init_syl_tab(lp) == FAIL)
2926 goto endFAIL;
2927 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002928
Bram Moolenaar5195e452005-08-19 20:32:47 +00002929 default:
2930 /* Unsupported section. When it's required give an error
2931 * message. When it's not required skip the contents. */
2932 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002933 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002934 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002935 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002936 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002937 while (--len >= 0)
2938 if (getc(fd) < 0)
2939 goto truncerr;
2940 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002941 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002942someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 if (res == SP_FORMERROR)
2944 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002945 EMSG(_(e_format));
2946 goto endFAIL;
2947 }
2948 if (res == SP_TRUNCERROR)
2949 {
2950truncerr:
2951 EMSG(_(e_spell_trunc));
2952 goto endFAIL;
2953 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002954 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002955 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002956 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002957
Bram Moolenaar4770d092006-01-12 23:22:24 +00002958 /* <LWORDTREE> */
2959 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2960 if (res != 0)
2961 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002962
Bram Moolenaar4770d092006-01-12 23:22:24 +00002963 /* <KWORDTREE> */
2964 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2965 if (res != 0)
2966 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002967
Bram Moolenaar4770d092006-01-12 23:22:24 +00002968 /* <PREFIXTREE> */
2969 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2970 lp->sl_prefixcnt);
2971 if (res != 0)
2972 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973
Bram Moolenaarb765d632005-06-07 21:00:02 +00002974 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002975 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002976 {
2977 lp->sl_next = first_lang;
2978 first_lang = lp;
2979 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002980
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002981 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002982
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002983endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002984 if (lang != NULL)
2985 /* truncating the name signals the error to spell_load_lang() */
2986 *lang = NUL;
2987 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002988 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002989 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002990
2991endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002992 if (fd != NULL)
2993 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002994 sourcing_name = save_sourcing_name;
2995 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002996
2997 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002998}
2999
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003000/*
3001 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003002 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003003 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003004 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3005 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003006 */
3007 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003008read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003009{
3010 int cnt = 0;
3011 int i;
3012 char_u *str;
3013
3014 /* read the length bytes, MSB first */
3015 for (i = 0; i < cnt_bytes; ++i)
3016 cnt = (cnt << 8) + getc(fd);
3017 if (cnt < 0)
3018 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003019 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003020 return NULL;
3021 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003022 *cntp = cnt;
3023 if (cnt == 0)
3024 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003025
Bram Moolenaar5195e452005-08-19 20:32:47 +00003026 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003027 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003028 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003029 return str;
3030}
3031
Bram Moolenaar7887d882005-07-01 22:33:52 +00003032/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003033 * Read SN_REGION: <regionname> ...
3034 * Return SP_*ERROR flags.
3035 */
3036 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003037read_region_section(FILE *fd, slang_T *lp, int len)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003038{
3039 int i;
3040
3041 if (len > 16)
3042 return SP_FORMERROR;
3043 for (i = 0; i < len; ++i)
3044 lp->sl_regions[i] = getc(fd); /* <regionname> */
3045 lp->sl_regions[len] = NUL;
3046 return 0;
3047}
3048
3049/*
3050 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3051 * <folcharslen> <folchars>
3052 * Return SP_*ERROR flags.
3053 */
3054 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003055read_charflags_section(FILE *fd)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003056{
3057 char_u *flags;
3058 char_u *fol;
3059 int flagslen, follen;
3060
3061 /* <charflagslen> <charflags> */
3062 flags = read_cnt_string(fd, 1, &flagslen);
3063 if (flagslen < 0)
3064 return flagslen;
3065
3066 /* <folcharslen> <folchars> */
3067 fol = read_cnt_string(fd, 2, &follen);
3068 if (follen < 0)
3069 {
3070 vim_free(flags);
3071 return follen;
3072 }
3073
3074 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3075 if (flags != NULL && fol != NULL)
3076 set_spell_charflags(flags, flagslen, fol);
3077
3078 vim_free(flags);
3079 vim_free(fol);
3080
3081 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3082 if ((flags == NULL) != (fol == NULL))
3083 return SP_FORMERROR;
3084 return 0;
3085}
3086
3087/*
3088 * Read SN_PREFCOND section.
3089 * Return SP_*ERROR flags.
3090 */
3091 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003092read_prefcond_section(FILE *fd, slang_T *lp)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003093{
3094 int cnt;
3095 int i;
3096 int n;
3097 char_u *p;
3098 char_u buf[MAXWLEN + 1];
3099
3100 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003101 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003102 if (cnt <= 0)
3103 return SP_FORMERROR;
3104
3105 lp->sl_prefprog = (regprog_T **)alloc_clear(
3106 (unsigned)sizeof(regprog_T *) * cnt);
3107 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003108 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003109 lp->sl_prefixcnt = cnt;
3110
3111 for (i = 0; i < cnt; ++i)
3112 {
3113 /* <prefcond> : <condlen> <condstr> */
3114 n = getc(fd); /* <condlen> */
3115 if (n < 0 || n >= MAXWLEN)
3116 return SP_FORMERROR;
3117
3118 /* When <condlen> is zero we have an empty condition. Otherwise
3119 * compile the regexp program used to check for the condition. */
3120 if (n > 0)
3121 {
3122 buf[0] = '^'; /* always match at one position only */
3123 p = buf + 1;
3124 while (n-- > 0)
3125 *p++ = getc(fd); /* <condstr> */
3126 *p = NUL;
3127 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3128 }
3129 }
3130 return 0;
3131}
3132
3133/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003134 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003135 * Return SP_*ERROR flags.
3136 */
3137 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003138read_rep_section(FILE *fd, garray_T *gap, short *first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003139{
3140 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003141 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003142 int i;
3143
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003144 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003145 if (cnt < 0)
3146 return SP_TRUNCERROR;
3147
Bram Moolenaar5195e452005-08-19 20:32:47 +00003148 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003149 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150
3151 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3152 for (; gap->ga_len < cnt; ++gap->ga_len)
3153 {
3154 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3155 ftp->ft_from = read_cnt_string(fd, 1, &i);
3156 if (i < 0)
3157 return i;
3158 if (i == 0)
3159 return SP_FORMERROR;
3160 ftp->ft_to = read_cnt_string(fd, 1, &i);
3161 if (i <= 0)
3162 {
3163 vim_free(ftp->ft_from);
3164 if (i < 0)
3165 return i;
3166 return SP_FORMERROR;
3167 }
3168 }
3169
3170 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003171 for (i = 0; i < 256; ++i)
3172 first[i] = -1;
3173 for (i = 0; i < gap->ga_len; ++i)
3174 {
3175 ftp = &((fromto_T *)gap->ga_data)[i];
3176 if (first[*ftp->ft_from] == -1)
3177 first[*ftp->ft_from] = i;
3178 }
3179 return 0;
3180}
3181
3182/*
3183 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3184 * Return SP_*ERROR flags.
3185 */
3186 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003187read_sal_section(FILE *fd, slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003188{
3189 int i;
3190 int cnt;
3191 garray_T *gap;
3192 salitem_T *smp;
3193 int ccnt;
3194 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003195 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003196
3197 slang->sl_sofo = FALSE;
3198
3199 i = getc(fd); /* <salflags> */
3200 if (i & SAL_F0LLOWUP)
3201 slang->sl_followup = TRUE;
3202 if (i & SAL_COLLAPSE)
3203 slang->sl_collapse = TRUE;
3204 if (i & SAL_REM_ACCENTS)
3205 slang->sl_rem_accents = TRUE;
3206
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003207 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003208 if (cnt < 0)
3209 return SP_TRUNCERROR;
3210
3211 gap = &slang->sl_sal;
3212 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003213 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003214 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003215
3216 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3217 for (; gap->ga_len < cnt; ++gap->ga_len)
3218 {
3219 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3220 ccnt = getc(fd); /* <salfromlen> */
3221 if (ccnt < 0)
3222 return SP_TRUNCERROR;
3223 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003224 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003225 smp->sm_lead = p;
3226
3227 /* Read up to the first special char into sm_lead. */
3228 for (i = 0; i < ccnt; ++i)
3229 {
3230 c = getc(fd); /* <salfrom> */
3231 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3232 break;
3233 *p++ = c;
3234 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003235 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003236 *p++ = NUL;
3237
3238 /* Put (abc) chars in sm_oneof, if any. */
3239 if (c == '(')
3240 {
3241 smp->sm_oneof = p;
3242 for (++i; i < ccnt; ++i)
3243 {
3244 c = getc(fd); /* <salfrom> */
3245 if (c == ')')
3246 break;
3247 *p++ = c;
3248 }
3249 *p++ = NUL;
3250 if (++i < ccnt)
3251 c = getc(fd);
3252 }
3253 else
3254 smp->sm_oneof = NULL;
3255
3256 /* Any following chars go in sm_rules. */
3257 smp->sm_rules = p;
3258 if (i < ccnt)
3259 /* store the char we got while checking for end of sm_lead */
3260 *p++ = c;
3261 for (++i; i < ccnt; ++i)
3262 *p++ = getc(fd); /* <salfrom> */
3263 *p++ = NUL;
3264
3265 /* <saltolen> <salto> */
3266 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3267 if (ccnt < 0)
3268 {
3269 vim_free(smp->sm_lead);
3270 return ccnt;
3271 }
3272
3273#ifdef FEAT_MBYTE
3274 if (has_mbyte)
3275 {
3276 /* convert the multi-byte strings to wide char strings */
3277 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3278 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3279 if (smp->sm_oneof == NULL)
3280 smp->sm_oneof_w = NULL;
3281 else
3282 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3283 if (smp->sm_to == NULL)
3284 smp->sm_to_w = NULL;
3285 else
3286 smp->sm_to_w = mb_str2wide(smp->sm_to);
3287 if (smp->sm_lead_w == NULL
3288 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3289 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3290 {
3291 vim_free(smp->sm_lead);
3292 vim_free(smp->sm_to);
3293 vim_free(smp->sm_lead_w);
3294 vim_free(smp->sm_oneof_w);
3295 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003296 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003297 }
3298 }
3299#endif
3300 }
3301
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003302 if (gap->ga_len > 0)
3303 {
3304 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3305 * that we need to check the index every time. */
3306 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3307 if ((p = alloc(1)) == NULL)
3308 return SP_OTHERERROR;
3309 p[0] = NUL;
3310 smp->sm_lead = p;
3311 smp->sm_leadlen = 0;
3312 smp->sm_oneof = NULL;
3313 smp->sm_rules = p;
3314 smp->sm_to = NULL;
3315#ifdef FEAT_MBYTE
3316 if (has_mbyte)
3317 {
3318 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3319 smp->sm_leadlen = 0;
3320 smp->sm_oneof_w = NULL;
3321 smp->sm_to_w = NULL;
3322 }
3323#endif
3324 ++gap->ga_len;
3325 }
3326
Bram Moolenaar5195e452005-08-19 20:32:47 +00003327 /* Fill the first-index table. */
3328 set_sal_first(slang);
3329
3330 return 0;
3331}
3332
3333/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003334 * Read SN_WORDS: <word> ...
3335 * Return SP_*ERROR flags.
3336 */
3337 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003338read_words_section(FILE *fd, slang_T *lp, int len)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003339{
3340 int done = 0;
3341 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003342 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003343 char_u word[MAXWLEN];
3344
3345 while (done < len)
3346 {
3347 /* Read one word at a time. */
3348 for (i = 0; ; ++i)
3349 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003350 c = getc(fd);
3351 if (c == EOF)
3352 return SP_TRUNCERROR;
3353 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003354 if (word[i] == NUL)
3355 break;
3356 if (i == MAXWLEN - 1)
3357 return SP_FORMERROR;
3358 }
3359
3360 /* Init the count to 10. */
3361 count_common_word(lp, word, -1, 10);
3362 done += i + 1;
3363 }
3364 return 0;
3365}
3366
3367/*
3368 * Add a word to the hashtable of common words.
3369 * If it's already there then the counter is increased.
3370 */
3371 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003372count_common_word(
3373 slang_T *lp,
3374 char_u *word,
3375 int len, /* word length, -1 for upto NUL */
3376 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003377{
3378 hash_T hash;
3379 hashitem_T *hi;
3380 wordcount_T *wc;
3381 char_u buf[MAXWLEN];
3382 char_u *p;
3383
3384 if (len == -1)
3385 p = word;
3386 else
3387 {
3388 vim_strncpy(buf, word, len);
3389 p = buf;
3390 }
3391
3392 hash = hash_hash(p);
3393 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3394 if (HASHITEM_EMPTY(hi))
3395 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003396 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003397 if (wc == NULL)
3398 return;
3399 STRCPY(wc->wc_word, p);
3400 wc->wc_count = count;
3401 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3402 }
3403 else
3404 {
3405 wc = HI2WC(hi);
3406 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3407 wc->wc_count = MAXWORDCOUNT;
3408 }
3409}
3410
3411/*
3412 * Adjust the score of common words.
3413 */
3414 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003415score_wordcount_adj(
3416 slang_T *slang,
3417 int score,
3418 char_u *word,
3419 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003420{
3421 hashitem_T *hi;
3422 wordcount_T *wc;
3423 int bonus;
3424 int newscore;
3425
3426 hi = hash_find(&slang->sl_wordcount, word);
3427 if (!HASHITEM_EMPTY(hi))
3428 {
3429 wc = HI2WC(hi);
3430 if (wc->wc_count < SCORE_THRES2)
3431 bonus = SCORE_COMMON1;
3432 else if (wc->wc_count < SCORE_THRES3)
3433 bonus = SCORE_COMMON2;
3434 else
3435 bonus = SCORE_COMMON3;
3436 if (split)
3437 newscore = score - bonus / 2;
3438 else
3439 newscore = score - bonus;
3440 if (newscore < 0)
3441 return 0;
3442 return newscore;
3443 }
3444 return score;
3445}
3446
3447/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003448 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3449 * Return SP_*ERROR flags.
3450 */
3451 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003452read_sofo_section(FILE *fd, slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003453{
3454 int cnt;
3455 char_u *from, *to;
3456 int res;
3457
3458 slang->sl_sofo = TRUE;
3459
3460 /* <sofofromlen> <sofofrom> */
3461 from = read_cnt_string(fd, 2, &cnt);
3462 if (cnt < 0)
3463 return cnt;
3464
3465 /* <sofotolen> <sofoto> */
3466 to = read_cnt_string(fd, 2, &cnt);
3467 if (cnt < 0)
3468 {
3469 vim_free(from);
3470 return cnt;
3471 }
3472
3473 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3474 if (from != NULL && to != NULL)
3475 res = set_sofo(slang, from, to);
3476 else if (from != NULL || to != NULL)
3477 res = SP_FORMERROR; /* only one of two strings is an error */
3478 else
3479 res = 0;
3480
3481 vim_free(from);
3482 vim_free(to);
3483 return res;
3484}
3485
3486/*
3487 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003488 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003489 * Returns SP_*ERROR flags.
3490 */
3491 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003492read_compound(FILE *fd, slang_T *slang, int len)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003493{
3494 int todo = len;
3495 int c;
3496 int atstart;
3497 char_u *pat;
3498 char_u *pp;
3499 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003500 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003501 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003502 int cnt;
3503 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003504
3505 if (todo < 2)
3506 return SP_FORMERROR; /* need at least two bytes */
3507
3508 --todo;
3509 c = getc(fd); /* <compmax> */
3510 if (c < 2)
3511 c = MAXWLEN;
3512 slang->sl_compmax = c;
3513
3514 --todo;
3515 c = getc(fd); /* <compminlen> */
3516 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003517 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003518 slang->sl_compminlen = c;
3519
3520 --todo;
3521 c = getc(fd); /* <compsylmax> */
3522 if (c < 1)
3523 c = MAXWLEN;
3524 slang->sl_compsylmax = c;
3525
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003526 c = getc(fd); /* <compoptions> */
3527 if (c != 0)
3528 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3529 else
3530 {
3531 --todo;
3532 c = getc(fd); /* only use the lower byte for now */
3533 --todo;
3534 slang->sl_compoptions = c;
3535
3536 gap = &slang->sl_comppat;
3537 c = get2c(fd); /* <comppatcount> */
3538 todo -= 2;
3539 ga_init2(gap, sizeof(char_u *), c);
3540 if (ga_grow(gap, c) == OK)
3541 while (--c >= 0)
3542 {
3543 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3544 read_cnt_string(fd, 1, &cnt);
3545 /* <comppatlen> <comppattext> */
3546 if (cnt < 0)
3547 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003548 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003549 }
3550 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003551 if (todo < 0)
3552 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003553
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003554 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003555 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003556 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3557 * Conversion to utf-8 may double the size. */
3558 c = todo * 2 + 7;
3559#ifdef FEAT_MBYTE
3560 if (enc_utf8)
3561 c += todo * 2;
3562#endif
3563 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003564 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003565 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003566
Bram Moolenaard12a1322005-08-21 22:08:24 +00003567 /* We also need a list of all flags that can appear at the start and one
3568 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003569 cp = alloc(todo + 1);
3570 if (cp == NULL)
3571 {
3572 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003573 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003574 }
3575 slang->sl_compstartflags = cp;
3576 *cp = NUL;
3577
Bram Moolenaard12a1322005-08-21 22:08:24 +00003578 ap = alloc(todo + 1);
3579 if (ap == NULL)
3580 {
3581 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003582 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003583 }
3584 slang->sl_compallflags = ap;
3585 *ap = NUL;
3586
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003587 /* And a list of all patterns in their original form, for checking whether
3588 * compounding may work in match_compoundrule(). This is freed when we
3589 * encounter a wildcard, the check doesn't work then. */
3590 crp = alloc(todo + 1);
3591 slang->sl_comprules = crp;
3592
Bram Moolenaar5195e452005-08-19 20:32:47 +00003593 pp = pat;
3594 *pp++ = '^';
3595 *pp++ = '\\';
3596 *pp++ = '(';
3597
3598 atstart = 1;
3599 while (todo-- > 0)
3600 {
3601 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003602 if (c == EOF)
3603 {
3604 vim_free(pat);
3605 return SP_TRUNCERROR;
3606 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003607
3608 /* Add all flags to "sl_compallflags". */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003609 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003610 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003611 {
3612 *ap++ = c;
3613 *ap = NUL;
3614 }
3615
Bram Moolenaar5195e452005-08-19 20:32:47 +00003616 if (atstart != 0)
3617 {
3618 /* At start of item: copy flags to "sl_compstartflags". For a
3619 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3620 if (c == '[')
3621 atstart = 2;
3622 else if (c == ']')
3623 atstart = 0;
3624 else
3625 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003626 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003627 {
3628 *cp++ = c;
3629 *cp = NUL;
3630 }
3631 if (atstart == 1)
3632 atstart = 0;
3633 }
3634 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003635
3636 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3637 if (crp != NULL)
3638 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003639 if (c == '?' || c == '+' || c == '*')
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003640 {
3641 vim_free(slang->sl_comprules);
3642 slang->sl_comprules = NULL;
3643 crp = NULL;
3644 }
3645 else
3646 *crp++ = c;
3647 }
3648
Bram Moolenaar5195e452005-08-19 20:32:47 +00003649 if (c == '/') /* slash separates two items */
3650 {
3651 *pp++ = '\\';
3652 *pp++ = '|';
3653 atstart = 1;
3654 }
3655 else /* normal char, "[abc]" and '*' are copied as-is */
3656 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003657 if (c == '?' || c == '+' || c == '~')
3658 *pp++ = '\\'; /* "a?" becomes "a\?", "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003659#ifdef FEAT_MBYTE
3660 if (enc_utf8)
3661 pp += mb_char2bytes(c, pp);
3662 else
3663#endif
3664 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003665 }
3666 }
3667
3668 *pp++ = '\\';
3669 *pp++ = ')';
3670 *pp++ = '$';
3671 *pp = NUL;
3672
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003673 if (crp != NULL)
3674 *crp = NUL;
3675
Bram Moolenaar5195e452005-08-19 20:32:47 +00003676 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3677 vim_free(pat);
3678 if (slang->sl_compprog == NULL)
3679 return SP_FORMERROR;
3680
3681 return 0;
3682}
3683
Bram Moolenaar6de68532005-08-24 22:08:48 +00003684/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003685 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003686 * Like strchr() but independent of locale.
3687 */
3688 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003689byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003690{
3691 char_u *p;
3692
3693 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003694 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003695 return TRUE;
3696 return FALSE;
3697}
3698
Bram Moolenaar5195e452005-08-19 20:32:47 +00003699#define SY_MAXLEN 30
3700typedef struct syl_item_S
3701{
3702 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3703 int sy_len;
3704} syl_item_T;
3705
3706/*
3707 * Truncate "slang->sl_syllable" at the first slash and put the following items
3708 * in "slang->sl_syl_items".
3709 */
3710 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003711init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003712{
3713 char_u *p;
3714 char_u *s;
3715 int l;
3716 syl_item_T *syl;
3717
3718 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3719 p = vim_strchr(slang->sl_syllable, '/');
3720 while (p != NULL)
3721 {
3722 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003723 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003724 break;
3725 s = p;
3726 p = vim_strchr(p, '/');
3727 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003728 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003729 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003730 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003731 if (l >= SY_MAXLEN)
3732 return SP_FORMERROR;
3733 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003734 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003735 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3736 + slang->sl_syl_items.ga_len++;
3737 vim_strncpy(syl->sy_chars, s, l);
3738 syl->sy_len = l;
3739 }
3740 return OK;
3741}
3742
3743/*
3744 * Count the number of syllables in "word".
3745 * When "word" contains spaces the syllables after the last space are counted.
3746 * Returns zero if syllables are not defines.
3747 */
3748 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003749count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003750{
3751 int cnt = 0;
3752 int skip = FALSE;
3753 char_u *p;
3754 int len;
3755 int i;
3756 syl_item_T *syl;
3757 int c;
3758
3759 if (slang->sl_syllable == NULL)
3760 return 0;
3761
3762 for (p = word; *p != NUL; p += len)
3763 {
3764 /* When running into a space reset counter. */
3765 if (*p == ' ')
3766 {
3767 len = 1;
3768 cnt = 0;
3769 continue;
3770 }
3771
3772 /* Find longest match of syllable items. */
3773 len = 0;
3774 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3775 {
3776 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3777 if (syl->sy_len > len
3778 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3779 len = syl->sy_len;
3780 }
3781 if (len != 0) /* found a match, count syllable */
3782 {
3783 ++cnt;
3784 skip = FALSE;
3785 }
3786 else
3787 {
3788 /* No recognized syllable item, at least a syllable char then? */
3789#ifdef FEAT_MBYTE
3790 c = mb_ptr2char(p);
3791 len = (*mb_ptr2len)(p);
3792#else
3793 c = *p;
3794 len = 1;
3795#endif
3796 if (vim_strchr(slang->sl_syllable, c) == NULL)
3797 skip = FALSE; /* No, search for next syllable */
3798 else if (!skip)
3799 {
3800 ++cnt; /* Yes, count it */
3801 skip = TRUE; /* don't count following syllable chars */
3802 }
3803 }
3804 }
3805 return cnt;
3806}
3807
3808/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003809 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003810 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003811 */
3812 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003813set_sofo(slang_T *lp, char_u *from, char_u *to)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003814{
3815 int i;
3816
3817#ifdef FEAT_MBYTE
3818 garray_T *gap;
3819 char_u *s;
3820 char_u *p;
3821 int c;
3822 int *inp;
3823
3824 if (has_mbyte)
3825 {
3826 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3827 * characters. The index is the low byte of the character.
3828 * The list contains from-to pairs with a terminating NUL.
3829 * sl_sal_first[] is used for latin1 "from" characters. */
3830 gap = &lp->sl_sal;
3831 ga_init2(gap, sizeof(int *), 1);
3832 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003833 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003834 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3835 gap->ga_len = 256;
3836
3837 /* First count the number of items for each list. Temporarily use
3838 * sl_sal_first[] for this. */
3839 for (p = from, s = to; *p != NUL && *s != NUL; )
3840 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003841 c = mb_cptr2char_adv(&p);
3842 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003843 if (c >= 256)
3844 ++lp->sl_sal_first[c & 0xff];
3845 }
3846 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003847 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003848
3849 /* Allocate the lists. */
3850 for (i = 0; i < 256; ++i)
3851 if (lp->sl_sal_first[i] > 0)
3852 {
3853 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3854 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003855 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003856 ((int **)gap->ga_data)[i] = (int *)p;
3857 *(int *)p = 0;
3858 }
3859
3860 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3861 * list. */
3862 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3863 for (p = from, s = to; *p != NUL && *s != NUL; )
3864 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003865 c = mb_cptr2char_adv(&p);
3866 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003867 if (c >= 256)
3868 {
3869 /* Append the from-to chars at the end of the list with
3870 * the low byte. */
3871 inp = ((int **)gap->ga_data)[c & 0xff];
3872 while (*inp != 0)
3873 ++inp;
3874 *inp++ = c; /* from char */
3875 *inp++ = i; /* to char */
3876 *inp++ = NUL; /* NUL at the end */
3877 }
3878 else
3879 /* mapping byte to char is done in sl_sal_first[] */
3880 lp->sl_sal_first[c] = i;
3881 }
3882 }
3883 else
3884#endif
3885 {
3886 /* mapping bytes to bytes is done in sl_sal_first[] */
3887 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003888 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003889
3890 for (i = 0; to[i] != NUL; ++i)
3891 lp->sl_sal_first[from[i]] = to[i];
3892 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3893 }
3894
Bram Moolenaar5195e452005-08-19 20:32:47 +00003895 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003896}
3897
3898/*
3899 * Fill the first-index table for "lp".
3900 */
3901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003902set_sal_first(slang_T *lp)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003903{
3904 salfirst_T *sfirst;
3905 int i;
3906 salitem_T *smp;
3907 int c;
3908 garray_T *gap = &lp->sl_sal;
3909
3910 sfirst = lp->sl_sal_first;
3911 for (i = 0; i < 256; ++i)
3912 sfirst[i] = -1;
3913 smp = (salitem_T *)gap->ga_data;
3914 for (i = 0; i < gap->ga_len; ++i)
3915 {
3916#ifdef FEAT_MBYTE
3917 if (has_mbyte)
3918 /* Use the lowest byte of the first character. For latin1 it's
3919 * the character, for other encodings it should differ for most
3920 * characters. */
3921 c = *smp[i].sm_lead_w & 0xff;
3922 else
3923#endif
3924 c = *smp[i].sm_lead;
3925 if (sfirst[c] == -1)
3926 {
3927 sfirst[c] = i;
3928#ifdef FEAT_MBYTE
3929 if (has_mbyte)
3930 {
3931 int n;
3932
3933 /* Make sure all entries with this byte are following each
3934 * other. Move the ones that are in the wrong position. Do
3935 * keep the same ordering! */
3936 while (i + 1 < gap->ga_len
3937 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3938 /* Skip over entry with same index byte. */
3939 ++i;
3940
3941 for (n = 1; i + n < gap->ga_len; ++n)
3942 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3943 {
3944 salitem_T tsal;
3945
3946 /* Move entry with same index byte after the entries
3947 * we already found. */
3948 ++i;
3949 --n;
3950 tsal = smp[i + n];
3951 mch_memmove(smp + i + 1, smp + i,
3952 sizeof(salitem_T) * n);
3953 smp[i] = tsal;
3954 }
3955 }
3956#endif
3957 }
3958 }
3959}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003960
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003961#ifdef FEAT_MBYTE
3962/*
3963 * Turn a multi-byte string into a wide character string.
3964 * Return it in allocated memory (NULL for out-of-memory)
3965 */
3966 static int *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003967mb_str2wide(char_u *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003968{
3969 int *res;
3970 char_u *p;
3971 int i = 0;
3972
3973 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3974 if (res != NULL)
3975 {
3976 for (p = s; *p != NUL; )
3977 res[i++] = mb_ptr2char_adv(&p);
3978 res[i] = NUL;
3979 }
3980 return res;
3981}
3982#endif
3983
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003984/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003985 * Read a tree from the .spl or .sug file.
3986 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3987 * This is skipped when the tree has zero length.
3988 * Returns zero when OK, SP_ value for an error.
3989 */
3990 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003991spell_read_tree(
3992 FILE *fd,
3993 char_u **bytsp,
3994 idx_T **idxsp,
3995 int prefixtree, /* TRUE for the prefix tree */
3996 int prefixcnt) /* when "prefixtree" is TRUE: prefix count */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003997{
3998 int len;
3999 int idx;
4000 char_u *bp;
4001 idx_T *ip;
4002
4003 /* The tree size was computed when writing the file, so that we can
4004 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004005 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004006 if (len < 0)
4007 return SP_TRUNCERROR;
4008 if (len > 0)
4009 {
4010 /* Allocate the byte array. */
4011 bp = lalloc((long_u)len, TRUE);
4012 if (bp == NULL)
4013 return SP_OTHERERROR;
4014 *bytsp = bp;
4015
4016 /* Allocate the index array. */
4017 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4018 if (ip == NULL)
4019 return SP_OTHERERROR;
4020 *idxsp = ip;
4021
4022 /* Recursively read the tree and store it in the array. */
4023 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4024 if (idx < 0)
4025 return idx;
4026 }
4027 return 0;
4028}
4029
4030/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004031 * Read one row of siblings from the spell file and store it in the byte array
4032 * "byts" and index array "idxs". Recursively read the children.
4033 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004034 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004035 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004036 * Returns the index (>= 0) following the siblings.
4037 * Returns SP_TRUNCERROR if the file is shorter than expected.
4038 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004039 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004040 static idx_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004041read_tree_node(
4042 FILE *fd,
4043 char_u *byts,
4044 idx_T *idxs,
4045 int maxidx, /* size of arrays */
4046 idx_T startidx, /* current index in "byts" and "idxs" */
4047 int prefixtree, /* TRUE for reading PREFIXTREE */
4048 int maxprefcondnr) /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004049{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004050 int len;
4051 int i;
4052 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004053 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004054 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004055 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004056#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004057
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058 len = getc(fd); /* <siblingcount> */
4059 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004060 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004061
4062 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004063 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004064 byts[idx++] = len;
4065
4066 /* Read the byte values, flag/region bytes and shared indexes. */
4067 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004068 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 c = getc(fd); /* <byte> */
4070 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004071 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004072 if (c <= BY_SPECIAL)
4073 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004074 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 {
4076 /* No flags, all regions. */
4077 idxs[idx] = 0;
4078 c = 0;
4079 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004080 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004081 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004082 if (prefixtree)
4083 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004084 /* Read the optional pflags byte, the prefix ID and the
4085 * condition nr. In idxs[] store the prefix ID in the low
4086 * byte, the condition index shifted up 8 bits, the flags
4087 * shifted up 24 bits. */
4088 if (c == BY_FLAGS)
4089 c = getc(fd) << 24; /* <pflags> */
4090 else
4091 c = 0;
4092
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004093 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004094
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004095 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004096 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004097 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004098 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004099 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004100 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004101 {
4102 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004103 * idxs[] the flags go in the low two bytes, region above
4104 * that and prefix ID above the region. */
4105 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004106 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004107 if (c2 == BY_FLAGS2)
4108 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004109 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004110 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004111 if (c & WF_AFX)
4112 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004113 }
4114
Bram Moolenaar51485f02005-06-04 21:55:20 +00004115 idxs[idx] = c;
4116 c = 0;
4117 }
4118 else /* c == BY_INDEX */
4119 {
4120 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004121 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004122 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004123 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004124 idxs[idx] = n + SHARED_MASK;
4125 c = getc(fd); /* <xbyte> */
4126 }
4127 }
4128 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004129 }
4130
Bram Moolenaar51485f02005-06-04 21:55:20 +00004131 /* Recursively read the children for non-shared siblings.
4132 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4133 * remove SHARED_MASK) */
4134 for (i = 1; i <= len; ++i)
4135 if (byts[startidx + i] != 0)
4136 {
4137 if (idxs[startidx + i] & SHARED_MASK)
4138 idxs[startidx + i] &= ~SHARED_MASK;
4139 else
4140 {
4141 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004142 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004143 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004144 if (idx < 0)
4145 break;
4146 }
4147 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004148
Bram Moolenaar51485f02005-06-04 21:55:20 +00004149 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004150}
4151
4152/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004153 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004154 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004155 */
4156 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004157did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004158{
4159 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004160 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004161 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004162 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004163 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004164 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004165 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004166 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004167 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004169 int len;
4170 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004171 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004172 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004173 char_u *use_region = NULL;
4174 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004175 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004176 int i, j;
4177 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004178 static int recursive = FALSE;
4179 char_u *ret_msg = NULL;
4180 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004181#ifdef FEAT_AUTOCMD
4182 bufref_T bufref;
4183
4184 set_bufref(&bufref, wp->w_buffer);
4185#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004186
4187 /* We don't want to do this recursively. May happen when a language is
4188 * not available and the SpellFileMissing autocommand opens a new buffer
4189 * in which 'spell' is set. */
4190 if (recursive)
4191 return NULL;
4192 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193
4194 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004195 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004196
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004197 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004198 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004199 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004200 if (spl_copy == NULL)
4201 goto theend;
4202
Bram Moolenaar2593e032013-11-14 03:54:07 +01004203#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004204 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004205#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004206
4207 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004208 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004210 /* Get one language name. */
4211 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004212 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004213 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004214
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004215 if (STRCMP(lang, "cjk") == 0)
4216 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01004217#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004218 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004219#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004220 continue;
4221 }
4222
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004223 /* If the name ends in ".spl" use it as the name of the spell file.
4224 * If there is a region name let "region" point to it and remove it
4225 * from the name. */
4226 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4227 {
4228 filename = TRUE;
4229
Bram Moolenaarb6356332005-07-18 21:40:44 +00004230 /* Locate a region and remove it from the file name. */
4231 p = vim_strchr(gettail(lang), '_');
4232 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4233 && !ASCII_ISALPHA(p[3]))
4234 {
4235 vim_strncpy(region_cp, p + 1, 2);
4236 mch_memmove(p, p + 3, len - (p - lang) - 2);
4237 len -= 3;
4238 region = region_cp;
4239 }
4240 else
4241 dont_use_region = TRUE;
4242
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004243 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004244 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4245 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004246 break;
4247 }
4248 else
4249 {
4250 filename = FALSE;
4251 if (len > 3 && lang[len - 3] == '_')
4252 {
4253 region = lang + len - 2;
4254 len -= 3;
4255 lang[len] = NUL;
4256 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004257 else
4258 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004259
4260 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004261 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4262 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004263 break;
4264 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004265
Bram Moolenaarb6356332005-07-18 21:40:44 +00004266 if (region != NULL)
4267 {
4268 /* If the region differs from what was used before then don't
4269 * use it for 'spellfile'. */
4270 if (use_region != NULL && STRCMP(region, use_region) != 0)
4271 dont_use_region = TRUE;
4272 use_region = region;
4273 }
4274
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004275 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004276 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004277 {
4278 if (filename)
4279 (void)spell_load_file(lang, lang, NULL, FALSE);
4280 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004281 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004282 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004283#ifdef FEAT_AUTOCMD
4284 /* SpellFileMissing autocommands may do anything, including
4285 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02004286 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004287 {
4288 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4289 goto theend;
4290 }
4291#endif
4292 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004293 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004294
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004295 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004296 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004297 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4299 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4300 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004301 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004302 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004303 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004304 {
4305 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004306 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004307 if (c == REGION_ALL)
4308 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004309 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004310 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004311 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004312 /* This addition file is for other regions. */
4313 region_mask = 0;
4314 }
4315 else
4316 /* This is probably an error. Give a warning and
4317 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004318 smsg((char_u *)
4319 _("Warning: region %s not supported"),
4320 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004321 }
4322 else
4323 region_mask = 1 << c;
4324 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004326 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004327 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004328 if (ga_grow(&ga, 1) == FAIL)
4329 {
4330 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004331 ret_msg = e_outofmem;
4332 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004333 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004334 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004335 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4336 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004337 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004338 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004339 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004340 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004341 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004342 }
4343
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004344 /* round 0: load int_wordlist, if possible.
4345 * round 1: load first name in 'spellfile'.
4346 * round 2: load second name in 'spellfile.
4347 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004348 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004349 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004351 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004352 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004353 /* Internal wordlist, if there is one. */
4354 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004355 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004356 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004357 }
4358 else
4359 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004360 /* One entry in 'spellfile'. */
4361 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4362 STRCAT(spf_name, ".spl");
4363
4364 /* If it was already found above then skip it. */
4365 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004366 {
4367 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4368 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004369 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004370 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004371 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004372 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004373 }
4374
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004375 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4377 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004378 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004379 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004381 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004382 * region name, the region is ignored otherwise. for int_wordlist
4383 * use an arbitrary name. */
4384 if (round == 0)
4385 STRCPY(lang, "internal wordlist");
4386 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004387 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004388 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004389 p = vim_strchr(lang, '.');
4390 if (p != NULL)
4391 *p = NUL; /* truncate at ".encoding.add" */
4392 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004393 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004394
4395 /* If one of the languages has NOBREAK we assume the addition
4396 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004397 if (slang != NULL && nobreak)
4398 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004399 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004400 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004402 region_mask = REGION_ALL;
4403 if (use_region != NULL && !dont_use_region)
4404 {
4405 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004406 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004407 if (c != REGION_ALL)
4408 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004409 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004410 /* This spell file is for other regions. */
4411 region_mask = 0;
4412 }
4413
4414 if (region_mask != 0)
4415 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004416 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4417 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4418 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004419 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4420 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004421 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004422 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 }
4424 }
4425
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004426 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004427 ga_clear(&wp->w_s->b_langp);
4428 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004429
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004430 /* For each language figure out what language to use for sound folding and
4431 * REP items. If the language doesn't support it itself use another one
4432 * with the same name. E.g. for "en-math" use "en". */
4433 for (i = 0; i < ga.ga_len; ++i)
4434 {
4435 lp = LANGP_ENTRY(ga, i);
4436
4437 /* sound folding */
4438 if (lp->lp_slang->sl_sal.ga_len > 0)
4439 /* language does sound folding itself */
4440 lp->lp_sallang = lp->lp_slang;
4441 else
4442 /* find first similar language that does sound folding */
4443 for (j = 0; j < ga.ga_len; ++j)
4444 {
4445 lp2 = LANGP_ENTRY(ga, j);
4446 if (lp2->lp_slang->sl_sal.ga_len > 0
4447 && STRNCMP(lp->lp_slang->sl_name,
4448 lp2->lp_slang->sl_name, 2) == 0)
4449 {
4450 lp->lp_sallang = lp2->lp_slang;
4451 break;
4452 }
4453 }
4454
4455 /* REP items */
4456 if (lp->lp_slang->sl_rep.ga_len > 0)
4457 /* language has REP items itself */
4458 lp->lp_replang = lp->lp_slang;
4459 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004460 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004461 for (j = 0; j < ga.ga_len; ++j)
4462 {
4463 lp2 = LANGP_ENTRY(ga, j);
4464 if (lp2->lp_slang->sl_rep.ga_len > 0
4465 && STRNCMP(lp->lp_slang->sl_name,
4466 lp2->lp_slang->sl_name, 2) == 0)
4467 {
4468 lp->lp_replang = lp2->lp_slang;
4469 break;
4470 }
4471 }
4472 }
4473
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004474theend:
4475 vim_free(spl_copy);
4476 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02004477 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004478 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479}
4480
4481/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004482 * Clear the midword characters for buffer "buf".
4483 */
4484 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004485clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004486{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004487 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004488#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004489 vim_free(wp->w_s->b_spell_ismw_mb);
4490 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004491#endif
4492}
4493
4494/*
4495 * Use the "sl_midword" field of language "lp" for buffer "buf".
4496 * They add up to any currently used midword characters.
4497 */
4498 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004499use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004500{
4501 char_u *p;
4502
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004503 if (lp->sl_midword == NULL) /* there aren't any */
4504 return;
4505
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004506 for (p = lp->sl_midword; *p != NUL; )
4507#ifdef FEAT_MBYTE
4508 if (has_mbyte)
4509 {
4510 int c, l, n;
4511 char_u *bp;
4512
4513 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004514 l = (*mb_ptr2len)(p);
4515 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004516 wp->w_s->b_spell_ismw[c] = TRUE;
4517 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004518 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004520 else
4521 {
4522 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004523 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4524 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004525 if (bp != NULL)
4526 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004527 vim_free(wp->w_s->b_spell_ismw_mb);
4528 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004529 vim_strncpy(bp + n, p, l);
4530 }
4531 }
4532 p += l;
4533 }
4534 else
4535#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004536 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004537}
4538
4539/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004540 * Find the region "region[2]" in "rp" (points to "sl_regions").
4541 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004542 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004543 */
4544 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004545find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004546{
4547 int i;
4548
4549 for (i = 0; ; i += 2)
4550 {
4551 if (rp[i] == NUL)
4552 return REGION_ALL;
4553 if (rp[i] == region[0] && rp[i + 1] == region[1])
4554 break;
4555 }
4556 return i / 2;
4557}
4558
4559/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004560 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004561 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004562 * Word WF_ONECAP
4563 * W WORD WF_ALLCAP
4564 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004565 */
4566 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004567captype(
4568 char_u *word,
4569 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570{
4571 char_u *p;
4572 int c;
4573 int firstcap;
4574 int allcap;
4575 int past_second = FALSE; /* past second word char */
4576
4577 /* find first letter */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004578 for (p = word; !spell_iswordp_nmw(p, curwin); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004580 return 0; /* only non-word characters, illegal word */
4581#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004582 if (has_mbyte)
4583 c = mb_ptr2char_adv(&p);
4584 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004586 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004587 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004588
4589 /*
4590 * Need to check all letters to find a word with mixed upper/lower.
4591 * But a word with an upper char only at start is a ONECAP.
4592 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004593 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004594 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004596 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004597 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004598 {
4599 /* UUl -> KEEPCAP */
4600 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004601 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004602 allcap = FALSE;
4603 }
4604 else if (!allcap)
4605 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607 past_second = TRUE;
4608 }
4609
4610 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004611 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004612 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004613 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004614 return 0;
4615}
4616
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004617/*
4618 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4619 * capital. So that make_case_word() can turn WOrd into Word.
4620 * Add ALLCAP for "WOrD".
4621 */
4622 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004623badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004624{
4625 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004626 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004627 int l, u;
4628 int first;
4629 char_u *p;
4630
4631 if (flags & WF_KEEPCAP)
4632 {
4633 /* Count the number of UPPER and lower case letters. */
4634 l = u = 0;
4635 first = FALSE;
4636 for (p = word; p < end; mb_ptr_adv(p))
4637 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004638 c = PTR2CHAR(p);
4639 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004640 {
4641 ++u;
4642 if (p == word)
4643 first = TRUE;
4644 }
4645 else
4646 ++l;
4647 }
4648
4649 /* If there are more UPPER than lower case letters suggest an
4650 * ALLCAP word. Otherwise, if the first letter is UPPER then
4651 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4652 * require three upper case letters. */
4653 if (u > l && u > 2)
4654 flags |= WF_ALLCAP;
4655 else if (first)
4656 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004657
4658 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4659 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004660 }
4661 return flags;
4662}
4663
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004664/*
4665 * Delete the internal wordlist and its .spl file.
4666 */
4667 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004668spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004669{
4670 char_u fname[MAXPATHL];
4671
4672 if (int_wordlist != NULL)
4673 {
4674 mch_remove(int_wordlist);
4675 int_wordlist_spl(fname);
4676 mch_remove(fname);
4677 vim_free(int_wordlist);
4678 int_wordlist = NULL;
4679 }
4680}
4681
4682#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004683/*
4684 * Free all languages.
4685 */
4686 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004687spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004688{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004689 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004690 buf_T *buf;
4691
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004692 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004693 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004694 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004695
4696 while (first_lang != NULL)
4697 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004698 slang = first_lang;
4699 first_lang = slang->sl_next;
4700 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004701 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004702
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004703 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00004704
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004705 vim_free(repl_to);
4706 repl_to = NULL;
4707 vim_free(repl_from);
4708 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004709}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004710#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004711
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004712#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004713/*
4714 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004715 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004716 */
4717 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004718spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004719{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004720 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004721
Bram Moolenaarea408852005-06-25 22:49:46 +00004722 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 init_spell_chartab();
4724
4725 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004726 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004727
4728 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004729 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004730 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004731 /* Only load the wordlists when 'spelllang' is set and there is a
4732 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004733 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004734 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004736 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004737 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004738# ifdef FEAT_WINDOWS
4739 break;
4740# endif
4741 }
4742 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743 }
4744}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004745#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004746
Bram Moolenaarb765d632005-06-07 21:00:02 +00004747/*
4748 * Reload the spell file "fname" if it's loaded.
4749 */
4750 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004751spell_reload_one(
4752 char_u *fname,
4753 int added_word) /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004754{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004755 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004758 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004759 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004760 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004761 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004762 slang_clear(slang);
4763 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004764 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004765 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004766 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004768 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770
4771 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004772 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004774 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004775}
4776
4777
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778/*
4779 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780 */
4781
Bram Moolenaar51485f02005-06-04 21:55:20 +00004782#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004783 and .dic file. */
4784/*
4785 * Main structure to store the contents of a ".aff" file.
4786 */
4787typedef struct afffile_S
4788{
4789 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004790 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004791 unsigned af_rare; /* RARE ID for rare word */
4792 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004793 unsigned af_bad; /* BAD ID for banned word */
4794 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004795 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004796 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004797 unsigned af_comproot; /* COMPOUNDROOT ID */
4798 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4799 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004800 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004801 int af_pfxpostpone; /* postpone prefixes without chop string and
4802 without flags */
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02004803 int af_ignoreextra; /* IGNOREEXTRA present */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004804 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4805 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004806 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004807} afffile_T;
4808
Bram Moolenaar6de68532005-08-24 22:08:48 +00004809#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004810#define AFT_LONG 1 /* flags are two characters */
4811#define AFT_CAPLONG 2 /* flags are one or two characters */
4812#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004813
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004814typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004815/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4816struct affentry_S
4817{
4818 affentry_T *ae_next; /* next affix with same name/number */
4819 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4820 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004821 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004822 char_u *ae_cond; /* condition (NULL for ".") */
4823 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004824 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4825 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826};
4827
Bram Moolenaar6de68532005-08-24 22:08:48 +00004828#ifdef FEAT_MBYTE
4829# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4830#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004831# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004832#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004833
Bram Moolenaar51485f02005-06-04 21:55:20 +00004834/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4835typedef struct affheader_S
4836{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004837 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4838 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4839 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004840 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004841 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004842 affentry_T *ah_first; /* first affix entry */
4843} affheader_T;
4844
4845#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4846
Bram Moolenaar6de68532005-08-24 22:08:48 +00004847/* Flag used in compound items. */
4848typedef struct compitem_S
4849{
4850 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4851 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4852 int ci_newID; /* affix ID after renumbering. */
4853} compitem_T;
4854
4855#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4856
Bram Moolenaar51485f02005-06-04 21:55:20 +00004857/*
4858 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004859 * the need to keep track of each allocated thing, everything is freed all at
4860 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004861 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4862 * "sb_data" is correct for systems where pointers must be aligned on
4863 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864 */
4865#define SBLOCKSIZE 16000 /* size of sb_data */
4866typedef struct sblock_S sblock_T;
4867struct sblock_S
4868{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004869 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004870 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004871 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004872};
4873
4874/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004875 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004876 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004877typedef struct wordnode_S wordnode_T;
4878struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004879{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004880 union /* shared to save space */
4881 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004882 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004883 int index; /* index in written nodes (valid after first
4884 round) */
4885 } wn_u1;
4886 union /* shared to save space */
4887 {
4888 wordnode_T *next; /* next node with same hash key */
4889 wordnode_T *wnode; /* parent node that will write this node */
4890 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004891 wordnode_T *wn_child; /* child (next byte in word) */
4892 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4893 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004894 int wn_refs; /* Nr. of references to this node. Only
4895 relevant for first node in a list of
4896 siblings, in following siblings it is
4897 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004898 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004899
4900 /* Info for when "wn_byte" is NUL.
4901 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4902 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4903 * "wn_region" the LSW of the wordnr. */
4904 char_u wn_affixID; /* supported/required prefix ID or 0 */
4905 short_u wn_flags; /* WF_ flags */
4906 short wn_region; /* region mask */
4907
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004908#ifdef SPELL_PRINTTREE
4909 int wn_nr; /* sequence nr for printing */
4910#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911};
4912
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004913#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4914
Bram Moolenaar51485f02005-06-04 21:55:20 +00004915#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004916
Bram Moolenaar51485f02005-06-04 21:55:20 +00004917/*
4918 * Info used while reading the spell files.
4919 */
4920typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004921{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004922 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004923 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004924
Bram Moolenaar51485f02005-06-04 21:55:20 +00004925 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004926 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004927
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004928 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004929
Bram Moolenaar4770d092006-01-12 23:22:24 +00004930 long si_sugtree; /* creating the soundfolding trie */
4931
Bram Moolenaar51485f02005-06-04 21:55:20 +00004932 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004933 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004934 int si_did_emsg; /* TRUE when ran out of memory */
4935
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004936 long si_compress_cnt; /* words to add before lowering
4937 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004938 wordnode_T *si_first_free; /* List of nodes that have been freed during
4939 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004940 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004941#ifdef SPELL_PRINTTREE
4942 int si_wordnode_nr; /* sequence nr for nodes */
4943#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004944 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004945
Bram Moolenaar51485f02005-06-04 21:55:20 +00004946 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004947 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004948 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004949 int si_region; /* region mask */
4950 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004951 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004952 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004953 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004954 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004955 int si_region_count; /* number of regions supported (1 when there
4956 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004957 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004958 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959
4960 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004961 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004963 char_u *si_sofofr; /* SOFOFROM text */
4964 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004965 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004966 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004967 int si_nocompoundsugs; /* NOCOMPOUNDSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004968 int si_followup; /* soundsalike: ? */
4969 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004970 hashtab_T si_commonwords; /* hashtable for common words */
4971 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004972 int si_rem_accents; /* soundsalike: remove accents */
4973 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004974 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004975 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004976 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004977 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004978 int si_compoptions; /* COMP_ flags */
4979 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4980 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004981 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004982 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004983 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004984 garray_T si_prefcond; /* table with conditions for postponed
4985 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004986 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004987 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004988} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004989
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004990static afffile_T *spell_read_aff(spellinfo_T *spin, char_u *fname);
4991static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
4992static void aff_process_flags(afffile_T *affile, affentry_T *entry);
4993static int spell_info_item(char_u *s);
4994static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
4995static unsigned get_affitem(int flagtype, char_u **pp);
4996static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
4997static void check_renumber(spellinfo_T *spin);
4998static int flag_in_afflist(int flagtype, char_u *afflist, unsigned flag);
4999static void aff_check_number(int spinval, int affval, char *name);
5000static void aff_check_string(char_u *spinval, char_u *affval, char *name);
5001static int str_equal(char_u *s1, char_u *s2);
5002static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
5003static int sal_to_bool(char_u *s);
5004static void spell_free_aff(afffile_T *aff);
5005static int spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile);
5006static int get_affix_flags(afffile_T *affile, char_u *afflist);
5007static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
5008static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
5009static int store_aff_word(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);
5010static int spell_read_wordfile(spellinfo_T *spin, char_u *fname);
5011static void *getroom(spellinfo_T *spin, size_t len, int align);
5012static char_u *getroom_save(spellinfo_T *spin, char_u *s);
5013static void free_blocks(sblock_T *bl);
5014static wordnode_T *wordtree_alloc(spellinfo_T *spin);
5015static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
5016static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
5017static wordnode_T *get_wordnode(spellinfo_T *spin);
5018static int deref_wordnode(spellinfo_T *spin, wordnode_T *node);
5019static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
5020static void wordtree_compress(spellinfo_T *spin, wordnode_T *root);
5021static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot);
5022static int node_equal(wordnode_T *n1, wordnode_T *n2);
5023static int write_vim_spell(spellinfo_T *spin, char_u *fname);
5024static void clear_node(wordnode_T *node);
5025static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
5026static void spell_make_sugfile(spellinfo_T *spin, char_u *wfname);
5027static int sug_filltree(spellinfo_T *spin, slang_T *slang);
5028static int sug_maketable(spellinfo_T *spin);
5029static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
5030static int offset2bytes(int nr, char_u *buf);
5031static int bytes2offset(char_u **pp);
5032static void sug_write(spellinfo_T *spin, char_u *fname);
5033static void mkspell(int fcount, char_u **fnames, int ascii, int over_write, int added_word);
5034static void spell_message(spellinfo_T *spin, char_u *str);
5035static void init_spellfile(void);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005036
Bram Moolenaar53805d12005-08-01 07:08:33 +00005037/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5038 * but it must be negative to indicate the prefix tree to tree_add_word().
5039 * Use a negative number with the lower 8 bits zero. */
5040#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005041
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005042/* flags for "condit" argument of store_aff_word() */
5043#define CONDIT_COMB 1 /* affix must combine */
5044#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5045#define CONDIT_SUF 4 /* add a suffix for matching flags */
5046#define CONDIT_AFF 8 /* word already has an affix */
5047
Bram Moolenaar5195e452005-08-19 20:32:47 +00005048/*
5049 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5050 */
5051static long compress_start = 30000; /* memory / SBLOCKSIZE */
5052static long compress_inc = 100; /* memory / SBLOCKSIZE */
5053static long compress_added = 500000; /* word count */
5054
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005055#ifdef SPELL_PRINTTREE
5056/*
5057 * For debugging the tree code: print the current tree in a (more or less)
5058 * readable format, so that we can see what happens when adding a word and/or
5059 * compressing the tree.
5060 * Based on code from Olaf Seibert.
5061 */
5062#define PRINTLINESIZE 1000
5063#define PRINTWIDTH 6
5064
5065#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5066 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5067
5068static char line1[PRINTLINESIZE];
5069static char line2[PRINTLINESIZE];
5070static char line3[PRINTLINESIZE];
5071
5072 static void
5073spell_clear_flags(wordnode_T *node)
5074{
5075 wordnode_T *np;
5076
5077 for (np = node; np != NULL; np = np->wn_sibling)
5078 {
5079 np->wn_u1.index = FALSE;
5080 spell_clear_flags(np->wn_child);
5081 }
5082}
5083
5084 static void
5085spell_print_node(wordnode_T *node, int depth)
5086{
5087 if (node->wn_u1.index)
5088 {
5089 /* Done this node before, print the reference. */
5090 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5091 PRINTSOME(line2, depth, " ", 0, 0);
5092 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar7b877b32016-01-09 13:51:34 +01005093 msg((char_u *)line1);
5094 msg((char_u *)line2);
5095 msg((char_u *)line3);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005096 }
5097 else
5098 {
5099 node->wn_u1.index = TRUE;
5100
5101 if (node->wn_byte != NUL)
5102 {
5103 if (node->wn_child != NULL)
5104 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5105 else
5106 /* Cannot happen? */
5107 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5108 }
5109 else
5110 PRINTSOME(line1, depth, " $ ", 0, 0);
5111
5112 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5113
5114 if (node->wn_sibling != NULL)
5115 PRINTSOME(line3, depth, " | ", 0, 0);
5116 else
5117 PRINTSOME(line3, depth, " ", 0, 0);
5118
5119 if (node->wn_byte == NUL)
5120 {
Bram Moolenaar7b877b32016-01-09 13:51:34 +01005121 msg((char_u *)line1);
5122 msg((char_u *)line2);
5123 msg((char_u *)line3);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005124 }
5125
5126 /* do the children */
5127 if (node->wn_byte != NUL && node->wn_child != NULL)
5128 spell_print_node(node->wn_child, depth + 1);
5129
5130 /* do the siblings */
5131 if (node->wn_sibling != NULL)
5132 {
5133 /* get rid of all parent details except | */
5134 STRCPY(line1, line3);
5135 STRCPY(line2, line3);
5136 spell_print_node(node->wn_sibling, depth);
5137 }
5138 }
5139}
5140
5141 static void
5142spell_print_tree(wordnode_T *root)
5143{
5144 if (root != NULL)
5145 {
5146 /* Clear the "wn_u1.index" fields, used to remember what has been
5147 * done. */
5148 spell_clear_flags(root);
5149
5150 /* Recursively print the tree. */
5151 spell_print_node(root, 0);
5152 }
5153}
5154#endif /* SPELL_PRINTTREE */
5155
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005156/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005157 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005158 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005159 */
5160 static afffile_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005161spell_read_aff(spellinfo_T *spin, char_u *fname)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005162{
5163 FILE *fd;
5164 afffile_T *aff;
5165 char_u rline[MAXLINELEN];
5166 char_u *line;
5167 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005168#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005169 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005170 int itemcnt;
5171 char_u *p;
5172 int lnum = 0;
5173 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005174 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005175 int aff_todo = 0;
5176 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005177 char_u *low = NULL;
5178 char_u *fol = NULL;
5179 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005180 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005181 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005182 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005183 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005184 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005185 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005186 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005187 int compminlen = 0; /* COMPOUNDMIN value */
5188 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005189 int compoptions = 0; /* COMP_ flags */
5190 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005191 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005192 concatenated */
5193 char_u *midword = NULL; /* MIDWORD value */
5194 char_u *syllable = NULL; /* SYLLABLE value */
5195 char_u *sofofrom = NULL; /* SOFOFROM value */
5196 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005197
Bram Moolenaar51485f02005-06-04 21:55:20 +00005198 /*
5199 * Open the file.
5200 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005202 if (fd == NULL)
5203 {
5204 EMSG2(_(e_notopen), fname);
5205 return NULL;
5206 }
5207
Bram Moolenaar4770d092006-01-12 23:22:24 +00005208 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5209 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 /* Only do REP lines when not done in another .aff file already. */
5212 do_rep = spin->si_rep.ga_len == 0;
5213
Bram Moolenaar4770d092006-01-12 23:22:24 +00005214 /* Only do REPSAL lines when not done in another .aff file already. */
5215 do_repsal = spin->si_repsal.ga_len == 0;
5216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 /* Only do SAL lines when not done in another .aff file already. */
5218 do_sal = spin->si_sal.ga_len == 0;
5219
5220 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005221 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222
Bram Moolenaar51485f02005-06-04 21:55:20 +00005223 /*
5224 * Allocate and init the afffile_T structure.
5225 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005226 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005227 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005228 {
5229 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005230 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005231 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232 hash_init(&aff->af_pref);
5233 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005234 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005235
5236 /*
5237 * Read all the lines in the file one by one.
5238 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005239 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005240 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005241 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005242 ++lnum;
5243
5244 /* Skip comment lines. */
5245 if (*rline == '#')
5246 continue;
5247
5248 /* Convert from "SET" to 'encoding' when needed. */
5249 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005250#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005251 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005253 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005254 if (pc == NULL)
5255 {
5256 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5257 fname, lnum, rline);
5258 continue;
5259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005260 line = pc;
5261 }
5262 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005263#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005264 {
5265 pc = NULL;
5266 line = rline;
5267 }
5268
5269 /* Split the line up in white separated items. Put a NUL after each
5270 * item. */
5271 itemcnt = 0;
5272 for (p = line; ; )
5273 {
5274 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5275 ++p;
5276 if (*p == NUL)
5277 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005278 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005279 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005280 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005281 /* A few items have arbitrary text argument, don't split them. */
5282 if (itemcnt == 2 && spell_info_item(items[0]))
5283 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5284 ++p;
5285 else
5286 while (*p > ' ') /* skip until white space or CR/NL */
5287 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005288 if (*p == NUL)
5289 break;
5290 *p++ = NUL;
5291 }
5292
5293 /* Handle non-empty lines. */
5294 if (itemcnt > 0)
5295 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005296 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005297 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005298#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005299 /* Setup for conversion from "ENC" to 'encoding'. */
5300 aff->af_enc = enc_canonize(items[1]);
5301 if (aff->af_enc != NULL && !spin->si_ascii
5302 && convert_setup(&spin->si_conv, aff->af_enc,
5303 p_enc) == FAIL)
5304 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5305 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005306 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005307#else
5308 smsg((char_u *)_("Conversion in %s not supported"), fname);
5309#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005310 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005311 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005312 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005313 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005314 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005315 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005316 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005317 aff->af_flagtype = AFT_NUM;
5318 else if (STRCMP(items[1], "caplong") == 0)
5319 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005320 else
5321 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5322 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005323 if (aff->af_rare != 0
5324 || aff->af_keepcase != 0
5325 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005326 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005327 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005328 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005329 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005330 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005331 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005332 || aff->af_suff.ht_used > 0
5333 || aff->af_pref.ht_used > 0)
5334 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5335 fname, lnum, items[1]);
5336 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005337 else if (spell_info_item(items[0]))
5338 {
5339 p = (char_u *)getroom(spin,
5340 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5341 + STRLEN(items[0])
5342 + STRLEN(items[1]) + 3, FALSE);
5343 if (p != NULL)
5344 {
5345 if (spin->si_info != NULL)
5346 {
5347 STRCPY(p, spin->si_info);
5348 STRCAT(p, "\n");
5349 }
5350 STRCAT(p, items[0]);
5351 STRCAT(p, " ");
5352 STRCAT(p, items[1]);
5353 spin->si_info = p;
5354 }
5355 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005356 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005357 && midword == NULL)
5358 {
5359 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005360 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005361 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005364 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005365 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005366 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5367 || is_aff_rule(items, itemcnt, "RARE", 2))
5368 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005369 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005370 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005371 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005372 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005373 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005374 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5375 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005376 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005377 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005378 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005379 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005380 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005381 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5382 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5383 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005384 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005385 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5386 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005387 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005388 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005389 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005390 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005391 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5392 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005393 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005394 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005395 && aff->af_circumfix == 0)
5396 {
5397 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5398 fname, lnum);
5399 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005400 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005401 && aff->af_nosuggest == 0)
5402 {
5403 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5404 fname, lnum);
5405 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005406 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5407 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005408 && aff->af_needcomp == 0)
5409 {
5410 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5411 fname, lnum);
5412 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005413 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005414 && aff->af_comproot == 0)
5415 {
5416 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5417 fname, lnum);
5418 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005419 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5420 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005421 {
5422 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5423 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005424 if (aff->af_pref.ht_used > 0)
5425 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5426 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005427 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005428 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5429 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005430 {
5431 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5432 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005433 if (aff->af_pref.ht_used > 0)
5434 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5435 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005436 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005437 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005438 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005439 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005440 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005441 * "Na" into "Na+", "1234" into "1234+". */
5442 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005443 if (p != NULL)
5444 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005445 STRCPY(p, items[1]);
5446 STRCAT(p, "+");
5447 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005448 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005449 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005450 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5451 {
5452 /* We don't use the count, but do check that it's a number and
5453 * not COMPOUNDRULE mistyped. */
5454 if (atoi((char *)items[1]) == 0)
5455 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5456 fname, lnum, items[1]);
5457 }
5458 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005459 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005460 /* Don't use the first rule if it is a number. */
5461 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005462 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005463 /* Concatenate this string to previously defined ones,
5464 * using a slash to separate them. */
5465 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005466 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005467 l += (int)STRLEN(compflags) + 1;
5468 p = getroom(spin, l, FALSE);
5469 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005470 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005471 if (compflags != NULL)
5472 {
5473 STRCPY(p, compflags);
5474 STRCAT(p, "/");
5475 }
5476 STRCAT(p, items[1]);
5477 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005478 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005479 }
5480 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005481 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005482 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005483 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005484 compmax = atoi((char *)items[1]);
5485 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005486 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005487 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005488 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005489 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005490 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005491 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005492 compminlen = atoi((char *)items[1]);
5493 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005494 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5495 fname, lnum, items[1]);
5496 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005497 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005498 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005499 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005500 compsylmax = atoi((char *)items[1]);
5501 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005502 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5503 fname, lnum, items[1]);
5504 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005505 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005506 {
5507 compoptions |= COMP_CHECKDUP;
5508 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005509 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005510 {
5511 compoptions |= COMP_CHECKREP;
5512 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005513 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005514 {
5515 compoptions |= COMP_CHECKCASE;
5516 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005517 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005518 {
5519 compoptions |= COMP_CHECKTRIPLE;
5520 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005521 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005522 {
5523 if (atoi((char *)items[1]) == 0)
5524 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5525 fname, lnum, items[1]);
5526 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005527 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005528 {
5529 garray_T *gap = &spin->si_comppat;
5530 int i;
5531
5532 /* Only add the couple if it isn't already there. */
5533 for (i = 0; i < gap->ga_len - 1; i += 2)
5534 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5535 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5536 items[2]) == 0)
5537 break;
5538 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5539 {
5540 ((char_u **)(gap->ga_data))[gap->ga_len++]
5541 = getroom_save(spin, items[1]);
5542 ((char_u **)(gap->ga_data))[gap->ga_len++]
5543 = getroom_save(spin, items[2]);
5544 }
5545 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005546 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005547 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005548 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005549 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005550 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005551 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005552 {
5553 spin->si_nobreak = TRUE;
5554 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005555 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005556 {
5557 spin->si_nosplitsugs = TRUE;
5558 }
Bram Moolenaar7b877b32016-01-09 13:51:34 +01005559 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
5560 {
5561 spin->si_nocompoundsugs = TRUE;
5562 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005563 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005564 {
5565 spin->si_nosugfile = TRUE;
5566 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005567 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005568 {
5569 aff->af_pfxpostpone = TRUE;
5570 }
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005571 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
5572 {
5573 aff->af_ignoreextra = TRUE;
5574 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005575 else if ((STRCMP(items[0], "PFX") == 0
5576 || STRCMP(items[0], "SFX") == 0)
5577 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005578 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005579 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005580 int lasti = 4;
5581 char_u key[AH_KEY_LEN];
5582
5583 if (*items[0] == 'P')
5584 tp = &aff->af_pref;
5585 else
5586 tp = &aff->af_suff;
5587
5588 /* Myspell allows the same affix name to be used multiple
5589 * times. The affix files that do this have an undocumented
5590 * "S" flag on all but the last block, thus we check for that
5591 * and store it in ah_follows. */
5592 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5593 hi = hash_find(tp, key);
5594 if (!HASHITEM_EMPTY(hi))
5595 {
5596 cur_aff = HI2AH(hi);
5597 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5598 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5599 fname, lnum, items[1]);
5600 if (!cur_aff->ah_follows)
5601 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5602 fname, lnum, items[1]);
5603 }
5604 else
5605 {
5606 /* New affix letter. */
5607 cur_aff = (affheader_T *)getroom(spin,
5608 sizeof(affheader_T), TRUE);
5609 if (cur_aff == NULL)
5610 break;
5611 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5612 fname, lnum);
5613 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5614 break;
5615 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005616 || cur_aff->ah_flag == aff->af_rare
5617 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005618 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005619 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005620 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005621 || cur_aff->ah_flag == aff->af_needcomp
5622 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005623 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 +00005624 fname, lnum, items[1]);
5625 STRCPY(cur_aff->ah_key, items[1]);
5626 hash_add(tp, cur_aff->ah_key);
5627
5628 cur_aff->ah_combine = (*items[2] == 'Y');
5629 }
5630
5631 /* Check for the "S" flag, which apparently means that another
5632 * block with the same affix name is following. */
5633 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5634 {
5635 ++lasti;
5636 cur_aff->ah_follows = TRUE;
5637 }
5638 else
5639 cur_aff->ah_follows = FALSE;
5640
Bram Moolenaar8db73182005-06-17 21:51:16 +00005641 /* Myspell allows extra text after the item, but that might
5642 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005643 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005644 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005645
Bram Moolenaar95529562005-08-25 21:21:38 +00005646 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005647 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5648 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005649
Bram Moolenaar95529562005-08-25 21:21:38 +00005650 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005651 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005652 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005653 {
5654 /* Use a new number in the .spl file later, to be able
5655 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005656 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005657 cur_aff->ah_newID = ++spin->si_newprefID;
5658
5659 /* We only really use ah_newID if the prefix is
5660 * postponed. We know that only after handling all
5661 * the items. */
5662 did_postpone_prefix = FALSE;
5663 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005664 else
5665 /* Did use the ID in a previous block. */
5666 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005667 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005668
Bram Moolenaar51485f02005-06-04 21:55:20 +00005669 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005670 }
5671 else if ((STRCMP(items[0], "PFX") == 0
5672 || STRCMP(items[0], "SFX") == 0)
5673 && aff_todo > 0
5674 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005675 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005676 {
5677 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005678 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005679 int lasti = 5;
5680
Bram Moolenaar8db73182005-06-17 21:51:16 +00005681 /* Myspell allows extra text after the item, but that might
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005682 * mean mistakes go unnoticed. Require a comment-starter,
5683 * unless IGNOREEXTRA is used. Hunspell uses a "-" item. */
5684 if (itemcnt > lasti
5685 && !aff->af_ignoreextra
5686 && *items[lasti] != '#'
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005687 && (STRCMP(items[lasti], "-") != 0
5688 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005689 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005690
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005691 /* New item for an affix letter. */
5692 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005693 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005694 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005695 if (aff_entry == NULL)
5696 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005697
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005698 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005699 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005700 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005701 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005702 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005704 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005705 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5706 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005707 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005708 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005709 aff_process_flags(aff, aff_entry);
5710 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005711 }
5712
Bram Moolenaar51485f02005-06-04 21:55:20 +00005713 /* Don't use an affix entry with non-ASCII characters when
5714 * "spin->si_ascii" is TRUE. */
5715 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005716 || has_non_ascii(aff_entry->ae_add)))
5717 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005718 aff_entry->ae_next = cur_aff->ah_first;
5719 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005720
5721 if (STRCMP(items[4], ".") != 0)
5722 {
5723 char_u buf[MAXLINELEN];
5724
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005725 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005726 if (*items[0] == 'P')
5727 sprintf((char *)buf, "^%s", items[4]);
5728 else
5729 sprintf((char *)buf, "%s$", items[4]);
5730 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005731 RE_MAGIC + RE_STRING + RE_STRICT);
5732 if (aff_entry->ae_prog == NULL)
5733 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5734 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005736
5737 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005738 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005739 * Can't be done for an affix with flags, ignoring
5740 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005741 if (*items[0] == 'P' && aff->af_pfxpostpone
5742 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005743 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005744 /* When the chop string is one lower-case letter and
5745 * the add string ends in the upper-case letter we set
5746 * the "upper" flag, clear "ae_chop" and remove the
5747 * letters from "ae_add". The condition must either
5748 * be empty or start with the same letter. */
5749 if (aff_entry->ae_chop != NULL
5750 && aff_entry->ae_add != NULL
5751#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005752 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005753 aff_entry->ae_chop)] == NUL
5754#else
5755 && aff_entry->ae_chop[1] == NUL
5756#endif
5757 )
5758 {
5759 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005760
Bram Moolenaar53805d12005-08-01 07:08:33 +00005761 c = PTR2CHAR(aff_entry->ae_chop);
5762 c_up = SPELL_TOUPPER(c);
5763 if (c_up != c
5764 && (aff_entry->ae_cond == NULL
5765 || PTR2CHAR(aff_entry->ae_cond) == c))
5766 {
5767 p = aff_entry->ae_add
5768 + STRLEN(aff_entry->ae_add);
5769 mb_ptr_back(aff_entry->ae_add, p);
5770 if (PTR2CHAR(p) == c_up)
5771 {
5772 upper = TRUE;
5773 aff_entry->ae_chop = NULL;
5774 *p = NUL;
5775
5776 /* The condition is matched with the
5777 * actual word, thus must check for the
5778 * upper-case letter. */
5779 if (aff_entry->ae_cond != NULL)
5780 {
5781 char_u buf[MAXLINELEN];
5782#ifdef FEAT_MBYTE
5783 if (has_mbyte)
5784 {
5785 onecap_copy(items[4], buf, TRUE);
5786 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005787 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005788 }
5789 else
5790#endif
5791 *aff_entry->ae_cond = c_up;
5792 if (aff_entry->ae_cond != NULL)
5793 {
5794 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005795 aff_entry->ae_cond);
Bram Moolenaar473de612013-06-08 18:19:48 +02005796 vim_regfree(aff_entry->ae_prog);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005797 aff_entry->ae_prog = vim_regcomp(
5798 buf, RE_MAGIC + RE_STRING);
5799 }
5800 }
5801 }
5802 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005803 }
5804
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005805 if (aff_entry->ae_chop == NULL
5806 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005807 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005808 int idx;
5809 char_u **pp;
5810 int n;
5811
Bram Moolenaar6de68532005-08-24 22:08:48 +00005812 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005813 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5814 --idx)
5815 {
5816 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5817 if (str_equal(p, aff_entry->ae_cond))
5818 break;
5819 }
5820 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5821 {
5822 /* Not found, add a new condition. */
5823 idx = spin->si_prefcond.ga_len++;
5824 pp = ((char_u **)spin->si_prefcond.ga_data)
5825 + idx;
5826 if (aff_entry->ae_cond == NULL)
5827 *pp = NULL;
5828 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005829 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005830 aff_entry->ae_cond);
5831 }
5832
5833 /* Add the prefix to the prefix tree. */
5834 if (aff_entry->ae_add == NULL)
5835 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005836 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005837 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005838
Bram Moolenaar53805d12005-08-01 07:08:33 +00005839 /* PFX_FLAGS is a negative number, so that
5840 * tree_add_word() knows this is the prefix tree. */
5841 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005842 if (!cur_aff->ah_combine)
5843 n |= WFP_NC;
5844 if (upper)
5845 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005846 if (aff_entry->ae_comppermit)
5847 n |= WFP_COMPPERMIT;
5848 if (aff_entry->ae_compforbid)
5849 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005850 tree_add_word(spin, p, spin->si_prefroot, n,
5851 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005852 did_postpone_prefix = TRUE;
5853 }
5854
5855 /* Didn't actually use ah_newID, backup si_newprefID. */
5856 if (aff_todo == 0 && !did_postpone_prefix)
5857 {
5858 --spin->si_newprefID;
5859 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005860 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005861 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005862 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005863 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005864 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005865 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005866 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005867 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005868 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005869 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005870 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005871 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005872 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005873 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005874 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005875 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005876 else if (is_aff_rule(items, itemcnt, "REP", 2)
5877 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005878 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005879 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005880 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005881 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005882 fname, lnum);
5883 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005884 else if ((STRCMP(items[0], "REP") == 0
5885 || STRCMP(items[0], "REPSAL") == 0)
5886 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005887 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005888 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005889 /* Myspell ignores extra arguments, we require it starts with
5890 * # to detect mistakes. */
5891 if (itemcnt > 3 && items[3][0] != '#')
5892 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005893 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005894 {
5895 /* Replace underscore with space (can't include a space
5896 * directly). */
5897 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5898 if (*p == '_')
5899 *p = ' ';
5900 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5901 if (*p == '_')
5902 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005903 add_fromto(spin, items[0][3] == 'S'
5904 ? &spin->si_repsal
5905 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005906 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005907 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005908 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005909 {
5910 /* MAP item or count */
5911 if (!found_map)
5912 {
5913 /* First line contains the count. */
5914 found_map = TRUE;
5915 if (!isdigit(*items[1]))
5916 smsg((char_u *)_("Expected MAP count in %s line %d"),
5917 fname, lnum);
5918 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005919 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005920 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005921 int c;
5922
5923 /* Check that every character appears only once. */
5924 for (p = items[1]; *p != NUL; )
5925 {
5926#ifdef FEAT_MBYTE
5927 c = mb_ptr2char_adv(&p);
5928#else
5929 c = *p++;
5930#endif
5931 if ((spin->si_map.ga_len > 0
5932 && vim_strchr(spin->si_map.ga_data, c)
5933 != NULL)
5934 || vim_strchr(p, c) != NULL)
5935 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5936 fname, lnum);
5937 }
5938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005939 /* We simply concatenate all the MAP strings, separated by
5940 * slashes. */
5941 ga_concat(&spin->si_map, items[1]);
5942 ga_append(&spin->si_map, '/');
5943 }
5944 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005945 /* Accept "SAL from to" and "SAL from to #comment". */
5946 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005947 {
5948 if (do_sal)
5949 {
5950 /* SAL item (sounds-a-like)
5951 * Either one of the known keys or a from-to pair. */
5952 if (STRCMP(items[1], "followup") == 0)
5953 spin->si_followup = sal_to_bool(items[2]);
5954 else if (STRCMP(items[1], "collapse_result") == 0)
5955 spin->si_collapse = sal_to_bool(items[2]);
5956 else if (STRCMP(items[1], "remove_accents") == 0)
5957 spin->si_rem_accents = sal_to_bool(items[2]);
5958 else
5959 /* when "to" is "_" it means empty */
5960 add_fromto(spin, &spin->si_sal, items[1],
5961 STRCMP(items[2], "_") == 0 ? (char_u *)""
5962 : items[2]);
5963 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005964 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005965 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005966 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005967 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005968 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005969 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005970 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005971 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005972 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005973 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005974 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005975 else if (STRCMP(items[0], "COMMON") == 0)
5976 {
5977 int i;
5978
5979 for (i = 1; i < itemcnt; ++i)
5980 {
5981 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5982 items[i])))
5983 {
5984 p = vim_strsave(items[i]);
5985 if (p == NULL)
5986 break;
5987 hash_add(&spin->si_commonwords, p);
5988 }
5989 }
5990 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005991 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005992 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005993 fname, lnum, items[0]);
5994 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005995 }
5996
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005997 if (fol != NULL || low != NULL || upp != NULL)
5998 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005999 if (spin->si_clear_chartab)
6000 {
6001 /* Clear the char type tables, don't want to use any of the
6002 * currently used spell properties. */
6003 init_spell_chartab();
6004 spin->si_clear_chartab = FALSE;
6005 }
6006
Bram Moolenaar3982c542005-06-08 21:56:31 +00006007 /*
6008 * Don't write a word table for an ASCII file, so that we don't check
6009 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006010 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006011 * mb_get_class(), the list of chars in the file will be incomplete.
6012 */
6013 if (!spin->si_ascii
6014#ifdef FEAT_MBYTE
6015 && !enc_utf8
6016#endif
6017 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006018 {
6019 if (fol == NULL || low == NULL || upp == NULL)
6020 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6021 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006022 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006023 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006024
6025 vim_free(fol);
6026 vim_free(low);
6027 vim_free(upp);
6028 }
6029
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006030 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006031 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006032 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006033 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006034 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006035 }
6036
Bram Moolenaar6de68532005-08-24 22:08:48 +00006037 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006038 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006039 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6040 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006041 }
6042
Bram Moolenaar6de68532005-08-24 22:08:48 +00006043 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006044 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006045 if (syllable == NULL)
6046 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6047 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6048 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006049 }
6050
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006051 if (compoptions != 0)
6052 {
6053 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6054 spin->si_compoptions |= compoptions;
6055 }
6056
Bram Moolenaar6de68532005-08-24 22:08:48 +00006057 if (compflags != NULL)
6058 process_compflags(spin, aff, compflags);
6059
6060 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006061 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006062 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006063 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006064 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006065 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006066 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006067 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006068 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006069 }
6070
Bram Moolenaar6de68532005-08-24 22:08:48 +00006071 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006072 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006073 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6074 spin->si_syllable = syllable;
6075 }
6076
6077 if (sofofrom != NULL || sofoto != NULL)
6078 {
6079 if (sofofrom == NULL || sofoto == NULL)
6080 smsg((char_u *)_("Missing SOFO%s line in %s"),
6081 sofofrom == NULL ? "FROM" : "TO", fname);
6082 else if (spin->si_sal.ga_len > 0)
6083 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006084 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006085 {
6086 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6087 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6088 spin->si_sofofr = sofofrom;
6089 spin->si_sofoto = sofoto;
6090 }
6091 }
6092
6093 if (midword != NULL)
6094 {
6095 aff_check_string(spin->si_midword, midword, "MIDWORD");
6096 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006097 }
6098
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006099 vim_free(pc);
6100 fclose(fd);
6101 return aff;
6102}
6103
6104/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006105 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6106 * a comment is following after item "mincount".
6107 */
6108 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006109is_aff_rule(
6110 char_u **items,
6111 int itemcnt,
6112 char *rulename,
6113 int mincount)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006114{
6115 return (STRCMP(items[0], rulename) == 0
6116 && (itemcnt == mincount
6117 || (itemcnt > mincount && items[mincount][0] == '#')));
6118}
6119
6120/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006121 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6122 * ae_flags to ae_comppermit and ae_compforbid.
6123 */
6124 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006125aff_process_flags(afffile_T *affile, affentry_T *entry)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006126{
6127 char_u *p;
6128 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006129 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006130
6131 if (entry->ae_flags != NULL
6132 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6133 {
6134 for (p = entry->ae_flags; *p != NUL; )
6135 {
6136 prevp = p;
6137 flag = get_affitem(affile->af_flagtype, &p);
6138 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6139 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006140 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006141 p = prevp;
6142 if (flag == affile->af_comppermit)
6143 entry->ae_comppermit = TRUE;
6144 else
6145 entry->ae_compforbid = TRUE;
6146 }
6147 if (affile->af_flagtype == AFT_NUM && *p == ',')
6148 ++p;
6149 }
6150 if (*entry->ae_flags == NUL)
6151 entry->ae_flags = NULL; /* nothing left */
6152 }
6153}
6154
6155/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006156 * Return TRUE if "s" is the name of an info item in the affix file.
6157 */
6158 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006159spell_info_item(char_u *s)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006160{
6161 return STRCMP(s, "NAME") == 0
6162 || STRCMP(s, "HOME") == 0
6163 || STRCMP(s, "VERSION") == 0
6164 || STRCMP(s, "AUTHOR") == 0
6165 || STRCMP(s, "EMAIL") == 0
6166 || STRCMP(s, "COPYRIGHT") == 0;
6167}
6168
6169/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006170 * Turn an affix flag name into a number, according to the FLAG type.
6171 * returns zero for failure.
6172 */
6173 static unsigned
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006174affitem2flag(
6175 int flagtype,
6176 char_u *item,
6177 char_u *fname,
6178 int lnum)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006179{
6180 unsigned res;
6181 char_u *p = item;
6182
6183 res = get_affitem(flagtype, &p);
6184 if (res == 0)
6185 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006186 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006187 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6188 fname, lnum, item);
6189 else
6190 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6191 fname, lnum, item);
6192 }
6193 if (*p != NUL)
6194 {
6195 smsg((char_u *)_(e_affname), fname, lnum, item);
6196 return 0;
6197 }
6198
6199 return res;
6200}
6201
6202/*
6203 * Get one affix name from "*pp" and advance the pointer.
6204 * Returns zero for an error, still advances the pointer then.
6205 */
6206 static unsigned
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006207get_affitem(int flagtype, char_u **pp)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006208{
6209 int res;
6210
Bram Moolenaar95529562005-08-25 21:21:38 +00006211 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006212 {
6213 if (!VIM_ISDIGIT(**pp))
6214 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006215 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006216 return 0;
6217 }
6218 res = getdigits(pp);
6219 }
6220 else
6221 {
6222#ifdef FEAT_MBYTE
6223 res = mb_ptr2char_adv(pp);
6224#else
6225 res = *(*pp)++;
6226#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006227 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006228 && res >= 'A' && res <= 'Z'))
6229 {
6230 if (**pp == NUL)
6231 return 0;
6232#ifdef FEAT_MBYTE
6233 res = mb_ptr2char_adv(pp) + (res << 16);
6234#else
6235 res = *(*pp)++ + (res << 16);
6236#endif
6237 }
6238 }
6239 return res;
6240}
6241
6242/*
6243 * Process the "compflags" string used in an affix file and append it to
6244 * spin->si_compflags.
6245 * The processing involves changing the affix names to ID numbers, so that
6246 * they fit in one byte.
6247 */
6248 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006249process_compflags(
6250 spellinfo_T *spin,
6251 afffile_T *aff,
6252 char_u *compflags)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006253{
6254 char_u *p;
6255 char_u *prevp;
6256 unsigned flag;
6257 compitem_T *ci;
6258 int id;
6259 int len;
6260 char_u *tp;
6261 char_u key[AH_KEY_LEN];
6262 hashitem_T *hi;
6263
6264 /* Make room for the old and the new compflags, concatenated with a / in
6265 * between. Processing it makes it shorter, but we don't know by how
6266 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006267 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006268 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006269 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006270 p = getroom(spin, len, FALSE);
6271 if (p == NULL)
6272 return;
6273 if (spin->si_compflags != NULL)
6274 {
6275 STRCPY(p, spin->si_compflags);
6276 STRCAT(p, "/");
6277 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006278 spin->si_compflags = p;
6279 tp = p + STRLEN(p);
6280
6281 for (p = compflags; *p != NUL; )
6282 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006283 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006284 /* Copy non-flag characters directly. */
6285 *tp++ = *p++;
6286 else
6287 {
6288 /* First get the flag number, also checks validity. */
6289 prevp = p;
6290 flag = get_affitem(aff->af_flagtype, &p);
6291 if (flag != 0)
6292 {
6293 /* Find the flag in the hashtable. If it was used before, use
6294 * the existing ID. Otherwise add a new entry. */
6295 vim_strncpy(key, prevp, p - prevp);
6296 hi = hash_find(&aff->af_comp, key);
6297 if (!HASHITEM_EMPTY(hi))
6298 id = HI2CI(hi)->ci_newID;
6299 else
6300 {
6301 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6302 if (ci == NULL)
6303 break;
6304 STRCPY(ci->ci_key, key);
6305 ci->ci_flag = flag;
6306 /* Avoid using a flag ID that has a special meaning in a
6307 * regexp (also inside []). */
6308 do
6309 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006310 check_renumber(spin);
6311 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006312 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006313 ci->ci_newID = id;
6314 hash_add(&aff->af_comp, ci->ci_key);
6315 }
6316 *tp++ = id;
6317 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006318 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006319 ++p;
6320 }
6321 }
6322
6323 *tp = NUL;
6324}
6325
6326/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006327 * Check that the new IDs for postponed affixes and compounding don't overrun
6328 * each other. We have almost 255 available, but start at 0-127 to avoid
6329 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6330 * When that is used up an error message is given.
6331 */
6332 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006333check_renumber(spellinfo_T *spin)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006334{
6335 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6336 {
6337 spin->si_newprefID = 127;
6338 spin->si_newcompID = 255;
6339 }
6340}
6341
6342/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006343 * Return TRUE if flag "flag" appears in affix list "afflist".
6344 */
6345 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006346flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006347{
6348 char_u *p;
6349 unsigned n;
6350
6351 switch (flagtype)
6352 {
6353 case AFT_CHAR:
6354 return vim_strchr(afflist, flag) != NULL;
6355
Bram Moolenaar95529562005-08-25 21:21:38 +00006356 case AFT_CAPLONG:
6357 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006358 for (p = afflist; *p != NUL; )
6359 {
6360#ifdef FEAT_MBYTE
6361 n = mb_ptr2char_adv(&p);
6362#else
6363 n = *p++;
6364#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006365 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006366 && *p != NUL)
6367#ifdef FEAT_MBYTE
6368 n = mb_ptr2char_adv(&p) + (n << 16);
6369#else
6370 n = *p++ + (n << 16);
6371#endif
6372 if (n == flag)
6373 return TRUE;
6374 }
6375 break;
6376
Bram Moolenaar95529562005-08-25 21:21:38 +00006377 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006378 for (p = afflist; *p != NUL; )
6379 {
6380 n = getdigits(&p);
6381 if (n == flag)
6382 return TRUE;
6383 if (*p != NUL) /* skip over comma */
6384 ++p;
6385 }
6386 break;
6387 }
6388 return FALSE;
6389}
6390
6391/*
6392 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6393 */
6394 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006395aff_check_number(int spinval, int affval, char *name)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006396{
6397 if (spinval != 0 && spinval != affval)
6398 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6399}
6400
6401/*
6402 * Give a warning when "spinval" and "affval" strings are set and not the same.
6403 */
6404 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006405aff_check_string(char_u *spinval, char_u *affval, char *name)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006406{
6407 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6408 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6409}
6410
6411/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006412 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6413 * NULL as equal.
6414 */
6415 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006416str_equal(char_u *s1, char_u *s2)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006417{
6418 if (s1 == NULL || s2 == NULL)
6419 return s1 == s2;
6420 return STRCMP(s1, s2) == 0;
6421}
6422
6423/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006424 * Add a from-to item to "gap". Used for REP and SAL items.
6425 * They are stored case-folded.
6426 */
6427 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006428add_fromto(
6429 spellinfo_T *spin,
6430 garray_T *gap,
6431 char_u *from,
6432 char_u *to)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006433{
6434 fromto_T *ftp;
6435 char_u word[MAXWLEN];
6436
6437 if (ga_grow(gap, 1) == OK)
6438 {
6439 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006440 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006441 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006442 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006443 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006444 ++gap->ga_len;
6445 }
6446}
6447
6448/*
6449 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6450 */
6451 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006452sal_to_bool(char_u *s)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006453{
6454 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6455}
6456
6457/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006458 * Free the structure filled by spell_read_aff().
6459 */
6460 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006461spell_free_aff(afffile_T *aff)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006462{
6463 hashtab_T *ht;
6464 hashitem_T *hi;
6465 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006466 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006467 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006468
6469 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006470
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006471 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006472 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6473 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006474 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006475 for (hi = ht->ht_array; todo > 0; ++hi)
6476 {
6477 if (!HASHITEM_EMPTY(hi))
6478 {
6479 --todo;
6480 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006481 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar473de612013-06-08 18:19:48 +02006482 vim_regfree(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006483 }
6484 }
6485 if (ht == &aff->af_suff)
6486 break;
6487 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006488
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006489 hash_clear(&aff->af_pref);
6490 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006491 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006492}
6493
6494/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006495 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006496 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006497 */
6498 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006499spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006500{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006501 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006502 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006503 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006504 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006505 char_u store_afflist[MAXWLEN];
6506 int pfxlen;
6507 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006508 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006509 char_u *pc;
6510 char_u *w;
6511 int l;
6512 hash_T hash;
6513 hashitem_T *hi;
6514 FILE *fd;
6515 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516 int non_ascii = 0;
6517 int retval = OK;
6518 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006519 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006520 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006521
Bram Moolenaar51485f02005-06-04 21:55:20 +00006522 /*
6523 * Open the file.
6524 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006525 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006526 if (fd == NULL)
6527 {
6528 EMSG2(_(e_notopen), fname);
6529 return FAIL;
6530 }
6531
Bram Moolenaar51485f02005-06-04 21:55:20 +00006532 /* The hashtable is only used to detect duplicated words. */
6533 hash_init(&ht);
6534
Bram Moolenaar4770d092006-01-12 23:22:24 +00006535 vim_snprintf((char *)IObuff, IOSIZE,
6536 _("Reading dictionary file %s ..."), fname);
6537 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006538
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006539 /* start with a message for the first line */
6540 spin->si_msg_count = 999999;
6541
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006542 /* Read and ignore the first line: word count. */
6543 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006544 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006545 EMSG2(_("E760: No word count in %s"), fname);
6546
6547 /*
6548 * Read all the lines in the file one by one.
6549 * The words are converted to 'encoding' here, before being added to
6550 * the hashtable.
6551 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006552 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006553 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006554 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006555 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006556 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006557 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006558
Bram Moolenaar51485f02005-06-04 21:55:20 +00006559 /* Remove CR, LF and white space from the end. White space halfway
6560 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006561 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562 while (l > 0 && line[l - 1] <= ' ')
6563 --l;
6564 if (l == 0)
6565 continue; /* empty line */
6566 line[l] = NUL;
6567
Bram Moolenaarb765d632005-06-07 21:00:02 +00006568#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006569 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006570 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006571 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006572 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006573 if (pc == NULL)
6574 {
6575 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6576 fname, lnum, line);
6577 continue;
6578 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006579 w = pc;
6580 }
6581 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006582#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006583 {
6584 pc = NULL;
6585 w = line;
6586 }
6587
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006588 /* Truncate the word at the "/", set "afflist" to what follows.
6589 * Replace "\/" by "/" and "\\" by "\". */
6590 afflist = NULL;
6591 for (p = w; *p != NUL; mb_ptr_adv(p))
6592 {
6593 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006594 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006595 else if (*p == '/')
6596 {
6597 *p = NUL;
6598 afflist = p + 1;
6599 break;
6600 }
6601 }
6602
6603 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6604 if (spin->si_ascii && has_non_ascii(w))
6605 {
6606 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006607 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006608 continue;
6609 }
6610
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006611 /* This takes time, print a message every 10000 words. */
6612 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006613 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006614 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006615 vim_snprintf((char *)message, sizeof(message),
6616 _("line %6d, word %6d - %s"),
6617 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6618 msg_start();
6619 msg_puts_long_attr(message, 0);
6620 msg_clr_eos();
6621 msg_didout = FALSE;
6622 msg_col = 0;
6623 out_flush();
6624 }
6625
Bram Moolenaar51485f02005-06-04 21:55:20 +00006626 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006627 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006628 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006629 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006630 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006631 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006632 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006633 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006634
Bram Moolenaar51485f02005-06-04 21:55:20 +00006635 hash = hash_hash(dw);
6636 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006637 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006638 {
6639 if (p_verbose > 0)
6640 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006641 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006642 else if (duplicate == 0)
6643 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6644 fname, lnum, dw);
6645 ++duplicate;
6646 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006647 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006648 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006649
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006650 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006651 store_afflist[0] = NUL;
6652 pfxlen = 0;
6653 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006654 if (afflist != NULL)
6655 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006656 /* Extract flags from the affix list. */
6657 flags |= get_affix_flags(affile, afflist);
6658
Bram Moolenaar6de68532005-08-24 22:08:48 +00006659 if (affile->af_needaffix != 0 && flag_in_afflist(
6660 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006661 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006662
6663 if (affile->af_pfxpostpone)
6664 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006665 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006666
Bram Moolenaar5195e452005-08-19 20:32:47 +00006667 if (spin->si_compflags != NULL)
6668 /* Need to store the list of compound flags with the word.
6669 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006670 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006671 }
6672
Bram Moolenaar51485f02005-06-04 21:55:20 +00006673 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006674 if (store_word(spin, dw, flags, spin->si_region,
6675 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006676 retval = FAIL;
6677
6678 if (afflist != NULL)
6679 {
6680 /* Find all matching suffixes and add the resulting words.
6681 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006682 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006683 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006684 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006685 retval = FAIL;
6686
6687 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006688 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006689 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006690 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006691 retval = FAIL;
6692 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006693
6694 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006695 }
6696
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006697 if (duplicate > 0)
6698 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006699 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006700 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6701 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006702 hash_clear(&ht);
6703
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006704 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006705 return retval;
6706}
6707
6708/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006709 * Check for affix flags in "afflist" that are turned into word flags.
6710 * Return WF_ flags.
6711 */
6712 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006713get_affix_flags(afffile_T *affile, char_u *afflist)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006714{
6715 int flags = 0;
6716
6717 if (affile->af_keepcase != 0 && flag_in_afflist(
6718 affile->af_flagtype, afflist, affile->af_keepcase))
6719 flags |= WF_KEEPCAP | WF_FIXCAP;
6720 if (affile->af_rare != 0 && flag_in_afflist(
6721 affile->af_flagtype, afflist, affile->af_rare))
6722 flags |= WF_RARE;
6723 if (affile->af_bad != 0 && flag_in_afflist(
6724 affile->af_flagtype, afflist, affile->af_bad))
6725 flags |= WF_BANNED;
6726 if (affile->af_needcomp != 0 && flag_in_afflist(
6727 affile->af_flagtype, afflist, affile->af_needcomp))
6728 flags |= WF_NEEDCOMP;
6729 if (affile->af_comproot != 0 && flag_in_afflist(
6730 affile->af_flagtype, afflist, affile->af_comproot))
6731 flags |= WF_COMPROOT;
6732 if (affile->af_nosuggest != 0 && flag_in_afflist(
6733 affile->af_flagtype, afflist, affile->af_nosuggest))
6734 flags |= WF_NOSUGGEST;
6735 return flags;
6736}
6737
6738/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006739 * Get the list of prefix IDs from the affix list "afflist".
6740 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006741 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6742 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006743 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006744 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006745get_pfxlist(
6746 afffile_T *affile,
6747 char_u *afflist,
6748 char_u *store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006749{
6750 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006751 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006752 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006753 int id;
6754 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006755 hashitem_T *hi;
6756
Bram Moolenaar6de68532005-08-24 22:08:48 +00006757 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006758 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006759 prevp = p;
6760 if (get_affitem(affile->af_flagtype, &p) != 0)
6761 {
6762 /* A flag is a postponed prefix flag if it appears in "af_pref"
6763 * and it's ID is not zero. */
6764 vim_strncpy(key, prevp, p - prevp);
6765 hi = hash_find(&affile->af_pref, key);
6766 if (!HASHITEM_EMPTY(hi))
6767 {
6768 id = HI2AH(hi)->ah_newID;
6769 if (id != 0)
6770 store_afflist[cnt++] = id;
6771 }
6772 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006773 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006774 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006775 }
6776
Bram Moolenaar5195e452005-08-19 20:32:47 +00006777 store_afflist[cnt] = NUL;
6778 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006779}
6780
6781/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006782 * Get the list of compound IDs from the affix list "afflist" that are used
6783 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006784 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006785 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006786 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006787get_compflags(
6788 afffile_T *affile,
6789 char_u *afflist,
6790 char_u *store_afflist)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006791{
6792 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006793 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006794 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006795 char_u key[AH_KEY_LEN];
6796 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006797
Bram Moolenaar6de68532005-08-24 22:08:48 +00006798 for (p = afflist; *p != NUL; )
6799 {
6800 prevp = p;
6801 if (get_affitem(affile->af_flagtype, &p) != 0)
6802 {
6803 /* A flag is a compound flag if it appears in "af_comp". */
6804 vim_strncpy(key, prevp, p - prevp);
6805 hi = hash_find(&affile->af_comp, key);
6806 if (!HASHITEM_EMPTY(hi))
6807 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6808 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006809 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006810 ++p;
6811 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006812
Bram Moolenaar5195e452005-08-19 20:32:47 +00006813 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006814}
6815
6816/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006817 * Apply affixes to a word and store the resulting words.
6818 * "ht" is the hashtable with affentry_T that need to be applied, either
6819 * prefixes or suffixes.
6820 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6821 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006822 *
6823 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006824 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006825 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006826store_aff_word(
6827 spellinfo_T *spin, /* spell info */
6828 char_u *word, /* basic word start */
6829 char_u *afflist, /* list of names of supported affixes */
6830 afffile_T *affile,
6831 hashtab_T *ht,
6832 hashtab_T *xht,
6833 int condit, /* CONDIT_SUF et al. */
6834 int flags, /* flags for the word */
6835 char_u *pfxlist, /* list of prefix IDs */
6836 int pfxlen) /* nr of flags in "pfxlist" for prefixes, rest
Bram Moolenaar5195e452005-08-19 20:32:47 +00006837 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006838{
6839 int todo;
6840 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006841 affheader_T *ah;
6842 affentry_T *ae;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006843 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006844 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006845 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006846 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006847 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006848 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006849 int use_pfxlen;
6850 int need_affix;
6851 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006852 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006853 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006854 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006855
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006856 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006857 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006858 {
6859 if (!HASHITEM_EMPTY(hi))
6860 {
6861 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006862 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006863
Bram Moolenaar51485f02005-06-04 21:55:20 +00006864 /* Check that the affix combines, if required, and that the word
6865 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006866 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6867 && flag_in_afflist(affile->af_flagtype, afflist,
6868 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006869 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006870 /* Loop over all affix entries with this name. */
6871 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006872 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006873 /* Check the condition. It's not logical to match case
6874 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006875 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006876 * Another requirement from Myspell is that the chop
6877 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006878 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006879 * prefixes with a chop string and/or flags.
6880 * When a previously added affix had CIRCUMFIX this one
6881 * must have it too, if it had not then this one must not
6882 * have one either. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006883 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006884 || ae->ae_chop != NULL
6885 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006886 && (ae->ae_chop == NULL
6887 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006888 && (ae->ae_prog == NULL
Bram Moolenaardffa5b82014-11-19 16:38:07 +01006889 || vim_regexec_prog(&ae->ae_prog, FALSE,
6890 word, (colnr_T)0))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006891 && (((condit & CONDIT_CFIX) == 0)
6892 == ((condit & CONDIT_AFF) == 0
6893 || ae->ae_flags == NULL
6894 || !flag_in_afflist(affile->af_flagtype,
6895 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006896 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006897 /* Match. Remove the chop and add the affix. */
6898 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006899 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006900 /* prefix: chop/add at the start of the word */
6901 if (ae->ae_add == NULL)
6902 *newword = NUL;
6903 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006904 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006905 p = word;
6906 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006907 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006908 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006909#ifdef FEAT_MBYTE
6910 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006911 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006912 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006913 for ( ; i > 0; --i)
6914 mb_ptr_adv(p);
6915 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006916 else
6917#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006918 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006919 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920 STRCAT(newword, p);
6921 }
6922 else
6923 {
6924 /* suffix: chop/add at the end of the word */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006925 vim_strncpy(newword, word, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006926 if (ae->ae_chop != NULL)
6927 {
6928 /* Remove chop string. */
6929 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006930 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006931 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006932 mb_ptr_back(newword, p);
6933 *p = NUL;
6934 }
6935 if (ae->ae_add != NULL)
6936 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006937 }
6938
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006939 use_flags = flags;
6940 use_pfxlist = pfxlist;
6941 use_pfxlen = pfxlen;
6942 need_affix = FALSE;
6943 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6944 if (ae->ae_flags != NULL)
6945 {
6946 /* Extract flags from the affix list. */
6947 use_flags |= get_affix_flags(affile, ae->ae_flags);
6948
6949 if (affile->af_needaffix != 0 && flag_in_afflist(
6950 affile->af_flagtype, ae->ae_flags,
6951 affile->af_needaffix))
6952 need_affix = TRUE;
6953
6954 /* When there is a CIRCUMFIX flag the other affix
6955 * must also have it and we don't add the word
6956 * with one affix. */
6957 if (affile->af_circumfix != 0 && flag_in_afflist(
6958 affile->af_flagtype, ae->ae_flags,
6959 affile->af_circumfix))
6960 {
6961 use_condit |= CONDIT_CFIX;
6962 if ((condit & CONDIT_CFIX) == 0)
6963 need_affix = TRUE;
6964 }
6965
6966 if (affile->af_pfxpostpone
6967 || spin->si_compflags != NULL)
6968 {
6969 if (affile->af_pfxpostpone)
6970 /* Get prefix IDS from the affix list. */
6971 use_pfxlen = get_pfxlist(affile,
6972 ae->ae_flags, store_afflist);
6973 else
6974 use_pfxlen = 0;
6975 use_pfxlist = store_afflist;
6976
6977 /* Combine the prefix IDs. Avoid adding the
6978 * same ID twice. */
6979 for (i = 0; i < pfxlen; ++i)
6980 {
6981 for (j = 0; j < use_pfxlen; ++j)
6982 if (pfxlist[i] == use_pfxlist[j])
6983 break;
6984 if (j == use_pfxlen)
6985 use_pfxlist[use_pfxlen++] = pfxlist[i];
6986 }
6987
6988 if (spin->si_compflags != NULL)
6989 /* Get compound IDS from the affix list. */
6990 get_compflags(affile, ae->ae_flags,
6991 use_pfxlist + use_pfxlen);
6992
6993 /* Combine the list of compound flags.
6994 * Concatenate them to the prefix IDs list.
6995 * Avoid adding the same ID twice. */
6996 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6997 {
6998 for (j = use_pfxlen;
6999 use_pfxlist[j] != NUL; ++j)
7000 if (pfxlist[i] == use_pfxlist[j])
7001 break;
7002 if (use_pfxlist[j] == NUL)
7003 {
7004 use_pfxlist[j++] = pfxlist[i];
7005 use_pfxlist[j] = NUL;
7006 }
7007 }
7008 }
7009 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007010
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007011 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007012 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007013 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007014 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007015 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007016 use_pfxlist = pfx_pfxlist;
7017 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007018
7019 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007020 if (spin->si_prefroot != NULL
7021 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007022 {
7023 /* ... add a flag to indicate an affix was used. */
7024 use_flags |= WF_HAS_AFF;
7025
7026 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007027 * affixes is not allowed. But do use the
7028 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007029 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007030 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007031 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007032
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007033 /* When compounding is supported and there is no
7034 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7035 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007036 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007037 {
7038 if (xht != NULL)
7039 use_flags |= WF_NOCOMPAFT;
7040 else
7041 use_flags |= WF_NOCOMPBEF;
7042 }
7043
Bram Moolenaar51485f02005-06-04 21:55:20 +00007044 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007045 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007046 spin->si_region, use_pfxlist,
7047 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007048 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007049
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007050 /* When added a prefix or a first suffix and the affix
7051 * has flags may add a(nother) suffix. RECURSIVE! */
7052 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7053 if (store_aff_word(spin, newword, ae->ae_flags,
7054 affile, &affile->af_suff, xht,
7055 use_condit & (xht == NULL
7056 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007057 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007058 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007059
7060 /* When added a suffix and combining is allowed also
7061 * try adding a prefix additionally. Both for the
7062 * word flags and for the affix flags. RECURSIVE! */
7063 if (xht != NULL && ah->ah_combine)
7064 {
7065 if (store_aff_word(spin, newword,
7066 afflist, affile,
7067 xht, NULL, use_condit,
7068 use_flags, use_pfxlist,
7069 pfxlen) == FAIL
7070 || (ae->ae_flags != NULL
7071 && store_aff_word(spin, newword,
7072 ae->ae_flags, affile,
7073 xht, NULL, use_condit,
7074 use_flags, use_pfxlist,
7075 pfxlen) == FAIL))
7076 retval = FAIL;
7077 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007078 }
7079 }
7080 }
7081 }
7082 }
7083
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007084 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007085}
7086
7087/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007088 * Read a file with a list of words.
7089 */
7090 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007091spell_read_wordfile(spellinfo_T *spin, char_u *fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007092{
7093 FILE *fd;
7094 long lnum = 0;
7095 char_u rline[MAXLINELEN];
7096 char_u *line;
7097 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007098 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007099 int l;
7100 int retval = OK;
7101 int did_word = FALSE;
7102 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007103 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007104 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007105
7106 /*
7107 * Open the file.
7108 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007109 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007110 if (fd == NULL)
7111 {
7112 EMSG2(_(e_notopen), fname);
7113 return FAIL;
7114 }
7115
Bram Moolenaar4770d092006-01-12 23:22:24 +00007116 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7117 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007118
7119 /*
7120 * Read all the lines in the file one by one.
7121 */
7122 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7123 {
7124 line_breakcheck();
7125 ++lnum;
7126
7127 /* Skip comment lines. */
7128 if (*rline == '#')
7129 continue;
7130
7131 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007132 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007133 while (l > 0 && rline[l - 1] <= ' ')
7134 --l;
7135 if (l == 0)
7136 continue; /* empty or blank line */
7137 rline[l] = NUL;
7138
Bram Moolenaar9c102382006-05-03 21:26:49 +00007139 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007140 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007141#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007142 if (spin->si_conv.vc_type != CONV_NONE)
7143 {
7144 pc = string_convert(&spin->si_conv, rline, NULL);
7145 if (pc == NULL)
7146 {
7147 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7148 fname, lnum, rline);
7149 continue;
7150 }
7151 line = pc;
7152 }
7153 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007154#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007155 {
7156 pc = NULL;
7157 line = rline;
7158 }
7159
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007160 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007161 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007162 ++line;
7163 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007164 {
7165 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007166 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7167 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007168 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007169 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7170 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007171 else
7172 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007173#ifdef FEAT_MBYTE
7174 char_u *enc;
7175
Bram Moolenaar51485f02005-06-04 21:55:20 +00007176 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007177 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007178 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007179 if (enc != NULL && !spin->si_ascii
7180 && convert_setup(&spin->si_conv, enc,
7181 p_enc) == FAIL)
7182 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007183 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007185 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007186#else
7187 smsg((char_u *)_("Conversion in %s not supported"), fname);
7188#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007189 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007190 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007191 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007192
Bram Moolenaar3982c542005-06-08 21:56:31 +00007193 if (STRNCMP(line, "regions=", 8) == 0)
7194 {
7195 if (spin->si_region_count > 1)
7196 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7197 fname, lnum, line);
7198 else
7199 {
7200 line += 8;
7201 if (STRLEN(line) > 16)
7202 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7203 fname, lnum, line);
7204 else
7205 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007206 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007207 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007208
7209 /* Adjust the mask for a word valid in all regions. */
7210 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007211 }
7212 }
7213 continue;
7214 }
7215
Bram Moolenaar7887d882005-07-01 22:33:52 +00007216 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7217 fname, lnum, line - 1);
7218 continue;
7219 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007220
Bram Moolenaar7887d882005-07-01 22:33:52 +00007221 flags = 0;
7222 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007223
Bram Moolenaar7887d882005-07-01 22:33:52 +00007224 /* Check for flags and region after a slash. */
7225 p = vim_strchr(line, '/');
7226 if (p != NULL)
7227 {
7228 *p++ = NUL;
7229 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007230 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007231 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007232 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007233 else if (*p == '!') /* Bad, bad, wicked word. */
7234 flags |= WF_BANNED;
7235 else if (*p == '?') /* Rare word. */
7236 flags |= WF_RARE;
7237 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007238 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007239 if ((flags & WF_REGION) == 0) /* first one */
7240 regionmask = 0;
7241 flags |= WF_REGION;
7242
7243 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007244 if (l > spin->si_region_count)
7245 {
7246 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007247 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007248 break;
7249 }
7250 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007251 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007252 else
7253 {
7254 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7255 fname, lnum, p);
7256 break;
7257 }
7258 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007259 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007260 }
7261
7262 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7263 if (spin->si_ascii && has_non_ascii(line))
7264 {
7265 ++non_ascii;
7266 continue;
7267 }
7268
7269 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007270 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007271 {
7272 retval = FAIL;
7273 break;
7274 }
7275 did_word = TRUE;
7276 }
7277
7278 vim_free(pc);
7279 fclose(fd);
7280
Bram Moolenaar4770d092006-01-12 23:22:24 +00007281 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007282 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007283 vim_snprintf((char *)IObuff, IOSIZE,
7284 _("Ignored %d words with non-ASCII characters"), non_ascii);
7285 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007286 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007287
Bram Moolenaar51485f02005-06-04 21:55:20 +00007288 return retval;
7289}
7290
7291/*
7292 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007293 * This avoids calling free() for every little struct we use (and keeping
7294 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007295 * The memory is cleared to all zeros.
7296 * Returns NULL when out of memory.
7297 */
7298 static void *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007299getroom(
7300 spellinfo_T *spin,
7301 size_t len, /* length needed */
7302 int align) /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007303{
7304 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007305 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007306
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007307 if (align && bl != NULL)
7308 /* Round size up for alignment. On some systems structures need to be
7309 * aligned to the size of a pointer (e.g., SPARC). */
7310 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7311 & ~(sizeof(char *) - 1);
7312
Bram Moolenaar51485f02005-06-04 21:55:20 +00007313 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7314 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007315 if (len >= SBLOCKSIZE)
7316 bl = NULL;
7317 else
7318 /* Allocate a block of memory. It is not freed until much later. */
7319 bl = (sblock_T *)alloc_clear(
7320 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007321 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007322 {
7323 if (!spin->si_did_emsg)
7324 {
7325 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7326 spin->si_did_emsg = TRUE;
7327 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007328 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007329 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007330 bl->sb_next = spin->si_blocks;
7331 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007332 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007333 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007334 }
7335
7336 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007337 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007338
7339 return p;
7340}
7341
7342/*
7343 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007344 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007345 */
7346 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007347getroom_save(spellinfo_T *spin, char_u *s)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007348{
7349 char_u *sc;
7350
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007351 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007352 if (sc != NULL)
7353 STRCPY(sc, s);
7354 return sc;
7355}
7356
7357
7358/*
7359 * Free the list of allocated sblock_T.
7360 */
7361 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007362free_blocks(sblock_T *bl)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007363{
7364 sblock_T *next;
7365
7366 while (bl != NULL)
7367 {
7368 next = bl->sb_next;
7369 vim_free(bl);
7370 bl = next;
7371 }
7372}
7373
7374/*
7375 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007376 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007377 */
7378 static wordnode_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007379wordtree_alloc(spellinfo_T *spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007380{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007381 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007382}
7383
7384/*
7385 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007386 * Always store it in the case-folded tree. For a keep-case word this is
7387 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7388 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007389 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007390 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7391 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007392 */
7393 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007394store_word(
7395 spellinfo_T *spin,
7396 char_u *word,
7397 int flags, /* extra flags, WF_BANNED */
7398 int region, /* supported region(s) */
7399 char_u *pfxlist, /* list of prefix IDs or NULL */
7400 int need_affix) /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007401{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007402 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007403 int ct = captype(word, word + len);
7404 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007405 int res = OK;
7406 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007408 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007409 for (p = pfxlist; res == OK; ++p)
7410 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007411 if (!need_affix || (p != NULL && *p != NUL))
7412 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007413 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007414 if (p == NULL || *p == NUL)
7415 break;
7416 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007417 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007418
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007419 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007420 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007421 for (p = pfxlist; res == OK; ++p)
7422 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007423 if (!need_affix || (p != NULL && *p != NUL))
7424 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007425 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007426 if (p == NULL || *p == NUL)
7427 break;
7428 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007429 ++spin->si_keepwcount;
7430 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007431 return res;
7432}
7433
7434/*
7435 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007436 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007437 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007438 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007439 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007440 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007441tree_add_word(
7442 spellinfo_T *spin,
7443 char_u *word,
7444 wordnode_T *root,
7445 int flags,
7446 int region,
7447 int affixID)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007448{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007449 wordnode_T *node = root;
7450 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007451 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007452 wordnode_T **prev = NULL;
7453 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007454
Bram Moolenaar51485f02005-06-04 21:55:20 +00007455 /* Add each byte of the word to the tree, including the NUL at the end. */
7456 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007457 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007458 /* When there is more than one reference to this node we need to make
7459 * a copy, so that we can modify it. Copy the whole list of siblings
7460 * (we don't optimize for a partly shared list of siblings). */
7461 if (node != NULL && node->wn_refs > 1)
7462 {
7463 --node->wn_refs;
7464 copyprev = prev;
7465 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7466 {
7467 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007468 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007469 if (np == NULL)
7470 return FAIL;
7471 np->wn_child = copyp->wn_child;
7472 if (np->wn_child != NULL)
7473 ++np->wn_child->wn_refs; /* child gets extra ref */
7474 np->wn_byte = copyp->wn_byte;
7475 if (np->wn_byte == NUL)
7476 {
7477 np->wn_flags = copyp->wn_flags;
7478 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007479 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007480 }
7481
7482 /* Link the new node in the list, there will be one ref. */
7483 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007484 if (copyprev != NULL)
7485 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007486 copyprev = &np->wn_sibling;
7487
7488 /* Let "node" point to the head of the copied list. */
7489 if (copyp == node)
7490 node = np;
7491 }
7492 }
7493
Bram Moolenaar51485f02005-06-04 21:55:20 +00007494 /* Look for the sibling that has the same character. They are sorted
7495 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007496 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007497 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007498 while (node != NULL
7499 && (node->wn_byte < word[i]
7500 || (node->wn_byte == NUL
7501 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007502 ? node->wn_affixID < (unsigned)affixID
7503 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007504 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007505 && (spin->si_sugtree
7506 ? (node->wn_region & 0xffff) < region
7507 : node->wn_affixID
7508 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007509 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007510 prev = &node->wn_sibling;
7511 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007512 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007513 if (node == NULL
7514 || node->wn_byte != word[i]
7515 || (word[i] == NUL
7516 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007517 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007518 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007519 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007520 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007521 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007522 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007523 if (np == NULL)
7524 return FAIL;
7525 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007526
7527 /* If "node" is NULL this is a new child or the end of the sibling
7528 * list: ref count is one. Otherwise use ref count of sibling and
7529 * make ref count of sibling one (matters when inserting in front
7530 * of the list of siblings). */
7531 if (node == NULL)
7532 np->wn_refs = 1;
7533 else
7534 {
7535 np->wn_refs = node->wn_refs;
7536 node->wn_refs = 1;
7537 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007538 if (prev != NULL)
7539 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007540 np->wn_sibling = node;
7541 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007542 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007543
Bram Moolenaar51485f02005-06-04 21:55:20 +00007544 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007545 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007546 node->wn_flags = flags;
7547 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007548 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007549 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007550 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007551 prev = &node->wn_child;
7552 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007553 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007554#ifdef SPELL_PRINTTREE
Bram Moolenaar7b877b32016-01-09 13:51:34 +01007555 smsg((char_u *)"Added \"%s\"", word);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007556 spell_print_tree(root->wn_sibling);
7557#endif
7558
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007559 /* count nr of words added since last message */
7560 ++spin->si_msg_count;
7561
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007562 if (spin->si_compress_cnt > 1)
7563 {
7564 if (--spin->si_compress_cnt == 1)
7565 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007566 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007567 }
7568
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007569 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007570 * When we have allocated lots of memory we need to compress the word tree
7571 * to free up some room. But compression is slow, and we might actually
7572 * need that room, thus only compress in the following situations:
7573 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007574 * "compress_start" blocks.
7575 * 2. When compressed before and used "compress_inc" blocks before
7576 * adding "compress_added" words (si_compress_cnt > 1).
7577 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007578 * (si_compress_cnt == 1) and the number of free nodes drops below the
7579 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007580 */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01007581#ifndef SPELL_COMPRESS_ALLWAYS
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007582 if (spin->si_compress_cnt == 1
7583 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007584 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007585#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007586 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007587 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007588 * when the freed up room has been used and another "compress_inc"
7589 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007590 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007591 spin->si_blocks_cnt -= compress_inc;
7592 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007593
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007594 if (spin->si_verbose)
7595 {
7596 msg_start();
7597 msg_puts((char_u *)_(msg_compressing));
7598 msg_clr_eos();
7599 msg_didout = FALSE;
7600 msg_col = 0;
7601 out_flush();
7602 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007603
7604 /* Compress both trees. Either they both have many nodes, which makes
7605 * compression useful, or one of them is small, which means
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007606 * compression goes fast. But when filling the soundfold word tree
Bram Moolenaar4770d092006-01-12 23:22:24 +00007607 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007608 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007609 if (affixID >= 0)
7610 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007611 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007612
7613 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007614}
7615
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007616/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007617 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7618 * Sets "sps_flags".
7619 */
7620 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007621spell_check_msm(void)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007622{
7623 char_u *p = p_msm;
7624 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007625 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007626 long added = 0;
7627
7628 if (!VIM_ISDIGIT(*p))
7629 return FAIL;
7630 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7631 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7632 if (*p != ',')
7633 return FAIL;
7634 ++p;
7635 if (!VIM_ISDIGIT(*p))
7636 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007637 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007638 if (*p != ',')
7639 return FAIL;
7640 ++p;
7641 if (!VIM_ISDIGIT(*p))
7642 return FAIL;
7643 added = getdigits(&p) * 1024;
7644 if (*p != NUL)
7645 return FAIL;
7646
Bram Moolenaar89d40322006-08-29 15:30:07 +00007647 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007648 return FAIL;
7649
7650 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007651 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007652 compress_added = added;
7653 return OK;
7654}
7655
7656
7657/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007658 * Get a wordnode_T, either from the list of previously freed nodes or
7659 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007660 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007661 */
7662 static wordnode_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007663get_wordnode(spellinfo_T *spin)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007664{
7665 wordnode_T *n;
7666
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007667 if (spin->si_first_free == NULL)
7668 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007669 else
7670 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007671 n = spin->si_first_free;
7672 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007673 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007674 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007675 }
7676#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007677 if (n != NULL)
7678 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007679#endif
7680 return n;
7681}
7682
7683/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007684 * Decrement the reference count on a node (which is the head of a list of
7685 * siblings). If the reference count becomes zero free the node and its
7686 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007687 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007688 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007689 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007690deref_wordnode(spellinfo_T *spin, wordnode_T *node)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007691{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007692 wordnode_T *np;
7693 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007694
7695 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007696 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007697 for (np = node; np != NULL; np = np->wn_sibling)
7698 {
7699 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007700 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007701 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007702 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007703 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007704 ++cnt; /* length field */
7705 }
7706 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007707}
7708
7709/*
7710 * Free a wordnode_T for re-use later.
7711 * Only the "wn_child" field becomes invalid.
7712 */
7713 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007714free_wordnode(spellinfo_T *spin, wordnode_T *n)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007715{
7716 n->wn_child = spin->si_first_free;
7717 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007718 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007719}
7720
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007721/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007722 * Compress a tree: find tails that are identical and can be shared.
7723 */
7724 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007725wordtree_compress(spellinfo_T *spin, wordnode_T *root)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007726{
7727 hashtab_T ht;
7728 int n;
7729 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007730 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007731
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007732 /* Skip the root itself, it's not actually used. The first sibling is the
7733 * start of the tree. */
7734 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007735 {
7736 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007737 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007738
7739#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007740 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007741#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007742 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007743 if (tot > 1000000)
7744 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007745 else if (tot == 0)
7746 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007747 else
7748 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007749 vim_snprintf((char *)IObuff, IOSIZE,
7750 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7751 n, tot, tot - n, perc);
7752 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007753 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007754#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007755 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007756#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007757 hash_clear(&ht);
7758 }
7759}
7760
7761/*
7762 * Compress a node, its siblings and its children, depth first.
7763 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007764 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007765 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007766node_compress(
7767 spellinfo_T *spin,
7768 wordnode_T *node,
7769 hashtab_T *ht,
7770 int *tot) /* total count of nodes before compressing,
Bram Moolenaar51485f02005-06-04 21:55:20 +00007771 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007772{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007773 wordnode_T *np;
7774 wordnode_T *tp;
7775 wordnode_T *child;
7776 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007777 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007778 int len = 0;
7779 unsigned nr, n;
7780 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007781
Bram Moolenaar51485f02005-06-04 21:55:20 +00007782 /*
7783 * Go through the list of siblings. Compress each child and then try
7784 * finding an identical child to replace it.
7785 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007786 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007787 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007788 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007789 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007790 ++len;
7791 if ((child = np->wn_child) != NULL)
7792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007793 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007794 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007795
7796 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007797 hash = hash_hash(child->wn_u1.hashkey);
7798 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007799 if (!HASHITEM_EMPTY(hi))
7800 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007801 /* There are children we encountered before with a hash value
7802 * identical to the current child. Now check if there is one
7803 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007804 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007805 if (node_equal(child, tp))
7806 {
7807 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007808 * current one. This means the current child and all
7809 * its siblings is unlinked from the tree. */
7810 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007811 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007812 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007813 break;
7814 }
7815 if (tp == NULL)
7816 {
7817 /* No other child with this hash value equals the child of
7818 * the node, add it to the linked list after the first
7819 * item. */
7820 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007821 child->wn_u2.next = tp->wn_u2.next;
7822 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007823 }
7824 }
7825 else
7826 /* No other child has this hash value, add it to the
7827 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007828 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007829 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007830 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007831 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007832
7833 /*
7834 * Make a hash key for the node and its siblings, so that we can quickly
7835 * find a lookalike node. This must be done after compressing the sibling
7836 * list, otherwise the hash key would become invalid by the compression.
7837 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007838 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 nr = 0;
7840 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007841 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007842 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007843 /* end node: use wn_flags, wn_region and wn_affixID */
7844 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007845 else
7846 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007847 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007848 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007849 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007850
7851 /* Avoid NUL bytes, it terminates the hash key. */
7852 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007853 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007854 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007855 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007856 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007857 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007858 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007859 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7860 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007861
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007862 /* Check for CTRL-C pressed now and then. */
7863 fast_breakcheck();
7864
Bram Moolenaar51485f02005-06-04 21:55:20 +00007865 return compressed;
7866}
7867
7868/*
7869 * Return TRUE when two nodes have identical siblings and children.
7870 */
7871 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007872node_equal(wordnode_T *n1, wordnode_T *n2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007873{
7874 wordnode_T *p1;
7875 wordnode_T *p2;
7876
7877 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7878 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7879 if (p1->wn_byte != p2->wn_byte
7880 || (p1->wn_byte == NUL
7881 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007882 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007883 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007884 : (p1->wn_child != p2->wn_child)))
7885 break;
7886
7887 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007888}
7889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007890static int
7891#ifdef __BORLANDC__
7892_RTLENTRYF
7893#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007894rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007895
7896/*
7897 * Function given to qsort() to sort the REP items on "from" string.
7898 */
7899 static int
7900#ifdef __BORLANDC__
7901_RTLENTRYF
7902#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007903rep_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007904{
7905 fromto_T *p1 = (fromto_T *)s1;
7906 fromto_T *p2 = (fromto_T *)s2;
7907
7908 return STRCMP(p1->ft_from, p2->ft_from);
7909}
7910
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007911/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007912 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007913 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007914 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007915 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007916write_vim_spell(spellinfo_T *spin, char_u *fname)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007917{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007918 FILE *fd;
7919 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007920 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007921 wordnode_T *tree;
7922 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007923 int i;
7924 int l;
7925 garray_T *gap;
7926 fromto_T *ftp;
7927 char_u *p;
7928 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007929 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007930 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007931 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007932
Bram Moolenaarb765d632005-06-07 21:00:02 +00007933 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007934 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007935 {
7936 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007937 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007938 }
7939
Bram Moolenaar5195e452005-08-19 20:32:47 +00007940 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007941 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007942 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007943 if (fwv != (size_t)1)
7944 /* Catch first write error, don't try writing more. */
7945 goto theend;
7946
Bram Moolenaar5195e452005-08-19 20:32:47 +00007947 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007948
Bram Moolenaar5195e452005-08-19 20:32:47 +00007949 /*
7950 * <SECTIONS>: <section> ... <sectionend>
7951 */
7952
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007953 /* SN_INFO: <infotext> */
7954 if (spin->si_info != NULL)
7955 {
7956 putc(SN_INFO, fd); /* <sectionID> */
7957 putc(0, fd); /* <sectionflags> */
7958
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007959 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007960 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007961 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007962 }
7963
Bram Moolenaar5195e452005-08-19 20:32:47 +00007964 /* SN_REGION: <regionname> ...
7965 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007966 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007967 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007968 putc(SN_REGION, fd); /* <sectionID> */
7969 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7970 l = spin->si_region_count * 2;
7971 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007972 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007974 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007975 }
7976 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007977 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007978
Bram Moolenaar5195e452005-08-19 20:32:47 +00007979 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7980 *
7981 * The table with character flags and the table for case folding.
7982 * This makes sure the same characters are recognized as word characters
7983 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007984 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007985 * 'encoding'.
7986 * Also skip this for an .add.spl file, the main spell file must contain
7987 * the table (avoids that it conflicts). File is shorter too.
7988 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007989 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007990 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007991 char_u folchars[128 * 8];
7992 int flags;
7993
Bram Moolenaard12a1322005-08-21 22:08:24 +00007994 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007995 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7996
7997 /* Form the <folchars> string first, we need to know its length. */
7998 l = 0;
7999 for (i = 128; i < 256; ++i)
8000 {
8001#ifdef FEAT_MBYTE
8002 if (has_mbyte)
8003 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8004 else
8005#endif
8006 folchars[l++] = spelltab.st_fold[i];
8007 }
8008 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8009
8010 fputc(128, fd); /* <charflagslen> */
8011 for (i = 128; i < 256; ++i)
8012 {
8013 flags = 0;
8014 if (spelltab.st_isw[i])
8015 flags |= CF_WORD;
8016 if (spelltab.st_isu[i])
8017 flags |= CF_UPPER;
8018 fputc(flags, fd); /* <charflags> */
8019 }
8020
8021 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008022 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008023 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008024
Bram Moolenaar5195e452005-08-19 20:32:47 +00008025 /* SN_MIDWORD: <midword> */
8026 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008027 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008028 putc(SN_MIDWORD, fd); /* <sectionID> */
8029 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8030
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008031 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008032 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008033 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8034 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008035 }
8036
Bram Moolenaar5195e452005-08-19 20:32:47 +00008037 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8038 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008039 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008040 putc(SN_PREFCOND, fd); /* <sectionID> */
8041 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8042
8043 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8044 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8045
8046 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008047 }
8048
Bram Moolenaar5195e452005-08-19 20:32:47 +00008049 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008050 * SN_SAL: <salflags> <salcount> <sal> ...
8051 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008052
Bram Moolenaar5195e452005-08-19 20:32:47 +00008053 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008054 * round 2: SN_SAL section (unless SN_SOFO is used)
8055 * round 3: SN_REPSAL section */
8056 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008057 {
8058 if (round == 1)
8059 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008060 else if (round == 2)
8061 {
8062 /* Don't write SN_SAL when using a SN_SOFO section */
8063 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8064 continue;
8065 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008068 gap = &spin->si_repsal;
8069
8070 /* Don't write the section if there are no items. */
8071 if (gap->ga_len == 0)
8072 continue;
8073
8074 /* Sort the REP/REPSAL items. */
8075 if (round != 2)
8076 qsort(gap->ga_data, (size_t)gap->ga_len,
8077 sizeof(fromto_T), rep_compare);
8078
8079 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8080 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008081
Bram Moolenaar5195e452005-08-19 20:32:47 +00008082 /* This is for making suggestions, section is not required. */
8083 putc(0, fd); /* <sectionflags> */
8084
8085 /* Compute the length of what follows. */
8086 l = 2; /* count <repcount> or <salcount> */
8087 for (i = 0; i < gap->ga_len; ++i)
8088 {
8089 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008090 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8091 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008092 }
8093 if (round == 2)
8094 ++l; /* count <salflags> */
8095 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8096
8097 if (round == 2)
8098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008099 i = 0;
8100 if (spin->si_followup)
8101 i |= SAL_F0LLOWUP;
8102 if (spin->si_collapse)
8103 i |= SAL_COLLAPSE;
8104 if (spin->si_rem_accents)
8105 i |= SAL_REM_ACCENTS;
8106 putc(i, fd); /* <salflags> */
8107 }
8108
8109 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8110 for (i = 0; i < gap->ga_len; ++i)
8111 {
8112 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8113 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8114 ftp = &((fromto_T *)gap->ga_data)[i];
8115 for (rr = 1; rr <= 2; ++rr)
8116 {
8117 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008118 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008119 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008120 if (l > 0)
8121 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 }
8123 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125 }
8126
Bram Moolenaar5195e452005-08-19 20:32:47 +00008127 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8128 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008129 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8130 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008131 putc(SN_SOFO, fd); /* <sectionID> */
8132 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008134 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008135 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8136 /* <sectionlen> */
8137
8138 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008139 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008141 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008142 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008143 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008144 }
8145
Bram Moolenaar4770d092006-01-12 23:22:24 +00008146 /* SN_WORDS: <word> ...
8147 * This is for making suggestions, section is not required. */
8148 if (spin->si_commonwords.ht_used > 0)
8149 {
8150 putc(SN_WORDS, fd); /* <sectionID> */
8151 putc(0, fd); /* <sectionflags> */
8152
8153 /* round 1: count the bytes
8154 * round 2: write the bytes */
8155 for (round = 1; round <= 2; ++round)
8156 {
8157 int todo;
8158 int len = 0;
8159 hashitem_T *hi;
8160
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008161 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008162 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8163 if (!HASHITEM_EMPTY(hi))
8164 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008165 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008166 len += l;
8167 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008168 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008169 --todo;
8170 }
8171 if (round == 1)
8172 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8173 }
8174 }
8175
Bram Moolenaar5195e452005-08-19 20:32:47 +00008176 /* SN_MAP: <mapstr>
8177 * This is for making suggestions, section is not required. */
8178 if (spin->si_map.ga_len > 0)
8179 {
8180 putc(SN_MAP, fd); /* <sectionID> */
8181 putc(0, fd); /* <sectionflags> */
8182 l = spin->si_map.ga_len;
8183 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008184 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008185 /* <mapstr> */
8186 }
8187
Bram Moolenaar4770d092006-01-12 23:22:24 +00008188 /* SN_SUGFILE: <timestamp>
8189 * This is used to notify that a .sug file may be available and at the
8190 * same time allows for checking that a .sug file that is found matches
8191 * with this .spl file. That's because the word numbers must be exactly
8192 * right. */
8193 if (!spin->si_nosugfile
8194 && (spin->si_sal.ga_len > 0
8195 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8196 {
8197 putc(SN_SUGFILE, fd); /* <sectionID> */
8198 putc(0, fd); /* <sectionflags> */
8199 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8200
8201 /* Set si_sugtime and write it to the file. */
8202 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008203 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008204 }
8205
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008206 /* SN_NOSPLITSUGS: nothing
8207 * This is used to notify that no suggestions with word splits are to be
8208 * made. */
8209 if (spin->si_nosplitsugs)
8210 {
8211 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8212 putc(0, fd); /* <sectionflags> */
8213 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8214 }
8215
Bram Moolenaar7b877b32016-01-09 13:51:34 +01008216 /* SN_NOCOMPUNDSUGS: nothing
8217 * This is used to notify that no suggestions with compounds are to be
8218 * made. */
8219 if (spin->si_nocompoundsugs)
8220 {
8221 putc(SN_NOCOMPOUNDSUGS, fd); /* <sectionID> */
8222 putc(0, fd); /* <sectionflags> */
8223 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8224 }
8225
Bram Moolenaar5195e452005-08-19 20:32:47 +00008226 /* SN_COMPOUND: compound info.
8227 * We don't mark it required, when not supported all compound words will
8228 * be bad words. */
8229 if (spin->si_compflags != NULL)
8230 {
8231 putc(SN_COMPOUND, fd); /* <sectionID> */
8232 putc(0, fd); /* <sectionflags> */
8233
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008234 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008235 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008236 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008237 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8238
Bram Moolenaar5195e452005-08-19 20:32:47 +00008239 putc(spin->si_compmax, fd); /* <compmax> */
8240 putc(spin->si_compminlen, fd); /* <compminlen> */
8241 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008242 putc(0, fd); /* for Vim 7.0b compatibility */
8243 putc(spin->si_compoptions, fd); /* <compoptions> */
8244 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8245 /* <comppatcount> */
8246 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8247 {
8248 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008249 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008250 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8251 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008252 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008253 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008254 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008255 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008256 }
8257
Bram Moolenaar78622822005-08-23 21:00:13 +00008258 /* SN_NOBREAK: NOBREAK flag */
8259 if (spin->si_nobreak)
8260 {
8261 putc(SN_NOBREAK, fd); /* <sectionID> */
8262 putc(0, fd); /* <sectionflags> */
8263
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008264 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008265 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8266 }
8267
Bram Moolenaar5195e452005-08-19 20:32:47 +00008268 /* SN_SYLLABLE: syllable info.
8269 * We don't mark it required, when not supported syllables will not be
8270 * counted. */
8271 if (spin->si_syllable != NULL)
8272 {
8273 putc(SN_SYLLABLE, fd); /* <sectionID> */
8274 putc(0, fd); /* <sectionflags> */
8275
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008276 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008277 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008278 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8279 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008280 }
8281
8282 /* end of <SECTIONS> */
8283 putc(SN_END, fd); /* <sectionend> */
8284
Bram Moolenaar50cde822005-06-05 21:54:54 +00008285
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008286 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008287 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008288 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008289 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008290 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008291 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008292 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008293 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008294 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008295 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008296 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008297 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008298
Bram Moolenaar0c405862005-06-22 22:26:26 +00008299 /* Clear the index and wnode fields in the tree. */
8300 clear_node(tree);
8301
Bram Moolenaar51485f02005-06-04 21:55:20 +00008302 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008303 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008304 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008305 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008306
Bram Moolenaar51485f02005-06-04 21:55:20 +00008307 /* number of nodes in 4 bytes */
8308 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008309 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008310
Bram Moolenaar51485f02005-06-04 21:55:20 +00008311 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008312 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008313 }
8314
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008315 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008316 if (putc(0, fd) == EOF)
8317 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008318theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008319 if (fclose(fd) == EOF)
8320 retval = FAIL;
8321
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008322 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008323 retval = FAIL;
8324 if (retval == FAIL)
8325 EMSG(_(e_write));
8326
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008327 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008328}
8329
8330/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008331 * Clear the index and wnode fields of "node", it siblings and its
8332 * children. This is needed because they are a union with other items to save
8333 * space.
8334 */
8335 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008336clear_node(wordnode_T *node)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008337{
8338 wordnode_T *np;
8339
8340 if (node != NULL)
8341 for (np = node; np != NULL; np = np->wn_sibling)
8342 {
8343 np->wn_u1.index = 0;
8344 np->wn_u2.wnode = NULL;
8345
8346 if (np->wn_byte != NUL)
8347 clear_node(np->wn_child);
8348 }
8349}
8350
8351
8352/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008353 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008354 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008355 * This first writes the list of possible bytes (siblings). Then for each
8356 * byte recursively write the children.
8357 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008358 * NOTE: The code here must match the code in read_tree_node(), since
8359 * assumptions are made about the indexes (so that we don't have to write them
8360 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008361 *
8362 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008363 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008364 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008365put_node(
8366 FILE *fd, /* NULL when only counting */
8367 wordnode_T *node,
8368 int idx,
8369 int regionmask,
8370 int prefixtree) /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008371{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008372 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008373 int siblingcount = 0;
8374 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008375 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008376
Bram Moolenaar51485f02005-06-04 21:55:20 +00008377 /* If "node" is zero the tree is empty. */
8378 if (node == NULL)
8379 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008380
Bram Moolenaar51485f02005-06-04 21:55:20 +00008381 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008382 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008383
8384 /* Count the number of siblings. */
8385 for (np = node; np != NULL; np = np->wn_sibling)
8386 ++siblingcount;
8387
8388 /* Write the sibling count. */
8389 if (fd != NULL)
8390 putc(siblingcount, fd); /* <siblingcount> */
8391
8392 /* Write each sibling byte and optionally extra info. */
8393 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008394 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008395 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008396 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008397 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008398 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008399 /* For a NUL byte (end of word) write the flags etc. */
8400 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008401 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008402 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008403 * associated condition nr (stored in wn_region). The
8404 * byte value is misused to store the "rare" and "not
8405 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008406 if (np->wn_flags == (short_u)PFX_FLAGS)
8407 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008408 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008409 {
8410 putc(BY_FLAGS, fd); /* <byte> */
8411 putc(np->wn_flags, fd); /* <pflags> */
8412 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008413 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008414 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008415 }
8416 else
8417 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008418 /* For word trees we write the flag/region items. */
8419 flags = np->wn_flags;
8420 if (regionmask != 0 && np->wn_region != regionmask)
8421 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008422 if (np->wn_affixID != 0)
8423 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008424 if (flags == 0)
8425 {
8426 /* word without flags or region */
8427 putc(BY_NOFLAGS, fd); /* <byte> */
8428 }
8429 else
8430 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008431 if (np->wn_flags >= 0x100)
8432 {
8433 putc(BY_FLAGS2, fd); /* <byte> */
8434 putc(flags, fd); /* <flags> */
8435 putc((unsigned)flags >> 8, fd); /* <flags2> */
8436 }
8437 else
8438 {
8439 putc(BY_FLAGS, fd); /* <byte> */
8440 putc(flags, fd); /* <flags> */
8441 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008442 if (flags & WF_REGION)
8443 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008444 if (flags & WF_AFX)
8445 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008446 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008447 }
8448 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008449 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008450 else
8451 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008452 if (np->wn_child->wn_u1.index != 0
8453 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008454 {
8455 /* The child is written elsewhere, write the reference. */
8456 if (fd != NULL)
8457 {
8458 putc(BY_INDEX, fd); /* <byte> */
8459 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008460 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008461 }
8462 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008463 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008464 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008465 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008466
Bram Moolenaar51485f02005-06-04 21:55:20 +00008467 if (fd != NULL)
8468 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8469 {
8470 EMSG(_(e_write));
8471 return 0;
8472 }
8473 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008474 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008475
8476 /* Space used in the array when reading: one for each sibling and one for
8477 * the count. */
8478 newindex += siblingcount + 1;
8479
8480 /* Recursively dump the children of each sibling. */
8481 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008482 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8483 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008484 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008485
8486 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008487}
8488
8489
8490/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008491 * ":mkspell [-ascii] outfile infile ..."
8492 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008493 */
8494 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008495ex_mkspell(exarg_T *eap)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008496{
8497 int fcount;
8498 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008499 char_u *arg = eap->arg;
8500 int ascii = FALSE;
8501
8502 if (STRNCMP(arg, "-ascii", 6) == 0)
8503 {
8504 ascii = TRUE;
8505 arg = skipwhite(arg + 6);
8506 }
8507
8508 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02008509 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008511 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008512 FreeWild(fcount, fnames);
8513 }
8514}
8515
8516/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008517 * Create the .sug file.
8518 * Uses the soundfold info in "spin".
8519 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8520 */
8521 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008522spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008523{
Bram Moolenaard9462e32011-04-11 21:35:11 +02008524 char_u *fname = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008525 int len;
8526 slang_T *slang;
8527 int free_slang = FALSE;
8528
8529 /*
8530 * Read back the .spl file that was written. This fills the required
8531 * info for soundfolding. This also uses less memory than the
8532 * pointer-linked version of the trie. And it avoids having two versions
8533 * of the code for the soundfolding stuff.
8534 * It might have been done already by spell_reload_one().
8535 */
8536 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8537 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8538 break;
8539 if (slang == NULL)
8540 {
8541 spell_message(spin, (char_u *)_("Reading back spell file..."));
8542 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8543 if (slang == NULL)
8544 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008545 free_slang = TRUE;
8546 }
8547
8548 /*
8549 * Clear the info in "spin" that is used.
8550 */
8551 spin->si_blocks = NULL;
8552 spin->si_blocks_cnt = 0;
8553 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8554 spin->si_free_count = 0;
8555 spin->si_first_free = NULL;
8556 spin->si_foldwcount = 0;
8557
8558 /*
8559 * Go through the trie of good words, soundfold each word and add it to
8560 * the soundfold trie.
8561 */
8562 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8563 if (sug_filltree(spin, slang) == FAIL)
8564 goto theend;
8565
8566 /*
8567 * Create the table which links each soundfold word with a list of the
8568 * good words it may come from. Creates buffer "spin->si_spellbuf".
8569 * This also removes the wordnr from the NUL byte entries to make
8570 * compression possible.
8571 */
8572 if (sug_maketable(spin) == FAIL)
8573 goto theend;
8574
8575 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8576 (long)spin->si_spellbuf->b_ml.ml_line_count);
8577
8578 /*
8579 * Compress the soundfold trie.
8580 */
8581 spell_message(spin, (char_u *)_(msg_compressing));
8582 wordtree_compress(spin, spin->si_foldroot);
8583
8584 /*
8585 * Write the .sug file.
8586 * Make the file name by changing ".spl" to ".sug".
8587 */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008588 fname = alloc(MAXPATHL);
8589 if (fname == NULL)
8590 goto theend;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008591 vim_strncpy(fname, wfname, MAXPATHL - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008592 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008593 fname[len - 2] = 'u';
8594 fname[len - 1] = 'g';
8595 sug_write(spin, fname);
8596
8597theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02008598 vim_free(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008599 if (free_slang)
8600 slang_free(slang);
8601 free_blocks(spin->si_blocks);
8602 close_spellbuf(spin->si_spellbuf);
8603}
8604
8605/*
8606 * Build the soundfold trie for language "slang".
8607 */
8608 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008609sug_filltree(spellinfo_T *spin, slang_T *slang)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008610{
8611 char_u *byts;
8612 idx_T *idxs;
8613 int depth;
8614 idx_T arridx[MAXWLEN];
8615 int curi[MAXWLEN];
8616 char_u tword[MAXWLEN];
8617 char_u tsalword[MAXWLEN];
8618 int c;
8619 idx_T n;
8620 unsigned words_done = 0;
8621 int wordcount[MAXWLEN];
8622
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008623 /* We use si_foldroot for the soundfolded trie. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008624 spin->si_foldroot = wordtree_alloc(spin);
8625 if (spin->si_foldroot == NULL)
8626 return FAIL;
8627
8628 /* let tree_add_word() know we're adding to the soundfolded tree */
8629 spin->si_sugtree = TRUE;
8630
8631 /*
8632 * Go through the whole case-folded tree, soundfold each word and put it
8633 * in the trie.
8634 */
8635 byts = slang->sl_fbyts;
8636 idxs = slang->sl_fidxs;
8637
8638 arridx[0] = 0;
8639 curi[0] = 1;
8640 wordcount[0] = 0;
8641
8642 depth = 0;
8643 while (depth >= 0 && !got_int)
8644 {
8645 if (curi[depth] > byts[arridx[depth]])
8646 {
8647 /* Done all bytes at this node, go up one level. */
8648 idxs[arridx[depth]] = wordcount[depth];
8649 if (depth > 0)
8650 wordcount[depth - 1] += wordcount[depth];
8651
8652 --depth;
8653 line_breakcheck();
8654 }
8655 else
8656 {
8657
8658 /* Do one more byte at this node. */
8659 n = arridx[depth] + curi[depth];
8660 ++curi[depth];
8661
8662 c = byts[n];
8663 if (c == 0)
8664 {
8665 /* Sound-fold the word. */
8666 tword[depth] = NUL;
8667 spell_soundfold(slang, tword, TRUE, tsalword);
8668
8669 /* We use the "flags" field for the MSB of the wordnr,
8670 * "region" for the LSB of the wordnr. */
8671 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8672 words_done >> 16, words_done & 0xffff,
8673 0) == FAIL)
8674 return FAIL;
8675
8676 ++words_done;
8677 ++wordcount[depth];
8678
8679 /* Reset the block count each time to avoid compression
8680 * kicking in. */
8681 spin->si_blocks_cnt = 0;
8682
8683 /* Skip over any other NUL bytes (same word with different
8684 * flags). */
8685 while (byts[n + 1] == 0)
8686 {
8687 ++n;
8688 ++curi[depth];
8689 }
8690 }
8691 else
8692 {
8693 /* Normal char, go one level deeper. */
8694 tword[depth++] = c;
8695 arridx[depth] = idxs[n];
8696 curi[depth] = 1;
8697 wordcount[depth] = 0;
8698 }
8699 }
8700 }
8701
8702 smsg((char_u *)_("Total number of words: %d"), words_done);
8703
8704 return OK;
8705}
8706
8707/*
8708 * Make the table that links each word in the soundfold trie to the words it
8709 * can be produced from.
8710 * This is not unlike lines in a file, thus use a memfile to be able to access
8711 * the table efficiently.
8712 * Returns FAIL when out of memory.
8713 */
8714 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008715sug_maketable(spellinfo_T *spin)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008716{
8717 garray_T ga;
8718 int res = OK;
8719
8720 /* Allocate a buffer, open a memline for it and create the swap file
8721 * (uses a temp file, not a .swp file). */
8722 spin->si_spellbuf = open_spellbuf();
8723 if (spin->si_spellbuf == NULL)
8724 return FAIL;
8725
8726 /* Use a buffer to store the line info, avoids allocating many small
8727 * pieces of memory. */
8728 ga_init2(&ga, 1, 100);
8729
8730 /* recursively go through the tree */
8731 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8732 res = FAIL;
8733
8734 ga_clear(&ga);
8735 return res;
8736}
8737
8738/*
8739 * Fill the table for one node and its children.
8740 * Returns the wordnr at the start of the node.
8741 * Returns -1 when out of memory.
8742 */
8743 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008744sug_filltable(
8745 spellinfo_T *spin,
8746 wordnode_T *node,
8747 int startwordnr,
8748 garray_T *gap) /* place to store line of numbers */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008749{
8750 wordnode_T *p, *np;
8751 int wordnr = startwordnr;
8752 int nr;
8753 int prev_nr;
8754
8755 for (p = node; p != NULL; p = p->wn_sibling)
8756 {
8757 if (p->wn_byte == NUL)
8758 {
8759 gap->ga_len = 0;
8760 prev_nr = 0;
8761 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8762 {
8763 if (ga_grow(gap, 10) == FAIL)
8764 return -1;
8765
8766 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8767 /* Compute the offset from the previous nr and store the
8768 * offset in a way that it takes a minimum number of bytes.
8769 * It's a bit like utf-8, but without the need to mark
8770 * following bytes. */
8771 nr -= prev_nr;
8772 prev_nr += nr;
8773 gap->ga_len += offset2bytes(nr,
8774 (char_u *)gap->ga_data + gap->ga_len);
8775 }
8776
8777 /* add the NUL byte */
8778 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8779
8780 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8781 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8782 return -1;
8783 ++wordnr;
8784
8785 /* Remove extra NUL entries, we no longer need them. We don't
8786 * bother freeing the nodes, the won't be reused anyway. */
8787 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8788 p->wn_sibling = p->wn_sibling->wn_sibling;
8789
8790 /* Clear the flags on the remaining NUL node, so that compression
8791 * works a lot better. */
8792 p->wn_flags = 0;
8793 p->wn_region = 0;
8794 }
8795 else
8796 {
8797 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8798 if (wordnr == -1)
8799 return -1;
8800 }
8801 }
8802 return wordnr;
8803}
8804
8805/*
8806 * Convert an offset into a minimal number of bytes.
8807 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8808 * bytes.
8809 */
8810 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008811offset2bytes(int nr, char_u *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008812{
8813 int rem;
8814 int b1, b2, b3, b4;
8815
8816 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8817 b1 = nr % 255 + 1;
8818 rem = nr / 255;
8819 b2 = rem % 255 + 1;
8820 rem = rem / 255;
8821 b3 = rem % 255 + 1;
8822 b4 = rem / 255 + 1;
8823
8824 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8825 {
8826 buf[0] = 0xe0 + b4;
8827 buf[1] = b3;
8828 buf[2] = b2;
8829 buf[3] = b1;
8830 return 4;
8831 }
8832 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8833 {
8834 buf[0] = 0xc0 + b3;
8835 buf[1] = b2;
8836 buf[2] = b1;
8837 return 3;
8838 }
8839 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8840 {
8841 buf[0] = 0x80 + b2;
8842 buf[1] = b1;
8843 return 2;
8844 }
8845 /* 1 byte */
8846 buf[0] = b1;
8847 return 1;
8848}
8849
8850/*
8851 * Opposite of offset2bytes().
8852 * "pp" points to the bytes and is advanced over it.
8853 * Returns the offset.
8854 */
8855 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008856bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008857{
8858 char_u *p = *pp;
8859 int nr;
8860 int c;
8861
8862 c = *p++;
8863 if ((c & 0x80) == 0x00) /* 1 byte */
8864 {
8865 nr = c - 1;
8866 }
8867 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8868 {
8869 nr = (c & 0x3f) - 1;
8870 nr = nr * 255 + (*p++ - 1);
8871 }
8872 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8873 {
8874 nr = (c & 0x1f) - 1;
8875 nr = nr * 255 + (*p++ - 1);
8876 nr = nr * 255 + (*p++ - 1);
8877 }
8878 else /* 4 bytes */
8879 {
8880 nr = (c & 0x0f) - 1;
8881 nr = nr * 255 + (*p++ - 1);
8882 nr = nr * 255 + (*p++ - 1);
8883 nr = nr * 255 + (*p++ - 1);
8884 }
8885
8886 *pp = p;
8887 return nr;
8888}
8889
8890/*
8891 * Write the .sug file in "fname".
8892 */
8893 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008894sug_write(spellinfo_T *spin, char_u *fname)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008895{
8896 FILE *fd;
8897 wordnode_T *tree;
8898 int nodecount;
8899 int wcount;
8900 char_u *line;
8901 linenr_T lnum;
8902 int len;
8903
8904 /* Create the file. Note that an existing file is silently overwritten! */
8905 fd = mch_fopen((char *)fname, "w");
8906 if (fd == NULL)
8907 {
8908 EMSG2(_(e_notopen), fname);
8909 return;
8910 }
8911
8912 vim_snprintf((char *)IObuff, IOSIZE,
8913 _("Writing suggestion file %s ..."), fname);
8914 spell_message(spin, IObuff);
8915
8916 /*
8917 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8918 */
8919 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8920 {
8921 EMSG(_(e_write));
8922 goto theend;
8923 }
8924 putc(VIMSUGVERSION, fd); /* <versionnr> */
8925
8926 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008927 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008928
8929 /*
8930 * <SUGWORDTREE>
8931 */
8932 spin->si_memtot = 0;
8933 tree = spin->si_foldroot->wn_sibling;
8934
8935 /* Clear the index and wnode fields in the tree. */
8936 clear_node(tree);
8937
8938 /* Count the number of nodes. Needed to be able to allocate the
8939 * memory when reading the nodes. Also fills in index for shared
8940 * nodes. */
8941 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8942
8943 /* number of nodes in 4 bytes */
8944 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8945 spin->si_memtot += nodecount + nodecount * sizeof(int);
8946
8947 /* Write the nodes. */
8948 (void)put_node(fd, tree, 0, 0, FALSE);
8949
8950 /*
8951 * <SUGTABLE>: <sugwcount> <sugline> ...
8952 */
8953 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8954 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8955
8956 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8957 {
8958 /* <sugline>: <sugnr> ... NUL */
8959 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008960 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008961 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8962 {
8963 EMSG(_(e_write));
8964 goto theend;
8965 }
8966 spin->si_memtot += len;
8967 }
8968
8969 /* Write another byte to check for errors. */
8970 if (putc(0, fd) == EOF)
8971 EMSG(_(e_write));
8972
8973 vim_snprintf((char *)IObuff, IOSIZE,
8974 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8975 spell_message(spin, IObuff);
8976
8977theend:
8978 /* close the file */
8979 fclose(fd);
8980}
8981
8982/*
8983 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8984 * list and only contains text lines. Can use a swapfile to reduce memory
8985 * use.
8986 * Most other fields are invalid! Esp. watch out for string options being
8987 * NULL and there is no undo info.
8988 * Returns NULL when out of memory.
8989 */
8990 static buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008991open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008992{
8993 buf_T *buf;
8994
8995 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8996 if (buf != NULL)
8997 {
8998 buf->b_spell = TRUE;
8999 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02009000#ifdef FEAT_CRYPT
9001 buf->b_p_key = empty_option;
9002#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00009003 ml_open(buf);
9004 ml_open_file(buf); /* create swap file now */
9005 }
9006 return buf;
9007}
9008
9009/*
9010 * Close the buffer used for spell info.
9011 */
9012 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009013close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009014{
9015 if (buf != NULL)
9016 {
9017 ml_close(buf, TRUE);
9018 vim_free(buf);
9019 }
9020}
9021
9022
9023/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009024 * Create a Vim spell file from one or more word lists.
9025 * "fnames[0]" is the output file name.
9026 * "fnames[fcount - 1]" is the last input file name.
9027 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9028 * and ".spl" is appended to make the output file name.
9029 */
9030 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009031mkspell(
9032 int fcount,
9033 char_u **fnames,
9034 int ascii, /* -ascii argument given */
9035 int over_write, /* overwrite existing output file */
9036 int added_word) /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009037{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009038 char_u *fname = NULL;
9039 char_u *wfname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009040 char_u **innames;
9041 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009042 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009043 int i;
9044 int len;
Bram Moolenaar8767f522016-07-01 17:17:39 +02009045 stat_T st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009046 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009047 spellinfo_T spin;
9048
9049 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009051 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 spin.si_followup = TRUE;
9053 spin.si_rem_accents = TRUE;
9054 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009055 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009056 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9057 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009058 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009059 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009060 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009061 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009062
Bram Moolenaarb765d632005-06-07 21:00:02 +00009063 /* default: fnames[0] is output file, following are input files */
9064 innames = &fnames[1];
9065 incount = fcount - 1;
9066
Bram Moolenaard9462e32011-04-11 21:35:11 +02009067 wfname = alloc(MAXPATHL);
9068 if (wfname == NULL)
9069 return;
9070
Bram Moolenaarb765d632005-06-07 21:00:02 +00009071 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009072 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009073 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009074 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9075 {
9076 /* For ":mkspell path/en.latin1.add" output file is
9077 * "path/en.latin1.add.spl". */
9078 innames = &fnames[0];
9079 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009080 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009081 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009082 else if (fcount == 1)
9083 {
9084 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9085 innames = &fnames[0];
9086 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009087 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009088 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009089 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009090 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9091 {
9092 /* Name ends in ".spl", use as the file name. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009093 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009094 }
9095 else
9096 /* Name should be language, make the file name from it. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009097 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009098 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009099
9100 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009101 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009102 spin.si_ascii = TRUE;
9103
9104 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009105 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009106 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009107 }
9108
Bram Moolenaarb765d632005-06-07 21:00:02 +00009109 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009110 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009111 else if (vim_strchr(gettail(wfname), '_') != NULL)
9112 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009113 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009114 EMSG(_("E754: Only up to 8 regions supported"));
9115 else
9116 {
9117 /* Check for overwriting before doing things that may take a lot of
9118 * time. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009119 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009120 {
9121 EMSG(_(e_exists));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009122 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009123 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009124 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009125 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009126 EMSG2(_(e_isadir2), wfname);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009127 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009128 }
9129
Bram Moolenaard9462e32011-04-11 21:35:11 +02009130 fname = alloc(MAXPATHL);
9131 if (fname == NULL)
9132 goto theend;
9133
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134 /*
9135 * Init the aff and dic pointers.
9136 * Get the region names if there are more than 2 arguments.
9137 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009138 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009139 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009140 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009141
Bram Moolenaar3982c542005-06-08 21:56:31 +00009142 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009144 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009145 if (STRLEN(gettail(innames[i])) < 5
9146 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009147 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009148 EMSG2(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009149 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009150 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009151 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9152 spin.si_region_name[i * 2 + 1] =
9153 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009154 }
9155 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009156 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009157
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009158 spin.si_foldroot = wordtree_alloc(&spin);
9159 spin.si_keeproot = wordtree_alloc(&spin);
9160 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009161 if (spin.si_foldroot == NULL
9162 || spin.si_keeproot == NULL
9163 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009164 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009165 free_blocks(spin.si_blocks);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009166 goto theend;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009167 }
9168
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009169 /* When not producing a .add.spl file clear the character table when
9170 * we encounter one in the .aff file. This means we dump the current
9171 * one in the .spl file if the .aff file doesn't define one. That's
9172 * better than guessing the contents, the table will match a
9173 * previously loaded spell file. */
9174 if (!spin.si_add)
9175 spin.si_clear_chartab = TRUE;
9176
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009177 /*
9178 * Read all the .aff and .dic files.
9179 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009180 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009181 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009182 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009183 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009184 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009185 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009186
Bram Moolenaard9462e32011-04-11 21:35:11 +02009187 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009188 if (mch_stat((char *)fname, &st) >= 0)
9189 {
9190 /* Read the .aff file. Will init "spin->si_conv" based on the
9191 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009192 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009193 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009194 error = TRUE;
9195 else
9196 {
9197 /* Read the .dic file and store the words in the trees. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009198 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009199 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009200 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009201 error = TRUE;
9202 }
9203 }
9204 else
9205 {
9206 /* No .aff file, try reading the file as a word list. Store
9207 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009208 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009209 error = TRUE;
9210 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009211
Bram Moolenaarb765d632005-06-07 21:00:02 +00009212#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009213 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009214 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009215#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009216 }
9217
Bram Moolenaar78622822005-08-23 21:00:13 +00009218 if (spin.si_compflags != NULL && spin.si_nobreak)
9219 MSG(_("Warning: both compounding and NOBREAK specified"));
9220
Bram Moolenaar4770d092006-01-12 23:22:24 +00009221 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009222 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009223 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009224 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009225 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009226 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009227 wordtree_compress(&spin, spin.si_foldroot);
9228 wordtree_compress(&spin, spin.si_keeproot);
9229 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009230 }
9231
Bram Moolenaar4770d092006-01-12 23:22:24 +00009232 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009233 {
9234 /*
9235 * Write the info in the spell file.
9236 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009237 vim_snprintf((char *)IObuff, IOSIZE,
9238 _("Writing spell file %s ..."), wfname);
9239 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009240
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009241 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009242
Bram Moolenaar4770d092006-01-12 23:22:24 +00009243 spell_message(&spin, (char_u *)_("Done!"));
9244 vim_snprintf((char *)IObuff, IOSIZE,
9245 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9246 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009247
Bram Moolenaar4770d092006-01-12 23:22:24 +00009248 /*
9249 * If the file is loaded need to reload it.
9250 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009251 if (!error)
9252 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009253 }
9254
9255 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009257 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 ga_clear(&spin.si_sal);
9259 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009260 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009261 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009262 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009263
9264 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009265 for (i = 0; i < incount; ++i)
9266 if (afile[i] != NULL)
9267 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009268
9269 /* Free all the bits and pieces at once. */
9270 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009271
9272 /*
9273 * If there is soundfolding info and no NOSUGFILE item create the
9274 * .sug file with the soundfolded word trie.
9275 */
9276 if (spin.si_sugtime != 0 && !error && !got_int)
9277 spell_make_sugfile(&spin, wfname);
9278
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009279 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009280
9281theend:
9282 vim_free(fname);
9283 vim_free(wfname);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009284}
9285
Bram Moolenaar4770d092006-01-12 23:22:24 +00009286/*
9287 * Display a message for spell file processing when 'verbose' is set or using
9288 * ":mkspell". "str" can be IObuff.
9289 */
9290 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009291spell_message(spellinfo_T *spin, char_u *str)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009292{
9293 if (spin->si_verbose || p_verbose > 2)
9294 {
9295 if (!spin->si_verbose)
9296 verbose_enter();
9297 MSG(str);
9298 out_flush();
9299 if (!spin->si_verbose)
9300 verbose_leave();
9301 }
9302}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009303
9304/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009305 * ":[count]spellgood {word}"
9306 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009307 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009308 */
9309 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009310ex_spell(exarg_T *eap)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009311{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009312 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009313 eap->forceit ? 0 : (int)eap->line2,
9314 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009315}
9316
9317/*
9318 * Add "word[len]" to 'spellfile' as a good or bad word.
9319 */
9320 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009321spell_add_word(
9322 char_u *word,
9323 int len,
9324 int bad,
9325 int idx, /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009326 'spellfile' */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009327 int undo) /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009328{
Bram Moolenaara3917072006-09-14 08:48:14 +00009329 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009330 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009331 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009332 char_u *fname;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009333 char_u *fnamebuf = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009334 char_u line[MAXWLEN * 2];
9335 long fpos, fpos_next = 0;
9336 int i;
9337 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009338
Bram Moolenaar89d40322006-08-29 15:30:07 +00009339 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009340 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009341 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009342 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02009343 int_wordlist = vim_tempname('s', FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009344 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009345 return;
9346 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009347 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009348 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009349 else
9350 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009351 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009352 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009353 {
9354 init_spellfile();
9355 new_spf = TRUE;
9356 }
9357
Bram Moolenaar860cae12010-06-05 23:22:07 +02009358 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009359 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009360 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009361 return;
9362 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009363 fnamebuf = alloc(MAXPATHL);
9364 if (fnamebuf == NULL)
9365 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009366
Bram Moolenaar860cae12010-06-05 23:22:07 +02009367 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009368 {
9369 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009370 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009371 break;
9372 if (*spf == NUL)
9373 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009374 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009375 vim_free(fnamebuf);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009376 return;
9377 }
9378 }
9379
Bram Moolenaarb765d632005-06-07 21:00:02 +00009380 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009381 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009382 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9383 buf = NULL;
9384 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009385 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009386 EMSG(_(e_bufloaded));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009387 vim_free(fnamebuf);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009388 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009389 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009390
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009391 fname = fnamebuf;
9392 }
9393
Bram Moolenaard0131a82006-03-04 21:46:13 +00009394 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009395 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009396 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009397 * since its flags sort before the one with WF_BANNED. */
9398 fd = mch_fopen((char *)fname, "r");
9399 if (fd != NULL)
9400 {
9401 while (!vim_fgets(line, MAXWLEN * 2, fd))
9402 {
9403 fpos = fpos_next;
9404 fpos_next = ftell(fd);
9405 if (STRNCMP(word, line, len) == 0
9406 && (line[len] == '/' || line[len] < ' '))
9407 {
9408 /* Found duplicate word. Remove it by writing a '#' at
9409 * the start of the line. Mixing reading and writing
9410 * doesn't work for all systems, close the file first. */
9411 fclose(fd);
9412 fd = mch_fopen((char *)fname, "r+");
9413 if (fd == NULL)
9414 break;
9415 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009416 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009417 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009418 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009419 {
9420 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009421 smsg((char_u *)_("Word '%.*s' removed from %s"),
9422 len, word, NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009423 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009424 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009425 fseek(fd, fpos_next, SEEK_SET);
9426 }
9427 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009428 if (fd != NULL)
9429 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009430 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009431 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009432
9433 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009434 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009435 fd = mch_fopen((char *)fname, "a");
9436 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009437 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009438 char_u *p;
9439
Bram Moolenaard0131a82006-03-04 21:46:13 +00009440 /* We just initialized the 'spellfile' option and can't open the
9441 * file. We may need to create the "spell" directory first. We
9442 * already checked the runtime directory is writable in
9443 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009444 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009445 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009446 int c = *p;
9447
Bram Moolenaard0131a82006-03-04 21:46:13 +00009448 /* The directory doesn't exist. Try creating it and opening
9449 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009450 *p = NUL;
9451 vim_mkdir(fname, 0755);
9452 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009453 fd = mch_fopen((char *)fname, "a");
9454 }
9455 }
9456
9457 if (fd == NULL)
9458 EMSG2(_(e_notopen), fname);
9459 else
9460 {
9461 if (bad)
9462 fprintf(fd, "%.*s/!\n", len, word);
9463 else
9464 fprintf(fd, "%.*s\n", len, word);
9465 fclose(fd);
9466
9467 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009468 smsg((char_u *)_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009469 }
9470 }
9471
Bram Moolenaard0131a82006-03-04 21:46:13 +00009472 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009473 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009474 /* Update the .add.spl file. */
9475 mkspell(1, &fname, FALSE, TRUE, TRUE);
9476
9477 /* If the .add file is edited somewhere, reload it. */
9478 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009479 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009480
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009481 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009482 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009483 vim_free(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009484}
9485
9486/*
9487 * Initialize 'spellfile' for the current buffer.
9488 */
9489 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009490init_spellfile(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009491{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009492 char_u *buf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009493 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009494 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009495 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009496 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009497 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009498 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009499
Bram Moolenaar860cae12010-06-05 23:22:07 +02009500 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009501 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009502 buf = alloc(MAXPATHL);
9503 if (buf == NULL)
9504 return;
9505
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009506 /* Find the end of the language name. Exclude the region. If there
9507 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009508 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009509 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009510 if (vim_ispathsep(*lend))
9511 {
9512 aspath = TRUE;
9513 lstart = lend + 1;
9514 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009515
9516 /* Loop over all entries in 'runtimepath'. Use the first one where we
9517 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009518 rtp = p_rtp;
9519 while (*rtp != NUL)
9520 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009521 if (aspath)
9522 /* Use directory of an entry with path, e.g., for
9523 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009524 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9525 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009526 else
9527 /* Copy the path from 'runtimepath' to buf[]. */
9528 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009529 if (filewritable(buf) == 2)
9530 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009531 /* Use the first language name from 'spelllang' and the
9532 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009533 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009534 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9535 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009536 else
9537 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009538 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009539 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009540 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009541 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009542 vim_mkdir(buf, 0755);
9543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009544 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009545 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009546 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009547 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009548 l = (int)STRLEN(buf);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009549 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9550 ->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009551 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9552 fname != NULL
9553 && strstr((char *)gettail(fname), ".ascii.") != NULL
9554 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009555 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9556 break;
9557 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009558 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009559 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009560
9561 vim_free(buf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009562 }
9563}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009564
Bram Moolenaar51485f02005-06-04 21:55:20 +00009565
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009566/*
9567 * Init the chartab used for spelling for ASCII.
9568 * EBCDIC is not supported!
9569 */
9570 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009571clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009572{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009573 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009574
9575 /* Init everything to FALSE. */
9576 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9577 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9578 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009579 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009580 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009581 sp->st_upper[i] = i;
9582 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009583
9584 /* We include digits. A word shouldn't start with a digit, but handling
9585 * that is done separately. */
9586 for (i = '0'; i <= '9'; ++i)
9587 sp->st_isw[i] = TRUE;
9588 for (i = 'A'; i <= 'Z'; ++i)
9589 {
9590 sp->st_isw[i] = TRUE;
9591 sp->st_isu[i] = TRUE;
9592 sp->st_fold[i] = i + 0x20;
9593 }
9594 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009595 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009596 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009597 sp->st_upper[i] = i - 0x20;
9598 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009599}
9600
9601/*
9602 * Init the chartab used for spelling. Only depends on 'encoding'.
9603 * Called once while starting up and when 'encoding' changes.
9604 * The default is to use isalpha(), but the spell file should define the word
9605 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009606 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009607 */
9608 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009609init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009610{
9611 int i;
9612
9613 did_set_spelltab = FALSE;
9614 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009615#ifdef FEAT_MBYTE
9616 if (enc_dbcs)
9617 {
9618 /* DBCS: assume double-wide characters are word characters. */
9619 for (i = 128; i <= 255; ++i)
9620 if (MB_BYTE2LEN(i) == 2)
9621 spelltab.st_isw[i] = TRUE;
9622 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009623 else if (enc_utf8)
9624 {
9625 for (i = 128; i < 256; ++i)
9626 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009627 int f = utf_fold(i);
9628 int u = utf_toupper(i);
9629
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009630 spelltab.st_isu[i] = utf_isupper(i);
9631 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009632 /* The folded/upper-cased value is different between latin1 and
9633 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9634 * value for utf-8 to avoid this. */
9635 spelltab.st_fold[i] = (f < 256) ? f : i;
9636 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009637 }
9638 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009639 else
9640#endif
9641 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009642 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009643 for (i = 128; i < 256; ++i)
9644 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009645 if (MB_ISUPPER(i))
9646 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009647 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009648 spelltab.st_isu[i] = TRUE;
9649 spelltab.st_fold[i] = MB_TOLOWER(i);
9650 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009651 else if (MB_ISLOWER(i))
9652 {
9653 spelltab.st_isw[i] = TRUE;
9654 spelltab.st_upper[i] = MB_TOUPPER(i);
9655 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009656 }
9657 }
9658}
9659
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660/*
9661 * Set the spell character tables from strings in the affix file.
9662 */
9663 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009664set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009665{
9666 /* We build the new tables here first, so that we can compare with the
9667 * previous one. */
9668 spelltab_T new_st;
9669 char_u *pf = fol, *pl = low, *pu = upp;
9670 int f, l, u;
9671
9672 clear_spell_chartab(&new_st);
9673
9674 while (*pf != NUL)
9675 {
9676 if (*pl == NUL || *pu == NUL)
9677 {
9678 EMSG(_(e_affform));
9679 return FAIL;
9680 }
9681#ifdef FEAT_MBYTE
9682 f = mb_ptr2char_adv(&pf);
9683 l = mb_ptr2char_adv(&pl);
9684 u = mb_ptr2char_adv(&pu);
9685#else
9686 f = *pf++;
9687 l = *pl++;
9688 u = *pu++;
9689#endif
9690 /* Every character that appears is a word character. */
9691 if (f < 256)
9692 new_st.st_isw[f] = TRUE;
9693 if (l < 256)
9694 new_st.st_isw[l] = TRUE;
9695 if (u < 256)
9696 new_st.st_isw[u] = TRUE;
9697
9698 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9699 * case-folding */
9700 if (l < 256 && l != f)
9701 {
9702 if (f >= 256)
9703 {
9704 EMSG(_(e_affrange));
9705 return FAIL;
9706 }
9707 new_st.st_fold[l] = f;
9708 }
9709
9710 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009711 * case-folding, it's upper case and the "UPP" is the upper case of
9712 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009713 if (u < 256 && u != f)
9714 {
9715 if (f >= 256)
9716 {
9717 EMSG(_(e_affrange));
9718 return FAIL;
9719 }
9720 new_st.st_fold[u] = f;
9721 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009722 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009723 }
9724 }
9725
9726 if (*pl != NUL || *pu != NUL)
9727 {
9728 EMSG(_(e_affform));
9729 return FAIL;
9730 }
9731
9732 return set_spell_finish(&new_st);
9733}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009734
9735/*
9736 * Set the spell character tables from strings in the .spl file.
9737 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009738 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009739set_spell_charflags(
9740 char_u *flags,
9741 int cnt, /* length of "flags" */
9742 char_u *fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009743{
9744 /* We build the new tables here first, so that we can compare with the
9745 * previous one. */
9746 spelltab_T new_st;
9747 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009748 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009749 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009750
9751 clear_spell_chartab(&new_st);
9752
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009753 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009754 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009755 if (i < cnt)
9756 {
9757 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9758 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9759 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009760
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009761 if (*p != NUL)
9762 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009763#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009764 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009765#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009766 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009767#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009768 new_st.st_fold[i + 128] = c;
9769 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9770 new_st.st_upper[c] = i + 128;
9771 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009772 }
9773
Bram Moolenaar5195e452005-08-19 20:32:47 +00009774 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009775}
9776
9777 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009778set_spell_finish(spelltab_T *new_st)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009779{
9780 int i;
9781
9782 if (did_set_spelltab)
9783 {
9784 /* check that it's the same table */
9785 for (i = 0; i < 256; ++i)
9786 {
9787 if (spelltab.st_isw[i] != new_st->st_isw[i]
9788 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009789 || spelltab.st_fold[i] != new_st->st_fold[i]
9790 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009791 {
9792 EMSG(_("E763: Word characters differ between spell files"));
9793 return FAIL;
9794 }
9795 }
9796 }
9797 else
9798 {
9799 /* copy the new spelltab into the one being used */
9800 spelltab = *new_st;
9801 did_set_spelltab = TRUE;
9802 }
9803
9804 return OK;
9805}
9806
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009807/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009808 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009809 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009810 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009811 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009812 */
9813 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009814spell_iswordp(
9815 char_u *p,
9816 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009817{
Bram Moolenaarea408852005-06-25 22:49:46 +00009818#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009819 char_u *s;
9820 int l;
9821 int c;
9822
9823 if (has_mbyte)
9824 {
9825 l = MB_BYTE2LEN(*p);
9826 s = p;
9827 if (l == 1)
9828 {
9829 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009830 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009831 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009832 }
9833 else
9834 {
9835 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009836 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9837 : (wp->w_s->b_spell_ismw_mb != NULL
9838 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009839 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009840 }
9841
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009842 c = mb_ptr2char(s);
9843 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009844 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009845 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009846 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009847#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009848
Bram Moolenaar860cae12010-06-05 23:22:07 +02009849 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009850}
9851
9852/*
9853 * Return TRUE if "p" points to a word character.
9854 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9855 */
9856 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009857spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009858{
9859#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009860 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009861
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009862 if (has_mbyte)
9863 {
9864 c = mb_ptr2char(p);
9865 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009866 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009867 return spelltab.st_isw[c];
9868 }
9869#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009870 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009871}
9872
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009873#ifdef FEAT_MBYTE
9874/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009875 * Return TRUE if word class indicates a word character.
9876 * Only for characters above 255.
9877 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009878 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009879 */
9880 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009881spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009882{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009883 if (wp->w_s->b_cjk)
9884 /* East Asian characters are not considered word characters. */
9885 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009886 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9887}
9888
9889/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009890 * Return TRUE if "p" points to a word character.
9891 * Wide version of spell_iswordp().
9892 */
9893 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009894spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009895{
9896 int *s;
9897
Bram Moolenaar860cae12010-06-05 23:22:07 +02009898 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9899 : (wp->w_s->b_spell_ismw_mb != NULL
9900 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009901 s = p + 1;
9902 else
9903 s = p;
9904
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009905 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009906 {
9907 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009908 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009909 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009910 return spell_mb_isword_class(
9911 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009912 return 0;
9913 }
9914 return spelltab.st_isw[*s];
9915}
9916#endif
9917
Bram Moolenaarea408852005-06-25 22:49:46 +00009918/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009919 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009920 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009921 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009922 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009923write_spell_prefcond(FILE *fd, garray_T *gap)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009924{
9925 int i;
9926 char_u *p;
9927 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009928 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009929 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009930
Bram Moolenaar5195e452005-08-19 20:32:47 +00009931 if (fd != NULL)
9932 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9933
9934 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009935
9936 for (i = 0; i < gap->ga_len; ++i)
9937 {
9938 /* <prefcond> : <condlen> <condstr> */
9939 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009940 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009941 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009942 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009943 if (fd != NULL)
9944 {
9945 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009946 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009947 }
9948 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009949 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009950 else if (fd != NULL)
9951 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009952 }
9953
Bram Moolenaar5195e452005-08-19 20:32:47 +00009954 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009955}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009956
9957/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009958 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9959 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009960 * When using a multi-byte 'encoding' the length may change!
9961 * Returns FAIL when something wrong.
9962 */
9963 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009964spell_casefold(
9965 char_u *str,
9966 int len,
9967 char_u *buf,
9968 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009969{
9970 int i;
9971
9972 if (len >= buflen)
9973 {
9974 buf[0] = NUL;
9975 return FAIL; /* result will not fit */
9976 }
9977
9978#ifdef FEAT_MBYTE
9979 if (has_mbyte)
9980 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009981 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009982 char_u *p;
9983 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009984
9985 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009986 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009987 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009988 if (outi + MB_MAXBYTES > buflen)
9989 {
9990 buf[outi] = NUL;
9991 return FAIL;
9992 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009993 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009994 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009995 }
9996 buf[outi] = NUL;
9997 }
9998 else
9999#endif
10000 {
10001 /* Be quick for non-multibyte encodings. */
10002 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010003 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010004 buf[i] = NUL;
10005 }
10006
10007 return OK;
10008}
10009
Bram Moolenaar4770d092006-01-12 23:22:24 +000010010/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010011#define SPS_BEST 1
10012#define SPS_FAST 2
10013#define SPS_DOUBLE 4
10014
Bram Moolenaar4770d092006-01-12 23:22:24 +000010015static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10016static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010017
10018/*
10019 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010020 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010021 */
10022 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010023spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010024{
10025 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010026 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010027 char_u buf[MAXPATHL];
10028 int f;
10029
10030 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010031 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010032
10033 for (p = p_sps; *p != NUL; )
10034 {
10035 copy_option_part(&p, buf, MAXPATHL, ",");
10036
10037 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010038 if (VIM_ISDIGIT(*buf))
10039 {
10040 s = buf;
10041 sps_limit = getdigits(&s);
10042 if (*s != NUL && !VIM_ISDIGIT(*s))
10043 f = -1;
10044 }
10045 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010046 f = SPS_BEST;
10047 else if (STRCMP(buf, "fast") == 0)
10048 f = SPS_FAST;
10049 else if (STRCMP(buf, "double") == 0)
10050 f = SPS_DOUBLE;
10051 else if (STRNCMP(buf, "expr:", 5) != 0
10052 && STRNCMP(buf, "file:", 5) != 0)
10053 f = -1;
10054
10055 if (f == -1 || (sps_flags != 0 && f != 0))
10056 {
10057 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010058 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010059 return FAIL;
10060 }
10061 if (f != 0)
10062 sps_flags = f;
10063 }
10064
10065 if (sps_flags == 0)
10066 sps_flags = SPS_BEST;
10067
10068 return OK;
10069}
10070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010071/*
Bram Moolenaar134bf072013-09-25 18:54:24 +020010072 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010073 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010074 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010075 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010076 */
10077 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010078spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010079{
10080 char_u *line;
10081 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 char_u wcopy[MAXWLEN + 2];
10083 char_u *p;
10084 int i;
10085 int c;
10086 suginfo_T sug;
10087 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010088 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010089 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010090 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010091 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010092 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010093 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010095 if (no_spell_checking(curwin))
10096 return;
10097
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010098 if (VIsual_active)
10099 {
10100 /* Use the Visually selected text as the bad word. But reject
10101 * a multi-line selection. */
10102 if (curwin->w_cursor.lnum != VIsual.lnum)
10103 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010104 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010105 return;
10106 }
10107 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10108 if (badlen < 0)
10109 badlen = -badlen;
10110 else
10111 curwin->w_cursor.col = VIsual.col;
10112 ++badlen;
10113 end_visual_mode();
10114 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010115 /* Find the start of the badly spelled word. */
10116 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010117 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010119 /* No bad word or it starts after the cursor: use the word under the
10120 * cursor. */
10121 curwin->w_cursor = prev_cursor;
10122 line = ml_get_curline();
10123 p = line + curwin->w_cursor.col;
10124 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010125 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010126 mb_ptr_back(line, p);
10127 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010128 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010129 mb_ptr_adv(p);
10130
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010131 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010132 {
10133 beep_flush();
10134 return;
10135 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010136 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 }
10138
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010139 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010141 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010142 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010143
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010144 /* Make a copy of current line since autocommands may free the line. */
10145 line = vim_strsave(ml_get_curline());
10146 if (line == NULL)
10147 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010148
Bram Moolenaar5195e452005-08-19 20:32:47 +000010149 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10150 * 'spellsuggest', whatever is smaller. */
10151 if (sps_limit > (int)Rows - 2)
10152 limit = (int)Rows - 2;
10153 else
10154 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010155 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010156 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157
10158 if (sug.su_ga.ga_len == 0)
10159 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010160 else if (count > 0)
10161 {
10162 if (count > sug.su_ga.ga_len)
10163 smsg((char_u *)_("Sorry, only %ld suggestions"),
10164 (long)sug.su_ga.ga_len);
10165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 else
10167 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010168 vim_free(repl_from);
10169 repl_from = NULL;
10170 vim_free(repl_to);
10171 repl_to = NULL;
10172
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010173#ifdef FEAT_RIGHTLEFT
10174 /* When 'rightleft' is set the list is drawn right-left. */
10175 cmdmsg_rl = curwin->w_p_rl;
10176 if (cmdmsg_rl)
10177 msg_col = Columns - 1;
10178#endif
10179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010180 /* List the suggestions. */
10181 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010182 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010183 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10185 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010186#ifdef FEAT_RIGHTLEFT
10187 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10188 {
10189 /* And now the rabbit from the high hat: Avoid showing the
10190 * untranslated message rightleft. */
10191 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10192 sug.su_badlen, sug.su_badptr);
10193 }
10194#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 msg_puts(IObuff);
10196 msg_clr_eos();
10197 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 msg_scroll = TRUE;
10200 for (i = 0; i < sug.su_ga.ga_len; ++i)
10201 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010202 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203
10204 /* The suggested word may replace only part of the bad word, add
10205 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020010206 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010208 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010209 sug.su_badptr + stp->st_orglen,
10210 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010211 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10212#ifdef FEAT_RIGHTLEFT
10213 if (cmdmsg_rl)
10214 rl_mirror(IObuff);
10215#endif
10216 msg_puts(IObuff);
10217
10218 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010219 msg_puts(IObuff);
10220
10221 /* The word may replace more than "su_badlen". */
10222 if (sug.su_badlen < stp->st_orglen)
10223 {
10224 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10225 stp->st_orglen, sug.su_badptr);
10226 msg_puts(IObuff);
10227 }
10228
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010229 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010230 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010231 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010232 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010233 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010234 stp->st_salscore ? "s " : "",
10235 stp->st_score, stp->st_altscore);
10236 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010237 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010238 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010239#ifdef FEAT_RIGHTLEFT
10240 if (cmdmsg_rl)
10241 /* Mirror the numbers, but keep the leading space. */
10242 rl_mirror(IObuff + 1);
10243#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010244 msg_advance(30);
10245 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 msg_putchar('\n');
10248 }
10249
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010250#ifdef FEAT_RIGHTLEFT
10251 cmdmsg_rl = FALSE;
10252 msg_col = 0;
10253#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010254 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010255 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010256 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010257 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010258 lines_left = Rows; /* avoid more prompt */
10259 /* don't delay for 'smd' in normal_cmd() */
10260 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 }
10262
Bram Moolenaard12a1322005-08-21 22:08:24 +000010263 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10264 {
10265 /* Save the from and to text for :spellrepall. */
10266 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010267 if (sug.su_badlen > stp->st_orglen)
10268 {
10269 /* Replacing less than "su_badlen", append the remainder to
10270 * repl_to. */
10271 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10272 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10273 sug.su_badlen - stp->st_orglen,
10274 sug.su_badptr + stp->st_orglen);
10275 repl_to = vim_strsave(IObuff);
10276 }
10277 else
10278 {
10279 /* Replacing su_badlen or more, use the whole word. */
10280 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10281 repl_to = vim_strsave(stp->st_word);
10282 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010283
10284 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010285 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10286 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010287 if (p != NULL)
10288 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010289 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010290 mch_memmove(p, line, c);
10291 STRCPY(p + c, stp->st_word);
10292 STRCAT(p, sug.su_badptr + stp->st_orglen);
10293 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10294 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010295
10296 /* For redo we use a change-word command. */
10297 ResetRedobuff();
10298 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010299 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010300 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010301 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010302
10303 /* After this "p" may be invalid. */
10304 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010305 }
10306 }
10307 else
10308 curwin->w_cursor = prev_cursor;
10309
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010310 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010311skip:
10312 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010313}
10314
10315/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010316 * Check if the word at line "lnum" column "col" is required to start with a
10317 * capital. This uses 'spellcapcheck' of the current buffer.
10318 */
10319 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010320check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010321{
10322 int need_cap = FALSE;
10323 char_u *line;
10324 char_u *line_copy = NULL;
10325 char_u *p;
10326 colnr_T endcol;
10327 regmatch_T regmatch;
10328
Bram Moolenaar860cae12010-06-05 23:22:07 +020010329 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010330 return FALSE;
10331
10332 line = ml_get_curline();
10333 endcol = 0;
10334 if ((int)(skipwhite(line) - line) >= (int)col)
10335 {
10336 /* At start of line, check if previous line is empty or sentence
10337 * ends there. */
10338 if (lnum == 1)
10339 need_cap = TRUE;
10340 else
10341 {
10342 line = ml_get(lnum - 1);
10343 if (*skipwhite(line) == NUL)
10344 need_cap = TRUE;
10345 else
10346 {
10347 /* Append a space in place of the line break. */
10348 line_copy = concat_str(line, (char_u *)" ");
10349 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010350 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010351 }
10352 }
10353 }
10354 else
10355 endcol = col;
10356
10357 if (endcol > 0)
10358 {
10359 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010360 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010361 regmatch.rm_ic = FALSE;
10362 p = line + endcol;
10363 for (;;)
10364 {
10365 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010366 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010367 break;
10368 if (vim_regexec(&regmatch, p, 0)
10369 && regmatch.endp[0] == line + endcol)
10370 {
10371 need_cap = TRUE;
10372 break;
10373 }
10374 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +010010375 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010376 }
10377
10378 vim_free(line_copy);
10379
10380 return need_cap;
10381}
10382
10383
10384/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010385 * ":spellrepall"
10386 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010387 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010388ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010389{
10390 pos_T pos = curwin->w_cursor;
10391 char_u *frompat;
10392 int addlen;
10393 char_u *line;
10394 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010395 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010396 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010397
10398 if (repl_from == NULL || repl_to == NULL)
10399 {
10400 EMSG(_("E752: No previous spell replacement"));
10401 return;
10402 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010403 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010405 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010406 if (frompat == NULL)
10407 return;
10408 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10409 p_ws = FALSE;
10410
Bram Moolenaar5195e452005-08-19 20:32:47 +000010411 sub_nsubs = 0;
10412 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010413 curwin->w_cursor.lnum = 0;
10414 while (!got_int)
10415 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010416 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010417 || u_save_cursor() == FAIL)
10418 break;
10419
10420 /* Only replace when the right word isn't there yet. This happens
10421 * when changing "etc" to "etc.". */
10422 line = ml_get_curline();
10423 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10424 repl_to, STRLEN(repl_to)) != 0)
10425 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010426 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010427 if (p == NULL)
10428 break;
10429 mch_memmove(p, line, curwin->w_cursor.col);
10430 STRCPY(p + curwin->w_cursor.col, repl_to);
10431 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10432 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10433 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010434
10435 if (curwin->w_cursor.lnum != prev_lnum)
10436 {
10437 ++sub_nlines;
10438 prev_lnum = curwin->w_cursor.lnum;
10439 }
10440 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010441 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010442 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010443 }
10444
10445 p_ws = save_ws;
10446 curwin->w_cursor = pos;
10447 vim_free(frompat);
10448
Bram Moolenaar5195e452005-08-19 20:32:47 +000010449 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010450 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010451 else
10452 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010453}
10454
10455/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010456 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10457 * a list of allocated strings.
10458 */
10459 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010460spell_suggest_list(
10461 garray_T *gap,
10462 char_u *word,
10463 int maxcount, /* maximum nr of suggestions */
10464 int need_cap, /* 'spellcapcheck' matched */
10465 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010466{
10467 suginfo_T sug;
10468 int i;
10469 suggest_T *stp;
10470 char_u *wcopy;
10471
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010472 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010473
10474 /* Make room in "gap". */
10475 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010476 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010477 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010478 for (i = 0; i < sug.su_ga.ga_len; ++i)
10479 {
10480 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010481
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010482 /* The suggested word may replace only part of "word", add the not
10483 * replaced part. */
10484 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010485 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010486 if (wcopy == NULL)
10487 break;
10488 STRCPY(wcopy, stp->st_word);
10489 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10490 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10491 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010492 }
10493
10494 spell_find_cleanup(&sug);
10495}
10496
10497/*
10498 * Find spell suggestions for the word at the start of "badptr".
10499 * Return the suggestions in "su->su_ga".
10500 * The maximum number of suggestions is "maxcount".
10501 * Note: does use info for the current window.
10502 * This is based on the mechanisms of Aspell, but completely reimplemented.
10503 */
10504 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010505spell_find_suggest(
10506 char_u *badptr,
10507 int badlen, /* length of bad word or 0 if unknown */
10508 suginfo_T *su,
10509 int maxcount,
10510 int banbadword, /* don't include badword in suggestions */
10511 int need_cap, /* word should start with capital */
10512 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010513{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010514 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010515 char_u buf[MAXPATHL];
10516 char_u *p;
10517 int do_combine = FALSE;
10518 char_u *sps_copy;
10519#ifdef FEAT_EVAL
10520 static int expr_busy = FALSE;
10521#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010522 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010523 int i;
10524 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010525
10526 /*
10527 * Set the info in "*su".
10528 */
10529 vim_memset(su, 0, sizeof(suginfo_T));
10530 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10531 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010532 if (*badptr == NUL)
10533 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010534 hash_init(&su->su_banned);
10535
10536 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010537 if (badlen != 0)
10538 su->su_badlen = badlen;
10539 else
10540 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010541 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010542 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010543
10544 if (su->su_badlen >= MAXWLEN)
10545 su->su_badlen = MAXWLEN - 1; /* just in case */
10546 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10547 (void)spell_casefold(su->su_badptr, su->su_badlen,
10548 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010549 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010550 su->su_badflags = badword_captype(su->su_badptr,
10551 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010552 if (need_cap)
10553 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010554
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010555 /* Find the default language for sound folding. We simply use the first
10556 * one in 'spelllang' that supports sound folding. That's good for when
10557 * using multiple files for one language, it's not that bad when mixing
10558 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010559 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010560 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010561 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010562 if (lp->lp_sallang != NULL)
10563 {
10564 su->su_sallang = lp->lp_sallang;
10565 break;
10566 }
10567 }
10568
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010569 /* Soundfold the bad word with the default sound folding, so that we don't
10570 * have to do this many times. */
10571 if (su->su_sallang != NULL)
10572 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10573 su->su_sal_badword);
10574
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010575 /* If the word is not capitalised and spell_check() doesn't consider the
10576 * word to be bad then it might need to be capitalised. Add a suggestion
10577 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010578 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010579 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010580 {
10581 make_case_word(su->su_badword, buf, WF_ONECAP);
10582 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010583 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010584 }
10585
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010586 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010587 if (banbadword)
10588 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010589
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010590 /* Make a copy of 'spellsuggest', because the expression may change it. */
10591 sps_copy = vim_strsave(p_sps);
10592 if (sps_copy == NULL)
10593 return;
10594
10595 /* Loop over the items in 'spellsuggest'. */
10596 for (p = sps_copy; *p != NUL; )
10597 {
10598 copy_option_part(&p, buf, MAXPATHL, ",");
10599
10600 if (STRNCMP(buf, "expr:", 5) == 0)
10601 {
10602#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010603 /* Evaluate an expression. Skip this when called recursively,
10604 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010605 if (!expr_busy)
10606 {
10607 expr_busy = TRUE;
10608 spell_suggest_expr(su, buf + 5);
10609 expr_busy = FALSE;
10610 }
10611#endif
10612 }
10613 else if (STRNCMP(buf, "file:", 5) == 0)
10614 /* Use list of suggestions in a file. */
10615 spell_suggest_file(su, buf + 5);
10616 else
10617 {
10618 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010619 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010620 if (sps_flags & SPS_DOUBLE)
10621 do_combine = TRUE;
10622 }
10623 }
10624
10625 vim_free(sps_copy);
10626
10627 if (do_combine)
10628 /* Combine the two list of suggestions. This must be done last,
10629 * because sorting changes the order again. */
10630 score_combine(su);
10631}
10632
10633#ifdef FEAT_EVAL
10634/*
10635 * Find suggestions by evaluating expression "expr".
10636 */
10637 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010638spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010639{
10640 list_T *list;
10641 listitem_T *li;
10642 int score;
10643 char_u *p;
10644
10645 /* The work is split up in a few parts to avoid having to export
10646 * suginfo_T.
10647 * First evaluate the expression and get the resulting list. */
10648 list = eval_spell_expr(su->su_badword, expr);
10649 if (list != NULL)
10650 {
10651 /* Loop over the items in the list. */
10652 for (li = list->lv_first; li != NULL; li = li->li_next)
10653 if (li->li_tv.v_type == VAR_LIST)
10654 {
10655 /* Get the word and the score from the items. */
10656 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010657 if (score >= 0 && score <= su->su_maxscore)
10658 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10659 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010660 }
10661 list_unref(list);
10662 }
10663
Bram Moolenaar4770d092006-01-12 23:22:24 +000010664 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10665 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010666 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10667}
10668#endif
10669
10670/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010671 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010672 */
10673 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010674spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010675{
10676 FILE *fd;
10677 char_u line[MAXWLEN * 2];
10678 char_u *p;
10679 int len;
10680 char_u cword[MAXWLEN];
10681
10682 /* Open the file. */
10683 fd = mch_fopen((char *)fname, "r");
10684 if (fd == NULL)
10685 {
10686 EMSG2(_(e_notopen), fname);
10687 return;
10688 }
10689
10690 /* Read it line by line. */
10691 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10692 {
10693 line_breakcheck();
10694
10695 p = vim_strchr(line, '/');
10696 if (p == NULL)
10697 continue; /* No Tab found, just skip the line. */
10698 *p++ = NUL;
10699 if (STRICMP(su->su_badword, line) == 0)
10700 {
10701 /* Match! Isolate the good word, until CR or NL. */
10702 for (len = 0; p[len] >= ' '; ++len)
10703 ;
10704 p[len] = NUL;
10705
10706 /* If the suggestion doesn't have specific case duplicate the case
10707 * of the bad word. */
10708 if (captype(p, NULL) == 0)
10709 {
10710 make_case_word(p, cword, su->su_badflags);
10711 p = cword;
10712 }
10713
10714 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010715 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010716 }
10717 }
10718
10719 fclose(fd);
10720
Bram Moolenaar4770d092006-01-12 23:22:24 +000010721 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10722 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010723 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10724}
10725
10726/*
10727 * Find suggestions for the internal method indicated by "sps_flags".
10728 */
10729 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010730spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010731{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010732 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010733 * Load the .sug file(s) that are available and not done yet.
10734 */
10735 suggest_load_files();
10736
10737 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010738 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010739 *
10740 * Set a maximum score to limit the combination of operations that is
10741 * tried.
10742 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010743 suggest_try_special(su);
10744
10745 /*
10746 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10747 * from the .aff file and inserting a space (split the word).
10748 */
10749 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010750
10751 /* For the resulting top-scorers compute the sound-a-like score. */
10752 if (sps_flags & SPS_DOUBLE)
10753 score_comp_sal(su);
10754
10755 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010756 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010757 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010758 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010759 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010760 if (sps_flags & SPS_BEST)
10761 /* Adjust the word score for the suggestions found so far for how
10762 * they sounds like. */
10763 rescore_suggestions(su);
10764
10765 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010766 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010767 * for the soundfold word, limits the changes that are being tried,
10768 * and "su_sfmaxscore" the rescored score, which is set by
10769 * cleanup_suggestions().
10770 * First find words with a small edit distance, because this is much
10771 * faster and often already finds the top-N suggestions. If we didn't
10772 * find many suggestions try again with a higher edit distance.
10773 * "sl_sounddone" is used to avoid doing the same word twice.
10774 */
10775 suggest_try_soundalike_prep();
10776 su->su_maxscore = SCORE_SFMAX1;
10777 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010778 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010779 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10780 {
10781 /* We didn't find enough matches, try again, allowing more
10782 * changes to the soundfold word. */
10783 su->su_maxscore = SCORE_SFMAX2;
10784 suggest_try_soundalike(su);
10785 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10786 {
10787 /* Still didn't find enough matches, try again, allowing even
10788 * more changes to the soundfold word. */
10789 su->su_maxscore = SCORE_SFMAX3;
10790 suggest_try_soundalike(su);
10791 }
10792 }
10793 su->su_maxscore = su->su_sfmaxscore;
10794 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010795 }
10796
Bram Moolenaar4770d092006-01-12 23:22:24 +000010797 /* When CTRL-C was hit while searching do show the results. Only clear
10798 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010799 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010800 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010801 {
10802 (void)vgetc();
10803 got_int = FALSE;
10804 }
10805
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010806 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010807 {
10808 if (sps_flags & SPS_BEST)
10809 /* Adjust the word score for how it sounds like. */
10810 rescore_suggestions(su);
10811
Bram Moolenaar4770d092006-01-12 23:22:24 +000010812 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10813 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010814 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010815 }
10816}
10817
10818/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010819 * Load the .sug files for languages that have one and weren't loaded yet.
10820 */
10821 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010822suggest_load_files(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010823{
10824 langp_T *lp;
10825 int lpi;
10826 slang_T *slang;
10827 char_u *dotp;
10828 FILE *fd;
10829 char_u buf[MAXWLEN];
10830 int i;
10831 time_t timestamp;
10832 int wcount;
10833 int wordnr;
10834 garray_T ga;
10835 int c;
10836
10837 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010838 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010839 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010840 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010841 slang = lp->lp_slang;
10842 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10843 {
10844 /* Change ".spl" to ".sug" and open the file. When the file isn't
10845 * found silently skip it. Do set "sl_sugloaded" so that we
10846 * don't try again and again. */
10847 slang->sl_sugloaded = TRUE;
10848
10849 dotp = vim_strrchr(slang->sl_fname, '.');
10850 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10851 continue;
10852 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010853 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010854 if (fd == NULL)
10855 goto nextone;
10856
10857 /*
10858 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10859 */
10860 for (i = 0; i < VIMSUGMAGICL; ++i)
10861 buf[i] = getc(fd); /* <fileID> */
10862 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10863 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010864 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010865 slang->sl_fname);
10866 goto nextone;
10867 }
10868 c = getc(fd); /* <versionnr> */
10869 if (c < VIMSUGVERSION)
10870 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010871 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010872 slang->sl_fname);
10873 goto nextone;
10874 }
10875 else if (c > VIMSUGVERSION)
10876 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010877 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010878 slang->sl_fname);
10879 goto nextone;
10880 }
10881
10882 /* Check the timestamp, it must be exactly the same as the one in
10883 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010884 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010885 if (timestamp != slang->sl_sugtime)
10886 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010887 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010888 slang->sl_fname);
10889 goto nextone;
10890 }
10891
10892 /*
10893 * <SUGWORDTREE>: <wordtree>
10894 * Read the trie with the soundfolded words.
10895 */
10896 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10897 FALSE, 0) != 0)
10898 {
10899someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010900 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010901 slang->sl_fname);
10902 slang_clear_sug(slang);
10903 goto nextone;
10904 }
10905
10906 /*
10907 * <SUGTABLE>: <sugwcount> <sugline> ...
10908 *
10909 * Read the table with word numbers. We use a file buffer for
10910 * this, because it's so much like a file with lines. Makes it
10911 * possible to swap the info and save on memory use.
10912 */
10913 slang->sl_sugbuf = open_spellbuf();
10914 if (slang->sl_sugbuf == NULL)
10915 goto someerror;
10916 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010917 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010918 if (wcount < 0)
10919 goto someerror;
10920
10921 /* Read all the wordnr lists into the buffer, one NUL terminated
10922 * list per line. */
10923 ga_init2(&ga, 1, 100);
10924 for (wordnr = 0; wordnr < wcount; ++wordnr)
10925 {
10926 ga.ga_len = 0;
10927 for (;;)
10928 {
10929 c = getc(fd); /* <sugline> */
10930 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10931 goto someerror;
10932 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10933 if (c == NUL)
10934 break;
10935 }
10936 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10937 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10938 goto someerror;
10939 }
10940 ga_clear(&ga);
10941
10942 /*
10943 * Need to put word counts in the word tries, so that we can find
10944 * a word by its number.
10945 */
10946 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10947 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10948
10949nextone:
10950 if (fd != NULL)
10951 fclose(fd);
10952 STRCPY(dotp, ".spl");
10953 }
10954 }
10955}
10956
10957
10958/*
10959 * Fill in the wordcount fields for a trie.
10960 * Returns the total number of words.
10961 */
10962 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010963tree_count_words(char_u *byts, idx_T *idxs)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010964{
10965 int depth;
10966 idx_T arridx[MAXWLEN];
10967 int curi[MAXWLEN];
10968 int c;
10969 idx_T n;
10970 int wordcount[MAXWLEN];
10971
10972 arridx[0] = 0;
10973 curi[0] = 1;
10974 wordcount[0] = 0;
10975 depth = 0;
10976 while (depth >= 0 && !got_int)
10977 {
10978 if (curi[depth] > byts[arridx[depth]])
10979 {
10980 /* Done all bytes at this node, go up one level. */
10981 idxs[arridx[depth]] = wordcount[depth];
10982 if (depth > 0)
10983 wordcount[depth - 1] += wordcount[depth];
10984
10985 --depth;
10986 fast_breakcheck();
10987 }
10988 else
10989 {
10990 /* Do one more byte at this node. */
10991 n = arridx[depth] + curi[depth];
10992 ++curi[depth];
10993
10994 c = byts[n];
10995 if (c == 0)
10996 {
10997 /* End of word, count it. */
10998 ++wordcount[depth];
10999
11000 /* Skip over any other NUL bytes (same word with different
11001 * flags). */
11002 while (byts[n + 1] == 0)
11003 {
11004 ++n;
11005 ++curi[depth];
11006 }
11007 }
11008 else
11009 {
11010 /* Normal char, go one level deeper to count the words. */
11011 ++depth;
11012 arridx[depth] = idxs[n];
11013 curi[depth] = 1;
11014 wordcount[depth] = 0;
11015 }
11016 }
11017 }
11018}
11019
11020/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011021 * Free the info put in "*su" by spell_find_suggest().
11022 */
11023 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011024spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011025{
11026 int i;
11027
11028 /* Free the suggestions. */
11029 for (i = 0; i < su->su_ga.ga_len; ++i)
11030 vim_free(SUG(su->su_ga, i).st_word);
11031 ga_clear(&su->su_ga);
11032 for (i = 0; i < su->su_sga.ga_len; ++i)
11033 vim_free(SUG(su->su_sga, i).st_word);
11034 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011035
11036 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011037 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038}
11039
11040/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011041 * Make a copy of "word", with the first letter upper or lower cased, to
11042 * "wcopy[MAXWLEN]". "word" must not be empty.
11043 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 */
11045 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011046onecap_copy(
11047 char_u *word,
11048 char_u *wcopy,
11049 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050{
11051 char_u *p;
11052 int c;
11053 int l;
11054
11055 p = word;
11056#ifdef FEAT_MBYTE
11057 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011058 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 else
11060#endif
11061 c = *p++;
11062 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011063 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011065 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066#ifdef FEAT_MBYTE
11067 if (has_mbyte)
11068 l = mb_char2bytes(c, wcopy);
11069 else
11070#endif
11071 {
11072 l = 1;
11073 wcopy[0] = c;
11074 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011075 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076}
11077
11078/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011079 * Make a copy of "word" with all the letters upper cased into
11080 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 */
11082 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011083allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084{
11085 char_u *s;
11086 char_u *d;
11087 int c;
11088
11089 d = wcopy;
11090 for (s = word; *s != NUL; )
11091 {
11092#ifdef FEAT_MBYTE
11093 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011094 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 else
11096#endif
11097 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011098
11099#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +020011100 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +000011101 * would cause weird errors in other 8-bit encodings. */
11102 if (enc_latin1like && c == 0xdf)
11103 {
11104 c = 'S';
11105 if (d - wcopy >= MAXWLEN - 1)
11106 break;
11107 *d++ = c;
11108 }
11109 else
11110#endif
11111 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112
11113#ifdef FEAT_MBYTE
11114 if (has_mbyte)
11115 {
11116 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11117 break;
11118 d += mb_char2bytes(c, d);
11119 }
11120 else
11121#endif
11122 {
11123 if (d - wcopy >= MAXWLEN - 1)
11124 break;
11125 *d++ = c;
11126 }
11127 }
11128 *d = NUL;
11129}
11130
11131/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011132 * Try finding suggestions by recognizing specific situations.
11133 */
11134 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011135suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011136{
11137 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011138 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011139 int c;
11140 char_u word[MAXWLEN];
11141
11142 /*
11143 * Recognize a word that is repeated: "the the".
11144 */
11145 p = skiptowhite(su->su_fbadword);
11146 len = p - su->su_fbadword;
11147 p = skipwhite(p);
11148 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11149 {
11150 /* Include badflags: if the badword is onecap or allcap
11151 * use that for the goodword too: "The the" -> "The". */
11152 c = su->su_fbadword[len];
11153 su->su_fbadword[len] = NUL;
11154 make_case_word(su->su_fbadword, word, su->su_badflags);
11155 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011156
11157 /* Give a soundalike score of 0, compute the score as if deleting one
11158 * character. */
11159 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011160 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011161 }
11162}
11163
11164/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011165 * Change the 0 to 1 to measure how much time is spent in each state.
11166 * Output is dumped in "suggestprof".
11167 */
11168#if 0
11169# define SUGGEST_PROFILE
11170proftime_T current;
11171proftime_T total;
11172proftime_T times[STATE_FINAL + 1];
11173long counts[STATE_FINAL + 1];
11174
11175 static void
11176prof_init(void)
11177{
11178 for (int i = 0; i <= STATE_FINAL; ++i)
11179 {
11180 profile_zero(&times[i]);
11181 counts[i] = 0;
11182 }
11183 profile_start(&current);
11184 profile_start(&total);
11185}
11186
11187/* call before changing state */
11188 static void
11189prof_store(state_T state)
11190{
11191 profile_end(&current);
11192 profile_add(&times[state], &current);
11193 ++counts[state];
11194 profile_start(&current);
11195}
11196# define PROF_STORE(state) prof_store(state);
11197
11198 static void
11199prof_report(char *name)
11200{
11201 FILE *fd = fopen("suggestprof", "a");
11202
11203 profile_end(&total);
11204 fprintf(fd, "-----------------------\n");
11205 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
11206 for (int i = 0; i <= STATE_FINAL; ++i)
11207 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
11208 fclose(fd);
11209}
11210#else
11211# define PROF_STORE(state)
11212#endif
11213
11214/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 * Try finding suggestions by adding/removing/swapping letters.
11216 */
11217 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011218suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011219{
11220 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011221 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011223 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011224 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225
11226 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011227 * to find matches (esp. REP items). Append some more text, changing
11228 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011230 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011231 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011232 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233
Bram Moolenaar860cae12010-06-05 23:22:07 +020011234 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011236 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011237
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011238 /* If reloading a spell file fails it's still in the list but
11239 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011240 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011241 continue;
11242
Bram Moolenaar4770d092006-01-12 23:22:24 +000011243 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011244#ifdef SUGGEST_PROFILE
11245 prof_init();
11246#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011247 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011248#ifdef SUGGEST_PROFILE
11249 prof_report("try_change");
11250#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011251 }
11252}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253
Bram Moolenaar4770d092006-01-12 23:22:24 +000011254/* Check the maximum score, if we go over it we won't try this change. */
11255#define TRY_DEEPER(su, stack, depth, add) \
11256 (stack[depth].ts_score + (add) < su->su_maxscore)
11257
11258/*
11259 * Try finding suggestions by adding/removing/swapping letters.
11260 *
11261 * This uses a state machine. At each node in the tree we try various
11262 * operations. When trying if an operation works "depth" is increased and the
11263 * stack[] is used to store info. This allows combinations, thus insert one
11264 * character, replace one and delete another. The number of changes is
11265 * limited by su->su_maxscore.
11266 *
11267 * After implementing this I noticed an article by Kemal Oflazer that
11268 * describes something similar: "Error-tolerant Finite State Recognition with
11269 * Applications to Morphological Analysis and Spelling Correction" (1996).
11270 * The implementation in the article is simplified and requires a stack of
11271 * unknown depth. The implementation here only needs a stack depth equal to
11272 * the length of the word.
11273 *
11274 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11275 * The mechanism is the same, but we find a match with a sound-folded word
11276 * that comes from one or more original words. Each of these words may be
11277 * added, this is done by add_sound_suggest().
11278 * Don't use:
11279 * the prefix tree or the keep-case tree
11280 * "su->su_badlen"
11281 * anything to do with upper and lower case
11282 * anything to do with word or non-word characters ("spell_iswordp()")
11283 * banned words
11284 * word flags (rare, region, compounding)
11285 * word splitting for now
11286 * "similar_chars()"
11287 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11288 */
11289 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011290suggest_trie_walk(
11291 suginfo_T *su,
11292 langp_T *lp,
11293 char_u *fword,
11294 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011295{
11296 char_u tword[MAXWLEN]; /* good word collected so far */
11297 trystate_T stack[MAXWLEN];
11298 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011299 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011300 * words and split word. NUL terminated
11301 * when going deeper but not when coming
11302 * back. */
11303 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11304 trystate_T *sp;
11305 int newscore;
11306 int score;
11307 char_u *byts, *fbyts, *pbyts;
11308 idx_T *idxs, *fidxs, *pidxs;
11309 int depth;
11310 int c, c2, c3;
11311 int n = 0;
11312 int flags;
11313 garray_T *gap;
11314 idx_T arridx;
11315 int len;
11316 char_u *p;
11317 fromto_T *ftp;
11318 int fl = 0, tl;
11319 int repextra = 0; /* extra bytes in fword[] from REP item */
11320 slang_T *slang = lp->lp_slang;
11321 int fword_ends;
11322 int goodword_ends;
11323#ifdef DEBUG_TRIEWALK
11324 /* Stores the name of the change made at each level. */
11325 char_u changename[MAXWLEN][80];
11326#endif
11327 int breakcheckcount = 1000;
11328 int compound_ok;
11329
11330 /*
11331 * Go through the whole case-fold tree, try changes at each node.
11332 * "tword[]" contains the word collected from nodes in the tree.
11333 * "fword[]" the word we are trying to match with (initially the bad
11334 * word).
11335 */
11336 depth = 0;
11337 sp = &stack[0];
11338 vim_memset(sp, 0, sizeof(trystate_T));
11339 sp->ts_curi = 1;
11340
11341 if (soundfold)
11342 {
11343 /* Going through the soundfold tree. */
11344 byts = fbyts = slang->sl_sbyts;
11345 idxs = fidxs = slang->sl_sidxs;
11346 pbyts = NULL;
11347 pidxs = NULL;
11348 sp->ts_prefixdepth = PFD_NOPREFIX;
11349 sp->ts_state = STATE_START;
11350 }
11351 else
11352 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011353 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011354 * When there are postponed prefixes we need to use these first. At
11355 * the end of the prefix we continue in the case-fold tree.
11356 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011357 fbyts = slang->sl_fbyts;
11358 fidxs = slang->sl_fidxs;
11359 pbyts = slang->sl_pbyts;
11360 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011361 if (pbyts != NULL)
11362 {
11363 byts = pbyts;
11364 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011365 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011366 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11367 }
11368 else
11369 {
11370 byts = fbyts;
11371 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011372 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011373 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011374 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011375 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011376
Bram Moolenaar4770d092006-01-12 23:22:24 +000011377 /*
11378 * Loop to find all suggestions. At each round we either:
11379 * - For the current state try one operation, advance "ts_curi",
11380 * increase "depth".
11381 * - When a state is done go to the next, set "ts_state".
11382 * - When all states are tried decrease "depth".
11383 */
11384 while (depth >= 0 && !got_int)
11385 {
11386 sp = &stack[depth];
11387 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011389 case STATE_START:
11390 case STATE_NOPREFIX:
11391 /*
11392 * Start of node: Deal with NUL bytes, which means
11393 * tword[] may end here.
11394 */
11395 arridx = sp->ts_arridx; /* current node in the tree */
11396 len = byts[arridx]; /* bytes in this node */
11397 arridx += sp->ts_curi; /* index of current byte */
11398
11399 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011401 /* Skip over the NUL bytes, we use them later. */
11402 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11403 ;
11404 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405
Bram Moolenaar4770d092006-01-12 23:22:24 +000011406 /* Always past NUL bytes now. */
11407 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011408 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011409 sp->ts_state = STATE_ENDNUL;
11410 sp->ts_save_badflags = su->su_badflags;
11411
11412 /* At end of a prefix or at start of prefixtree: check for
11413 * following word. */
11414 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011415 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011416 /* Set su->su_badflags to the caps type at this position.
11417 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011418#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011419 if (has_mbyte)
11420 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11421 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011422#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011423 n = sp->ts_fidx;
11424 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11425 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011426 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011427#ifdef DEBUG_TRIEWALK
11428 sprintf(changename[depth], "prefix");
11429#endif
11430 go_deeper(stack, depth, 0);
11431 ++depth;
11432 sp = &stack[depth];
11433 sp->ts_prefixdepth = depth - 1;
11434 byts = fbyts;
11435 idxs = fidxs;
11436 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011437
Bram Moolenaar4770d092006-01-12 23:22:24 +000011438 /* Move the prefix to preword[] with the right case
11439 * and make find_keepcap_word() works. */
11440 tword[sp->ts_twordlen] = NUL;
11441 make_case_word(tword + sp->ts_splitoff,
11442 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011443 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011444 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011445 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011446 break;
11447 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011448
Bram Moolenaar4770d092006-01-12 23:22:24 +000011449 if (sp->ts_curi > len || byts[arridx] != 0)
11450 {
11451 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011452 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011453 sp->ts_state = STATE_ENDNUL;
11454 sp->ts_save_badflags = su->su_badflags;
11455 break;
11456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457
Bram Moolenaar4770d092006-01-12 23:22:24 +000011458 /*
11459 * End of word in tree.
11460 */
11461 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462
Bram Moolenaar4770d092006-01-12 23:22:24 +000011463 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011464
11465 /* Skip words with the NOSUGGEST flag. */
11466 if (flags & WF_NOSUGGEST)
11467 break;
11468
Bram Moolenaar4770d092006-01-12 23:22:24 +000011469 fword_ends = (fword[sp->ts_fidx] == NUL
11470 || (soundfold
11471 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011472 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474
Bram Moolenaar4770d092006-01-12 23:22:24 +000011475 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011476 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011477 {
11478 /* There was a prefix before the word. Check that the prefix
11479 * can be used with this word. */
11480 /* Count the length of the NULs in the prefix. If there are
11481 * none this must be the first try without a prefix. */
11482 n = stack[sp->ts_prefixdepth].ts_arridx;
11483 len = pbyts[n++];
11484 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11485 ;
11486 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011487 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011488 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011489 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011490 if (c == 0)
11491 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011492
Bram Moolenaar4770d092006-01-12 23:22:24 +000011493 /* Use the WF_RARE flag for a rare prefix. */
11494 if (c & WF_RAREPFX)
11495 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011496
Bram Moolenaar4770d092006-01-12 23:22:24 +000011497 /* Tricky: when checking for both prefix and compounding
11498 * we run into the prefix flag first.
11499 * Remember that it's OK, so that we accept the prefix
11500 * when arriving at a compound flag. */
11501 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011502 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011503 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011504
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11506 * appending another compound word below. */
11507 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011508 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011509 goodword_ends = FALSE;
11510 else
11511 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011512
Bram Moolenaar4770d092006-01-12 23:22:24 +000011513 p = NULL;
11514 compound_ok = TRUE;
11515 if (sp->ts_complen > sp->ts_compsplit)
11516 {
11517 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011518 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011519 /* There was a word before this word. When there was no
11520 * change in this word (it was correct) add the first word
11521 * as a suggestion. If this word was corrected too, we
11522 * need to check if a correct word follows. */
11523 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011524 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011525 && STRNCMP(fword + sp->ts_splitfidx,
11526 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011527 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011528 {
11529 preword[sp->ts_prewordlen] = NUL;
11530 newscore = score_wordcount_adj(slang, sp->ts_score,
11531 preword + sp->ts_prewordlen,
11532 sp->ts_prewordlen > 0);
11533 /* Add the suggestion if the score isn't too bad. */
11534 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011535 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011536 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011537 newscore, 0, FALSE,
11538 lp->lp_sallang, FALSE);
11539 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011540 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011541 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011542 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011543 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011544 /* There was a compound word before this word. If this
11545 * word does not support compounding then give up
11546 * (splitting is tried for the word without compound
11547 * flag). */
11548 if (((unsigned)flags >> 24) == 0
11549 || sp->ts_twordlen - sp->ts_splitoff
11550 < slang->sl_compminlen)
11551 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011552#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011553 /* For multi-byte chars check character length against
11554 * COMPOUNDMIN. */
11555 if (has_mbyte
11556 && slang->sl_compminlen > 0
11557 && mb_charlen(tword + sp->ts_splitoff)
11558 < slang->sl_compminlen)
11559 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011560#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011561
Bram Moolenaar4770d092006-01-12 23:22:24 +000011562 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11563 compflags[sp->ts_complen + 1] = NUL;
11564 vim_strncpy(preword + sp->ts_prewordlen,
11565 tword + sp->ts_splitoff,
11566 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011567
11568 /* Verify CHECKCOMPOUNDPATTERN rules. */
11569 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11570 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011571 compound_ok = FALSE;
11572
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011573 if (compound_ok)
11574 {
11575 p = preword;
11576 while (*skiptowhite(p) != NUL)
11577 p = skipwhite(skiptowhite(p));
11578 if (fword_ends && !can_compound(slang, p,
11579 compflags + sp->ts_compsplit))
11580 /* Compound is not allowed. But it may still be
11581 * possible if we add another (short) word. */
11582 compound_ok = FALSE;
11583 }
11584
Bram Moolenaar4770d092006-01-12 23:22:24 +000011585 /* Get pointer to last char of previous word. */
11586 p = preword + sp->ts_prewordlen;
11587 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011588 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 /*
11592 * Form the word with proper case in preword.
11593 * If there is a word from a previous split, append.
11594 * For the soundfold tree don't change the case, simply append.
11595 */
11596 if (soundfold)
11597 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11598 else if (flags & WF_KEEPCAP)
11599 /* Must find the word in the keep-case tree. */
11600 find_keepcap_word(slang, tword + sp->ts_splitoff,
11601 preword + sp->ts_prewordlen);
11602 else
11603 {
11604 /* Include badflags: If the badword is onecap or allcap
11605 * use that for the goodword too. But if the badword is
11606 * allcap and it's only one char long use onecap. */
11607 c = su->su_badflags;
11608 if ((c & WF_ALLCAP)
11609#ifdef FEAT_MBYTE
11610 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11611#else
11612 && su->su_badlen == 1
11613#endif
11614 )
11615 c = WF_ONECAP;
11616 c |= flags;
11617
11618 /* When appending a compound word after a word character don't
11619 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011620 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011621 c &= ~WF_ONECAP;
11622 make_case_word(tword + sp->ts_splitoff,
11623 preword + sp->ts_prewordlen, c);
11624 }
11625
11626 if (!soundfold)
11627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011628 /* Don't use a banned word. It may appear again as a good
11629 * word, thus remember it. */
11630 if (flags & WF_BANNED)
11631 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011632 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 break;
11634 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011635 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011636 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11637 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011638 {
11639 if (slang->sl_compprog == NULL)
11640 break;
11641 /* the word so far was banned but we may try compounding */
11642 goodword_ends = FALSE;
11643 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011645
Bram Moolenaar4770d092006-01-12 23:22:24 +000011646 newscore = 0;
11647 if (!soundfold) /* soundfold words don't have flags */
11648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011649 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011650 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 newscore += SCORE_REGION;
11652 if (flags & WF_RARE)
11653 newscore += SCORE_RARE;
11654
Bram Moolenaar0c405862005-06-22 22:26:26 +000011655 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011656 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011658 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011659
Bram Moolenaar4770d092006-01-12 23:22:24 +000011660 /* TODO: how about splitting in the soundfold tree? */
11661 if (fword_ends
11662 && goodword_ends
11663 && sp->ts_fidx >= sp->ts_fidxtry
11664 && compound_ok)
11665 {
11666 /* The badword also ends: add suggestions. */
11667#ifdef DEBUG_TRIEWALK
11668 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011669 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011670 int j;
11671
11672 /* print the stack of changes that brought us here */
11673 smsg("------ %s -------", fword);
11674 for (j = 0; j < depth; ++j)
11675 smsg("%s", changename[j]);
11676 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011677#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011678 if (soundfold)
11679 {
11680 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011681 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011682 add_sound_suggest(su, preword, sp->ts_score, lp);
11683 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011684 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011685 {
11686 /* Give a penalty when changing non-word char to word
11687 * char, e.g., "thes," -> "these". */
11688 p = fword + sp->ts_fidx;
11689 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011690 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011691 {
11692 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011693 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011694 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011695 newscore += SCORE_NONWORD;
11696 }
11697
Bram Moolenaar4770d092006-01-12 23:22:24 +000011698 /* Give a bonus to words seen before. */
11699 score = score_wordcount_adj(slang,
11700 sp->ts_score + newscore,
11701 preword + sp->ts_prewordlen,
11702 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011703
Bram Moolenaar4770d092006-01-12 23:22:24 +000011704 /* Add the suggestion if the score isn't too bad. */
11705 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011706 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011707 add_suggestion(su, &su->su_ga, preword,
11708 sp->ts_fidx - repextra,
11709 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011710
11711 if (su->su_badflags & WF_MIXCAP)
11712 {
11713 /* We really don't know if the word should be
11714 * upper or lower case, add both. */
11715 c = captype(preword, NULL);
11716 if (c == 0 || c == WF_ALLCAP)
11717 {
11718 make_case_word(tword + sp->ts_splitoff,
11719 preword + sp->ts_prewordlen,
11720 c == 0 ? WF_ALLCAP : 0);
11721
11722 add_suggestion(su, &su->su_ga, preword,
11723 sp->ts_fidx - repextra,
11724 score + SCORE_ICASE, 0, FALSE,
11725 lp->lp_sallang, FALSE);
11726 }
11727 }
11728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011730 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011731
Bram Moolenaar4770d092006-01-12 23:22:24 +000011732 /*
11733 * Try word split and/or compounding.
11734 */
11735 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011736#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011737 /* Don't split halfway a character. */
11738 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011739#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011740 )
11741 {
11742 int try_compound;
11743 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011744
Bram Moolenaar4770d092006-01-12 23:22:24 +000011745 /* If past the end of the bad word don't try a split.
11746 * Otherwise try changing the next word. E.g., find
11747 * suggestions for "the the" where the second "the" is
11748 * different. It's done like a split.
11749 * TODO: word split for soundfold words */
11750 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11751 && !soundfold;
11752
11753 /* Get here in several situations:
11754 * 1. The word in the tree ends:
11755 * If the word allows compounding try that. Otherwise try
11756 * a split by inserting a space. For both check that a
11757 * valid words starts at fword[sp->ts_fidx].
11758 * For NOBREAK do like compounding to be able to check if
11759 * the next word is valid.
11760 * 2. The badword does end, but it was due to a change (e.g.,
11761 * a swap). No need to split, but do check that the
11762 * following word is valid.
11763 * 3. The badword and the word in the tree end. It may still
11764 * be possible to compound another (short) word.
11765 */
11766 try_compound = FALSE;
11767 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +010011768 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +000011769 && slang->sl_compprog != NULL
11770 && ((unsigned)flags >> 24) != 0
11771 && sp->ts_twordlen - sp->ts_splitoff
11772 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011773#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011774 && (!has_mbyte
11775 || slang->sl_compminlen == 0
11776 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011777 >= slang->sl_compminlen)
11778#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011779 && (slang->sl_compsylmax < MAXWLEN
11780 || sp->ts_complen + 1 - sp->ts_compsplit
11781 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011782 && (can_be_compound(sp, slang,
11783 compflags, ((unsigned)flags >> 24))))
11784
Bram Moolenaar4770d092006-01-12 23:22:24 +000011785 {
11786 try_compound = TRUE;
11787 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11788 compflags[sp->ts_complen + 1] = NUL;
11789 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011790
Bram Moolenaar4770d092006-01-12 23:22:24 +000011791 /* For NOBREAK we never try splitting, it won't make any word
11792 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +010011793 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011794 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011795
Bram Moolenaar4770d092006-01-12 23:22:24 +000011796 /* If we could add a compound word, and it's also possible to
11797 * split at this point, do the split first and set
11798 * TSF_DIDSPLIT to avoid doing it again. */
11799 else if (!fword_ends
11800 && try_compound
11801 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11802 {
11803 try_compound = FALSE;
11804 sp->ts_flags |= TSF_DIDSPLIT;
11805 --sp->ts_curi; /* do the same NUL again */
11806 compflags[sp->ts_complen] = NUL;
11807 }
11808 else
11809 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011810
Bram Moolenaar4770d092006-01-12 23:22:24 +000011811 if (try_split || try_compound)
11812 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011813 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011814 {
11815 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011816 * words so far are valid for compounding. If there
11817 * is only one word it must not have the NEEDCOMPOUND
11818 * flag. */
11819 if (sp->ts_complen == sp->ts_compsplit
11820 && (flags & WF_NEEDCOMP))
11821 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011822 p = preword;
11823 while (*skiptowhite(p) != NUL)
11824 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011825 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011826 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011827 compflags + sp->ts_compsplit))
11828 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011829
11830 if (slang->sl_nosplitsugs)
11831 newscore += SCORE_SPLIT_NO;
11832 else
11833 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011834
11835 /* Give a bonus to words seen before. */
11836 newscore = score_wordcount_adj(slang, newscore,
11837 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011838 }
11839
Bram Moolenaar4770d092006-01-12 23:22:24 +000011840 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011842 go_deeper(stack, depth, newscore);
11843#ifdef DEBUG_TRIEWALK
11844 if (!try_compound && !fword_ends)
11845 sprintf(changename[depth], "%.*s-%s: split",
11846 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11847 else
11848 sprintf(changename[depth], "%.*s-%s: compound",
11849 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11850#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011852 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011853 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011854 sp->ts_state = STATE_SPLITUNDO;
11855
11856 ++depth;
11857 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011859 /* Append a space to preword when splitting. */
11860 if (!try_compound && !fword_ends)
11861 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011862 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011863 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011864 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011865
11866 /* If the badword has a non-word character at this
11867 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011868 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011869 * character when the word ends. But only when the
11870 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011871 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011872 + sp->ts_fidx,
11873 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011874 || fword_ends)
11875 && fword[sp->ts_fidx] != NUL
11876 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011877 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011878 int l;
11879
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011880#ifdef FEAT_MBYTE
11881 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011882 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011883 else
11884#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011885 l = 1;
11886 if (fword_ends)
11887 {
11888 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011889 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011890 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011891 sp->ts_prewordlen += l;
11892 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011893 }
11894 else
11895 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11896 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011897 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011898
Bram Moolenaard12a1322005-08-21 22:08:24 +000011899 /* When compounding include compound flag in
11900 * compflags[] (already set above). When splitting we
11901 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011902 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011903 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011904 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011905 sp->ts_compsplit = sp->ts_complen;
11906 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011907
Bram Moolenaar53805d12005-08-01 07:08:33 +000011908 /* set su->su_badflags to the caps type at this
11909 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011910#ifdef FEAT_MBYTE
11911 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011912 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011913 else
11914#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011915 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011916 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011917 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011919 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011920 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011921
11922 /* If there are postponed prefixes, try these too. */
11923 if (pbyts != NULL)
11924 {
11925 byts = pbyts;
11926 idxs = pidxs;
11927 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011928 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011929 sp->ts_state = STATE_NOPREFIX;
11930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 }
11932 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011933 }
11934 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011935
Bram Moolenaar4770d092006-01-12 23:22:24 +000011936 case STATE_SPLITUNDO:
11937 /* Undo the changes done for word split or compound word. */
11938 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011939
Bram Moolenaar4770d092006-01-12 23:22:24 +000011940 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011941 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011942 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011943
Bram Moolenaar4770d092006-01-12 23:22:24 +000011944 /* In case we went into the prefix tree. */
11945 byts = fbyts;
11946 idxs = fidxs;
11947 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948
Bram Moolenaar4770d092006-01-12 23:22:24 +000011949 case STATE_ENDNUL:
11950 /* Past the NUL bytes in the node. */
11951 su->su_badflags = sp->ts_save_badflags;
11952 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011953#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011954 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011955#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011956 )
11957 {
11958 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011959 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011960 sp->ts_state = STATE_DEL;
11961 break;
11962 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011963 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 sp->ts_state = STATE_PLAIN;
11965 /*FALLTHROUGH*/
11966
11967 case STATE_PLAIN:
11968 /*
11969 * Go over all possible bytes at this node, add each to tword[]
11970 * and use child node. "ts_curi" is the index.
11971 */
11972 arridx = sp->ts_arridx;
11973 if (sp->ts_curi > byts[arridx])
11974 {
11975 /* Done all bytes at this node, do next state. When still at
11976 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011977 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011978 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011981 sp->ts_state = STATE_FINAL;
11982 }
11983 else
11984 {
11985 arridx += sp->ts_curi++;
11986 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987
Bram Moolenaar4770d092006-01-12 23:22:24 +000011988 /* Normal byte, go one level deeper. If it's not equal to the
11989 * byte in the bad word adjust the score. But don't even try
11990 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +010011991 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +000011992 * delete + substitute. */
11993 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011994#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011995 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011996#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011997 )
11998 newscore = 0;
11999 else
12000 newscore = SCORE_SUBST;
12001 if ((newscore == 0
12002 || (sp->ts_fidx >= sp->ts_fidxtry
12003 && ((sp->ts_flags & TSF_DIDDEL) == 0
12004 || c != fword[sp->ts_delidx])))
12005 && TRY_DEEPER(su, stack, depth, newscore))
12006 {
12007 go_deeper(stack, depth, newscore);
12008#ifdef DEBUG_TRIEWALK
12009 if (newscore > 0)
12010 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12011 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12012 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012014 sprintf(changename[depth], "%.*s-%s: accept %c",
12015 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12016 fword[sp->ts_fidx]);
12017#endif
12018 ++depth;
12019 sp = &stack[depth];
12020 ++sp->ts_fidx;
12021 tword[sp->ts_twordlen++] = c;
12022 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012023#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012024 if (newscore == SCORE_SUBST)
12025 sp->ts_isdiff = DIFF_YES;
12026 if (has_mbyte)
12027 {
12028 /* Multi-byte characters are a bit complicated to
12029 * handle: They differ when any of the bytes differ
12030 * and then their length may also differ. */
12031 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012032 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012033 /* First byte. */
12034 sp->ts_tcharidx = 0;
12035 sp->ts_tcharlen = MB_BYTE2LEN(c);
12036 sp->ts_fcharstart = sp->ts_fidx - 1;
12037 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012038 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012039 }
12040 else if (sp->ts_isdiff == DIFF_INSERT)
12041 /* When inserting trail bytes don't advance in the
12042 * bad word. */
12043 --sp->ts_fidx;
12044 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12045 {
12046 /* Last byte of character. */
12047 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012048 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012049 /* Correct ts_fidx for the byte length of the
12050 * character (we didn't check that before). */
12051 sp->ts_fidx = sp->ts_fcharstart
12052 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012053 fword[sp->ts_fcharstart]);
12054
Bram Moolenaar4770d092006-01-12 23:22:24 +000012055 /* For changing a composing character adjust
12056 * the score from SCORE_SUBST to
12057 * SCORE_SUBCOMP. */
12058 if (enc_utf8
12059 && utf_iscomposing(
12060 mb_ptr2char(tword
12061 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012062 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012063 && utf_iscomposing(
12064 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012065 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012066 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012067 SCORE_SUBST - SCORE_SUBCOMP;
12068
Bram Moolenaar4770d092006-01-12 23:22:24 +000012069 /* For a similar character adjust score from
12070 * SCORE_SUBST to SCORE_SIMILAR. */
12071 else if (!soundfold
12072 && slang->sl_has_map
12073 && similar_chars(slang,
12074 mb_ptr2char(tword
12075 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012076 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012077 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012078 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012079 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012080 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012081 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012082 else if (sp->ts_isdiff == DIFF_INSERT
12083 && sp->ts_twordlen > sp->ts_tcharlen)
12084 {
12085 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12086 c = mb_ptr2char(p);
12087 if (enc_utf8 && utf_iscomposing(c))
12088 {
12089 /* Inserting a composing char doesn't
12090 * count that much. */
12091 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12092 }
12093 else
12094 {
12095 /* If the previous character was the same,
12096 * thus doubling a character, give a bonus
12097 * to the score. Also for the soundfold
12098 * tree (might seem illogical but does
12099 * give better scores). */
12100 mb_ptr_back(tword, p);
12101 if (c == mb_ptr2char(p))
12102 sp->ts_score -= SCORE_INS
12103 - SCORE_INSDUP;
12104 }
12105 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012106
Bram Moolenaar4770d092006-01-12 23:22:24 +000012107 /* Starting a new char, reset the length. */
12108 sp->ts_tcharlen = 0;
12109 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012110 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012111 else
12112#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012113 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012114 /* If we found a similar char adjust the score.
12115 * We do this after calling go_deeper() because
12116 * it's slow. */
12117 if (newscore != 0
12118 && !soundfold
12119 && slang->sl_has_map
12120 && similar_chars(slang,
12121 c, fword[sp->ts_fidx - 1]))
12122 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012123 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012124 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012125 }
12126 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012127
Bram Moolenaar4770d092006-01-12 23:22:24 +000012128 case STATE_DEL:
12129#ifdef FEAT_MBYTE
12130 /* When past the first byte of a multi-byte char don't try
12131 * delete/insert/swap a character. */
12132 if (has_mbyte && sp->ts_tcharlen > 0)
12133 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012134 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012135 sp->ts_state = STATE_FINAL;
12136 break;
12137 }
12138#endif
12139 /*
12140 * Try skipping one character in the bad word (delete it).
12141 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012142 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012143 sp->ts_state = STATE_INS_PREP;
12144 sp->ts_curi = 1;
12145 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12146 /* Deleting a vowel at the start of a word counts less, see
12147 * soundalike_score(). */
12148 newscore = 2 * SCORE_DEL / 3;
12149 else
12150 newscore = SCORE_DEL;
12151 if (fword[sp->ts_fidx] != NUL
12152 && TRY_DEEPER(su, stack, depth, newscore))
12153 {
12154 go_deeper(stack, depth, newscore);
12155#ifdef DEBUG_TRIEWALK
12156 sprintf(changename[depth], "%.*s-%s: delete %c",
12157 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12158 fword[sp->ts_fidx]);
12159#endif
12160 ++depth;
12161
12162 /* Remember what character we deleted, so that we can avoid
12163 * inserting it again. */
12164 stack[depth].ts_flags |= TSF_DIDDEL;
12165 stack[depth].ts_delidx = sp->ts_fidx;
12166
12167 /* Advance over the character in fword[]. Give a bonus to the
12168 * score if the same character is following "nn" -> "n". It's
12169 * a bit illogical for soundfold tree but it does give better
12170 * results. */
12171#ifdef FEAT_MBYTE
12172 if (has_mbyte)
12173 {
12174 c = mb_ptr2char(fword + sp->ts_fidx);
12175 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12176 if (enc_utf8 && utf_iscomposing(c))
12177 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12178 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12179 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12180 }
12181 else
12182#endif
12183 {
12184 ++stack[depth].ts_fidx;
12185 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12186 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12187 }
12188 break;
12189 }
12190 /*FALLTHROUGH*/
12191
12192 case STATE_INS_PREP:
12193 if (sp->ts_flags & TSF_DIDDEL)
12194 {
12195 /* If we just deleted a byte then inserting won't make sense,
12196 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012197 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012198 sp->ts_state = STATE_SWAP;
12199 break;
12200 }
12201
12202 /* skip over NUL bytes */
12203 n = sp->ts_arridx;
12204 for (;;)
12205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012206 if (sp->ts_curi > byts[n])
12207 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012208 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012209 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012210 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012211 break;
12212 }
12213 if (byts[n + sp->ts_curi] != NUL)
12214 {
12215 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012216 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012217 sp->ts_state = STATE_INS;
12218 break;
12219 }
12220 ++sp->ts_curi;
12221 }
12222 break;
12223
12224 /*FALLTHROUGH*/
12225
12226 case STATE_INS:
12227 /* Insert one byte. Repeat this for each possible byte at this
12228 * node. */
12229 n = sp->ts_arridx;
12230 if (sp->ts_curi > byts[n])
12231 {
12232 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012233 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012234 sp->ts_state = STATE_SWAP;
12235 break;
12236 }
12237
12238 /* Do one more byte at this node, but:
12239 * - Skip NUL bytes.
12240 * - Skip the byte if it's equal to the byte in the word,
12241 * accepting that byte is always better.
12242 */
12243 n += sp->ts_curi++;
12244 c = byts[n];
12245 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12246 /* Inserting a vowel at the start of a word counts less,
12247 * see soundalike_score(). */
12248 newscore = 2 * SCORE_INS / 3;
12249 else
12250 newscore = SCORE_INS;
12251 if (c != fword[sp->ts_fidx]
12252 && TRY_DEEPER(su, stack, depth, newscore))
12253 {
12254 go_deeper(stack, depth, newscore);
12255#ifdef DEBUG_TRIEWALK
12256 sprintf(changename[depth], "%.*s-%s: insert %c",
12257 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12258 c);
12259#endif
12260 ++depth;
12261 sp = &stack[depth];
12262 tword[sp->ts_twordlen++] = c;
12263 sp->ts_arridx = idxs[n];
12264#ifdef FEAT_MBYTE
12265 if (has_mbyte)
12266 {
12267 fl = MB_BYTE2LEN(c);
12268 if (fl > 1)
12269 {
12270 /* There are following bytes for the same character.
12271 * We must find all bytes before trying
12272 * delete/insert/swap/etc. */
12273 sp->ts_tcharlen = fl;
12274 sp->ts_tcharidx = 1;
12275 sp->ts_isdiff = DIFF_INSERT;
12276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012277 }
12278 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012279 fl = 1;
12280 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012281#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012282 {
12283 /* If the previous character was the same, thus doubling a
12284 * character, give a bonus to the score. Also for
12285 * soundfold words (illogical but does give a better
12286 * score). */
12287 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012288 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012289 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012291 }
12292 break;
12293
12294 case STATE_SWAP:
12295 /*
12296 * Swap two bytes in the bad word: "12" -> "21".
12297 * We change "fword" here, it's changed back afterwards at
12298 * STATE_UNSWAP.
12299 */
12300 p = fword + sp->ts_fidx;
12301 c = *p;
12302 if (c == NUL)
12303 {
12304 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012305 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012306 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012307 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012309
Bram Moolenaar4770d092006-01-12 23:22:24 +000012310 /* Don't swap if the first character is not a word character.
12311 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012312 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012313 {
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_REP_INI;
12316 break;
12317 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012318
Bram Moolenaar4770d092006-01-12 23:22:24 +000012319#ifdef FEAT_MBYTE
12320 if (has_mbyte)
12321 {
12322 n = mb_cptr2len(p);
12323 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012324 if (p[n] == NUL)
12325 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012326 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012327 c2 = c; /* don't swap non-word char */
12328 else
12329 c2 = mb_ptr2char(p + n);
12330 }
12331 else
12332#endif
12333 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012334 if (p[1] == NUL)
12335 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012336 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012337 c2 = c; /* don't swap non-word char */
12338 else
12339 c2 = p[1];
12340 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012341
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012342 /* When the second character is NUL we can't swap. */
12343 if (c2 == NUL)
12344 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012345 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012346 sp->ts_state = STATE_REP_INI;
12347 break;
12348 }
12349
Bram Moolenaar4770d092006-01-12 23:22:24 +000012350 /* When characters are identical, swap won't do anything.
12351 * Also get here if the second char is not a word character. */
12352 if (c == c2)
12353 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012354 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012355 sp->ts_state = STATE_SWAP3;
12356 break;
12357 }
12358 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12359 {
12360 go_deeper(stack, depth, SCORE_SWAP);
12361#ifdef DEBUG_TRIEWALK
12362 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12363 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12364 c, c2);
12365#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012366 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012367 sp->ts_state = STATE_UNSWAP;
12368 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012369#ifdef FEAT_MBYTE
12370 if (has_mbyte)
12371 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012372 fl = mb_char2len(c2);
12373 mch_memmove(p, p + n, fl);
12374 mb_char2bytes(c, p + fl);
12375 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012376 }
12377 else
12378#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012379 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012380 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012381 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012382 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012383 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012384 }
12385 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012386 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012387 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012388 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012389 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012390 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012391 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012392
Bram Moolenaar4770d092006-01-12 23:22:24 +000012393 case STATE_UNSWAP:
12394 /* Undo the STATE_SWAP swap: "21" -> "12". */
12395 p = fword + sp->ts_fidx;
12396#ifdef FEAT_MBYTE
12397 if (has_mbyte)
12398 {
12399 n = MB_BYTE2LEN(*p);
12400 c = mb_ptr2char(p + n);
12401 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12402 mb_char2bytes(c, p);
12403 }
12404 else
12405#endif
12406 {
12407 c = *p;
12408 *p = p[1];
12409 p[1] = c;
12410 }
12411 /*FALLTHROUGH*/
12412
12413 case STATE_SWAP3:
12414 /* Swap two bytes, skipping one: "123" -> "321". We change
12415 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12416 p = fword + sp->ts_fidx;
12417#ifdef FEAT_MBYTE
12418 if (has_mbyte)
12419 {
12420 n = mb_cptr2len(p);
12421 c = mb_ptr2char(p);
12422 fl = mb_cptr2len(p + n);
12423 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012424 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012425 c3 = c; /* don't swap non-word char */
12426 else
12427 c3 = mb_ptr2char(p + n + fl);
12428 }
12429 else
12430#endif
12431 {
12432 c = *p;
12433 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012434 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012435 c3 = c; /* don't swap non-word char */
12436 else
12437 c3 = p[2];
12438 }
12439
12440 /* When characters are identical: "121" then SWAP3 result is
12441 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12442 * same as SWAP on next char: "112". Thus skip all swapping.
12443 * Also skip when c3 is NUL.
12444 * Also get here when the third character is not a word character.
12445 * Second character may any char: "a.b" -> "b.a" */
12446 if (c == c3 || c3 == NUL)
12447 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012448 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012449 sp->ts_state = STATE_REP_INI;
12450 break;
12451 }
12452 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12453 {
12454 go_deeper(stack, depth, SCORE_SWAP3);
12455#ifdef DEBUG_TRIEWALK
12456 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12457 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12458 c, c3);
12459#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012460 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012461 sp->ts_state = STATE_UNSWAP3;
12462 ++depth;
12463#ifdef FEAT_MBYTE
12464 if (has_mbyte)
12465 {
12466 tl = mb_char2len(c3);
12467 mch_memmove(p, p + n + fl, tl);
12468 mb_char2bytes(c2, p + tl);
12469 mb_char2bytes(c, p + fl + tl);
12470 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12471 }
12472 else
12473#endif
12474 {
12475 p[0] = p[2];
12476 p[2] = c;
12477 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12478 }
12479 }
12480 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012481 {
12482 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012483 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012484 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012485 break;
12486
12487 case STATE_UNSWAP3:
12488 /* Undo STATE_SWAP3: "321" -> "123" */
12489 p = fword + sp->ts_fidx;
12490#ifdef FEAT_MBYTE
12491 if (has_mbyte)
12492 {
12493 n = MB_BYTE2LEN(*p);
12494 c2 = mb_ptr2char(p + n);
12495 fl = MB_BYTE2LEN(p[n]);
12496 c = mb_ptr2char(p + n + fl);
12497 tl = MB_BYTE2LEN(p[n + fl]);
12498 mch_memmove(p + fl + tl, p, n);
12499 mb_char2bytes(c, p);
12500 mb_char2bytes(c2, p + tl);
12501 p = p + tl;
12502 }
12503 else
12504#endif
12505 {
12506 c = *p;
12507 *p = p[2];
12508 p[2] = c;
12509 ++p;
12510 }
12511
Bram Moolenaar860cae12010-06-05 23:22:07 +020012512 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012513 {
12514 /* Middle char is not a word char, skip the rotate. First and
12515 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012516 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012517 sp->ts_state = STATE_REP_INI;
12518 break;
12519 }
12520
12521 /* Rotate three characters left: "123" -> "231". We change
12522 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12523 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12524 {
12525 go_deeper(stack, depth, SCORE_SWAP3);
12526#ifdef DEBUG_TRIEWALK
12527 p = fword + sp->ts_fidx;
12528 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12529 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12530 p[0], p[1], p[2]);
12531#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012532 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012533 sp->ts_state = STATE_UNROT3L;
12534 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012535 p = fword + sp->ts_fidx;
12536#ifdef FEAT_MBYTE
12537 if (has_mbyte)
12538 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012539 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012540 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012541 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012542 fl += mb_cptr2len(p + n + fl);
12543 mch_memmove(p, p + n, fl);
12544 mb_char2bytes(c, p + fl);
12545 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012546 }
12547 else
12548#endif
12549 {
12550 c = *p;
12551 *p = p[1];
12552 p[1] = p[2];
12553 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012554 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012555 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012556 }
12557 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012558 {
12559 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012560 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012561 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012562 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563
Bram Moolenaar4770d092006-01-12 23:22:24 +000012564 case STATE_UNROT3L:
12565 /* Undo ROT3L: "231" -> "123" */
12566 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012567#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012568 if (has_mbyte)
12569 {
12570 n = MB_BYTE2LEN(*p);
12571 n += MB_BYTE2LEN(p[n]);
12572 c = mb_ptr2char(p + n);
12573 tl = MB_BYTE2LEN(p[n]);
12574 mch_memmove(p + tl, p, n);
12575 mb_char2bytes(c, p);
12576 }
12577 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012578#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012579 {
12580 c = p[2];
12581 p[2] = p[1];
12582 p[1] = *p;
12583 *p = c;
12584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012585
Bram Moolenaar4770d092006-01-12 23:22:24 +000012586 /* Rotate three bytes right: "123" -> "312". We change "fword"
12587 * here, it's changed back afterwards at STATE_UNROT3R. */
12588 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12589 {
12590 go_deeper(stack, depth, SCORE_SWAP3);
12591#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012592 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012593 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12594 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12595 p[0], p[1], p[2]);
12596#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012597 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012598 sp->ts_state = STATE_UNROT3R;
12599 ++depth;
12600 p = fword + sp->ts_fidx;
12601#ifdef FEAT_MBYTE
12602 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012603 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012604 n = mb_cptr2len(p);
12605 n += mb_cptr2len(p + n);
12606 c = mb_ptr2char(p + n);
12607 tl = mb_cptr2len(p + n);
12608 mch_memmove(p + tl, p, n);
12609 mb_char2bytes(c, p);
12610 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012611 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012612 else
12613#endif
12614 {
12615 c = p[2];
12616 p[2] = p[1];
12617 p[1] = *p;
12618 *p = c;
12619 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12620 }
12621 }
12622 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012623 {
12624 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012625 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012626 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012627 break;
12628
12629 case STATE_UNROT3R:
12630 /* Undo ROT3R: "312" -> "123" */
12631 p = fword + sp->ts_fidx;
12632#ifdef FEAT_MBYTE
12633 if (has_mbyte)
12634 {
12635 c = mb_ptr2char(p);
12636 tl = MB_BYTE2LEN(*p);
12637 n = MB_BYTE2LEN(p[tl]);
12638 n += MB_BYTE2LEN(p[tl + n]);
12639 mch_memmove(p, p + tl, n);
12640 mb_char2bytes(c, p + n);
12641 }
12642 else
12643#endif
12644 {
12645 c = *p;
12646 *p = p[1];
12647 p[1] = p[2];
12648 p[2] = c;
12649 }
12650 /*FALLTHROUGH*/
12651
12652 case STATE_REP_INI:
12653 /* Check if matching with REP items from the .aff file would work.
12654 * Quickly skip if:
12655 * - there are no REP items and we are not in the soundfold trie
12656 * - the score is going to be too high anyway
12657 * - already applied a REP item or swapped here */
12658 if ((lp->lp_replang == NULL && !soundfold)
12659 || sp->ts_score + SCORE_REP >= su->su_maxscore
12660 || sp->ts_fidx < sp->ts_fidxtry)
12661 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012662 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012663 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666
Bram Moolenaar4770d092006-01-12 23:22:24 +000012667 /* Use the first byte to quickly find the first entry that may
12668 * match. If the index is -1 there is none. */
12669 if (soundfold)
12670 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12671 else
12672 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673
Bram Moolenaar4770d092006-01-12 23:22:24 +000012674 if (sp->ts_curi < 0)
12675 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012676 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012677 sp->ts_state = STATE_FINAL;
12678 break;
12679 }
12680
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012681 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012682 sp->ts_state = STATE_REP;
12683 /*FALLTHROUGH*/
12684
12685 case STATE_REP:
12686 /* Try matching with REP items from the .aff file. For each match
12687 * replace the characters and check if the resulting word is
12688 * valid. */
12689 p = fword + sp->ts_fidx;
12690
12691 if (soundfold)
12692 gap = &slang->sl_repsal;
12693 else
12694 gap = &lp->lp_replang->sl_rep;
12695 while (sp->ts_curi < gap->ga_len)
12696 {
12697 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12698 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012699 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012700 /* past possible matching entries */
12701 sp->ts_curi = gap->ga_len;
12702 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012703 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012704 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12705 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12706 {
12707 go_deeper(stack, depth, SCORE_REP);
12708#ifdef DEBUG_TRIEWALK
12709 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12710 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12711 ftp->ft_from, ftp->ft_to);
12712#endif
12713 /* Need to undo this afterwards. */
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_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012716
Bram Moolenaar4770d092006-01-12 23:22:24 +000012717 /* Change the "from" to the "to" string. */
12718 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012719 fl = (int)STRLEN(ftp->ft_from);
12720 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012721 if (fl != tl)
12722 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012723 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012724 repextra += tl - fl;
12725 }
12726 mch_memmove(p, ftp->ft_to, tl);
12727 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12728#ifdef FEAT_MBYTE
12729 stack[depth].ts_tcharlen = 0;
12730#endif
12731 break;
12732 }
12733 }
12734
12735 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012736 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012737 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012738 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012739 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012740 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012741
12742 break;
12743
12744 case STATE_REP_UNDO:
12745 /* Undo a REP replacement and continue with the next one. */
12746 if (soundfold)
12747 gap = &slang->sl_repsal;
12748 else
12749 gap = &lp->lp_replang->sl_rep;
12750 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012751 fl = (int)STRLEN(ftp->ft_from);
12752 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012753 p = fword + sp->ts_fidx;
12754 if (fl != tl)
12755 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012756 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012757 repextra -= tl - fl;
12758 }
12759 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012760 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012761 sp->ts_state = STATE_REP;
12762 break;
12763
12764 default:
12765 /* Did all possible states at this level, go up one level. */
12766 --depth;
12767
12768 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12769 {
12770 /* Continue in or go back to the prefix tree. */
12771 byts = pbyts;
12772 idxs = pidxs;
12773 }
12774
12775 /* Don't check for CTRL-C too often, it takes time. */
12776 if (--breakcheckcount == 0)
12777 {
12778 ui_breakcheck();
12779 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012781 }
12782 }
12783}
12784
Bram Moolenaar4770d092006-01-12 23:22:24 +000012785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012787 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012789 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012790go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012791{
Bram Moolenaarea424162005-06-16 21:51:00 +000012792 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012794 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012796 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797}
12798
Bram Moolenaar53805d12005-08-01 07:08:33 +000012799#ifdef FEAT_MBYTE
12800/*
12801 * Case-folding may change the number of bytes: Count nr of chars in
12802 * fword[flen] and return the byte length of that many chars in "word".
12803 */
12804 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012805nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +000012806{
12807 char_u *p;
12808 int i = 0;
12809
12810 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12811 ++i;
12812 for (p = word; i > 0; mb_ptr_adv(p))
12813 --i;
12814 return (int)(p - word);
12815}
12816#endif
12817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012818/*
12819 * "fword" is a good word with case folded. Find the matching keep-case
12820 * words and put it in "kword".
12821 * Theoretically there could be several keep-case words that result in the
12822 * same case-folded word, but we only find one...
12823 */
12824 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012825find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826{
12827 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12828 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012829 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830
12831 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012832 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012833 int round[MAXWLEN];
12834 int fwordidx[MAXWLEN];
12835 int uwordidx[MAXWLEN];
12836 int kwordlen[MAXWLEN];
12837
12838 int flen, ulen;
12839 int l;
12840 int len;
12841 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012842 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 char_u *p;
12844 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012845 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012846
12847 if (byts == NULL)
12848 {
12849 /* array is empty: "cannot happen" */
12850 *kword = NUL;
12851 return;
12852 }
12853
12854 /* Make an all-cap version of "fword". */
12855 allcap_copy(fword, uword);
12856
12857 /*
12858 * Each character needs to be tried both case-folded and upper-case.
12859 * All this gets very complicated if we keep in mind that changing case
12860 * may change the byte length of a multi-byte character...
12861 */
12862 depth = 0;
12863 arridx[0] = 0;
12864 round[0] = 0;
12865 fwordidx[0] = 0;
12866 uwordidx[0] = 0;
12867 kwordlen[0] = 0;
12868 while (depth >= 0)
12869 {
12870 if (fword[fwordidx[depth]] == NUL)
12871 {
12872 /* We are at the end of "fword". If the tree allows a word to end
12873 * here we have found a match. */
12874 if (byts[arridx[depth] + 1] == 0)
12875 {
12876 kword[kwordlen[depth]] = NUL;
12877 return;
12878 }
12879
12880 /* kword is getting too long, continue one level up */
12881 --depth;
12882 }
12883 else if (++round[depth] > 2)
12884 {
12885 /* tried both fold-case and upper-case character, continue one
12886 * level up */
12887 --depth;
12888 }
12889 else
12890 {
12891 /*
12892 * round[depth] == 1: Try using the folded-case character.
12893 * round[depth] == 2: Try using the upper-case character.
12894 */
12895#ifdef FEAT_MBYTE
12896 if (has_mbyte)
12897 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012898 flen = mb_cptr2len(fword + fwordidx[depth]);
12899 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 }
12901 else
12902#endif
12903 ulen = flen = 1;
12904 if (round[depth] == 1)
12905 {
12906 p = fword + fwordidx[depth];
12907 l = flen;
12908 }
12909 else
12910 {
12911 p = uword + uwordidx[depth];
12912 l = ulen;
12913 }
12914
12915 for (tryidx = arridx[depth]; l > 0; --l)
12916 {
12917 /* Perform a binary search in the list of accepted bytes. */
12918 len = byts[tryidx++];
12919 c = *p++;
12920 lo = tryidx;
12921 hi = tryidx + len - 1;
12922 while (lo < hi)
12923 {
12924 m = (lo + hi) / 2;
12925 if (byts[m] > c)
12926 hi = m - 1;
12927 else if (byts[m] < c)
12928 lo = m + 1;
12929 else
12930 {
12931 lo = hi = m;
12932 break;
12933 }
12934 }
12935
12936 /* Stop if there is no matching byte. */
12937 if (hi < lo || byts[lo] != c)
12938 break;
12939
12940 /* Continue at the child (if there is one). */
12941 tryidx = idxs[lo];
12942 }
12943
12944 if (l == 0)
12945 {
12946 /*
12947 * Found the matching char. Copy it to "kword" and go a
12948 * level deeper.
12949 */
12950 if (round[depth] == 1)
12951 {
12952 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12953 flen);
12954 kwordlen[depth + 1] = kwordlen[depth] + flen;
12955 }
12956 else
12957 {
12958 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12959 ulen);
12960 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12961 }
12962 fwordidx[depth + 1] = fwordidx[depth] + flen;
12963 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12964
12965 ++depth;
12966 arridx[depth] = tryidx;
12967 round[depth] = 0;
12968 }
12969 }
12970 }
12971
12972 /* Didn't find it: "cannot happen". */
12973 *kword = NUL;
12974}
12975
12976/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012977 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12978 * su->su_sga.
12979 */
12980 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012981score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012982{
12983 langp_T *lp;
12984 char_u badsound[MAXWLEN];
12985 int i;
12986 suggest_T *stp;
12987 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012988 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012989 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012990
12991 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12992 return;
12993
12994 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012995 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012996 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012997 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012998 if (lp->lp_slang->sl_sal.ga_len > 0)
12999 {
13000 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013001 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013002
13003 for (i = 0; i < su->su_ga.ga_len; ++i)
13004 {
13005 stp = &SUG(su->su_ga, i);
13006
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013007 /* Case-fold the suggested word, sound-fold it and compute the
13008 * sound-a-like score. */
13009 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013010 if (score < SCORE_MAXMAX)
13011 {
13012 /* Add the suggestion. */
13013 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13014 sstp->st_word = vim_strsave(stp->st_word);
13015 if (sstp->st_word != NULL)
13016 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013017 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013018 sstp->st_score = score;
13019 sstp->st_altscore = 0;
13020 sstp->st_orglen = stp->st_orglen;
13021 ++su->su_sga.ga_len;
13022 }
13023 }
13024 }
13025 break;
13026 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013027 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013028}
13029
13030/*
13031 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013032 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013033 */
13034 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013035score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013036{
13037 int i;
13038 int j;
13039 garray_T ga;
13040 garray_T *gap;
13041 langp_T *lp;
13042 suggest_T *stp;
13043 char_u *p;
13044 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013045 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013046 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013047 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013048
13049 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013050 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013051 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013052 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013053 if (lp->lp_slang->sl_sal.ga_len > 0)
13054 {
13055 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013056 slang = lp->lp_slang;
13057 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013058
13059 for (i = 0; i < su->su_ga.ga_len; ++i)
13060 {
13061 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013062 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013063 if (stp->st_altscore == SCORE_MAXMAX)
13064 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13065 else
13066 stp->st_score = (stp->st_score * 3
13067 + stp->st_altscore) / 4;
13068 stp->st_salscore = FALSE;
13069 }
13070 break;
13071 }
13072 }
13073
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013074 if (slang == NULL) /* Using "double" without sound folding. */
13075 {
13076 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13077 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013078 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013079 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013080
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013081 /* Add the alternate score to su_sga. */
13082 for (i = 0; i < su->su_sga.ga_len; ++i)
13083 {
13084 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013085 stp->st_altscore = spell_edit_score(slang,
13086 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013087 if (stp->st_score == SCORE_MAXMAX)
13088 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13089 else
13090 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13091 stp->st_salscore = TRUE;
13092 }
13093
Bram Moolenaar4770d092006-01-12 23:22:24 +000013094 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13095 * for both lists. */
13096 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013097 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013098 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013099 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13100
13101 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13102 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13103 return;
13104
13105 stp = &SUG(ga, 0);
13106 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13107 {
13108 /* round 1: get a suggestion from su_ga
13109 * round 2: get a suggestion from su_sga */
13110 for (round = 1; round <= 2; ++round)
13111 {
13112 gap = round == 1 ? &su->su_ga : &su->su_sga;
13113 if (i < gap->ga_len)
13114 {
13115 /* Don't add a word if it's already there. */
13116 p = SUG(*gap, i).st_word;
13117 for (j = 0; j < ga.ga_len; ++j)
13118 if (STRCMP(stp[j].st_word, p) == 0)
13119 break;
13120 if (j == ga.ga_len)
13121 stp[ga.ga_len++] = SUG(*gap, i);
13122 else
13123 vim_free(p);
13124 }
13125 }
13126 }
13127
13128 ga_clear(&su->su_ga);
13129 ga_clear(&su->su_sga);
13130
13131 /* Truncate the list to the number of suggestions that will be displayed. */
13132 if (ga.ga_len > su->su_maxcount)
13133 {
13134 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13135 vim_free(stp[i].st_word);
13136 ga.ga_len = su->su_maxcount;
13137 }
13138
13139 su->su_ga = ga;
13140}
13141
13142/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013143 * For the goodword in "stp" compute the soundalike score compared to the
13144 * badword.
13145 */
13146 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013147stp_sal_score(
13148 suggest_T *stp,
13149 suginfo_T *su,
13150 slang_T *slang,
13151 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013152{
13153 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013154 char_u *pbad;
13155 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013156 char_u badsound2[MAXWLEN];
13157 char_u fword[MAXWLEN];
13158 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013159 char_u goodword[MAXWLEN];
13160 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013161
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013162 lendiff = (int)(su->su_badlen - stp->st_orglen);
13163 if (lendiff >= 0)
13164 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013165 else
13166 {
13167 /* soundfold the bad word with more characters following */
13168 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13169
13170 /* When joining two words the sound often changes a lot. E.g., "t he"
13171 * sounds like "t h" while "the" sounds like "@". Avoid that by
13172 * removing the space. Don't do it when the good word also contains a
13173 * space. */
13174 if (vim_iswhite(su->su_badptr[su->su_badlen])
13175 && *skiptowhite(stp->st_word) == NUL)
13176 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013177 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013178
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013179 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013180 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013181 }
13182
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013183 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013184 {
13185 /* Add part of the bad word to the good word, so that we soundfold
13186 * what replaces the bad word. */
13187 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013188 vim_strncpy(goodword + stp->st_wordlen,
13189 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013190 pgood = goodword;
13191 }
13192 else
13193 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013194
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013195 /* Sound-fold the word and compute the score for the difference. */
13196 spell_soundfold(slang, pgood, FALSE, goodsound);
13197
13198 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013199}
13200
Bram Moolenaar4770d092006-01-12 23:22:24 +000013201/* structure used to store soundfolded words that add_sound_suggest() has
13202 * handled already. */
13203typedef struct
13204{
13205 short sft_score; /* lowest score used */
13206 char_u sft_word[1]; /* soundfolded word, actually longer */
13207} sftword_T;
13208
13209static sftword_T dumsft;
13210#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13211#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13212
13213/*
13214 * Prepare for calling suggest_try_soundalike().
13215 */
13216 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013217suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013218{
13219 langp_T *lp;
13220 int lpi;
13221 slang_T *slang;
13222
13223 /* Do this for all languages that support sound folding and for which a
13224 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013225 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013226 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013227 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013228 slang = lp->lp_slang;
13229 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13230 /* prepare the hashtable used by add_sound_suggest() */
13231 hash_init(&slang->sl_sounddone);
13232 }
13233}
13234
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013235/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013237 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013238 */
13239 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013240suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013241{
13242 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013243 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013244 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013245 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013246
Bram Moolenaar4770d092006-01-12 23:22:24 +000013247 /* Do this for all languages that support sound folding and for which a
13248 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013249 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013250 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013251 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013252 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013253 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013254 {
13255 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013256 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013257
Bram Moolenaar4770d092006-01-12 23:22:24 +000013258 /* try all kinds of inserts/deletes/swaps/etc. */
13259 /* TODO: also soundfold the next words, so that we can try joining
13260 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010013261#ifdef SUGGEST_PROFILE
13262 prof_init();
13263#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000013264 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010013265#ifdef SUGGEST_PROFILE
13266 prof_report("soundalike");
13267#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000013268 }
13269 }
13270}
13271
13272/*
13273 * Finish up after calling suggest_try_soundalike().
13274 */
13275 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013276suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013277{
13278 langp_T *lp;
13279 int lpi;
13280 slang_T *slang;
13281 int todo;
13282 hashitem_T *hi;
13283
13284 /* Do this for all languages that support sound folding and for which a
13285 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013286 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013287 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013288 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013289 slang = lp->lp_slang;
13290 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13291 {
13292 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013293 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013294 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13295 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013296 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013297 vim_free(HI2SFT(hi));
13298 --todo;
13299 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013300
13301 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013302 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013303 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013304 }
13305 }
13306}
13307
13308/*
13309 * A match with a soundfolded word is found. Add the good word(s) that
13310 * produce this soundfolded word.
13311 */
13312 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013313add_sound_suggest(
13314 suginfo_T *su,
13315 char_u *goodword,
13316 int score, /* soundfold score */
13317 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013318{
13319 slang_T *slang = lp->lp_slang; /* language for sound folding */
13320 int sfwordnr;
13321 char_u *nrline;
13322 int orgnr;
13323 char_u theword[MAXWLEN];
13324 int i;
13325 int wlen;
13326 char_u *byts;
13327 idx_T *idxs;
13328 int n;
13329 int wordcount;
13330 int wc;
13331 int goodscore;
13332 hash_T hash;
13333 hashitem_T *hi;
13334 sftword_T *sft;
13335 int bc, gc;
13336 int limit;
13337
13338 /*
13339 * It's very well possible that the same soundfold word is found several
13340 * times with different scores. Since the following is quite slow only do
13341 * the words that have a better score than before. Use a hashtable to
13342 * remember the words that have been done.
13343 */
13344 hash = hash_hash(goodword);
13345 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13346 if (HASHITEM_EMPTY(hi))
13347 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013348 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13349 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013350 if (sft != NULL)
13351 {
13352 sft->sft_score = score;
13353 STRCPY(sft->sft_word, goodword);
13354 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13355 }
13356 }
13357 else
13358 {
13359 sft = HI2SFT(hi);
13360 if (score >= sft->sft_score)
13361 return;
13362 sft->sft_score = score;
13363 }
13364
13365 /*
13366 * Find the word nr in the soundfold tree.
13367 */
13368 sfwordnr = soundfold_find(slang, goodword);
13369 if (sfwordnr < 0)
13370 {
13371 EMSG2(_(e_intern2), "add_sound_suggest()");
13372 return;
13373 }
13374
13375 /*
13376 * go over the list of good words that produce this soundfold word
13377 */
13378 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13379 orgnr = 0;
13380 while (*nrline != NUL)
13381 {
13382 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13383 * previous wordnr. */
13384 orgnr += bytes2offset(&nrline);
13385
13386 byts = slang->sl_fbyts;
13387 idxs = slang->sl_fidxs;
13388
13389 /* Lookup the word "orgnr" one of the two tries. */
13390 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013391 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013392 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013393 {
13394 i = 1;
13395 if (wordcount == orgnr && byts[n + 1] == NUL)
13396 break; /* found end of word */
13397
13398 if (byts[n + 1] == NUL)
13399 ++wordcount;
13400
13401 /* skip over the NUL bytes */
13402 for ( ; byts[n + i] == NUL; ++i)
13403 if (i > byts[n]) /* safety check */
13404 {
13405 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013406 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013407 goto badword;
13408 }
13409
13410 /* One of the siblings must have the word. */
13411 for ( ; i < byts[n]; ++i)
13412 {
13413 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13414 if (wordcount + wc > orgnr)
13415 break;
13416 wordcount += wc;
13417 }
13418
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013419 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +000013420 n = idxs[n + i];
13421 }
13422badword:
13423 theword[wlen] = NUL;
13424
13425 /* Go over the possible flags and regions. */
13426 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13427 {
13428 char_u cword[MAXWLEN];
13429 char_u *p;
13430 int flags = (int)idxs[n + i];
13431
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013432 /* Skip words with the NOSUGGEST flag */
13433 if (flags & WF_NOSUGGEST)
13434 continue;
13435
Bram Moolenaar4770d092006-01-12 23:22:24 +000013436 if (flags & WF_KEEPCAP)
13437 {
13438 /* Must find the word in the keep-case tree. */
13439 find_keepcap_word(slang, theword, cword);
13440 p = cword;
13441 }
13442 else
13443 {
13444 flags |= su->su_badflags;
13445 if ((flags & WF_CAPMASK) != 0)
13446 {
13447 /* Need to fix case according to "flags". */
13448 make_case_word(theword, cword, flags);
13449 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013450 }
13451 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013452 p = theword;
13453 }
13454
13455 /* Add the suggestion. */
13456 if (sps_flags & SPS_DOUBLE)
13457 {
13458 /* Add the suggestion if the score isn't too bad. */
13459 if (score <= su->su_maxscore)
13460 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13461 score, 0, FALSE, slang, FALSE);
13462 }
13463 else
13464 {
13465 /* Add a penalty for words in another region. */
13466 if ((flags & WF_REGION)
13467 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13468 goodscore = SCORE_REGION;
13469 else
13470 goodscore = 0;
13471
13472 /* Add a small penalty for changing the first letter from
13473 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013474 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +000013475 * letter is the same, that has already been counted. */
13476 gc = PTR2CHAR(p);
13477 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013479 bc = PTR2CHAR(su->su_badword);
13480 if (!SPELL_ISUPPER(bc)
13481 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13482 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 }
13484
Bram Moolenaar4770d092006-01-12 23:22:24 +000013485 /* Compute the score for the good word. This only does letter
13486 * insert/delete/swap/replace. REP items are not considered,
13487 * which may make the score a bit higher.
13488 * Use a limit for the score to make it work faster. Use
13489 * MAXSCORE(), because RESCORE() will change the score.
13490 * If the limit is very high then the iterative method is
13491 * inefficient, using an array is quicker. */
13492 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13493 if (limit > SCORE_LIMITMAX)
13494 goodscore += spell_edit_score(slang, su->su_badword, p);
13495 else
13496 goodscore += spell_edit_score_limit(slang, su->su_badword,
13497 p, limit);
13498
13499 /* When going over the limit don't bother to do the rest. */
13500 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013502 /* Give a bonus to words seen before. */
13503 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504
Bram Moolenaar4770d092006-01-12 23:22:24 +000013505 /* Add the suggestion if the score isn't too bad. */
13506 goodscore = RESCORE(goodscore, score);
13507 if (goodscore <= su->su_sfmaxscore)
13508 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13509 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 }
13512 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013513 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013514 }
13515}
13516
13517/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013518 * Find word "word" in fold-case tree for "slang" and return the word number.
13519 */
13520 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013521soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013522{
13523 idx_T arridx = 0;
13524 int len;
13525 int wlen = 0;
13526 int c;
13527 char_u *ptr = word;
13528 char_u *byts;
13529 idx_T *idxs;
13530 int wordnr = 0;
13531
13532 byts = slang->sl_sbyts;
13533 idxs = slang->sl_sidxs;
13534
13535 for (;;)
13536 {
13537 /* First byte is the number of possible bytes. */
13538 len = byts[arridx++];
13539
13540 /* If the first possible byte is a zero the word could end here.
13541 * If the word ends we found the word. If not skip the NUL bytes. */
13542 c = ptr[wlen];
13543 if (byts[arridx] == NUL)
13544 {
13545 if (c == NUL)
13546 break;
13547
13548 /* Skip over the zeros, there can be several. */
13549 while (len > 0 && byts[arridx] == NUL)
13550 {
13551 ++arridx;
13552 --len;
13553 }
13554 if (len == 0)
13555 return -1; /* no children, word should have ended here */
13556 ++wordnr;
13557 }
13558
13559 /* If the word ends we didn't find it. */
13560 if (c == NUL)
13561 return -1;
13562
13563 /* Perform a binary search in the list of accepted bytes. */
13564 if (c == TAB) /* <Tab> is handled like <Space> */
13565 c = ' ';
13566 while (byts[arridx] < c)
13567 {
13568 /* The word count is in the first idxs[] entry of the child. */
13569 wordnr += idxs[idxs[arridx]];
13570 ++arridx;
13571 if (--len == 0) /* end of the bytes, didn't find it */
13572 return -1;
13573 }
13574 if (byts[arridx] != c) /* didn't find the byte */
13575 return -1;
13576
13577 /* Continue at the child (if there is one). */
13578 arridx = idxs[arridx];
13579 ++wlen;
13580
13581 /* One space in the good word may stand for several spaces in the
13582 * checked word. */
13583 if (c == ' ')
13584 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13585 ++wlen;
13586 }
13587
13588 return wordnr;
13589}
13590
13591/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013592 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 */
13594 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013595make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013596{
13597 if (flags & WF_ALLCAP)
13598 /* Make it all upper-case */
13599 allcap_copy(fword, cword);
13600 else if (flags & WF_ONECAP)
13601 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013602 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 else
13604 /* Use goodword as-is. */
13605 STRCPY(cword, fword);
13606}
13607
Bram Moolenaarea424162005-06-16 21:51:00 +000013608/*
13609 * Use map string "map" for languages "lp".
13610 */
13611 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013612set_map_str(slang_T *lp, char_u *map)
Bram Moolenaarea424162005-06-16 21:51:00 +000013613{
13614 char_u *p;
13615 int headc = 0;
13616 int c;
13617 int i;
13618
13619 if (*map == NUL)
13620 {
13621 lp->sl_has_map = FALSE;
13622 return;
13623 }
13624 lp->sl_has_map = TRUE;
13625
Bram Moolenaar4770d092006-01-12 23:22:24 +000013626 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013627 for (i = 0; i < 256; ++i)
13628 lp->sl_map_array[i] = 0;
13629#ifdef FEAT_MBYTE
13630 hash_init(&lp->sl_map_hash);
13631#endif
13632
13633 /*
13634 * The similar characters are stored separated with slashes:
13635 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13636 * before the same slash. For characters above 255 sl_map_hash is used.
13637 */
13638 for (p = map; *p != NUL; )
13639 {
13640#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013641 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013642#else
13643 c = *p++;
13644#endif
13645 if (c == '/')
13646 headc = 0;
13647 else
13648 {
13649 if (headc == 0)
13650 headc = c;
13651
13652#ifdef FEAT_MBYTE
13653 /* Characters above 255 don't fit in sl_map_array[], put them in
13654 * the hash table. Each entry is the char, a NUL the headchar and
13655 * a NUL. */
13656 if (c >= 256)
13657 {
13658 int cl = mb_char2len(c);
13659 int headcl = mb_char2len(headc);
13660 char_u *b;
13661 hash_T hash;
13662 hashitem_T *hi;
13663
13664 b = alloc((unsigned)(cl + headcl + 2));
13665 if (b == NULL)
13666 return;
13667 mb_char2bytes(c, b);
13668 b[cl] = NUL;
13669 mb_char2bytes(headc, b + cl + 1);
13670 b[cl + 1 + headcl] = NUL;
13671 hash = hash_hash(b);
13672 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13673 if (HASHITEM_EMPTY(hi))
13674 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13675 else
13676 {
13677 /* This should have been checked when generating the .spl
13678 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013679 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013680 vim_free(b);
13681 }
13682 }
13683 else
13684#endif
13685 lp->sl_map_array[c] = headc;
13686 }
13687 }
13688}
13689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690/*
13691 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13692 * lines in the .aff file.
13693 */
13694 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013695similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696{
Bram Moolenaarea424162005-06-16 21:51:00 +000013697 int m1, m2;
13698#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +020013699 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +000013700 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701
Bram Moolenaarea424162005-06-16 21:51:00 +000013702 if (c1 >= 256)
13703 {
13704 buf[mb_char2bytes(c1, buf)] = 0;
13705 hi = hash_find(&slang->sl_map_hash, buf);
13706 if (HASHITEM_EMPTY(hi))
13707 m1 = 0;
13708 else
13709 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13710 }
13711 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013712#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013713 m1 = slang->sl_map_array[c1];
13714 if (m1 == 0)
13715 return FALSE;
13716
13717
13718#ifdef FEAT_MBYTE
13719 if (c2 >= 256)
13720 {
13721 buf[mb_char2bytes(c2, buf)] = 0;
13722 hi = hash_find(&slang->sl_map_hash, buf);
13723 if (HASHITEM_EMPTY(hi))
13724 m2 = 0;
13725 else
13726 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13727 }
13728 else
13729#endif
13730 m2 = slang->sl_map_array[c2];
13731
13732 return m1 == m2;
13733}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734
13735/*
13736 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013737 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 */
13739 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013740add_suggestion(
13741 suginfo_T *su,
13742 garray_T *gap, /* either su_ga or su_sga */
13743 char_u *goodword,
13744 int badlenarg, /* len of bad word replaced with "goodword" */
13745 int score,
13746 int altscore,
13747 int had_bonus, /* value for st_had_bonus */
13748 slang_T *slang, /* language for sound folding */
13749 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +000013750 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013752 int goodlen; /* len of goodword changed */
13753 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013755 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013756 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013757 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013758
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013759 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13760 * "thee the" is added next to changing the first "the" the "thee". */
13761 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013762 pbad = su->su_badptr + badlenarg;
13763 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013765 goodlen = (int)(pgood - goodword);
13766 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013767 if (goodlen <= 0 || badlen <= 0)
13768 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013769 mb_ptr_back(goodword, pgood);
13770 mb_ptr_back(su->su_badptr, pbad);
13771#ifdef FEAT_MBYTE
13772 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013773 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013774 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13775 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013776 }
13777 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013778#endif
13779 if (*pgood != *pbad)
13780 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013781 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013782
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013783 if (badlen == 0 && goodlen == 0)
13784 /* goodword doesn't change anything; may happen for "the the" changing
13785 * the first "the" to itself. */
13786 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013787
Bram Moolenaar89d40322006-08-29 15:30:07 +000013788 if (gap->ga_len == 0)
13789 i = -1;
13790 else
13791 {
13792 /* Check if the word is already there. Also check the length that is
13793 * being replaced "thes," -> "these" is a different suggestion from
13794 * "thes" -> "these". */
13795 stp = &SUG(*gap, 0);
13796 for (i = gap->ga_len; --i >= 0; ++stp)
13797 if (stp->st_wordlen == goodlen
13798 && stp->st_orglen == badlen
13799 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013801 /*
13802 * Found it. Remember the word with the lowest score.
13803 */
13804 if (stp->st_slang == NULL)
13805 stp->st_slang = slang;
13806
13807 new_sug.st_score = score;
13808 new_sug.st_altscore = altscore;
13809 new_sug.st_had_bonus = had_bonus;
13810
13811 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013812 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013813 /* Only one of the two had the soundalike score computed.
13814 * Need to do that for the other one now, otherwise the
13815 * scores can't be compared. This happens because
13816 * suggest_try_change() doesn't compute the soundalike
13817 * word to keep it fast, while some special methods set
13818 * the soundalike score to zero. */
13819 if (had_bonus)
13820 rescore_one(su, stp);
13821 else
13822 {
13823 new_sug.st_word = stp->st_word;
13824 new_sug.st_wordlen = stp->st_wordlen;
13825 new_sug.st_slang = stp->st_slang;
13826 new_sug.st_orglen = badlen;
13827 rescore_one(su, &new_sug);
13828 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013829 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013830
Bram Moolenaar89d40322006-08-29 15:30:07 +000013831 if (stp->st_score > new_sug.st_score)
13832 {
13833 stp->st_score = new_sug.st_score;
13834 stp->st_altscore = new_sug.st_altscore;
13835 stp->st_had_bonus = new_sug.st_had_bonus;
13836 }
13837 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013838 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013839 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840
Bram Moolenaar4770d092006-01-12 23:22:24 +000013841 if (i < 0 && ga_grow(gap, 1) == OK)
13842 {
13843 /* Add a suggestion. */
13844 stp = &SUG(*gap, gap->ga_len);
13845 stp->st_word = vim_strnsave(goodword, goodlen);
13846 if (stp->st_word != NULL)
13847 {
13848 stp->st_wordlen = goodlen;
13849 stp->st_score = score;
13850 stp->st_altscore = altscore;
13851 stp->st_had_bonus = had_bonus;
13852 stp->st_orglen = badlen;
13853 stp->st_slang = slang;
13854 ++gap->ga_len;
13855
13856 /* If we have too many suggestions now, sort the list and keep
13857 * the best suggestions. */
13858 if (gap->ga_len > SUG_MAX_COUNT(su))
13859 {
13860 if (maxsf)
13861 su->su_sfmaxscore = cleanup_suggestions(gap,
13862 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13863 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013864 su->su_maxscore = cleanup_suggestions(gap,
13865 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013866 }
13867 }
13868 }
13869}
13870
13871/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013872 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13873 * for split words, such as "the the". Remove these from the list here.
13874 */
13875 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013876check_suggestions(
13877 suginfo_T *su,
13878 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013879{
13880 suggest_T *stp;
13881 int i;
13882 char_u longword[MAXWLEN + 1];
13883 int len;
13884 hlf_T attr;
13885
13886 stp = &SUG(*gap, 0);
13887 for (i = gap->ga_len - 1; i >= 0; --i)
13888 {
13889 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013890 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013891 len = stp[i].st_wordlen;
13892 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13893 MAXWLEN - len);
13894 attr = HLF_COUNT;
13895 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13896 if (attr != HLF_COUNT)
13897 {
13898 /* Remove this entry. */
13899 vim_free(stp[i].st_word);
13900 --gap->ga_len;
13901 if (i < gap->ga_len)
13902 mch_memmove(stp + i, stp + i + 1,
13903 sizeof(suggest_T) * (gap->ga_len - i));
13904 }
13905 }
13906}
13907
13908
13909/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013910 * Add a word to be banned.
13911 */
13912 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013913add_banned(
13914 suginfo_T *su,
13915 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013917 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013918 hash_T hash;
13919 hashitem_T *hi;
13920
Bram Moolenaar4770d092006-01-12 23:22:24 +000013921 hash = hash_hash(word);
13922 hi = hash_lookup(&su->su_banned, word, hash);
13923 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013924 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013925 s = vim_strsave(word);
13926 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013927 hash_add_item(&su->su_banned, hi, s, hash);
13928 }
13929}
13930
13931/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013932 * Recompute the score for all suggestions if sound-folding is possible. This
13933 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013934 */
13935 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013936rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013937{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013938 int i;
13939
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013940 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013941 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013942 rescore_one(su, &SUG(su->su_ga, i));
13943}
13944
13945/*
13946 * Recompute the score for one suggestion if sound-folding is possible.
13947 */
13948 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013949rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013950{
13951 slang_T *slang = stp->st_slang;
13952 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013953 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013954
13955 /* Only rescore suggestions that have no sal score yet and do have a
13956 * language. */
13957 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13958 {
13959 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013960 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013961 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013962 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013963 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013964 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013965 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013966
13967 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013968 if (stp->st_altscore == SCORE_MAXMAX)
13969 stp->st_altscore = SCORE_BIG;
13970 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13971 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013972 }
13973}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975static int
13976#ifdef __BORLANDC__
13977_RTLENTRYF
13978#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010013979sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980
13981/*
13982 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013983 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013984 */
13985 static int
13986#ifdef __BORLANDC__
13987_RTLENTRYF
13988#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013989sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990{
13991 suggest_T *p1 = (suggest_T *)s1;
13992 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013993 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013995 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013996 {
13997 n = p1->st_altscore - p2->st_altscore;
13998 if (n == 0)
13999 n = STRICMP(p1->st_word, p2->st_word);
14000 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014001 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002}
14003
14004/*
14005 * Cleanup the suggestions:
14006 * - Sort on score.
14007 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014008 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014010 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014011cleanup_suggestions(
14012 garray_T *gap,
14013 int maxscore,
14014 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014016 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014017 int i;
14018
14019 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014020 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021
14022 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014023 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014025 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014027 gap->ga_len = keep;
14028 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014030 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031}
14032
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014033#if defined(FEAT_EVAL) || defined(PROTO)
14034/*
14035 * Soundfold a string, for soundfold().
14036 * Result is in allocated memory, NULL for an error.
14037 */
14038 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014039eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014040{
14041 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014042 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014043 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014044
Bram Moolenaar860cae12010-06-05 23:22:07 +020014045 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014046 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014047 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014048 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014049 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014050 if (lp->lp_slang->sl_sal.ga_len > 0)
14051 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014052 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014053 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014054 return vim_strsave(sound);
14055 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014056 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014057
14058 /* No language with sound folding, return word as-is. */
14059 return vim_strsave(word);
14060}
14061#endif
14062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063/*
14064 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014065 *
14066 * There are many ways to turn a word into a sound-a-like representation. The
14067 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14068 * swedish name matching - survey and test of different algorithms" by Klas
14069 * Erikson.
14070 *
14071 * We support two methods:
14072 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14073 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014074 */
14075 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014076spell_soundfold(
14077 slang_T *slang,
14078 char_u *inword,
14079 int folded, /* "inword" is already case-folded */
14080 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014081{
14082 char_u fword[MAXWLEN];
14083 char_u *word;
14084
14085 if (slang->sl_sofo)
14086 /* SOFOFROM and SOFOTO used */
14087 spell_soundfold_sofo(slang, inword, res);
14088 else
14089 {
14090 /* SAL items used. Requires the word to be case-folded. */
14091 if (folded)
14092 word = inword;
14093 else
14094 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014095 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014096 word = fword;
14097 }
14098
14099#ifdef FEAT_MBYTE
14100 if (has_mbyte)
14101 spell_soundfold_wsal(slang, word, res);
14102 else
14103#endif
14104 spell_soundfold_sal(slang, word, res);
14105 }
14106}
14107
14108/*
14109 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14110 * SOFOTO lines.
14111 */
14112 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014113spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014114{
14115 char_u *s;
14116 int ri = 0;
14117 int c;
14118
14119#ifdef FEAT_MBYTE
14120 if (has_mbyte)
14121 {
14122 int prevc = 0;
14123 int *ip;
14124
14125 /* The sl_sal_first[] table contains the translation for chars up to
14126 * 255, sl_sal the rest. */
14127 for (s = inword; *s != NUL; )
14128 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014129 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014130 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14131 c = ' ';
14132 else if (c < 256)
14133 c = slang->sl_sal_first[c];
14134 else
14135 {
14136 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14137 if (ip == NULL) /* empty list, can't match */
14138 c = NUL;
14139 else
14140 for (;;) /* find "c" in the list */
14141 {
14142 if (*ip == 0) /* not found */
14143 {
14144 c = NUL;
14145 break;
14146 }
14147 if (*ip == c) /* match! */
14148 {
14149 c = ip[1];
14150 break;
14151 }
14152 ip += 2;
14153 }
14154 }
14155
14156 if (c != NUL && c != prevc)
14157 {
14158 ri += mb_char2bytes(c, res + ri);
14159 if (ri + MB_MAXBYTES > MAXWLEN)
14160 break;
14161 prevc = c;
14162 }
14163 }
14164 }
14165 else
14166#endif
14167 {
14168 /* The sl_sal_first[] table contains the translation. */
14169 for (s = inword; (c = *s) != NUL; ++s)
14170 {
14171 if (vim_iswhite(c))
14172 c = ' ';
14173 else
14174 c = slang->sl_sal_first[c];
14175 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14176 res[ri++] = c;
14177 }
14178 }
14179
14180 res[ri] = NUL;
14181}
14182
14183 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014184spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014186 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014188 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014190 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014192 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 int n, k = 0;
14194 int z0;
14195 int k0;
14196 int n0;
14197 int c;
14198 int pri;
14199 int p0 = -333;
14200 int c0;
14201
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014202 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014203 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 if (slang->sl_rem_accents)
14205 {
14206 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014207 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014209 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014210 {
14211 *t++ = ' ';
14212 s = skipwhite(s);
14213 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014214 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014216 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 *t++ = *s;
14218 ++s;
14219 }
14220 }
14221 *t = NUL;
14222 }
14223 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014224 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227
14228 /*
14229 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014230 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 while ((c = word[i]) != NUL)
14234 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014235 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 n = slang->sl_sal_first[c];
14237 z0 = 0;
14238
14239 if (n >= 0)
14240 {
14241 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014242 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014244 /* Quickly skip entries that don't match the word. Most
14245 * entries are less then three chars, optimize for that. */
14246 k = smp[n].sm_leadlen;
14247 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014249 if (word[i + 1] != s[1])
14250 continue;
14251 if (k > 2)
14252 {
14253 for (j = 2; j < k; ++j)
14254 if (word[i + j] != s[j])
14255 break;
14256 if (j < k)
14257 continue;
14258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 }
14260
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014261 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014263 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014264 while (*pf != NUL && *pf != word[i + k])
14265 ++pf;
14266 if (*pf == NUL)
14267 continue;
14268 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014270 s = smp[n].sm_rules;
14271 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272
14273 p0 = *s;
14274 k0 = k;
14275 while (*s == '-' && k > 1)
14276 {
14277 k--;
14278 s++;
14279 }
14280 if (*s == '<')
14281 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014282 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 {
14284 /* determine priority */
14285 pri = *s - '0';
14286 s++;
14287 }
14288 if (*s == '^' && *(s + 1) == '^')
14289 s++;
14290
14291 if (*s == NUL
14292 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014293 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014294 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014296 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014298 && spell_iswordp(word + i - 1, curwin)
14299 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 {
14301 /* search for followup rules, if: */
14302 /* followup and k > 1 and NO '-' in searchstring */
14303 c0 = word[i + k - 1];
14304 n0 = slang->sl_sal_first[c0];
14305
14306 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 {
14309 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014310 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014312 /* Quickly skip entries that don't match the word.
14313 * */
14314 k0 = smp[n0].sm_leadlen;
14315 if (k0 > 1)
14316 {
14317 if (word[i + k] != s[1])
14318 continue;
14319 if (k0 > 2)
14320 {
14321 pf = word + i + k + 1;
14322 for (j = 2; j < k0; ++j)
14323 if (*pf++ != s[j])
14324 break;
14325 if (j < k0)
14326 continue;
14327 }
14328 }
14329 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014331 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014332 {
14333 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014334 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014335 while (*pf != NUL && *pf != word[i + k0])
14336 ++pf;
14337 if (*pf == NUL)
14338 continue;
14339 ++k0;
14340 }
14341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014343 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 while (*s == '-')
14345 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014346 /* "k0" gets NOT reduced because
14347 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 s++;
14349 }
14350 if (*s == '<')
14351 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014352 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 {
14354 p0 = *s - '0';
14355 s++;
14356 }
14357
14358 if (*s == NUL
14359 /* *s == '^' cuts */
14360 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014361 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014362 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 {
14364 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014365 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367
14368 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 /* rule fits; stop search */
14372 break;
14373 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 }
14375
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 }
14379
14380 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014381 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014382 if (s == NULL)
14383 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014384 pf = smp[n].sm_rules;
14385 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 if (p0 == 1 && z == 0)
14387 {
14388 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14390 || res[reslen - 1] == *s))
14391 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 z0 = 1;
14393 z = 1;
14394 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014395 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 {
14397 word[i + k0] = *s;
14398 k0++;
14399 s++;
14400 }
14401 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014402 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014403
14404 /* new "actual letter" */
14405 c = word[i];
14406 }
14407 else
14408 {
14409 /* no '<' rule used */
14410 i += k - 1;
14411 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014412 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014414 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014415 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 s++;
14417 }
14418 /* new "actual letter" */
14419 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014420 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 {
14422 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014423 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014424 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 i = 0;
14426 z0 = 1;
14427 }
14428 }
14429 break;
14430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 }
14432 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014433 else if (vim_iswhite(c))
14434 {
14435 c = ' ';
14436 k = 1;
14437 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438
14439 if (z0 == 0)
14440 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014441 if (k && !p0 && reslen < MAXWLEN && c != NUL
14442 && (!slang->sl_collapse || reslen == 0
14443 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014445 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446
14447 i++;
14448 z = 0;
14449 k = 0;
14450 }
14451 }
14452
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014453 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454}
14455
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014456#ifdef FEAT_MBYTE
14457/*
14458 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14459 * Multi-byte version of spell_soundfold().
14460 */
14461 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014462spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014463{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014464 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014465 int word[MAXWLEN];
14466 int wres[MAXWLEN];
14467 int l;
14468 char_u *s;
14469 int *ws;
14470 char_u *t;
14471 int *pf;
14472 int i, j, z;
14473 int reslen;
14474 int n, k = 0;
14475 int z0;
14476 int k0;
14477 int n0;
14478 int c;
14479 int pri;
14480 int p0 = -333;
14481 int c0;
14482 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014483 int wordlen;
14484
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014485
14486 /*
14487 * Convert the multi-byte string to a wide-character string.
14488 * Remove accents, if wanted. We actually remove all non-word characters.
14489 * But keep white space.
14490 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014491 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014492 for (s = inword; *s != NUL; )
14493 {
14494 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014495 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014496 if (slang->sl_rem_accents)
14497 {
14498 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14499 {
14500 if (did_white)
14501 continue;
14502 c = ' ';
14503 did_white = TRUE;
14504 }
14505 else
14506 {
14507 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014508 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014509 continue;
14510 }
14511 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014512 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014513 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014514 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014515
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014516 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014517 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014518 * Converted from C++ to C. Added support for multi-byte chars.
14519 * Changed to keep spaces.
14520 */
14521 i = reslen = z = 0;
14522 while ((c = word[i]) != NUL)
14523 {
14524 /* Start with the first rule that has the character in the word. */
14525 n = slang->sl_sal_first[c & 0xff];
14526 z0 = 0;
14527
14528 if (n >= 0)
14529 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014530 /* Check all rules for the same index byte.
14531 * If c is 0x300 need extra check for the end of the array, as
14532 * (c & 0xff) is NUL. */
14533 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14534 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014535 {
14536 /* Quickly skip entries that don't match the word. Most
14537 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014538 if (c != ws[0])
14539 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014540 k = smp[n].sm_leadlen;
14541 if (k > 1)
14542 {
14543 if (word[i + 1] != ws[1])
14544 continue;
14545 if (k > 2)
14546 {
14547 for (j = 2; j < k; ++j)
14548 if (word[i + j] != ws[j])
14549 break;
14550 if (j < k)
14551 continue;
14552 }
14553 }
14554
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014555 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014556 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014557 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014558 while (*pf != NUL && *pf != word[i + k])
14559 ++pf;
14560 if (*pf == NUL)
14561 continue;
14562 ++k;
14563 }
14564 s = smp[n].sm_rules;
14565 pri = 5; /* default priority */
14566
14567 p0 = *s;
14568 k0 = k;
14569 while (*s == '-' && k > 1)
14570 {
14571 k--;
14572 s++;
14573 }
14574 if (*s == '<')
14575 s++;
14576 if (VIM_ISDIGIT(*s))
14577 {
14578 /* determine priority */
14579 pri = *s - '0';
14580 s++;
14581 }
14582 if (*s == '^' && *(s + 1) == '^')
14583 s++;
14584
14585 if (*s == NUL
14586 || (*s == '^'
14587 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014588 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014589 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014590 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014591 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014592 && spell_iswordp_w(word + i - 1, curwin)
14593 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014594 {
14595 /* search for followup rules, if: */
14596 /* followup and k > 1 and NO '-' in searchstring */
14597 c0 = word[i + k - 1];
14598 n0 = slang->sl_sal_first[c0 & 0xff];
14599
14600 if (slang->sl_followup && k > 1 && n0 >= 0
14601 && p0 != '-' && word[i + k] != NUL)
14602 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014603 /* Test follow-up rule for "word[i + k]"; loop over
14604 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014605 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14606 == (c0 & 0xff); ++n0)
14607 {
14608 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014609 */
14610 if (c0 != ws[0])
14611 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014612 k0 = smp[n0].sm_leadlen;
14613 if (k0 > 1)
14614 {
14615 if (word[i + k] != ws[1])
14616 continue;
14617 if (k0 > 2)
14618 {
14619 pf = word + i + k + 1;
14620 for (j = 2; j < k0; ++j)
14621 if (*pf++ != ws[j])
14622 break;
14623 if (j < k0)
14624 continue;
14625 }
14626 }
14627 k0 += k - 1;
14628
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014629 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014630 {
14631 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014632 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014633 while (*pf != NUL && *pf != word[i + k0])
14634 ++pf;
14635 if (*pf == NUL)
14636 continue;
14637 ++k0;
14638 }
14639
14640 p0 = 5;
14641 s = smp[n0].sm_rules;
14642 while (*s == '-')
14643 {
14644 /* "k0" gets NOT reduced because
14645 * "if (k0 == k)" */
14646 s++;
14647 }
14648 if (*s == '<')
14649 s++;
14650 if (VIM_ISDIGIT(*s))
14651 {
14652 p0 = *s - '0';
14653 s++;
14654 }
14655
14656 if (*s == NUL
14657 /* *s == '^' cuts */
14658 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014659 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014660 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014661 {
14662 if (k0 == k)
14663 /* this is just a piece of the string */
14664 continue;
14665
14666 if (p0 < pri)
14667 /* priority too low */
14668 continue;
14669 /* rule fits; stop search */
14670 break;
14671 }
14672 }
14673
14674 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14675 == (c0 & 0xff))
14676 continue;
14677 }
14678
14679 /* replace string */
14680 ws = smp[n].sm_to_w;
14681 s = smp[n].sm_rules;
14682 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14683 if (p0 == 1 && z == 0)
14684 {
14685 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014686 if (reslen > 0 && ws != NULL && *ws != NUL
14687 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014688 || wres[reslen - 1] == *ws))
14689 reslen--;
14690 z0 = 1;
14691 z = 1;
14692 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014693 if (ws != NULL)
14694 while (*ws != NUL && word[i + k0] != NUL)
14695 {
14696 word[i + k0] = *ws;
14697 k0++;
14698 ws++;
14699 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014700 if (k > k0)
14701 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014702 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014703
14704 /* new "actual letter" */
14705 c = word[i];
14706 }
14707 else
14708 {
14709 /* no '<' rule used */
14710 i += k - 1;
14711 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014712 if (ws != NULL)
14713 while (*ws != NUL && ws[1] != NUL
14714 && reslen < MAXWLEN)
14715 {
14716 if (reslen == 0 || wres[reslen - 1] != *ws)
14717 wres[reslen++] = *ws;
14718 ws++;
14719 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014720 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014721 if (ws == NULL)
14722 c = NUL;
14723 else
14724 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014725 if (strstr((char *)s, "^^") != NULL)
14726 {
14727 if (c != NUL)
14728 wres[reslen++] = c;
14729 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014730 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014731 i = 0;
14732 z0 = 1;
14733 }
14734 }
14735 break;
14736 }
14737 }
14738 }
14739 else if (vim_iswhite(c))
14740 {
14741 c = ' ';
14742 k = 1;
14743 }
14744
14745 if (z0 == 0)
14746 {
14747 if (k && !p0 && reslen < MAXWLEN && c != NUL
14748 && (!slang->sl_collapse || reslen == 0
14749 || wres[reslen - 1] != c))
14750 /* condense only double letters */
14751 wres[reslen++] = c;
14752
14753 i++;
14754 z = 0;
14755 k = 0;
14756 }
14757 }
14758
14759 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14760 l = 0;
14761 for (n = 0; n < reslen; ++n)
14762 {
14763 l += mb_char2bytes(wres[n], res + l);
14764 if (l + MB_MAXBYTES > MAXWLEN)
14765 break;
14766 }
14767 res[l] = NUL;
14768}
14769#endif
14770
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014771/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014772 * Compute a score for two sound-a-like words.
14773 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14774 * Instead of a generic loop we write out the code. That keeps it fast by
14775 * avoiding checks that will not be possible.
14776 */
14777 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014778soundalike_score(
14779 char_u *goodstart, /* sound-folded good word */
14780 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014782 char_u *goodsound = goodstart;
14783 char_u *badsound = badstart;
14784 int goodlen;
14785 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014786 int n;
14787 char_u *pl, *ps;
14788 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014789 int score = 0;
14790
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014791 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014792 * counted so much, vowels halfway the word aren't counted at all. */
14793 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14794 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014795 if ((badsound[0] == NUL && goodsound[1] == NUL)
14796 || (goodsound[0] == NUL && badsound[1] == NUL))
14797 /* changing word with vowel to word without a sound */
14798 return SCORE_DEL;
14799 if (badsound[0] == NUL || goodsound[0] == NUL)
14800 /* more than two changes */
14801 return SCORE_MAXMAX;
14802
Bram Moolenaar4770d092006-01-12 23:22:24 +000014803 if (badsound[1] == goodsound[1]
14804 || (badsound[1] != NUL
14805 && goodsound[1] != NUL
14806 && badsound[2] == goodsound[2]))
14807 {
14808 /* handle like a substitute */
14809 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014810 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014811 {
14812 score = 2 * SCORE_DEL / 3;
14813 if (*badsound == '*')
14814 ++badsound;
14815 else
14816 ++goodsound;
14817 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014818 }
14819
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014820 goodlen = (int)STRLEN(goodsound);
14821 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014822
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014823 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014824 * changes. */
14825 n = goodlen - badlen;
14826 if (n < -2 || n > 2)
14827 return SCORE_MAXMAX;
14828
14829 if (n > 0)
14830 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014831 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014832 ps = badsound;
14833 }
14834 else
14835 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014836 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014837 ps = goodsound;
14838 }
14839
14840 /* Skip over the identical part. */
14841 while (*pl == *ps && *pl != NUL)
14842 {
14843 ++pl;
14844 ++ps;
14845 }
14846
14847 switch (n)
14848 {
14849 case -2:
14850 case 2:
14851 /*
14852 * Must delete two characters from "pl".
14853 */
14854 ++pl; /* first delete */
14855 while (*pl == *ps)
14856 {
14857 ++pl;
14858 ++ps;
14859 }
14860 /* strings must be equal after second delete */
14861 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014862 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014863
14864 /* Failed to compare. */
14865 break;
14866
14867 case -1:
14868 case 1:
14869 /*
14870 * Minimal one delete from "pl" required.
14871 */
14872
14873 /* 1: delete */
14874 pl2 = pl + 1;
14875 ps2 = ps;
14876 while (*pl2 == *ps2)
14877 {
14878 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014879 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014880 ++pl2;
14881 ++ps2;
14882 }
14883
14884 /* 2: delete then swap, then rest must be equal */
14885 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14886 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014887 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014888
14889 /* 3: delete then substitute, then the rest must be equal */
14890 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014891 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014892
14893 /* 4: first swap then delete */
14894 if (pl[0] == ps[1] && pl[1] == ps[0])
14895 {
14896 pl2 = pl + 2; /* swap, skip two chars */
14897 ps2 = ps + 2;
14898 while (*pl2 == *ps2)
14899 {
14900 ++pl2;
14901 ++ps2;
14902 }
14903 /* delete a char and then strings must be equal */
14904 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014905 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014906 }
14907
14908 /* 5: first substitute then delete */
14909 pl2 = pl + 1; /* substitute, skip one char */
14910 ps2 = ps + 1;
14911 while (*pl2 == *ps2)
14912 {
14913 ++pl2;
14914 ++ps2;
14915 }
14916 /* delete a char and then strings must be equal */
14917 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014918 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014919
14920 /* Failed to compare. */
14921 break;
14922
14923 case 0:
14924 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014925 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014926 * insert is only possible in combination with a delete.
14927 * 1: check if for identical strings
14928 */
14929 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014930 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014931
14932 /* 2: swap */
14933 if (pl[0] == ps[1] && pl[1] == ps[0])
14934 {
14935 pl2 = pl + 2; /* swap, skip two chars */
14936 ps2 = ps + 2;
14937 while (*pl2 == *ps2)
14938 {
14939 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014940 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014941 ++pl2;
14942 ++ps2;
14943 }
14944 /* 3: swap and swap again */
14945 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14946 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014947 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014948
14949 /* 4: swap and substitute */
14950 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014951 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014952 }
14953
14954 /* 5: substitute */
14955 pl2 = pl + 1;
14956 ps2 = ps + 1;
14957 while (*pl2 == *ps2)
14958 {
14959 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014960 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014961 ++pl2;
14962 ++ps2;
14963 }
14964
14965 /* 6: substitute and swap */
14966 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14967 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014968 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014969
14970 /* 7: substitute and substitute */
14971 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014972 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014973
14974 /* 8: insert then delete */
14975 pl2 = pl;
14976 ps2 = ps + 1;
14977 while (*pl2 == *ps2)
14978 {
14979 ++pl2;
14980 ++ps2;
14981 }
14982 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014983 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014984
14985 /* 9: delete then insert */
14986 pl2 = pl + 1;
14987 ps2 = ps;
14988 while (*pl2 == *ps2)
14989 {
14990 ++pl2;
14991 ++ps2;
14992 }
14993 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014994 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014995
14996 /* Failed to compare. */
14997 break;
14998 }
14999
15000 return SCORE_MAXMAX;
15001}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015003/*
15004 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015005 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015006 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015007 * The algorithm is described by Du and Chang, 1992.
15008 * The implementation of the algorithm comes from Aspell editdist.cpp,
15009 * edit_distance(). It has been converted from C++ to C and modified to
15010 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015011 */
15012 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015013spell_edit_score(
15014 slang_T *slang,
15015 char_u *badword,
15016 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015017{
15018 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015019 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015020 int j, i;
15021 int t;
15022 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015023 int pbc, pgc;
15024#ifdef FEAT_MBYTE
15025 char_u *p;
15026 int wbadword[MAXWLEN];
15027 int wgoodword[MAXWLEN];
15028
15029 if (has_mbyte)
15030 {
15031 /* Get the characters from the multi-byte strings and put them in an
15032 * int array for easy access. */
15033 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015034 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015035 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015036 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015037 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015038 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015039 }
15040 else
15041#endif
15042 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015043 badlen = (int)STRLEN(badword) + 1;
15044 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046
15047 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15048#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15050 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015051 if (cnt == NULL)
15052 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015053
15054 CNT(0, 0) = 0;
15055 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015056 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015057
15058 for (i = 1; i <= badlen; ++i)
15059 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015060 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 for (j = 1; j <= goodlen; ++j)
15062 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015063#ifdef FEAT_MBYTE
15064 if (has_mbyte)
15065 {
15066 bc = wbadword[i - 1];
15067 gc = wgoodword[j - 1];
15068 }
15069 else
15070#endif
15071 {
15072 bc = badword[i - 1];
15073 gc = goodword[j - 1];
15074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015075 if (bc == gc)
15076 CNT(i, j) = CNT(i - 1, j - 1);
15077 else
15078 {
15079 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015080 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015081 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15082 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015083 {
15084 /* For a similar character use SCORE_SIMILAR. */
15085 if (slang != NULL
15086 && slang->sl_has_map
15087 && similar_chars(slang, gc, bc))
15088 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15089 else
15090 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015093 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015094 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015095#ifdef FEAT_MBYTE
15096 if (has_mbyte)
15097 {
15098 pbc = wbadword[i - 2];
15099 pgc = wgoodword[j - 2];
15100 }
15101 else
15102#endif
15103 {
15104 pbc = badword[i - 2];
15105 pgc = goodword[j - 2];
15106 }
15107 if (bc == pgc && pbc == gc)
15108 {
15109 t = SCORE_SWAP + CNT(i - 2, j - 2);
15110 if (t < CNT(i, j))
15111 CNT(i, j) = t;
15112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015113 }
15114 t = SCORE_DEL + CNT(i - 1, j);
15115 if (t < CNT(i, j))
15116 CNT(i, j) = t;
15117 t = SCORE_INS + CNT(i, j - 1);
15118 if (t < CNT(i, j))
15119 CNT(i, j) = t;
15120 }
15121 }
15122 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015123
15124 i = CNT(badlen - 1, goodlen - 1);
15125 vim_free(cnt);
15126 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015127}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015128
Bram Moolenaar4770d092006-01-12 23:22:24 +000015129typedef struct
15130{
15131 int badi;
15132 int goodi;
15133 int score;
15134} limitscore_T;
15135
15136/*
15137 * Like spell_edit_score(), but with a limit on the score to make it faster.
15138 * May return SCORE_MAXMAX when the score is higher than "limit".
15139 *
15140 * This uses a stack for the edits still to be tried.
15141 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15142 * for multi-byte characters.
15143 */
15144 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015145spell_edit_score_limit(
15146 slang_T *slang,
15147 char_u *badword,
15148 char_u *goodword,
15149 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015150{
15151 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15152 int stackidx;
15153 int bi, gi;
15154 int bi2, gi2;
15155 int bc, gc;
15156 int score;
15157 int score_off;
15158 int minscore;
15159 int round;
15160
15161#ifdef FEAT_MBYTE
15162 /* Multi-byte characters require a bit more work, use a different function
15163 * to avoid testing "has_mbyte" quite often. */
15164 if (has_mbyte)
15165 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15166#endif
15167
15168 /*
15169 * The idea is to go from start to end over the words. So long as
15170 * characters are equal just continue, this always gives the lowest score.
15171 * When there is a difference try several alternatives. Each alternative
15172 * increases "score" for the edit distance. Some of the alternatives are
15173 * pushed unto a stack and tried later, some are tried right away. At the
15174 * end of the word the score for one alternative is known. The lowest
15175 * possible score is stored in "minscore".
15176 */
15177 stackidx = 0;
15178 bi = 0;
15179 gi = 0;
15180 score = 0;
15181 minscore = limit + 1;
15182
15183 for (;;)
15184 {
15185 /* Skip over an equal part, score remains the same. */
15186 for (;;)
15187 {
15188 bc = badword[bi];
15189 gc = goodword[gi];
15190 if (bc != gc) /* stop at a char that's different */
15191 break;
15192 if (bc == NUL) /* both words end */
15193 {
15194 if (score < minscore)
15195 minscore = score;
15196 goto pop; /* do next alternative */
15197 }
15198 ++bi;
15199 ++gi;
15200 }
15201
15202 if (gc == NUL) /* goodword ends, delete badword chars */
15203 {
15204 do
15205 {
15206 if ((score += SCORE_DEL) >= minscore)
15207 goto pop; /* do next alternative */
15208 } while (badword[++bi] != NUL);
15209 minscore = score;
15210 }
15211 else if (bc == NUL) /* badword ends, insert badword chars */
15212 {
15213 do
15214 {
15215 if ((score += SCORE_INS) >= minscore)
15216 goto pop; /* do next alternative */
15217 } while (goodword[++gi] != NUL);
15218 minscore = score;
15219 }
15220 else /* both words continue */
15221 {
15222 /* If not close to the limit, perform a change. Only try changes
15223 * that may lead to a lower score than "minscore".
15224 * round 0: try deleting a char from badword
15225 * round 1: try inserting a char in badword */
15226 for (round = 0; round <= 1; ++round)
15227 {
15228 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15229 if (score_off < minscore)
15230 {
15231 if (score_off + SCORE_EDIT_MIN >= minscore)
15232 {
15233 /* Near the limit, rest of the words must match. We
15234 * can check that right now, no need to push an item
15235 * onto the stack. */
15236 bi2 = bi + 1 - round;
15237 gi2 = gi + round;
15238 while (goodword[gi2] == badword[bi2])
15239 {
15240 if (goodword[gi2] == NUL)
15241 {
15242 minscore = score_off;
15243 break;
15244 }
15245 ++bi2;
15246 ++gi2;
15247 }
15248 }
15249 else
15250 {
15251 /* try deleting/inserting a character later */
15252 stack[stackidx].badi = bi + 1 - round;
15253 stack[stackidx].goodi = gi + round;
15254 stack[stackidx].score = score_off;
15255 ++stackidx;
15256 }
15257 }
15258 }
15259
15260 if (score + SCORE_SWAP < minscore)
15261 {
15262 /* If swapping two characters makes a match then the
15263 * substitution is more expensive, thus there is no need to
15264 * try both. */
15265 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15266 {
15267 /* Swap two characters, that is: skip them. */
15268 gi += 2;
15269 bi += 2;
15270 score += SCORE_SWAP;
15271 continue;
15272 }
15273 }
15274
15275 /* Substitute one character for another which is the same
15276 * thing as deleting a character from both goodword and badword.
15277 * Use a better score when there is only a case difference. */
15278 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15279 score += SCORE_ICASE;
15280 else
15281 {
15282 /* For a similar character use SCORE_SIMILAR. */
15283 if (slang != NULL
15284 && slang->sl_has_map
15285 && similar_chars(slang, gc, bc))
15286 score += SCORE_SIMILAR;
15287 else
15288 score += SCORE_SUBST;
15289 }
15290
15291 if (score < minscore)
15292 {
15293 /* Do the substitution. */
15294 ++gi;
15295 ++bi;
15296 continue;
15297 }
15298 }
15299pop:
15300 /*
15301 * Get here to try the next alternative, pop it from the stack.
15302 */
15303 if (stackidx == 0) /* stack is empty, finished */
15304 break;
15305
15306 /* pop an item from the stack */
15307 --stackidx;
15308 gi = stack[stackidx].goodi;
15309 bi = stack[stackidx].badi;
15310 score = stack[stackidx].score;
15311 }
15312
15313 /* When the score goes over "limit" it may actually be much higher.
15314 * Return a very large number to avoid going below the limit when giving a
15315 * bonus. */
15316 if (minscore > limit)
15317 return SCORE_MAXMAX;
15318 return minscore;
15319}
15320
15321#ifdef FEAT_MBYTE
15322/*
15323 * Multi-byte version of spell_edit_score_limit().
15324 * Keep it in sync with the above!
15325 */
15326 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015327spell_edit_score_limit_w(
15328 slang_T *slang,
15329 char_u *badword,
15330 char_u *goodword,
15331 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015332{
15333 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15334 int stackidx;
15335 int bi, gi;
15336 int bi2, gi2;
15337 int bc, gc;
15338 int score;
15339 int score_off;
15340 int minscore;
15341 int round;
15342 char_u *p;
15343 int wbadword[MAXWLEN];
15344 int wgoodword[MAXWLEN];
15345
15346 /* Get the characters from the multi-byte strings and put them in an
15347 * int array for easy access. */
15348 bi = 0;
15349 for (p = badword; *p != NUL; )
15350 wbadword[bi++] = mb_cptr2char_adv(&p);
15351 wbadword[bi++] = 0;
15352 gi = 0;
15353 for (p = goodword; *p != NUL; )
15354 wgoodword[gi++] = mb_cptr2char_adv(&p);
15355 wgoodword[gi++] = 0;
15356
15357 /*
15358 * The idea is to go from start to end over the words. So long as
15359 * characters are equal just continue, this always gives the lowest score.
15360 * When there is a difference try several alternatives. Each alternative
15361 * increases "score" for the edit distance. Some of the alternatives are
15362 * pushed unto a stack and tried later, some are tried right away. At the
15363 * end of the word the score for one alternative is known. The lowest
15364 * possible score is stored in "minscore".
15365 */
15366 stackidx = 0;
15367 bi = 0;
15368 gi = 0;
15369 score = 0;
15370 minscore = limit + 1;
15371
15372 for (;;)
15373 {
15374 /* Skip over an equal part, score remains the same. */
15375 for (;;)
15376 {
15377 bc = wbadword[bi];
15378 gc = wgoodword[gi];
15379
15380 if (bc != gc) /* stop at a char that's different */
15381 break;
15382 if (bc == NUL) /* both words end */
15383 {
15384 if (score < minscore)
15385 minscore = score;
15386 goto pop; /* do next alternative */
15387 }
15388 ++bi;
15389 ++gi;
15390 }
15391
15392 if (gc == NUL) /* goodword ends, delete badword chars */
15393 {
15394 do
15395 {
15396 if ((score += SCORE_DEL) >= minscore)
15397 goto pop; /* do next alternative */
15398 } while (wbadword[++bi] != NUL);
15399 minscore = score;
15400 }
15401 else if (bc == NUL) /* badword ends, insert badword chars */
15402 {
15403 do
15404 {
15405 if ((score += SCORE_INS) >= minscore)
15406 goto pop; /* do next alternative */
15407 } while (wgoodword[++gi] != NUL);
15408 minscore = score;
15409 }
15410 else /* both words continue */
15411 {
15412 /* If not close to the limit, perform a change. Only try changes
15413 * that may lead to a lower score than "minscore".
15414 * round 0: try deleting a char from badword
15415 * round 1: try inserting a char in badword */
15416 for (round = 0; round <= 1; ++round)
15417 {
15418 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15419 if (score_off < minscore)
15420 {
15421 if (score_off + SCORE_EDIT_MIN >= minscore)
15422 {
15423 /* Near the limit, rest of the words must match. We
15424 * can check that right now, no need to push an item
15425 * onto the stack. */
15426 bi2 = bi + 1 - round;
15427 gi2 = gi + round;
15428 while (wgoodword[gi2] == wbadword[bi2])
15429 {
15430 if (wgoodword[gi2] == NUL)
15431 {
15432 minscore = score_off;
15433 break;
15434 }
15435 ++bi2;
15436 ++gi2;
15437 }
15438 }
15439 else
15440 {
15441 /* try deleting a character from badword later */
15442 stack[stackidx].badi = bi + 1 - round;
15443 stack[stackidx].goodi = gi + round;
15444 stack[stackidx].score = score_off;
15445 ++stackidx;
15446 }
15447 }
15448 }
15449
15450 if (score + SCORE_SWAP < minscore)
15451 {
15452 /* If swapping two characters makes a match then the
15453 * substitution is more expensive, thus there is no need to
15454 * try both. */
15455 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15456 {
15457 /* Swap two characters, that is: skip them. */
15458 gi += 2;
15459 bi += 2;
15460 score += SCORE_SWAP;
15461 continue;
15462 }
15463 }
15464
15465 /* Substitute one character for another which is the same
15466 * thing as deleting a character from both goodword and badword.
15467 * Use a better score when there is only a case difference. */
15468 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15469 score += SCORE_ICASE;
15470 else
15471 {
15472 /* For a similar character use SCORE_SIMILAR. */
15473 if (slang != NULL
15474 && slang->sl_has_map
15475 && similar_chars(slang, gc, bc))
15476 score += SCORE_SIMILAR;
15477 else
15478 score += SCORE_SUBST;
15479 }
15480
15481 if (score < minscore)
15482 {
15483 /* Do the substitution. */
15484 ++gi;
15485 ++bi;
15486 continue;
15487 }
15488 }
15489pop:
15490 /*
15491 * Get here to try the next alternative, pop it from the stack.
15492 */
15493 if (stackidx == 0) /* stack is empty, finished */
15494 break;
15495
15496 /* pop an item from the stack */
15497 --stackidx;
15498 gi = stack[stackidx].goodi;
15499 bi = stack[stackidx].badi;
15500 score = stack[stackidx].score;
15501 }
15502
15503 /* When the score goes over "limit" it may actually be much higher.
15504 * Return a very large number to avoid going below the limit when giving a
15505 * bonus. */
15506 if (minscore > limit)
15507 return SCORE_MAXMAX;
15508 return minscore;
15509}
15510#endif
15511
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015512/*
15513 * ":spellinfo"
15514 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015515 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015516ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015517{
15518 int lpi;
15519 langp_T *lp;
15520 char_u *p;
15521
15522 if (no_spell_checking(curwin))
15523 return;
15524
15525 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015526 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015527 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015528 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015529 msg_puts((char_u *)"file: ");
15530 msg_puts(lp->lp_slang->sl_fname);
15531 msg_putchar('\n');
15532 p = lp->lp_slang->sl_info;
15533 if (p != NULL)
15534 {
15535 msg_puts(p);
15536 msg_putchar('\n');
15537 }
15538 }
15539 msg_end();
15540}
15541
Bram Moolenaar4770d092006-01-12 23:22:24 +000015542#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15543#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015544#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015545#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15546#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015547
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548/*
15549 * ":spelldump"
15550 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015552ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015553{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015554 char_u *spl;
15555 long dummy;
15556
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015557 if (no_spell_checking(curwin))
15558 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015559 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015560
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015561 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015562 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015563
15564 /* enable spelling locally in the new window */
15565 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010015566 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015567 vim_free(spl);
15568
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020015569 if (!bufempty())
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015570 return;
15571
Bram Moolenaar860cae12010-06-05 23:22:07 +020015572 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015573
15574 /* Delete the empty line that we started with. */
15575 if (curbuf->b_ml.ml_line_count > 1)
15576 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15577
15578 redraw_later(NOT_VALID);
15579}
15580
15581/*
15582 * Go through all possible words and:
15583 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15584 * "ic" and "dir" are not used.
15585 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15586 */
15587 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015588spell_dump_compl(
15589 char_u *pat, /* leading part of the word */
15590 int ic, /* ignore case */
15591 int *dir, /* direction for adding matches */
15592 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015593{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015594 langp_T *lp;
15595 slang_T *slang;
15596 idx_T arridx[MAXWLEN];
15597 int curi[MAXWLEN];
15598 char_u word[MAXWLEN];
15599 int c;
15600 char_u *byts;
15601 idx_T *idxs;
15602 linenr_T lnum = 0;
15603 int round;
15604 int depth;
15605 int n;
15606 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015607 char_u *region_names = NULL; /* region names being used */
15608 int do_region = TRUE; /* dump region names and numbers */
15609 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015610 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015611 int dumpflags = dumpflags_arg;
15612 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015613
Bram Moolenaard0131a82006-03-04 21:46:13 +000015614 /* When ignoring case or when the pattern starts with capital pass this on
15615 * to dump_word(). */
15616 if (pat != NULL)
15617 {
15618 if (ic)
15619 dumpflags |= DUMPFLAG_ICASE;
15620 else
15621 {
15622 n = captype(pat, NULL);
15623 if (n == WF_ONECAP)
15624 dumpflags |= DUMPFLAG_ONECAP;
15625 else if (n == WF_ALLCAP
15626#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015627 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015628#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015629 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015630#endif
15631 )
15632 dumpflags |= DUMPFLAG_ALLCAP;
15633 }
15634 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015635
Bram Moolenaar7887d882005-07-01 22:33:52 +000015636 /* Find out if we can support regions: All languages must support the same
15637 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015638 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015639 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015640 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015641 p = lp->lp_slang->sl_regions;
15642 if (p[0] != 0)
15643 {
15644 if (region_names == NULL) /* first language with regions */
15645 region_names = p;
15646 else if (STRCMP(region_names, p) != 0)
15647 {
15648 do_region = FALSE; /* region names are different */
15649 break;
15650 }
15651 }
15652 }
15653
15654 if (do_region && region_names != NULL)
15655 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015656 if (pat == NULL)
15657 {
15658 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15659 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15660 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015661 }
15662 else
15663 do_region = FALSE;
15664
15665 /*
15666 * Loop over all files loaded for the entries in 'spelllang'.
15667 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015668 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015669 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015670 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015671 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015672 if (slang->sl_fbyts == NULL) /* reloading failed */
15673 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015674
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015675 if (pat == NULL)
15676 {
15677 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15678 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15679 }
15680
15681 /* When matching with a pattern and there are no prefixes only use
15682 * parts of the tree that match "pat". */
15683 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015684 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015685 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015686 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015687
15688 /* round 1: case-folded tree
15689 * round 2: keep-case tree */
15690 for (round = 1; round <= 2; ++round)
15691 {
15692 if (round == 1)
15693 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015694 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015695 byts = slang->sl_fbyts;
15696 idxs = slang->sl_fidxs;
15697 }
15698 else
15699 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015700 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015701 byts = slang->sl_kbyts;
15702 idxs = slang->sl_kidxs;
15703 }
15704 if (byts == NULL)
15705 continue; /* array is empty */
15706
15707 depth = 0;
15708 arridx[0] = 0;
15709 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015710 while (depth >= 0 && !got_int
15711 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015712 {
15713 if (curi[depth] > byts[arridx[depth]])
15714 {
15715 /* Done all bytes at this node, go up one level. */
15716 --depth;
15717 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015718 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015719 }
15720 else
15721 {
15722 /* Do one more byte at this node. */
15723 n = arridx[depth] + curi[depth];
15724 ++curi[depth];
15725 c = byts[n];
15726 if (c == 0)
15727 {
15728 /* End of word, deal with the word.
15729 * Don't use keep-case words in the fold-case tree,
15730 * they will appear in the keep-case tree.
15731 * Only use the word when the region matches. */
15732 flags = (int)idxs[n];
15733 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015734 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015735 && (do_region
15736 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015737 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015738 & lp->lp_region) != 0))
15739 {
15740 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015741 if (!do_region)
15742 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015743
15744 /* Dump the basic word if there is no prefix or
15745 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015746 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015747 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015748 {
15749 dump_word(slang, word, pat, dir,
15750 dumpflags, flags, lnum);
15751 if (pat == NULL)
15752 ++lnum;
15753 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015754
15755 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015756 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015757 lnum = dump_prefixes(slang, word, pat, dir,
15758 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015759 }
15760 }
15761 else
15762 {
15763 /* Normal char, go one level deeper. */
15764 word[depth++] = c;
15765 arridx[depth] = idxs[n];
15766 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015767
15768 /* Check if this characters matches with the pattern.
15769 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015770 * Always ignore case here, dump_word() will check
15771 * proper case later. This isn't exactly right when
15772 * length changes for multi-byte characters with
15773 * ignore case... */
15774 if (depth <= patlen
15775 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015776 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015777 }
15778 }
15779 }
15780 }
15781 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015782}
15783
15784/*
15785 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015786 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015787 */
15788 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015789dump_word(
15790 slang_T *slang,
15791 char_u *word,
15792 char_u *pat,
15793 int *dir,
15794 int dumpflags,
15795 int wordflags,
15796 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015797{
15798 int keepcap = FALSE;
15799 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015800 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015801 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015802 char_u badword[MAXWLEN + 10];
15803 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015804 int flags = wordflags;
15805
15806 if (dumpflags & DUMPFLAG_ONECAP)
15807 flags |= WF_ONECAP;
15808 if (dumpflags & DUMPFLAG_ALLCAP)
15809 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015810
Bram Moolenaar4770d092006-01-12 23:22:24 +000015811 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015812 {
15813 /* Need to fix case according to "flags". */
15814 make_case_word(word, cword, flags);
15815 p = cword;
15816 }
15817 else
15818 {
15819 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015820 if ((dumpflags & DUMPFLAG_KEEPCASE)
15821 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015822 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015823 keepcap = TRUE;
15824 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015825 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015826
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015827 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015828 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015829 /* Add flags and regions after a slash. */
15830 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015831 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015832 STRCPY(badword, p);
15833 STRCAT(badword, "/");
15834 if (keepcap)
15835 STRCAT(badword, "=");
15836 if (flags & WF_BANNED)
15837 STRCAT(badword, "!");
15838 else if (flags & WF_RARE)
15839 STRCAT(badword, "?");
15840 if (flags & WF_REGION)
15841 for (i = 0; i < 7; ++i)
15842 if (flags & (0x10000 << i))
15843 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15844 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015845 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015846
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015847 if (dumpflags & DUMPFLAG_COUNT)
15848 {
15849 hashitem_T *hi;
15850
15851 /* Include the word count for ":spelldump!". */
15852 hi = hash_find(&slang->sl_wordcount, tw);
15853 if (!HASHITEM_EMPTY(hi))
15854 {
15855 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15856 tw, HI2WC(hi)->wc_count);
15857 p = IObuff;
15858 }
15859 }
15860
15861 ml_append(lnum, p, (colnr_T)0, FALSE);
15862 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015863 else if (((dumpflags & DUMPFLAG_ICASE)
15864 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15865 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015866 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015867 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015868 /* if dir was BACKWARD then honor it just once */
15869 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015870}
15871
15872/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015873 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15874 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015875 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015876 * Return the updated line number.
15877 */
15878 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015879dump_prefixes(
15880 slang_T *slang,
15881 char_u *word, /* case-folded word */
15882 char_u *pat,
15883 int *dir,
15884 int dumpflags,
15885 int flags, /* flags with prefix ID */
15886 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015887{
15888 idx_T arridx[MAXWLEN];
15889 int curi[MAXWLEN];
15890 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015891 char_u word_up[MAXWLEN];
15892 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015893 int c;
15894 char_u *byts;
15895 idx_T *idxs;
15896 linenr_T lnum = startlnum;
15897 int depth;
15898 int n;
15899 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015900 int i;
15901
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015902 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015903 * upper-case letter in word_up[]. */
15904 c = PTR2CHAR(word);
15905 if (SPELL_TOUPPER(c) != c)
15906 {
15907 onecap_copy(word, word_up, TRUE);
15908 has_word_up = TRUE;
15909 }
15910
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015911 byts = slang->sl_pbyts;
15912 idxs = slang->sl_pidxs;
15913 if (byts != NULL) /* array not is empty */
15914 {
15915 /*
15916 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015917 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015918 */
15919 depth = 0;
15920 arridx[0] = 0;
15921 curi[0] = 1;
15922 while (depth >= 0 && !got_int)
15923 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015924 n = arridx[depth];
15925 len = byts[n];
15926 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015927 {
15928 /* Done all bytes at this node, go up one level. */
15929 --depth;
15930 line_breakcheck();
15931 }
15932 else
15933 {
15934 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015935 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015936 ++curi[depth];
15937 c = byts[n];
15938 if (c == 0)
15939 {
15940 /* End of prefix, find out how many IDs there are. */
15941 for (i = 1; i < len; ++i)
15942 if (byts[n + i] != 0)
15943 break;
15944 curi[depth] += i - 1;
15945
Bram Moolenaar53805d12005-08-01 07:08:33 +000015946 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15947 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015948 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015949 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015950 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015951 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015952 : flags, lnum);
15953 if (lnum != 0)
15954 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015955 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015956
15957 /* Check for prefix that matches the word when the
15958 * first letter is upper-case, but only if the prefix has
15959 * a condition. */
15960 if (has_word_up)
15961 {
15962 c = valid_word_prefix(i, n, flags, word_up, slang,
15963 TRUE);
15964 if (c != 0)
15965 {
15966 vim_strncpy(prefix + depth, word_up,
15967 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015968 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015969 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015970 : flags, lnum);
15971 if (lnum != 0)
15972 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015973 }
15974 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015975 }
15976 else
15977 {
15978 /* Normal char, go one level deeper. */
15979 prefix[depth++] = c;
15980 arridx[depth] = idxs[n];
15981 curi[depth] = 1;
15982 }
15983 }
15984 }
15985 }
15986
15987 return lnum;
15988}
15989
Bram Moolenaar95529562005-08-25 21:21:38 +000015990/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015991 * Move "p" to the end of word "start".
15992 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015993 */
15994 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015995spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015996{
15997 char_u *p = start;
15998
Bram Moolenaar860cae12010-06-05 23:22:07 +020015999 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000016000 mb_ptr_adv(p);
16001 return p;
16002}
16003
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016004#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016005/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016006 * For Insert mode completion CTRL-X s:
16007 * Find start of the word in front of column "startcol".
16008 * We don't check if it is badly spelled, with completion we can only change
16009 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016010 * Returns the column number of the word.
16011 */
16012 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010016013spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016014{
16015 char_u *line;
16016 char_u *p;
16017 int col = 0;
16018
Bram Moolenaar95529562005-08-25 21:21:38 +000016019 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016020 return startcol;
16021
16022 /* Find a word character before "startcol". */
16023 line = ml_get_curline();
16024 for (p = line + startcol; p > line; )
16025 {
16026 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010016027 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016028 break;
16029 }
16030
16031 /* Go back to start of the word. */
16032 while (p > line)
16033 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016034 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016035 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016036 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016037 break;
16038 col = 0;
16039 }
16040
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016041 return col;
16042}
16043
16044/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016045 * Need to check for 'spellcapcheck' now, the word is removed before
16046 * expand_spelling() is called. Therefore the ugly global variable.
16047 */
16048static int spell_expand_need_cap;
16049
16050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010016051spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +000016052{
16053 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16054}
16055
16056/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016057 * Get list of spelling suggestions.
16058 * Used for Insert mode completion CTRL-X ?.
16059 * Returns the number of matches. The matches are in "matchp[]", array of
16060 * allocated strings.
16061 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016062 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010016063expand_spelling(
16064 linenr_T lnum UNUSED,
16065 char_u *pat,
16066 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016067{
16068 garray_T ga;
16069
Bram Moolenaar4770d092006-01-12 23:22:24 +000016070 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016071 *matchp = ga.ga_data;
16072 return ga.ga_len;
16073}
16074#endif
16075
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016076#endif /* FEAT_SPELL */