blob: 259f6d7df2da9ca6255ffd5ecb5ff1aff78ed945 [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;
4181
4182 /* We don't want to do this recursively. May happen when a language is
4183 * not available and the SpellFileMissing autocommand opens a new buffer
4184 * in which 'spell' is set. */
4185 if (recursive)
4186 return NULL;
4187 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004188
4189 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004190 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004191
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004192 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004193 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004194 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004195 if (spl_copy == NULL)
4196 goto theend;
4197
Bram Moolenaar2593e032013-11-14 03:54:07 +01004198#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004199 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004200#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004201
4202 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004203 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004204 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004205 /* Get one language name. */
4206 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004207 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004208 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004210 if (STRCMP(lang, "cjk") == 0)
4211 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01004212#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004213 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004214#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004215 continue;
4216 }
4217
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004218 /* If the name ends in ".spl" use it as the name of the spell file.
4219 * If there is a region name let "region" point to it and remove it
4220 * from the name. */
4221 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4222 {
4223 filename = TRUE;
4224
Bram Moolenaarb6356332005-07-18 21:40:44 +00004225 /* Locate a region and remove it from the file name. */
4226 p = vim_strchr(gettail(lang), '_');
4227 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4228 && !ASCII_ISALPHA(p[3]))
4229 {
4230 vim_strncpy(region_cp, p + 1, 2);
4231 mch_memmove(p, p + 3, len - (p - lang) - 2);
4232 len -= 3;
4233 region = region_cp;
4234 }
4235 else
4236 dont_use_region = TRUE;
4237
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004238 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004239 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4240 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004241 break;
4242 }
4243 else
4244 {
4245 filename = FALSE;
4246 if (len > 3 && lang[len - 3] == '_')
4247 {
4248 region = lang + len - 2;
4249 len -= 3;
4250 lang[len] = NUL;
4251 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004252 else
4253 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004254
4255 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004256 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4257 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004258 break;
4259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260
Bram Moolenaarb6356332005-07-18 21:40:44 +00004261 if (region != NULL)
4262 {
4263 /* If the region differs from what was used before then don't
4264 * use it for 'spellfile'. */
4265 if (use_region != NULL && STRCMP(region, use_region) != 0)
4266 dont_use_region = TRUE;
4267 use_region = region;
4268 }
4269
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004270 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004271 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004272 {
4273 if (filename)
4274 (void)spell_load_file(lang, lang, NULL, FALSE);
4275 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004276 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004277 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004278#ifdef FEAT_AUTOCMD
4279 /* SpellFileMissing autocommands may do anything, including
4280 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004281 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004282 {
4283 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4284 goto theend;
4285 }
4286#endif
4287 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004288 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004289
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004290 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004291 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004292 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4294 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4295 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004296 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004297 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004298 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004299 {
4300 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004302 if (c == REGION_ALL)
4303 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004304 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004305 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004306 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004307 /* This addition file is for other regions. */
4308 region_mask = 0;
4309 }
4310 else
4311 /* This is probably an error. Give a warning and
4312 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004313 smsg((char_u *)
4314 _("Warning: region %s not supported"),
4315 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004316 }
4317 else
4318 region_mask = 1 << c;
4319 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004320
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004321 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004322 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004323 if (ga_grow(&ga, 1) == FAIL)
4324 {
4325 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004326 ret_msg = e_outofmem;
4327 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004328 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004329 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004330 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4331 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004332 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004333 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004334 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004335 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004336 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004337 }
4338
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004339 /* round 0: load int_wordlist, if possible.
4340 * round 1: load first name in 'spellfile'.
4341 * round 2: load second name in 'spellfile.
4342 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004343 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004344 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004346 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004347 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004348 /* Internal wordlist, if there is one. */
4349 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004350 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004351 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004352 }
4353 else
4354 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004355 /* One entry in 'spellfile'. */
4356 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4357 STRCAT(spf_name, ".spl");
4358
4359 /* If it was already found above then skip it. */
4360 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004361 {
4362 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4363 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004364 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004365 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004366 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004367 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004368 }
4369
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004370 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004371 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4372 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004374 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004375 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004376 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004377 * region name, the region is ignored otherwise. for int_wordlist
4378 * use an arbitrary name. */
4379 if (round == 0)
4380 STRCPY(lang, "internal wordlist");
4381 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004382 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004383 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004384 p = vim_strchr(lang, '.');
4385 if (p != NULL)
4386 *p = NUL; /* truncate at ".encoding.add" */
4387 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004388 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004389
4390 /* If one of the languages has NOBREAK we assume the addition
4391 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004392 if (slang != NULL && nobreak)
4393 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004394 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004395 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004396 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004397 region_mask = REGION_ALL;
4398 if (use_region != NULL && !dont_use_region)
4399 {
4400 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004401 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004402 if (c != REGION_ALL)
4403 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004404 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004405 /* This spell file is for other regions. */
4406 region_mask = 0;
4407 }
4408
4409 if (region_mask != 0)
4410 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004411 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4412 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4413 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004414 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4415 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004416 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004417 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004418 }
4419 }
4420
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004421 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004422 ga_clear(&wp->w_s->b_langp);
4423 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004424
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004425 /* For each language figure out what language to use for sound folding and
4426 * REP items. If the language doesn't support it itself use another one
4427 * with the same name. E.g. for "en-math" use "en". */
4428 for (i = 0; i < ga.ga_len; ++i)
4429 {
4430 lp = LANGP_ENTRY(ga, i);
4431
4432 /* sound folding */
4433 if (lp->lp_slang->sl_sal.ga_len > 0)
4434 /* language does sound folding itself */
4435 lp->lp_sallang = lp->lp_slang;
4436 else
4437 /* find first similar language that does sound folding */
4438 for (j = 0; j < ga.ga_len; ++j)
4439 {
4440 lp2 = LANGP_ENTRY(ga, j);
4441 if (lp2->lp_slang->sl_sal.ga_len > 0
4442 && STRNCMP(lp->lp_slang->sl_name,
4443 lp2->lp_slang->sl_name, 2) == 0)
4444 {
4445 lp->lp_sallang = lp2->lp_slang;
4446 break;
4447 }
4448 }
4449
4450 /* REP items */
4451 if (lp->lp_slang->sl_rep.ga_len > 0)
4452 /* language has REP items itself */
4453 lp->lp_replang = lp->lp_slang;
4454 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004455 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004456 for (j = 0; j < ga.ga_len; ++j)
4457 {
4458 lp2 = LANGP_ENTRY(ga, j);
4459 if (lp2->lp_slang->sl_rep.ga_len > 0
4460 && STRNCMP(lp->lp_slang->sl_name,
4461 lp2->lp_slang->sl_name, 2) == 0)
4462 {
4463 lp->lp_replang = lp2->lp_slang;
4464 break;
4465 }
4466 }
4467 }
4468
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004469theend:
4470 vim_free(spl_copy);
4471 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02004472 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004473 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004474}
4475
4476/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004477 * Clear the midword characters for buffer "buf".
4478 */
4479 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004480clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004481{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004482 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004483#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004484 vim_free(wp->w_s->b_spell_ismw_mb);
4485 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004486#endif
4487}
4488
4489/*
4490 * Use the "sl_midword" field of language "lp" for buffer "buf".
4491 * They add up to any currently used midword characters.
4492 */
4493 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004494use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004495{
4496 char_u *p;
4497
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004498 if (lp->sl_midword == NULL) /* there aren't any */
4499 return;
4500
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004501 for (p = lp->sl_midword; *p != NUL; )
4502#ifdef FEAT_MBYTE
4503 if (has_mbyte)
4504 {
4505 int c, l, n;
4506 char_u *bp;
4507
4508 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004509 l = (*mb_ptr2len)(p);
4510 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004511 wp->w_s->b_spell_ismw[c] = TRUE;
4512 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004513 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004514 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004515 else
4516 {
4517 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004518 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4519 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004520 if (bp != NULL)
4521 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004522 vim_free(wp->w_s->b_spell_ismw_mb);
4523 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004524 vim_strncpy(bp + n, p, l);
4525 }
4526 }
4527 p += l;
4528 }
4529 else
4530#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004531 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004532}
4533
4534/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535 * Find the region "region[2]" in "rp" (points to "sl_regions").
4536 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004537 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004538 */
4539 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004540find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004541{
4542 int i;
4543
4544 for (i = 0; ; i += 2)
4545 {
4546 if (rp[i] == NUL)
4547 return REGION_ALL;
4548 if (rp[i] == region[0] && rp[i + 1] == region[1])
4549 break;
4550 }
4551 return i / 2;
4552}
4553
4554/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004555 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004556 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004557 * Word WF_ONECAP
4558 * W WORD WF_ALLCAP
4559 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004560 */
4561 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004562captype(
4563 char_u *word,
4564 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004565{
4566 char_u *p;
4567 int c;
4568 int firstcap;
4569 int allcap;
4570 int past_second = FALSE; /* past second word char */
4571
4572 /* find first letter */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004573 for (p = word; !spell_iswordp_nmw(p, curwin); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575 return 0; /* only non-word characters, illegal word */
4576#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004577 if (has_mbyte)
4578 c = mb_ptr2char_adv(&p);
4579 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004580#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004581 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004582 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583
4584 /*
4585 * Need to check all letters to find a word with mixed upper/lower.
4586 * But a word with an upper char only at start is a ONECAP.
4587 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004588 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004589 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004590 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004591 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004592 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593 {
4594 /* UUl -> KEEPCAP */
4595 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004596 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004597 allcap = FALSE;
4598 }
4599 else if (!allcap)
4600 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004601 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004602 past_second = TRUE;
4603 }
4604
4605 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004608 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004609 return 0;
4610}
4611
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004612/*
4613 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4614 * capital. So that make_case_word() can turn WOrd into Word.
4615 * Add ALLCAP for "WOrD".
4616 */
4617 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004618badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004619{
4620 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004621 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004622 int l, u;
4623 int first;
4624 char_u *p;
4625
4626 if (flags & WF_KEEPCAP)
4627 {
4628 /* Count the number of UPPER and lower case letters. */
4629 l = u = 0;
4630 first = FALSE;
4631 for (p = word; p < end; mb_ptr_adv(p))
4632 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004633 c = PTR2CHAR(p);
4634 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004635 {
4636 ++u;
4637 if (p == word)
4638 first = TRUE;
4639 }
4640 else
4641 ++l;
4642 }
4643
4644 /* If there are more UPPER than lower case letters suggest an
4645 * ALLCAP word. Otherwise, if the first letter is UPPER then
4646 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4647 * require three upper case letters. */
4648 if (u > l && u > 2)
4649 flags |= WF_ALLCAP;
4650 else if (first)
4651 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004652
4653 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4654 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004655 }
4656 return flags;
4657}
4658
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004659/*
4660 * Delete the internal wordlist and its .spl file.
4661 */
4662 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004663spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004664{
4665 char_u fname[MAXPATHL];
4666
4667 if (int_wordlist != NULL)
4668 {
4669 mch_remove(int_wordlist);
4670 int_wordlist_spl(fname);
4671 mch_remove(fname);
4672 vim_free(int_wordlist);
4673 int_wordlist = NULL;
4674 }
4675}
4676
4677#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004678/*
4679 * Free all languages.
4680 */
4681 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004682spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004683{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004684 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004685 buf_T *buf;
4686
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004687 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004688 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004689 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004690
4691 while (first_lang != NULL)
4692 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004693 slang = first_lang;
4694 first_lang = slang->sl_next;
4695 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004696 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004697
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004698 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00004699
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004700 vim_free(repl_to);
4701 repl_to = NULL;
4702 vim_free(repl_from);
4703 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004704}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004705#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004706
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004707#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004708/*
4709 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004710 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004711 */
4712 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004713spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004714{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004715 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004716
Bram Moolenaarea408852005-06-25 22:49:46 +00004717 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004718 init_spell_chartab();
4719
4720 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004721 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004722
4723 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004724 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004725 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004726 /* Only load the wordlists when 'spelllang' is set and there is a
4727 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004728 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004729 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004730 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004731 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004732 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004733# ifdef FEAT_WINDOWS
4734 break;
4735# endif
4736 }
4737 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004738 }
4739}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004740#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004741
Bram Moolenaarb765d632005-06-07 21:00:02 +00004742/*
4743 * Reload the spell file "fname" if it's loaded.
4744 */
4745 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004746spell_reload_one(
4747 char_u *fname,
4748 int added_word) /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004749{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004750 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004752
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004753 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004754 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004755 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004756 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004757 slang_clear(slang);
4758 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004759 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004760 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004761 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004763 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765
4766 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004767 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004768 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004769 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004770}
4771
4772
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004773/*
4774 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004775 */
4776
Bram Moolenaar51485f02005-06-04 21:55:20 +00004777#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778 and .dic file. */
4779/*
4780 * Main structure to store the contents of a ".aff" file.
4781 */
4782typedef struct afffile_S
4783{
4784 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004785 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004786 unsigned af_rare; /* RARE ID for rare word */
4787 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004788 unsigned af_bad; /* BAD ID for banned word */
4789 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004790 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004791 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004792 unsigned af_comproot; /* COMPOUNDROOT ID */
4793 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4794 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004795 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004796 int af_pfxpostpone; /* postpone prefixes without chop string and
4797 without flags */
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02004798 int af_ignoreextra; /* IGNOREEXTRA present */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4800 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004801 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802} afffile_T;
4803
Bram Moolenaar6de68532005-08-24 22:08:48 +00004804#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004805#define AFT_LONG 1 /* flags are two characters */
4806#define AFT_CAPLONG 2 /* flags are one or two characters */
4807#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004808
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004809typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004810/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4811struct affentry_S
4812{
4813 affentry_T *ae_next; /* next affix with same name/number */
4814 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4815 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004816 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004817 char_u *ae_cond; /* condition (NULL for ".") */
4818 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004819 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4820 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004821};
4822
Bram Moolenaar6de68532005-08-24 22:08:48 +00004823#ifdef FEAT_MBYTE
4824# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4825#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004826# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004827#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004828
Bram Moolenaar51485f02005-06-04 21:55:20 +00004829/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4830typedef struct affheader_S
4831{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004832 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4833 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4834 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004835 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004836 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004837 affentry_T *ah_first; /* first affix entry */
4838} affheader_T;
4839
4840#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4841
Bram Moolenaar6de68532005-08-24 22:08:48 +00004842/* Flag used in compound items. */
4843typedef struct compitem_S
4844{
4845 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4846 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4847 int ci_newID; /* affix ID after renumbering. */
4848} compitem_T;
4849
4850#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4851
Bram Moolenaar51485f02005-06-04 21:55:20 +00004852/*
4853 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004854 * the need to keep track of each allocated thing, everything is freed all at
4855 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004856 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4857 * "sb_data" is correct for systems where pointers must be aligned on
4858 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004859 */
4860#define SBLOCKSIZE 16000 /* size of sb_data */
4861typedef struct sblock_S sblock_T;
4862struct sblock_S
4863{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004865 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004867};
4868
4869/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004870 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004871 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004872typedef struct wordnode_S wordnode_T;
4873struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004874{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004875 union /* shared to save space */
4876 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004877 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004878 int index; /* index in written nodes (valid after first
4879 round) */
4880 } wn_u1;
4881 union /* shared to save space */
4882 {
4883 wordnode_T *next; /* next node with same hash key */
4884 wordnode_T *wnode; /* parent node that will write this node */
4885 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004886 wordnode_T *wn_child; /* child (next byte in word) */
4887 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4888 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004889 int wn_refs; /* Nr. of references to this node. Only
4890 relevant for first node in a list of
4891 siblings, in following siblings it is
4892 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004893 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004894
4895 /* Info for when "wn_byte" is NUL.
4896 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4897 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4898 * "wn_region" the LSW of the wordnr. */
4899 char_u wn_affixID; /* supported/required prefix ID or 0 */
4900 short_u wn_flags; /* WF_ flags */
4901 short wn_region; /* region mask */
4902
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004903#ifdef SPELL_PRINTTREE
4904 int wn_nr; /* sequence nr for printing */
4905#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906};
4907
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004908#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4909
Bram Moolenaar51485f02005-06-04 21:55:20 +00004910#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912/*
4913 * Info used while reading the spell files.
4914 */
4915typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004916{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004917 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004918 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004919
Bram Moolenaar51485f02005-06-04 21:55:20 +00004920 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004921 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004922
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004923 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004924
Bram Moolenaar4770d092006-01-12 23:22:24 +00004925 long si_sugtree; /* creating the soundfolding trie */
4926
Bram Moolenaar51485f02005-06-04 21:55:20 +00004927 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004928 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004929 int si_did_emsg; /* TRUE when ran out of memory */
4930
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004931 long si_compress_cnt; /* words to add before lowering
4932 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004933 wordnode_T *si_first_free; /* List of nodes that have been freed during
4934 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004935 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004936#ifdef SPELL_PRINTTREE
4937 int si_wordnode_nr; /* sequence nr for nodes */
4938#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004939 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004940
Bram Moolenaar51485f02005-06-04 21:55:20 +00004941 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004942 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004943 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004944 int si_region; /* region mask */
4945 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004946 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004947 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004948 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004949 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004950 int si_region_count; /* number of regions supported (1 when there
4951 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004952 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004953 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004954
4955 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004956 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004957 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004958 char_u *si_sofofr; /* SOFOFROM text */
4959 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004960 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004961 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004962 int si_nocompoundsugs; /* NOCOMPOUNDSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004963 int si_followup; /* soundsalike: ? */
4964 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004965 hashtab_T si_commonwords; /* hashtable for common words */
4966 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004967 int si_rem_accents; /* soundsalike: remove accents */
4968 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004969 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004970 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004971 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004972 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004973 int si_compoptions; /* COMP_ flags */
4974 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4975 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004976 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004977 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004978 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004979 garray_T si_prefcond; /* table with conditions for postponed
4980 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004981 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004982 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004983} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004984
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004985static afffile_T *spell_read_aff(spellinfo_T *spin, char_u *fname);
4986static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
4987static void aff_process_flags(afffile_T *affile, affentry_T *entry);
4988static int spell_info_item(char_u *s);
4989static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
4990static unsigned get_affitem(int flagtype, char_u **pp);
4991static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
4992static void check_renumber(spellinfo_T *spin);
4993static int flag_in_afflist(int flagtype, char_u *afflist, unsigned flag);
4994static void aff_check_number(int spinval, int affval, char *name);
4995static void aff_check_string(char_u *spinval, char_u *affval, char *name);
4996static int str_equal(char_u *s1, char_u *s2);
4997static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
4998static int sal_to_bool(char_u *s);
4999static void spell_free_aff(afffile_T *aff);
5000static int spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile);
5001static int get_affix_flags(afffile_T *affile, char_u *afflist);
5002static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
5003static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
5004static 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);
5005static int spell_read_wordfile(spellinfo_T *spin, char_u *fname);
5006static void *getroom(spellinfo_T *spin, size_t len, int align);
5007static char_u *getroom_save(spellinfo_T *spin, char_u *s);
5008static void free_blocks(sblock_T *bl);
5009static wordnode_T *wordtree_alloc(spellinfo_T *spin);
5010static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
5011static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
5012static wordnode_T *get_wordnode(spellinfo_T *spin);
5013static int deref_wordnode(spellinfo_T *spin, wordnode_T *node);
5014static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
5015static void wordtree_compress(spellinfo_T *spin, wordnode_T *root);
5016static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot);
5017static int node_equal(wordnode_T *n1, wordnode_T *n2);
5018static int write_vim_spell(spellinfo_T *spin, char_u *fname);
5019static void clear_node(wordnode_T *node);
5020static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
5021static void spell_make_sugfile(spellinfo_T *spin, char_u *wfname);
5022static int sug_filltree(spellinfo_T *spin, slang_T *slang);
5023static int sug_maketable(spellinfo_T *spin);
5024static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
5025static int offset2bytes(int nr, char_u *buf);
5026static int bytes2offset(char_u **pp);
5027static void sug_write(spellinfo_T *spin, char_u *fname);
5028static void mkspell(int fcount, char_u **fnames, int ascii, int over_write, int added_word);
5029static void spell_message(spellinfo_T *spin, char_u *str);
5030static void init_spellfile(void);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031
Bram Moolenaar53805d12005-08-01 07:08:33 +00005032/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5033 * but it must be negative to indicate the prefix tree to tree_add_word().
5034 * Use a negative number with the lower 8 bits zero. */
5035#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005036
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005037/* flags for "condit" argument of store_aff_word() */
5038#define CONDIT_COMB 1 /* affix must combine */
5039#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5040#define CONDIT_SUF 4 /* add a suffix for matching flags */
5041#define CONDIT_AFF 8 /* word already has an affix */
5042
Bram Moolenaar5195e452005-08-19 20:32:47 +00005043/*
5044 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5045 */
5046static long compress_start = 30000; /* memory / SBLOCKSIZE */
5047static long compress_inc = 100; /* memory / SBLOCKSIZE */
5048static long compress_added = 500000; /* word count */
5049
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005050#ifdef SPELL_PRINTTREE
5051/*
5052 * For debugging the tree code: print the current tree in a (more or less)
5053 * readable format, so that we can see what happens when adding a word and/or
5054 * compressing the tree.
5055 * Based on code from Olaf Seibert.
5056 */
5057#define PRINTLINESIZE 1000
5058#define PRINTWIDTH 6
5059
5060#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5061 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5062
5063static char line1[PRINTLINESIZE];
5064static char line2[PRINTLINESIZE];
5065static char line3[PRINTLINESIZE];
5066
5067 static void
5068spell_clear_flags(wordnode_T *node)
5069{
5070 wordnode_T *np;
5071
5072 for (np = node; np != NULL; np = np->wn_sibling)
5073 {
5074 np->wn_u1.index = FALSE;
5075 spell_clear_flags(np->wn_child);
5076 }
5077}
5078
5079 static void
5080spell_print_node(wordnode_T *node, int depth)
5081{
5082 if (node->wn_u1.index)
5083 {
5084 /* Done this node before, print the reference. */
5085 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5086 PRINTSOME(line2, depth, " ", 0, 0);
5087 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar7b877b32016-01-09 13:51:34 +01005088 msg((char_u *)line1);
5089 msg((char_u *)line2);
5090 msg((char_u *)line3);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005091 }
5092 else
5093 {
5094 node->wn_u1.index = TRUE;
5095
5096 if (node->wn_byte != NUL)
5097 {
5098 if (node->wn_child != NULL)
5099 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5100 else
5101 /* Cannot happen? */
5102 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5103 }
5104 else
5105 PRINTSOME(line1, depth, " $ ", 0, 0);
5106
5107 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5108
5109 if (node->wn_sibling != NULL)
5110 PRINTSOME(line3, depth, " | ", 0, 0);
5111 else
5112 PRINTSOME(line3, depth, " ", 0, 0);
5113
5114 if (node->wn_byte == NUL)
5115 {
Bram Moolenaar7b877b32016-01-09 13:51:34 +01005116 msg((char_u *)line1);
5117 msg((char_u *)line2);
5118 msg((char_u *)line3);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005119 }
5120
5121 /* do the children */
5122 if (node->wn_byte != NUL && node->wn_child != NULL)
5123 spell_print_node(node->wn_child, depth + 1);
5124
5125 /* do the siblings */
5126 if (node->wn_sibling != NULL)
5127 {
5128 /* get rid of all parent details except | */
5129 STRCPY(line1, line3);
5130 STRCPY(line2, line3);
5131 spell_print_node(node->wn_sibling, depth);
5132 }
5133 }
5134}
5135
5136 static void
5137spell_print_tree(wordnode_T *root)
5138{
5139 if (root != NULL)
5140 {
5141 /* Clear the "wn_u1.index" fields, used to remember what has been
5142 * done. */
5143 spell_clear_flags(root);
5144
5145 /* Recursively print the tree. */
5146 spell_print_node(root, 0);
5147 }
5148}
5149#endif /* SPELL_PRINTTREE */
5150
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005151/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005152 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005153 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005154 */
5155 static afffile_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005156spell_read_aff(spellinfo_T *spin, char_u *fname)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005157{
5158 FILE *fd;
5159 afffile_T *aff;
5160 char_u rline[MAXLINELEN];
5161 char_u *line;
5162 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005163#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005164 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165 int itemcnt;
5166 char_u *p;
5167 int lnum = 0;
5168 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005169 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005170 int aff_todo = 0;
5171 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005172 char_u *low = NULL;
5173 char_u *fol = NULL;
5174 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005175 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005176 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005177 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005178 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005180 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005181 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005182 int compminlen = 0; /* COMPOUNDMIN value */
5183 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005184 int compoptions = 0; /* COMP_ flags */
5185 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005186 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005187 concatenated */
5188 char_u *midword = NULL; /* MIDWORD value */
5189 char_u *syllable = NULL; /* SYLLABLE value */
5190 char_u *sofofrom = NULL; /* SOFOFROM value */
5191 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005192
Bram Moolenaar51485f02005-06-04 21:55:20 +00005193 /*
5194 * Open the file.
5195 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005196 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005197 if (fd == NULL)
5198 {
5199 EMSG2(_(e_notopen), fname);
5200 return NULL;
5201 }
5202
Bram Moolenaar4770d092006-01-12 23:22:24 +00005203 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5204 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 /* Only do REP lines when not done in another .aff file already. */
5207 do_rep = spin->si_rep.ga_len == 0;
5208
Bram Moolenaar4770d092006-01-12 23:22:24 +00005209 /* Only do REPSAL lines when not done in another .aff file already. */
5210 do_repsal = spin->si_repsal.ga_len == 0;
5211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 /* Only do SAL lines when not done in another .aff file already. */
5213 do_sal = spin->si_sal.ga_len == 0;
5214
5215 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005216 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217
Bram Moolenaar51485f02005-06-04 21:55:20 +00005218 /*
5219 * Allocate and init the afffile_T structure.
5220 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005221 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005222 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005223 {
5224 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005225 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005226 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005227 hash_init(&aff->af_pref);
5228 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005229 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005230
5231 /*
5232 * Read all the lines in the file one by one.
5233 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005234 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005235 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005236 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005237 ++lnum;
5238
5239 /* Skip comment lines. */
5240 if (*rline == '#')
5241 continue;
5242
5243 /* Convert from "SET" to 'encoding' when needed. */
5244 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005245#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005246 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005247 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005248 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005249 if (pc == NULL)
5250 {
5251 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5252 fname, lnum, rline);
5253 continue;
5254 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005255 line = pc;
5256 }
5257 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005258#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 {
5260 pc = NULL;
5261 line = rline;
5262 }
5263
5264 /* Split the line up in white separated items. Put a NUL after each
5265 * item. */
5266 itemcnt = 0;
5267 for (p = line; ; )
5268 {
5269 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5270 ++p;
5271 if (*p == NUL)
5272 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005273 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005274 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005275 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005276 /* A few items have arbitrary text argument, don't split them. */
5277 if (itemcnt == 2 && spell_info_item(items[0]))
5278 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5279 ++p;
5280 else
5281 while (*p > ' ') /* skip until white space or CR/NL */
5282 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005283 if (*p == NUL)
5284 break;
5285 *p++ = NUL;
5286 }
5287
5288 /* Handle non-empty lines. */
5289 if (itemcnt > 0)
5290 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005291 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005292 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005293#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005294 /* Setup for conversion from "ENC" to 'encoding'. */
5295 aff->af_enc = enc_canonize(items[1]);
5296 if (aff->af_enc != NULL && !spin->si_ascii
5297 && convert_setup(&spin->si_conv, aff->af_enc,
5298 p_enc) == FAIL)
5299 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5300 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005301 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005302#else
5303 smsg((char_u *)_("Conversion in %s not supported"), fname);
5304#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005305 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005306 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005307 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005308 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005309 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005310 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005311 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005312 aff->af_flagtype = AFT_NUM;
5313 else if (STRCMP(items[1], "caplong") == 0)
5314 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005315 else
5316 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5317 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005318 if (aff->af_rare != 0
5319 || aff->af_keepcase != 0
5320 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005321 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005322 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005323 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005324 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005325 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005326 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005327 || aff->af_suff.ht_used > 0
5328 || aff->af_pref.ht_used > 0)
5329 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5330 fname, lnum, items[1]);
5331 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005332 else if (spell_info_item(items[0]))
5333 {
5334 p = (char_u *)getroom(spin,
5335 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5336 + STRLEN(items[0])
5337 + STRLEN(items[1]) + 3, FALSE);
5338 if (p != NULL)
5339 {
5340 if (spin->si_info != NULL)
5341 {
5342 STRCPY(p, spin->si_info);
5343 STRCAT(p, "\n");
5344 }
5345 STRCAT(p, items[0]);
5346 STRCAT(p, " ");
5347 STRCAT(p, items[1]);
5348 spin->si_info = p;
5349 }
5350 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005351 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005352 && midword == NULL)
5353 {
5354 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005355 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005356 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005359 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005360 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005361 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5362 || is_aff_rule(items, itemcnt, "RARE", 2))
5363 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005364 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005365 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005366 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005367 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005368 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005369 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5370 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005371 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005372 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005373 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005374 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005375 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005376 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5377 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5378 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005379 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005380 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5381 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005382 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005383 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005384 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005385 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005386 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5387 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005388 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005389 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005390 && aff->af_circumfix == 0)
5391 {
5392 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5393 fname, lnum);
5394 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005395 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005396 && aff->af_nosuggest == 0)
5397 {
5398 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5399 fname, lnum);
5400 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005401 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5402 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005403 && aff->af_needcomp == 0)
5404 {
5405 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5406 fname, lnum);
5407 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005408 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005409 && aff->af_comproot == 0)
5410 {
5411 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5412 fname, lnum);
5413 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005414 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5415 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005416 {
5417 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5418 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005419 if (aff->af_pref.ht_used > 0)
5420 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5421 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005422 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005423 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5424 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005425 {
5426 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5427 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005428 if (aff->af_pref.ht_used > 0)
5429 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5430 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005431 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005432 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005433 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005434 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005435 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005436 * "Na" into "Na+", "1234" into "1234+". */
5437 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005438 if (p != NULL)
5439 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005440 STRCPY(p, items[1]);
5441 STRCAT(p, "+");
5442 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005443 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005444 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005445 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5446 {
5447 /* We don't use the count, but do check that it's a number and
5448 * not COMPOUNDRULE mistyped. */
5449 if (atoi((char *)items[1]) == 0)
5450 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5451 fname, lnum, items[1]);
5452 }
5453 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005454 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005455 /* Don't use the first rule if it is a number. */
5456 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005457 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005458 /* Concatenate this string to previously defined ones,
5459 * using a slash to separate them. */
5460 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005461 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005462 l += (int)STRLEN(compflags) + 1;
5463 p = getroom(spin, l, FALSE);
5464 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005465 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005466 if (compflags != NULL)
5467 {
5468 STRCPY(p, compflags);
5469 STRCAT(p, "/");
5470 }
5471 STRCAT(p, items[1]);
5472 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005473 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005474 }
5475 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005476 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005477 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005478 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005479 compmax = atoi((char *)items[1]);
5480 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005481 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005482 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005483 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005484 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005485 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005486 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005487 compminlen = atoi((char *)items[1]);
5488 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005489 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5490 fname, lnum, items[1]);
5491 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005492 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005493 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005494 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005495 compsylmax = atoi((char *)items[1]);
5496 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005497 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5498 fname, lnum, items[1]);
5499 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005500 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005501 {
5502 compoptions |= COMP_CHECKDUP;
5503 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005504 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005505 {
5506 compoptions |= COMP_CHECKREP;
5507 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005508 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005509 {
5510 compoptions |= COMP_CHECKCASE;
5511 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005512 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005513 {
5514 compoptions |= COMP_CHECKTRIPLE;
5515 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005516 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005517 {
5518 if (atoi((char *)items[1]) == 0)
5519 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5520 fname, lnum, items[1]);
5521 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005522 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005523 {
5524 garray_T *gap = &spin->si_comppat;
5525 int i;
5526
5527 /* Only add the couple if it isn't already there. */
5528 for (i = 0; i < gap->ga_len - 1; i += 2)
5529 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5530 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5531 items[2]) == 0)
5532 break;
5533 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5534 {
5535 ((char_u **)(gap->ga_data))[gap->ga_len++]
5536 = getroom_save(spin, items[1]);
5537 ((char_u **)(gap->ga_data))[gap->ga_len++]
5538 = getroom_save(spin, items[2]);
5539 }
5540 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005541 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005542 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005543 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005544 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005545 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005546 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005547 {
5548 spin->si_nobreak = TRUE;
5549 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005550 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005551 {
5552 spin->si_nosplitsugs = TRUE;
5553 }
Bram Moolenaar7b877b32016-01-09 13:51:34 +01005554 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
5555 {
5556 spin->si_nocompoundsugs = TRUE;
5557 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005558 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005559 {
5560 spin->si_nosugfile = TRUE;
5561 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005562 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005563 {
5564 aff->af_pfxpostpone = TRUE;
5565 }
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005566 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
5567 {
5568 aff->af_ignoreextra = TRUE;
5569 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005570 else if ((STRCMP(items[0], "PFX") == 0
5571 || STRCMP(items[0], "SFX") == 0)
5572 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005573 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005574 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005575 int lasti = 4;
5576 char_u key[AH_KEY_LEN];
5577
5578 if (*items[0] == 'P')
5579 tp = &aff->af_pref;
5580 else
5581 tp = &aff->af_suff;
5582
5583 /* Myspell allows the same affix name to be used multiple
5584 * times. The affix files that do this have an undocumented
5585 * "S" flag on all but the last block, thus we check for that
5586 * and store it in ah_follows. */
5587 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5588 hi = hash_find(tp, key);
5589 if (!HASHITEM_EMPTY(hi))
5590 {
5591 cur_aff = HI2AH(hi);
5592 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5593 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5594 fname, lnum, items[1]);
5595 if (!cur_aff->ah_follows)
5596 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5597 fname, lnum, items[1]);
5598 }
5599 else
5600 {
5601 /* New affix letter. */
5602 cur_aff = (affheader_T *)getroom(spin,
5603 sizeof(affheader_T), TRUE);
5604 if (cur_aff == NULL)
5605 break;
5606 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5607 fname, lnum);
5608 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5609 break;
5610 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005611 || cur_aff->ah_flag == aff->af_rare
5612 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005613 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005614 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005615 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005616 || cur_aff->ah_flag == aff->af_needcomp
5617 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005618 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 +00005619 fname, lnum, items[1]);
5620 STRCPY(cur_aff->ah_key, items[1]);
5621 hash_add(tp, cur_aff->ah_key);
5622
5623 cur_aff->ah_combine = (*items[2] == 'Y');
5624 }
5625
5626 /* Check for the "S" flag, which apparently means that another
5627 * block with the same affix name is following. */
5628 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5629 {
5630 ++lasti;
5631 cur_aff->ah_follows = TRUE;
5632 }
5633 else
5634 cur_aff->ah_follows = FALSE;
5635
Bram Moolenaar8db73182005-06-17 21:51:16 +00005636 /* Myspell allows extra text after the item, but that might
5637 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005638 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005639 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005640
Bram Moolenaar95529562005-08-25 21:21:38 +00005641 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005642 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5643 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005644
Bram Moolenaar95529562005-08-25 21:21:38 +00005645 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005646 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005647 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005648 {
5649 /* Use a new number in the .spl file later, to be able
5650 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005651 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005652 cur_aff->ah_newID = ++spin->si_newprefID;
5653
5654 /* We only really use ah_newID if the prefix is
5655 * postponed. We know that only after handling all
5656 * the items. */
5657 did_postpone_prefix = FALSE;
5658 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005659 else
5660 /* Did use the ID in a previous block. */
5661 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005662 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005663
Bram Moolenaar51485f02005-06-04 21:55:20 +00005664 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005665 }
5666 else if ((STRCMP(items[0], "PFX") == 0
5667 || STRCMP(items[0], "SFX") == 0)
5668 && aff_todo > 0
5669 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005670 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005671 {
5672 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005673 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005674 int lasti = 5;
5675
Bram Moolenaar8db73182005-06-17 21:51:16 +00005676 /* Myspell allows extra text after the item, but that might
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005677 * mean mistakes go unnoticed. Require a comment-starter,
5678 * unless IGNOREEXTRA is used. Hunspell uses a "-" item. */
5679 if (itemcnt > lasti
5680 && !aff->af_ignoreextra
5681 && *items[lasti] != '#'
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005682 && (STRCMP(items[lasti], "-") != 0
5683 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005684 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005685
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005686 /* New item for an affix letter. */
5687 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005688 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005689 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005690 if (aff_entry == NULL)
5691 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005692
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005693 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005694 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005695 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005696 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005697 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005698
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005699 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005700 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5701 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005702 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005703 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005704 aff_process_flags(aff, aff_entry);
5705 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005706 }
5707
Bram Moolenaar51485f02005-06-04 21:55:20 +00005708 /* Don't use an affix entry with non-ASCII characters when
5709 * "spin->si_ascii" is TRUE. */
5710 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005711 || has_non_ascii(aff_entry->ae_add)))
5712 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005713 aff_entry->ae_next = cur_aff->ah_first;
5714 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005715
5716 if (STRCMP(items[4], ".") != 0)
5717 {
5718 char_u buf[MAXLINELEN];
5719
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005720 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005721 if (*items[0] == 'P')
5722 sprintf((char *)buf, "^%s", items[4]);
5723 else
5724 sprintf((char *)buf, "%s$", items[4]);
5725 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005726 RE_MAGIC + RE_STRING + RE_STRICT);
5727 if (aff_entry->ae_prog == NULL)
5728 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5729 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005730 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005731
5732 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005733 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005734 * Can't be done for an affix with flags, ignoring
5735 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005736 if (*items[0] == 'P' && aff->af_pfxpostpone
5737 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005738 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005739 /* When the chop string is one lower-case letter and
5740 * the add string ends in the upper-case letter we set
5741 * the "upper" flag, clear "ae_chop" and remove the
5742 * letters from "ae_add". The condition must either
5743 * be empty or start with the same letter. */
5744 if (aff_entry->ae_chop != NULL
5745 && aff_entry->ae_add != NULL
5746#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005747 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005748 aff_entry->ae_chop)] == NUL
5749#else
5750 && aff_entry->ae_chop[1] == NUL
5751#endif
5752 )
5753 {
5754 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005755
Bram Moolenaar53805d12005-08-01 07:08:33 +00005756 c = PTR2CHAR(aff_entry->ae_chop);
5757 c_up = SPELL_TOUPPER(c);
5758 if (c_up != c
5759 && (aff_entry->ae_cond == NULL
5760 || PTR2CHAR(aff_entry->ae_cond) == c))
5761 {
5762 p = aff_entry->ae_add
5763 + STRLEN(aff_entry->ae_add);
5764 mb_ptr_back(aff_entry->ae_add, p);
5765 if (PTR2CHAR(p) == c_up)
5766 {
5767 upper = TRUE;
5768 aff_entry->ae_chop = NULL;
5769 *p = NUL;
5770
5771 /* The condition is matched with the
5772 * actual word, thus must check for the
5773 * upper-case letter. */
5774 if (aff_entry->ae_cond != NULL)
5775 {
5776 char_u buf[MAXLINELEN];
5777#ifdef FEAT_MBYTE
5778 if (has_mbyte)
5779 {
5780 onecap_copy(items[4], buf, TRUE);
5781 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005782 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005783 }
5784 else
5785#endif
5786 *aff_entry->ae_cond = c_up;
5787 if (aff_entry->ae_cond != NULL)
5788 {
5789 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005790 aff_entry->ae_cond);
Bram Moolenaar473de612013-06-08 18:19:48 +02005791 vim_regfree(aff_entry->ae_prog);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005792 aff_entry->ae_prog = vim_regcomp(
5793 buf, RE_MAGIC + RE_STRING);
5794 }
5795 }
5796 }
5797 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005798 }
5799
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005800 if (aff_entry->ae_chop == NULL
5801 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005802 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005803 int idx;
5804 char_u **pp;
5805 int n;
5806
Bram Moolenaar6de68532005-08-24 22:08:48 +00005807 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005808 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5809 --idx)
5810 {
5811 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5812 if (str_equal(p, aff_entry->ae_cond))
5813 break;
5814 }
5815 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5816 {
5817 /* Not found, add a new condition. */
5818 idx = spin->si_prefcond.ga_len++;
5819 pp = ((char_u **)spin->si_prefcond.ga_data)
5820 + idx;
5821 if (aff_entry->ae_cond == NULL)
5822 *pp = NULL;
5823 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005824 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005825 aff_entry->ae_cond);
5826 }
5827
5828 /* Add the prefix to the prefix tree. */
5829 if (aff_entry->ae_add == NULL)
5830 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005831 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005832 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005833
Bram Moolenaar53805d12005-08-01 07:08:33 +00005834 /* PFX_FLAGS is a negative number, so that
5835 * tree_add_word() knows this is the prefix tree. */
5836 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005837 if (!cur_aff->ah_combine)
5838 n |= WFP_NC;
5839 if (upper)
5840 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005841 if (aff_entry->ae_comppermit)
5842 n |= WFP_COMPPERMIT;
5843 if (aff_entry->ae_compforbid)
5844 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005845 tree_add_word(spin, p, spin->si_prefroot, n,
5846 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005847 did_postpone_prefix = TRUE;
5848 }
5849
5850 /* Didn't actually use ah_newID, backup si_newprefID. */
5851 if (aff_todo == 0 && !did_postpone_prefix)
5852 {
5853 --spin->si_newprefID;
5854 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005855 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005856 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005857 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005858 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005859 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005860 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005861 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005862 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005863 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005864 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005865 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005866 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005867 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005868 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005870 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005871 else if (is_aff_rule(items, itemcnt, "REP", 2)
5872 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005873 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005874 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005875 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005876 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005877 fname, lnum);
5878 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005879 else if ((STRCMP(items[0], "REP") == 0
5880 || STRCMP(items[0], "REPSAL") == 0)
5881 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005882 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005883 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005884 /* Myspell ignores extra arguments, we require it starts with
5885 * # to detect mistakes. */
5886 if (itemcnt > 3 && items[3][0] != '#')
5887 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005888 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005889 {
5890 /* Replace underscore with space (can't include a space
5891 * directly). */
5892 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5893 if (*p == '_')
5894 *p = ' ';
5895 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5896 if (*p == '_')
5897 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005898 add_fromto(spin, items[0][3] == 'S'
5899 ? &spin->si_repsal
5900 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005902 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005903 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005904 {
5905 /* MAP item or count */
5906 if (!found_map)
5907 {
5908 /* First line contains the count. */
5909 found_map = TRUE;
5910 if (!isdigit(*items[1]))
5911 smsg((char_u *)_("Expected MAP count in %s line %d"),
5912 fname, lnum);
5913 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005914 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005915 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005916 int c;
5917
5918 /* Check that every character appears only once. */
5919 for (p = items[1]; *p != NUL; )
5920 {
5921#ifdef FEAT_MBYTE
5922 c = mb_ptr2char_adv(&p);
5923#else
5924 c = *p++;
5925#endif
5926 if ((spin->si_map.ga_len > 0
5927 && vim_strchr(spin->si_map.ga_data, c)
5928 != NULL)
5929 || vim_strchr(p, c) != NULL)
5930 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5931 fname, lnum);
5932 }
5933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005934 /* We simply concatenate all the MAP strings, separated by
5935 * slashes. */
5936 ga_concat(&spin->si_map, items[1]);
5937 ga_append(&spin->si_map, '/');
5938 }
5939 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005940 /* Accept "SAL from to" and "SAL from to #comment". */
5941 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005942 {
5943 if (do_sal)
5944 {
5945 /* SAL item (sounds-a-like)
5946 * Either one of the known keys or a from-to pair. */
5947 if (STRCMP(items[1], "followup") == 0)
5948 spin->si_followup = sal_to_bool(items[2]);
5949 else if (STRCMP(items[1], "collapse_result") == 0)
5950 spin->si_collapse = sal_to_bool(items[2]);
5951 else if (STRCMP(items[1], "remove_accents") == 0)
5952 spin->si_rem_accents = sal_to_bool(items[2]);
5953 else
5954 /* when "to" is "_" it means empty */
5955 add_fromto(spin, &spin->si_sal, items[1],
5956 STRCMP(items[2], "_") == 0 ? (char_u *)""
5957 : items[2]);
5958 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005959 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005960 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005961 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005962 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005963 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005964 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005965 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005966 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005967 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005968 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005969 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005970 else if (STRCMP(items[0], "COMMON") == 0)
5971 {
5972 int i;
5973
5974 for (i = 1; i < itemcnt; ++i)
5975 {
5976 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5977 items[i])))
5978 {
5979 p = vim_strsave(items[i]);
5980 if (p == NULL)
5981 break;
5982 hash_add(&spin->si_commonwords, p);
5983 }
5984 }
5985 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005986 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005987 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005988 fname, lnum, items[0]);
5989 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005990 }
5991
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005992 if (fol != NULL || low != NULL || upp != NULL)
5993 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005994 if (spin->si_clear_chartab)
5995 {
5996 /* Clear the char type tables, don't want to use any of the
5997 * currently used spell properties. */
5998 init_spell_chartab();
5999 spin->si_clear_chartab = FALSE;
6000 }
6001
Bram Moolenaar3982c542005-06-08 21:56:31 +00006002 /*
6003 * Don't write a word table for an ASCII file, so that we don't check
6004 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006005 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006006 * mb_get_class(), the list of chars in the file will be incomplete.
6007 */
6008 if (!spin->si_ascii
6009#ifdef FEAT_MBYTE
6010 && !enc_utf8
6011#endif
6012 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006013 {
6014 if (fol == NULL || low == NULL || upp == NULL)
6015 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6016 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006017 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006018 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006019
6020 vim_free(fol);
6021 vim_free(low);
6022 vim_free(upp);
6023 }
6024
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006025 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006026 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006027 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006028 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006029 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006030 }
6031
Bram Moolenaar6de68532005-08-24 22:08:48 +00006032 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006033 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006034 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6035 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006036 }
6037
Bram Moolenaar6de68532005-08-24 22:08:48 +00006038 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006039 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006040 if (syllable == NULL)
6041 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6042 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6043 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006044 }
6045
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006046 if (compoptions != 0)
6047 {
6048 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6049 spin->si_compoptions |= compoptions;
6050 }
6051
Bram Moolenaar6de68532005-08-24 22:08:48 +00006052 if (compflags != NULL)
6053 process_compflags(spin, aff, compflags);
6054
6055 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006056 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006057 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006058 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006059 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006060 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006061 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006062 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006063 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006064 }
6065
Bram Moolenaar6de68532005-08-24 22:08:48 +00006066 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006067 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006068 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6069 spin->si_syllable = syllable;
6070 }
6071
6072 if (sofofrom != NULL || sofoto != NULL)
6073 {
6074 if (sofofrom == NULL || sofoto == NULL)
6075 smsg((char_u *)_("Missing SOFO%s line in %s"),
6076 sofofrom == NULL ? "FROM" : "TO", fname);
6077 else if (spin->si_sal.ga_len > 0)
6078 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006079 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006080 {
6081 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6082 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6083 spin->si_sofofr = sofofrom;
6084 spin->si_sofoto = sofoto;
6085 }
6086 }
6087
6088 if (midword != NULL)
6089 {
6090 aff_check_string(spin->si_midword, midword, "MIDWORD");
6091 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006092 }
6093
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006094 vim_free(pc);
6095 fclose(fd);
6096 return aff;
6097}
6098
6099/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006100 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6101 * a comment is following after item "mincount".
6102 */
6103 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006104is_aff_rule(
6105 char_u **items,
6106 int itemcnt,
6107 char *rulename,
6108 int mincount)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006109{
6110 return (STRCMP(items[0], rulename) == 0
6111 && (itemcnt == mincount
6112 || (itemcnt > mincount && items[mincount][0] == '#')));
6113}
6114
6115/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006116 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6117 * ae_flags to ae_comppermit and ae_compforbid.
6118 */
6119 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006120aff_process_flags(afffile_T *affile, affentry_T *entry)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006121{
6122 char_u *p;
6123 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006124 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006125
6126 if (entry->ae_flags != NULL
6127 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6128 {
6129 for (p = entry->ae_flags; *p != NUL; )
6130 {
6131 prevp = p;
6132 flag = get_affitem(affile->af_flagtype, &p);
6133 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6134 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006135 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006136 p = prevp;
6137 if (flag == affile->af_comppermit)
6138 entry->ae_comppermit = TRUE;
6139 else
6140 entry->ae_compforbid = TRUE;
6141 }
6142 if (affile->af_flagtype == AFT_NUM && *p == ',')
6143 ++p;
6144 }
6145 if (*entry->ae_flags == NUL)
6146 entry->ae_flags = NULL; /* nothing left */
6147 }
6148}
6149
6150/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006151 * Return TRUE if "s" is the name of an info item in the affix file.
6152 */
6153 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006154spell_info_item(char_u *s)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006155{
6156 return STRCMP(s, "NAME") == 0
6157 || STRCMP(s, "HOME") == 0
6158 || STRCMP(s, "VERSION") == 0
6159 || STRCMP(s, "AUTHOR") == 0
6160 || STRCMP(s, "EMAIL") == 0
6161 || STRCMP(s, "COPYRIGHT") == 0;
6162}
6163
6164/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006165 * Turn an affix flag name into a number, according to the FLAG type.
6166 * returns zero for failure.
6167 */
6168 static unsigned
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006169affitem2flag(
6170 int flagtype,
6171 char_u *item,
6172 char_u *fname,
6173 int lnum)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006174{
6175 unsigned res;
6176 char_u *p = item;
6177
6178 res = get_affitem(flagtype, &p);
6179 if (res == 0)
6180 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006181 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006182 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6183 fname, lnum, item);
6184 else
6185 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6186 fname, lnum, item);
6187 }
6188 if (*p != NUL)
6189 {
6190 smsg((char_u *)_(e_affname), fname, lnum, item);
6191 return 0;
6192 }
6193
6194 return res;
6195}
6196
6197/*
6198 * Get one affix name from "*pp" and advance the pointer.
6199 * Returns zero for an error, still advances the pointer then.
6200 */
6201 static unsigned
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006202get_affitem(int flagtype, char_u **pp)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006203{
6204 int res;
6205
Bram Moolenaar95529562005-08-25 21:21:38 +00006206 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006207 {
6208 if (!VIM_ISDIGIT(**pp))
6209 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006210 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006211 return 0;
6212 }
6213 res = getdigits(pp);
6214 }
6215 else
6216 {
6217#ifdef FEAT_MBYTE
6218 res = mb_ptr2char_adv(pp);
6219#else
6220 res = *(*pp)++;
6221#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006222 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006223 && res >= 'A' && res <= 'Z'))
6224 {
6225 if (**pp == NUL)
6226 return 0;
6227#ifdef FEAT_MBYTE
6228 res = mb_ptr2char_adv(pp) + (res << 16);
6229#else
6230 res = *(*pp)++ + (res << 16);
6231#endif
6232 }
6233 }
6234 return res;
6235}
6236
6237/*
6238 * Process the "compflags" string used in an affix file and append it to
6239 * spin->si_compflags.
6240 * The processing involves changing the affix names to ID numbers, so that
6241 * they fit in one byte.
6242 */
6243 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006244process_compflags(
6245 spellinfo_T *spin,
6246 afffile_T *aff,
6247 char_u *compflags)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006248{
6249 char_u *p;
6250 char_u *prevp;
6251 unsigned flag;
6252 compitem_T *ci;
6253 int id;
6254 int len;
6255 char_u *tp;
6256 char_u key[AH_KEY_LEN];
6257 hashitem_T *hi;
6258
6259 /* Make room for the old and the new compflags, concatenated with a / in
6260 * between. Processing it makes it shorter, but we don't know by how
6261 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006262 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006263 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006264 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006265 p = getroom(spin, len, FALSE);
6266 if (p == NULL)
6267 return;
6268 if (spin->si_compflags != NULL)
6269 {
6270 STRCPY(p, spin->si_compflags);
6271 STRCAT(p, "/");
6272 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006273 spin->si_compflags = p;
6274 tp = p + STRLEN(p);
6275
6276 for (p = compflags; *p != NUL; )
6277 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006278 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006279 /* Copy non-flag characters directly. */
6280 *tp++ = *p++;
6281 else
6282 {
6283 /* First get the flag number, also checks validity. */
6284 prevp = p;
6285 flag = get_affitem(aff->af_flagtype, &p);
6286 if (flag != 0)
6287 {
6288 /* Find the flag in the hashtable. If it was used before, use
6289 * the existing ID. Otherwise add a new entry. */
6290 vim_strncpy(key, prevp, p - prevp);
6291 hi = hash_find(&aff->af_comp, key);
6292 if (!HASHITEM_EMPTY(hi))
6293 id = HI2CI(hi)->ci_newID;
6294 else
6295 {
6296 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6297 if (ci == NULL)
6298 break;
6299 STRCPY(ci->ci_key, key);
6300 ci->ci_flag = flag;
6301 /* Avoid using a flag ID that has a special meaning in a
6302 * regexp (also inside []). */
6303 do
6304 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006305 check_renumber(spin);
6306 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006307 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006308 ci->ci_newID = id;
6309 hash_add(&aff->af_comp, ci->ci_key);
6310 }
6311 *tp++ = id;
6312 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006313 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006314 ++p;
6315 }
6316 }
6317
6318 *tp = NUL;
6319}
6320
6321/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006322 * Check that the new IDs for postponed affixes and compounding don't overrun
6323 * each other. We have almost 255 available, but start at 0-127 to avoid
6324 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6325 * When that is used up an error message is given.
6326 */
6327 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006328check_renumber(spellinfo_T *spin)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006329{
6330 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6331 {
6332 spin->si_newprefID = 127;
6333 spin->si_newcompID = 255;
6334 }
6335}
6336
6337/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006338 * Return TRUE if flag "flag" appears in affix list "afflist".
6339 */
6340 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006341flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006342{
6343 char_u *p;
6344 unsigned n;
6345
6346 switch (flagtype)
6347 {
6348 case AFT_CHAR:
6349 return vim_strchr(afflist, flag) != NULL;
6350
Bram Moolenaar95529562005-08-25 21:21:38 +00006351 case AFT_CAPLONG:
6352 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006353 for (p = afflist; *p != NUL; )
6354 {
6355#ifdef FEAT_MBYTE
6356 n = mb_ptr2char_adv(&p);
6357#else
6358 n = *p++;
6359#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006360 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006361 && *p != NUL)
6362#ifdef FEAT_MBYTE
6363 n = mb_ptr2char_adv(&p) + (n << 16);
6364#else
6365 n = *p++ + (n << 16);
6366#endif
6367 if (n == flag)
6368 return TRUE;
6369 }
6370 break;
6371
Bram Moolenaar95529562005-08-25 21:21:38 +00006372 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006373 for (p = afflist; *p != NUL; )
6374 {
6375 n = getdigits(&p);
6376 if (n == flag)
6377 return TRUE;
6378 if (*p != NUL) /* skip over comma */
6379 ++p;
6380 }
6381 break;
6382 }
6383 return FALSE;
6384}
6385
6386/*
6387 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6388 */
6389 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006390aff_check_number(int spinval, int affval, char *name)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006391{
6392 if (spinval != 0 && spinval != affval)
6393 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6394}
6395
6396/*
6397 * Give a warning when "spinval" and "affval" strings are set and not the same.
6398 */
6399 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006400aff_check_string(char_u *spinval, char_u *affval, char *name)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006401{
6402 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6403 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6404}
6405
6406/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006407 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6408 * NULL as equal.
6409 */
6410 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006411str_equal(char_u *s1, char_u *s2)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006412{
6413 if (s1 == NULL || s2 == NULL)
6414 return s1 == s2;
6415 return STRCMP(s1, s2) == 0;
6416}
6417
6418/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006419 * Add a from-to item to "gap". Used for REP and SAL items.
6420 * They are stored case-folded.
6421 */
6422 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006423add_fromto(
6424 spellinfo_T *spin,
6425 garray_T *gap,
6426 char_u *from,
6427 char_u *to)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006428{
6429 fromto_T *ftp;
6430 char_u word[MAXWLEN];
6431
6432 if (ga_grow(gap, 1) == OK)
6433 {
6434 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006435 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006437 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006438 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006439 ++gap->ga_len;
6440 }
6441}
6442
6443/*
6444 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6445 */
6446 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006447sal_to_bool(char_u *s)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006448{
6449 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6450}
6451
6452/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006453 * Free the structure filled by spell_read_aff().
6454 */
6455 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006456spell_free_aff(afffile_T *aff)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006457{
6458 hashtab_T *ht;
6459 hashitem_T *hi;
6460 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006461 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006462 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006463
6464 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006465
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006466 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006467 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6468 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006469 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006470 for (hi = ht->ht_array; todo > 0; ++hi)
6471 {
6472 if (!HASHITEM_EMPTY(hi))
6473 {
6474 --todo;
6475 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006476 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar473de612013-06-08 18:19:48 +02006477 vim_regfree(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006478 }
6479 }
6480 if (ht == &aff->af_suff)
6481 break;
6482 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006483
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006484 hash_clear(&aff->af_pref);
6485 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006486 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006487}
6488
6489/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006490 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006491 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006492 */
6493 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006494spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006495{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006496 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006497 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006498 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006499 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006500 char_u store_afflist[MAXWLEN];
6501 int pfxlen;
6502 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006503 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006504 char_u *pc;
6505 char_u *w;
6506 int l;
6507 hash_T hash;
6508 hashitem_T *hi;
6509 FILE *fd;
6510 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006511 int non_ascii = 0;
6512 int retval = OK;
6513 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006514 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006515 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516
Bram Moolenaar51485f02005-06-04 21:55:20 +00006517 /*
6518 * Open the file.
6519 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006520 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006521 if (fd == NULL)
6522 {
6523 EMSG2(_(e_notopen), fname);
6524 return FAIL;
6525 }
6526
Bram Moolenaar51485f02005-06-04 21:55:20 +00006527 /* The hashtable is only used to detect duplicated words. */
6528 hash_init(&ht);
6529
Bram Moolenaar4770d092006-01-12 23:22:24 +00006530 vim_snprintf((char *)IObuff, IOSIZE,
6531 _("Reading dictionary file %s ..."), fname);
6532 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006533
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006534 /* start with a message for the first line */
6535 spin->si_msg_count = 999999;
6536
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006537 /* Read and ignore the first line: word count. */
6538 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006539 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006540 EMSG2(_("E760: No word count in %s"), fname);
6541
6542 /*
6543 * Read all the lines in the file one by one.
6544 * The words are converted to 'encoding' here, before being added to
6545 * the hashtable.
6546 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006547 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006548 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006549 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006550 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006551 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006552 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006553
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 /* Remove CR, LF and white space from the end. White space halfway
6555 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006556 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006557 while (l > 0 && line[l - 1] <= ' ')
6558 --l;
6559 if (l == 0)
6560 continue; /* empty line */
6561 line[l] = NUL;
6562
Bram Moolenaarb765d632005-06-07 21:00:02 +00006563#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006564 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006565 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006566 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006567 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006568 if (pc == NULL)
6569 {
6570 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6571 fname, lnum, line);
6572 continue;
6573 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006574 w = pc;
6575 }
6576 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006577#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006578 {
6579 pc = NULL;
6580 w = line;
6581 }
6582
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006583 /* Truncate the word at the "/", set "afflist" to what follows.
6584 * Replace "\/" by "/" and "\\" by "\". */
6585 afflist = NULL;
6586 for (p = w; *p != NUL; mb_ptr_adv(p))
6587 {
6588 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006589 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006590 else if (*p == '/')
6591 {
6592 *p = NUL;
6593 afflist = p + 1;
6594 break;
6595 }
6596 }
6597
6598 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6599 if (spin->si_ascii && has_non_ascii(w))
6600 {
6601 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006602 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006603 continue;
6604 }
6605
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006606 /* This takes time, print a message every 10000 words. */
6607 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006608 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006609 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006610 vim_snprintf((char *)message, sizeof(message),
6611 _("line %6d, word %6d - %s"),
6612 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6613 msg_start();
6614 msg_puts_long_attr(message, 0);
6615 msg_clr_eos();
6616 msg_didout = FALSE;
6617 msg_col = 0;
6618 out_flush();
6619 }
6620
Bram Moolenaar51485f02005-06-04 21:55:20 +00006621 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006622 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006623 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006624 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006625 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006626 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006627 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006628 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006629
Bram Moolenaar51485f02005-06-04 21:55:20 +00006630 hash = hash_hash(dw);
6631 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006632 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006633 {
6634 if (p_verbose > 0)
6635 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006636 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006637 else if (duplicate == 0)
6638 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6639 fname, lnum, dw);
6640 ++duplicate;
6641 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006642 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006643 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006644
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006645 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006646 store_afflist[0] = NUL;
6647 pfxlen = 0;
6648 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006649 if (afflist != NULL)
6650 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006651 /* Extract flags from the affix list. */
6652 flags |= get_affix_flags(affile, afflist);
6653
Bram Moolenaar6de68532005-08-24 22:08:48 +00006654 if (affile->af_needaffix != 0 && flag_in_afflist(
6655 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006656 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006657
6658 if (affile->af_pfxpostpone)
6659 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006660 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006661
Bram Moolenaar5195e452005-08-19 20:32:47 +00006662 if (spin->si_compflags != NULL)
6663 /* Need to store the list of compound flags with the word.
6664 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006665 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006666 }
6667
Bram Moolenaar51485f02005-06-04 21:55:20 +00006668 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006669 if (store_word(spin, dw, flags, spin->si_region,
6670 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006671 retval = FAIL;
6672
6673 if (afflist != NULL)
6674 {
6675 /* Find all matching suffixes and add the resulting words.
6676 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006677 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006678 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006679 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006680 retval = FAIL;
6681
6682 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006684 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006685 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006686 retval = FAIL;
6687 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006688
6689 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006690 }
6691
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006692 if (duplicate > 0)
6693 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006694 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006695 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6696 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006697 hash_clear(&ht);
6698
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006699 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700 return retval;
6701}
6702
6703/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006704 * Check for affix flags in "afflist" that are turned into word flags.
6705 * Return WF_ flags.
6706 */
6707 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006708get_affix_flags(afffile_T *affile, char_u *afflist)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006709{
6710 int flags = 0;
6711
6712 if (affile->af_keepcase != 0 && flag_in_afflist(
6713 affile->af_flagtype, afflist, affile->af_keepcase))
6714 flags |= WF_KEEPCAP | WF_FIXCAP;
6715 if (affile->af_rare != 0 && flag_in_afflist(
6716 affile->af_flagtype, afflist, affile->af_rare))
6717 flags |= WF_RARE;
6718 if (affile->af_bad != 0 && flag_in_afflist(
6719 affile->af_flagtype, afflist, affile->af_bad))
6720 flags |= WF_BANNED;
6721 if (affile->af_needcomp != 0 && flag_in_afflist(
6722 affile->af_flagtype, afflist, affile->af_needcomp))
6723 flags |= WF_NEEDCOMP;
6724 if (affile->af_comproot != 0 && flag_in_afflist(
6725 affile->af_flagtype, afflist, affile->af_comproot))
6726 flags |= WF_COMPROOT;
6727 if (affile->af_nosuggest != 0 && flag_in_afflist(
6728 affile->af_flagtype, afflist, affile->af_nosuggest))
6729 flags |= WF_NOSUGGEST;
6730 return flags;
6731}
6732
6733/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006734 * Get the list of prefix IDs from the affix list "afflist".
6735 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006736 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6737 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006738 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006739 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006740get_pfxlist(
6741 afffile_T *affile,
6742 char_u *afflist,
6743 char_u *store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006744{
6745 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006746 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006747 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006748 int id;
6749 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006750 hashitem_T *hi;
6751
Bram Moolenaar6de68532005-08-24 22:08:48 +00006752 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006753 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006754 prevp = p;
6755 if (get_affitem(affile->af_flagtype, &p) != 0)
6756 {
6757 /* A flag is a postponed prefix flag if it appears in "af_pref"
6758 * and it's ID is not zero. */
6759 vim_strncpy(key, prevp, p - prevp);
6760 hi = hash_find(&affile->af_pref, key);
6761 if (!HASHITEM_EMPTY(hi))
6762 {
6763 id = HI2AH(hi)->ah_newID;
6764 if (id != 0)
6765 store_afflist[cnt++] = id;
6766 }
6767 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006768 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006769 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006770 }
6771
Bram Moolenaar5195e452005-08-19 20:32:47 +00006772 store_afflist[cnt] = NUL;
6773 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006774}
6775
6776/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006777 * Get the list of compound IDs from the affix list "afflist" that are used
6778 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006779 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006780 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006781 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006782get_compflags(
6783 afffile_T *affile,
6784 char_u *afflist,
6785 char_u *store_afflist)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006786{
6787 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006788 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006789 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006790 char_u key[AH_KEY_LEN];
6791 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006792
Bram Moolenaar6de68532005-08-24 22:08:48 +00006793 for (p = afflist; *p != NUL; )
6794 {
6795 prevp = p;
6796 if (get_affitem(affile->af_flagtype, &p) != 0)
6797 {
6798 /* A flag is a compound flag if it appears in "af_comp". */
6799 vim_strncpy(key, prevp, p - prevp);
6800 hi = hash_find(&affile->af_comp, key);
6801 if (!HASHITEM_EMPTY(hi))
6802 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6803 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006804 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006805 ++p;
6806 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006807
Bram Moolenaar5195e452005-08-19 20:32:47 +00006808 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006809}
6810
6811/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006812 * Apply affixes to a word and store the resulting words.
6813 * "ht" is the hashtable with affentry_T that need to be applied, either
6814 * prefixes or suffixes.
6815 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6816 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006817 *
6818 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006819 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006820 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006821store_aff_word(
6822 spellinfo_T *spin, /* spell info */
6823 char_u *word, /* basic word start */
6824 char_u *afflist, /* list of names of supported affixes */
6825 afffile_T *affile,
6826 hashtab_T *ht,
6827 hashtab_T *xht,
6828 int condit, /* CONDIT_SUF et al. */
6829 int flags, /* flags for the word */
6830 char_u *pfxlist, /* list of prefix IDs */
6831 int pfxlen) /* nr of flags in "pfxlist" for prefixes, rest
Bram Moolenaar5195e452005-08-19 20:32:47 +00006832 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006833{
6834 int todo;
6835 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006836 affheader_T *ah;
6837 affentry_T *ae;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006838 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006839 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006840 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006841 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006842 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006843 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006844 int use_pfxlen;
6845 int need_affix;
6846 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006847 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006848 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006849 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006850
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006851 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006852 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006853 {
6854 if (!HASHITEM_EMPTY(hi))
6855 {
6856 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006857 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006858
Bram Moolenaar51485f02005-06-04 21:55:20 +00006859 /* Check that the affix combines, if required, and that the word
6860 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006861 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6862 && flag_in_afflist(affile->af_flagtype, afflist,
6863 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006864 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006865 /* Loop over all affix entries with this name. */
6866 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006867 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006868 /* Check the condition. It's not logical to match case
6869 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006870 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006871 * Another requirement from Myspell is that the chop
6872 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006873 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006874 * prefixes with a chop string and/or flags.
6875 * When a previously added affix had CIRCUMFIX this one
6876 * must have it too, if it had not then this one must not
6877 * have one either. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006878 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006879 || ae->ae_chop != NULL
6880 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006881 && (ae->ae_chop == NULL
6882 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006883 && (ae->ae_prog == NULL
Bram Moolenaardffa5b82014-11-19 16:38:07 +01006884 || vim_regexec_prog(&ae->ae_prog, FALSE,
6885 word, (colnr_T)0))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006886 && (((condit & CONDIT_CFIX) == 0)
6887 == ((condit & CONDIT_AFF) == 0
6888 || ae->ae_flags == NULL
6889 || !flag_in_afflist(affile->af_flagtype,
6890 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006891 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006892 /* Match. Remove the chop and add the affix. */
6893 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006894 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006895 /* prefix: chop/add at the start of the word */
6896 if (ae->ae_add == NULL)
6897 *newword = NUL;
6898 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006899 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006900 p = word;
6901 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006902 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006903 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006904#ifdef FEAT_MBYTE
6905 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006906 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006907 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006908 for ( ; i > 0; --i)
6909 mb_ptr_adv(p);
6910 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006911 else
6912#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006913 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006914 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006915 STRCAT(newword, p);
6916 }
6917 else
6918 {
6919 /* suffix: chop/add at the end of the word */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006920 vim_strncpy(newword, word, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006921 if (ae->ae_chop != NULL)
6922 {
6923 /* Remove chop string. */
6924 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006925 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006926 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006927 mb_ptr_back(newword, p);
6928 *p = NUL;
6929 }
6930 if (ae->ae_add != NULL)
6931 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006932 }
6933
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006934 use_flags = flags;
6935 use_pfxlist = pfxlist;
6936 use_pfxlen = pfxlen;
6937 need_affix = FALSE;
6938 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6939 if (ae->ae_flags != NULL)
6940 {
6941 /* Extract flags from the affix list. */
6942 use_flags |= get_affix_flags(affile, ae->ae_flags);
6943
6944 if (affile->af_needaffix != 0 && flag_in_afflist(
6945 affile->af_flagtype, ae->ae_flags,
6946 affile->af_needaffix))
6947 need_affix = TRUE;
6948
6949 /* When there is a CIRCUMFIX flag the other affix
6950 * must also have it and we don't add the word
6951 * with one affix. */
6952 if (affile->af_circumfix != 0 && flag_in_afflist(
6953 affile->af_flagtype, ae->ae_flags,
6954 affile->af_circumfix))
6955 {
6956 use_condit |= CONDIT_CFIX;
6957 if ((condit & CONDIT_CFIX) == 0)
6958 need_affix = TRUE;
6959 }
6960
6961 if (affile->af_pfxpostpone
6962 || spin->si_compflags != NULL)
6963 {
6964 if (affile->af_pfxpostpone)
6965 /* Get prefix IDS from the affix list. */
6966 use_pfxlen = get_pfxlist(affile,
6967 ae->ae_flags, store_afflist);
6968 else
6969 use_pfxlen = 0;
6970 use_pfxlist = store_afflist;
6971
6972 /* Combine the prefix IDs. Avoid adding the
6973 * same ID twice. */
6974 for (i = 0; i < pfxlen; ++i)
6975 {
6976 for (j = 0; j < use_pfxlen; ++j)
6977 if (pfxlist[i] == use_pfxlist[j])
6978 break;
6979 if (j == use_pfxlen)
6980 use_pfxlist[use_pfxlen++] = pfxlist[i];
6981 }
6982
6983 if (spin->si_compflags != NULL)
6984 /* Get compound IDS from the affix list. */
6985 get_compflags(affile, ae->ae_flags,
6986 use_pfxlist + use_pfxlen);
6987
6988 /* Combine the list of compound flags.
6989 * Concatenate them to the prefix IDs list.
6990 * Avoid adding the same ID twice. */
6991 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6992 {
6993 for (j = use_pfxlen;
6994 use_pfxlist[j] != NUL; ++j)
6995 if (pfxlist[i] == use_pfxlist[j])
6996 break;
6997 if (use_pfxlist[j] == NUL)
6998 {
6999 use_pfxlist[j++] = pfxlist[i];
7000 use_pfxlist[j] = NUL;
7001 }
7002 }
7003 }
7004 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007005
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007006 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007007 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007008 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007009 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007010 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007011 use_pfxlist = pfx_pfxlist;
7012 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007013
7014 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007015 if (spin->si_prefroot != NULL
7016 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007017 {
7018 /* ... add a flag to indicate an affix was used. */
7019 use_flags |= WF_HAS_AFF;
7020
7021 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007022 * affixes is not allowed. But do use the
7023 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007024 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007025 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007026 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007027
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007028 /* When compounding is supported and there is no
7029 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7030 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007031 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007032 {
7033 if (xht != NULL)
7034 use_flags |= WF_NOCOMPAFT;
7035 else
7036 use_flags |= WF_NOCOMPBEF;
7037 }
7038
Bram Moolenaar51485f02005-06-04 21:55:20 +00007039 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007040 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007041 spin->si_region, use_pfxlist,
7042 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007043 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007044
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007045 /* When added a prefix or a first suffix and the affix
7046 * has flags may add a(nother) suffix. RECURSIVE! */
7047 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7048 if (store_aff_word(spin, newword, ae->ae_flags,
7049 affile, &affile->af_suff, xht,
7050 use_condit & (xht == NULL
7051 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007052 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007053 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007054
7055 /* When added a suffix and combining is allowed also
7056 * try adding a prefix additionally. Both for the
7057 * word flags and for the affix flags. RECURSIVE! */
7058 if (xht != NULL && ah->ah_combine)
7059 {
7060 if (store_aff_word(spin, newword,
7061 afflist, affile,
7062 xht, NULL, use_condit,
7063 use_flags, use_pfxlist,
7064 pfxlen) == FAIL
7065 || (ae->ae_flags != NULL
7066 && store_aff_word(spin, newword,
7067 ae->ae_flags, affile,
7068 xht, NULL, use_condit,
7069 use_flags, use_pfxlist,
7070 pfxlen) == FAIL))
7071 retval = FAIL;
7072 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007073 }
7074 }
7075 }
7076 }
7077 }
7078
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007079 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007080}
7081
7082/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007083 * Read a file with a list of words.
7084 */
7085 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007086spell_read_wordfile(spellinfo_T *spin, char_u *fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087{
7088 FILE *fd;
7089 long lnum = 0;
7090 char_u rline[MAXLINELEN];
7091 char_u *line;
7092 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007093 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007094 int l;
7095 int retval = OK;
7096 int did_word = FALSE;
7097 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007098 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007099 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007100
7101 /*
7102 * Open the file.
7103 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007104 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007105 if (fd == NULL)
7106 {
7107 EMSG2(_(e_notopen), fname);
7108 return FAIL;
7109 }
7110
Bram Moolenaar4770d092006-01-12 23:22:24 +00007111 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7112 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007113
7114 /*
7115 * Read all the lines in the file one by one.
7116 */
7117 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7118 {
7119 line_breakcheck();
7120 ++lnum;
7121
7122 /* Skip comment lines. */
7123 if (*rline == '#')
7124 continue;
7125
7126 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007127 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007128 while (l > 0 && rline[l - 1] <= ' ')
7129 --l;
7130 if (l == 0)
7131 continue; /* empty or blank line */
7132 rline[l] = NUL;
7133
Bram Moolenaar9c102382006-05-03 21:26:49 +00007134 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007135 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007136#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007137 if (spin->si_conv.vc_type != CONV_NONE)
7138 {
7139 pc = string_convert(&spin->si_conv, rline, NULL);
7140 if (pc == NULL)
7141 {
7142 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7143 fname, lnum, rline);
7144 continue;
7145 }
7146 line = pc;
7147 }
7148 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007149#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007150 {
7151 pc = NULL;
7152 line = rline;
7153 }
7154
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007155 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007156 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007157 ++line;
7158 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007159 {
7160 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007161 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7162 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007163 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007164 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7165 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007166 else
7167 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007168#ifdef FEAT_MBYTE
7169 char_u *enc;
7170
Bram Moolenaar51485f02005-06-04 21:55:20 +00007171 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007172 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007173 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007174 if (enc != NULL && !spin->si_ascii
7175 && convert_setup(&spin->si_conv, enc,
7176 p_enc) == FAIL)
7177 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007178 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007179 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007180 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007181#else
7182 smsg((char_u *)_("Conversion in %s not supported"), fname);
7183#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007185 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007186 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007187
Bram Moolenaar3982c542005-06-08 21:56:31 +00007188 if (STRNCMP(line, "regions=", 8) == 0)
7189 {
7190 if (spin->si_region_count > 1)
7191 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7192 fname, lnum, line);
7193 else
7194 {
7195 line += 8;
7196 if (STRLEN(line) > 16)
7197 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7198 fname, lnum, line);
7199 else
7200 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007201 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007202 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007203
7204 /* Adjust the mask for a word valid in all regions. */
7205 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007206 }
7207 }
7208 continue;
7209 }
7210
Bram Moolenaar7887d882005-07-01 22:33:52 +00007211 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7212 fname, lnum, line - 1);
7213 continue;
7214 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007215
Bram Moolenaar7887d882005-07-01 22:33:52 +00007216 flags = 0;
7217 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007218
Bram Moolenaar7887d882005-07-01 22:33:52 +00007219 /* Check for flags and region after a slash. */
7220 p = vim_strchr(line, '/');
7221 if (p != NULL)
7222 {
7223 *p++ = NUL;
7224 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007225 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007226 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007227 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007228 else if (*p == '!') /* Bad, bad, wicked word. */
7229 flags |= WF_BANNED;
7230 else if (*p == '?') /* Rare word. */
7231 flags |= WF_RARE;
7232 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007233 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007234 if ((flags & WF_REGION) == 0) /* first one */
7235 regionmask = 0;
7236 flags |= WF_REGION;
7237
7238 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007239 if (l > spin->si_region_count)
7240 {
7241 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007242 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007243 break;
7244 }
7245 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007246 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007247 else
7248 {
7249 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7250 fname, lnum, p);
7251 break;
7252 }
7253 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007254 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007255 }
7256
7257 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7258 if (spin->si_ascii && has_non_ascii(line))
7259 {
7260 ++non_ascii;
7261 continue;
7262 }
7263
7264 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007265 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007266 {
7267 retval = FAIL;
7268 break;
7269 }
7270 did_word = TRUE;
7271 }
7272
7273 vim_free(pc);
7274 fclose(fd);
7275
Bram Moolenaar4770d092006-01-12 23:22:24 +00007276 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007277 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007278 vim_snprintf((char *)IObuff, IOSIZE,
7279 _("Ignored %d words with non-ASCII characters"), non_ascii);
7280 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007281 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007282
Bram Moolenaar51485f02005-06-04 21:55:20 +00007283 return retval;
7284}
7285
7286/*
7287 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007288 * This avoids calling free() for every little struct we use (and keeping
7289 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007290 * The memory is cleared to all zeros.
7291 * Returns NULL when out of memory.
7292 */
7293 static void *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007294getroom(
7295 spellinfo_T *spin,
7296 size_t len, /* length needed */
7297 int align) /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007298{
7299 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007300 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007301
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007302 if (align && bl != NULL)
7303 /* Round size up for alignment. On some systems structures need to be
7304 * aligned to the size of a pointer (e.g., SPARC). */
7305 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7306 & ~(sizeof(char *) - 1);
7307
Bram Moolenaar51485f02005-06-04 21:55:20 +00007308 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7309 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007310 if (len >= SBLOCKSIZE)
7311 bl = NULL;
7312 else
7313 /* Allocate a block of memory. It is not freed until much later. */
7314 bl = (sblock_T *)alloc_clear(
7315 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007316 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007317 {
7318 if (!spin->si_did_emsg)
7319 {
7320 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7321 spin->si_did_emsg = TRUE;
7322 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007323 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007324 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007325 bl->sb_next = spin->si_blocks;
7326 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007327 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007328 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007329 }
7330
7331 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007332 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007333
7334 return p;
7335}
7336
7337/*
7338 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007339 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007340 */
7341 static char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007342getroom_save(spellinfo_T *spin, char_u *s)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007343{
7344 char_u *sc;
7345
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007346 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007347 if (sc != NULL)
7348 STRCPY(sc, s);
7349 return sc;
7350}
7351
7352
7353/*
7354 * Free the list of allocated sblock_T.
7355 */
7356 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007357free_blocks(sblock_T *bl)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007358{
7359 sblock_T *next;
7360
7361 while (bl != NULL)
7362 {
7363 next = bl->sb_next;
7364 vim_free(bl);
7365 bl = next;
7366 }
7367}
7368
7369/*
7370 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007371 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007372 */
7373 static wordnode_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007374wordtree_alloc(spellinfo_T *spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007375{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007376 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007377}
7378
7379/*
7380 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007381 * Always store it in the case-folded tree. For a keep-case word this is
7382 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7383 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007384 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007385 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7386 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007387 */
7388 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007389store_word(
7390 spellinfo_T *spin,
7391 char_u *word,
7392 int flags, /* extra flags, WF_BANNED */
7393 int region, /* supported region(s) */
7394 char_u *pfxlist, /* list of prefix IDs or NULL */
7395 int need_affix) /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007396{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007398 int ct = captype(word, word + len);
7399 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007400 int res = OK;
7401 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007403 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007404 for (p = pfxlist; res == OK; ++p)
7405 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007406 if (!need_affix || (p != NULL && *p != NUL))
7407 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007408 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007409 if (p == NULL || *p == NUL)
7410 break;
7411 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007412 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007413
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007414 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007415 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007416 for (p = pfxlist; res == OK; ++p)
7417 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007418 if (!need_affix || (p != NULL && *p != NUL))
7419 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007420 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007421 if (p == NULL || *p == NUL)
7422 break;
7423 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007424 ++spin->si_keepwcount;
7425 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007426 return res;
7427}
7428
7429/*
7430 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007431 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007432 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007433 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007434 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007435 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007436tree_add_word(
7437 spellinfo_T *spin,
7438 char_u *word,
7439 wordnode_T *root,
7440 int flags,
7441 int region,
7442 int affixID)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007443{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007444 wordnode_T *node = root;
7445 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007446 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007447 wordnode_T **prev = NULL;
7448 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007449
Bram Moolenaar51485f02005-06-04 21:55:20 +00007450 /* Add each byte of the word to the tree, including the NUL at the end. */
7451 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007452 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007453 /* When there is more than one reference to this node we need to make
7454 * a copy, so that we can modify it. Copy the whole list of siblings
7455 * (we don't optimize for a partly shared list of siblings). */
7456 if (node != NULL && node->wn_refs > 1)
7457 {
7458 --node->wn_refs;
7459 copyprev = prev;
7460 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7461 {
7462 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007463 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007464 if (np == NULL)
7465 return FAIL;
7466 np->wn_child = copyp->wn_child;
7467 if (np->wn_child != NULL)
7468 ++np->wn_child->wn_refs; /* child gets extra ref */
7469 np->wn_byte = copyp->wn_byte;
7470 if (np->wn_byte == NUL)
7471 {
7472 np->wn_flags = copyp->wn_flags;
7473 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007474 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007475 }
7476
7477 /* Link the new node in the list, there will be one ref. */
7478 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007479 if (copyprev != NULL)
7480 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007481 copyprev = &np->wn_sibling;
7482
7483 /* Let "node" point to the head of the copied list. */
7484 if (copyp == node)
7485 node = np;
7486 }
7487 }
7488
Bram Moolenaar51485f02005-06-04 21:55:20 +00007489 /* Look for the sibling that has the same character. They are sorted
7490 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007491 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007492 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007493 while (node != NULL
7494 && (node->wn_byte < word[i]
7495 || (node->wn_byte == NUL
7496 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007497 ? node->wn_affixID < (unsigned)affixID
7498 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007499 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007500 && (spin->si_sugtree
7501 ? (node->wn_region & 0xffff) < region
7502 : node->wn_affixID
7503 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007504 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007505 prev = &node->wn_sibling;
7506 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007507 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007508 if (node == NULL
7509 || node->wn_byte != word[i]
7510 || (word[i] == NUL
7511 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007512 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007513 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007514 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007515 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007516 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007517 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007518 if (np == NULL)
7519 return FAIL;
7520 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007521
7522 /* If "node" is NULL this is a new child or the end of the sibling
7523 * list: ref count is one. Otherwise use ref count of sibling and
7524 * make ref count of sibling one (matters when inserting in front
7525 * of the list of siblings). */
7526 if (node == NULL)
7527 np->wn_refs = 1;
7528 else
7529 {
7530 np->wn_refs = node->wn_refs;
7531 node->wn_refs = 1;
7532 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007533 if (prev != NULL)
7534 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007535 np->wn_sibling = node;
7536 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007537 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007538
Bram Moolenaar51485f02005-06-04 21:55:20 +00007539 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007540 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007541 node->wn_flags = flags;
7542 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007543 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007544 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007545 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007546 prev = &node->wn_child;
7547 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007548 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007549#ifdef SPELL_PRINTTREE
Bram Moolenaar7b877b32016-01-09 13:51:34 +01007550 smsg((char_u *)"Added \"%s\"", word);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007551 spell_print_tree(root->wn_sibling);
7552#endif
7553
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007554 /* count nr of words added since last message */
7555 ++spin->si_msg_count;
7556
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007557 if (spin->si_compress_cnt > 1)
7558 {
7559 if (--spin->si_compress_cnt == 1)
7560 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007561 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007562 }
7563
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007564 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007565 * When we have allocated lots of memory we need to compress the word tree
7566 * to free up some room. But compression is slow, and we might actually
7567 * need that room, thus only compress in the following situations:
7568 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007569 * "compress_start" blocks.
7570 * 2. When compressed before and used "compress_inc" blocks before
7571 * adding "compress_added" words (si_compress_cnt > 1).
7572 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007573 * (si_compress_cnt == 1) and the number of free nodes drops below the
7574 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007575 */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01007576#ifndef SPELL_COMPRESS_ALLWAYS
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007577 if (spin->si_compress_cnt == 1
7578 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007579 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007580#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007581 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007582 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007583 * when the freed up room has been used and another "compress_inc"
7584 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007585 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007586 spin->si_blocks_cnt -= compress_inc;
7587 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007588
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007589 if (spin->si_verbose)
7590 {
7591 msg_start();
7592 msg_puts((char_u *)_(msg_compressing));
7593 msg_clr_eos();
7594 msg_didout = FALSE;
7595 msg_col = 0;
7596 out_flush();
7597 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007598
7599 /* Compress both trees. Either they both have many nodes, which makes
7600 * compression useful, or one of them is small, which means
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007601 * compression goes fast. But when filling the soundfold word tree
Bram Moolenaar4770d092006-01-12 23:22:24 +00007602 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007603 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007604 if (affixID >= 0)
7605 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007606 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007607
7608 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007609}
7610
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007611/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007612 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7613 * Sets "sps_flags".
7614 */
7615 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007616spell_check_msm(void)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007617{
7618 char_u *p = p_msm;
7619 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007620 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007621 long added = 0;
7622
7623 if (!VIM_ISDIGIT(*p))
7624 return FAIL;
7625 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7626 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7627 if (*p != ',')
7628 return FAIL;
7629 ++p;
7630 if (!VIM_ISDIGIT(*p))
7631 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007632 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007633 if (*p != ',')
7634 return FAIL;
7635 ++p;
7636 if (!VIM_ISDIGIT(*p))
7637 return FAIL;
7638 added = getdigits(&p) * 1024;
7639 if (*p != NUL)
7640 return FAIL;
7641
Bram Moolenaar89d40322006-08-29 15:30:07 +00007642 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007643 return FAIL;
7644
7645 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007646 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007647 compress_added = added;
7648 return OK;
7649}
7650
7651
7652/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007653 * Get a wordnode_T, either from the list of previously freed nodes or
7654 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007655 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007656 */
7657 static wordnode_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007658get_wordnode(spellinfo_T *spin)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007659{
7660 wordnode_T *n;
7661
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007662 if (spin->si_first_free == NULL)
7663 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007664 else
7665 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007666 n = spin->si_first_free;
7667 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007668 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007669 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007670 }
7671#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007672 if (n != NULL)
7673 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007674#endif
7675 return n;
7676}
7677
7678/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007679 * Decrement the reference count on a node (which is the head of a list of
7680 * siblings). If the reference count becomes zero free the node and its
7681 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007682 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007683 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007684 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007685deref_wordnode(spellinfo_T *spin, wordnode_T *node)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007686{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007687 wordnode_T *np;
7688 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007689
7690 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007691 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007692 for (np = node; np != NULL; np = np->wn_sibling)
7693 {
7694 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007695 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007696 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007697 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007698 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007699 ++cnt; /* length field */
7700 }
7701 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007702}
7703
7704/*
7705 * Free a wordnode_T for re-use later.
7706 * Only the "wn_child" field becomes invalid.
7707 */
7708 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007709free_wordnode(spellinfo_T *spin, wordnode_T *n)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007710{
7711 n->wn_child = spin->si_first_free;
7712 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007713 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007714}
7715
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007716/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007717 * Compress a tree: find tails that are identical and can be shared.
7718 */
7719 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007720wordtree_compress(spellinfo_T *spin, wordnode_T *root)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007721{
7722 hashtab_T ht;
7723 int n;
7724 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007725 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007726
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007727 /* Skip the root itself, it's not actually used. The first sibling is the
7728 * start of the tree. */
7729 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007730 {
7731 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007732 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007733
7734#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007735 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007736#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007737 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007738 if (tot > 1000000)
7739 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007740 else if (tot == 0)
7741 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007742 else
7743 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007744 vim_snprintf((char *)IObuff, IOSIZE,
7745 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7746 n, tot, tot - n, perc);
7747 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007748 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007749#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007750 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007751#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007752 hash_clear(&ht);
7753 }
7754}
7755
7756/*
7757 * Compress a node, its siblings and its children, depth first.
7758 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007759 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007760 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007761node_compress(
7762 spellinfo_T *spin,
7763 wordnode_T *node,
7764 hashtab_T *ht,
7765 int *tot) /* total count of nodes before compressing,
Bram Moolenaar51485f02005-06-04 21:55:20 +00007766 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007767{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007768 wordnode_T *np;
7769 wordnode_T *tp;
7770 wordnode_T *child;
7771 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007772 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007773 int len = 0;
7774 unsigned nr, n;
7775 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007776
Bram Moolenaar51485f02005-06-04 21:55:20 +00007777 /*
7778 * Go through the list of siblings. Compress each child and then try
7779 * finding an identical child to replace it.
7780 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007781 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007782 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007783 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007784 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007785 ++len;
7786 if ((child = np->wn_child) != NULL)
7787 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007788 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007789 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007790
7791 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007792 hash = hash_hash(child->wn_u1.hashkey);
7793 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007794 if (!HASHITEM_EMPTY(hi))
7795 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007796 /* There are children we encountered before with a hash value
7797 * identical to the current child. Now check if there is one
7798 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007799 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007800 if (node_equal(child, tp))
7801 {
7802 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007803 * current one. This means the current child and all
7804 * its siblings is unlinked from the tree. */
7805 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007806 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007807 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007808 break;
7809 }
7810 if (tp == NULL)
7811 {
7812 /* No other child with this hash value equals the child of
7813 * the node, add it to the linked list after the first
7814 * item. */
7815 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007816 child->wn_u2.next = tp->wn_u2.next;
7817 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007818 }
7819 }
7820 else
7821 /* No other child has this hash value, add it to the
7822 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007823 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007824 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007825 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007826 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007827
7828 /*
7829 * Make a hash key for the node and its siblings, so that we can quickly
7830 * find a lookalike node. This must be done after compressing the sibling
7831 * list, otherwise the hash key would become invalid by the compression.
7832 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007833 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007834 nr = 0;
7835 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007836 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007837 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007838 /* end node: use wn_flags, wn_region and wn_affixID */
7839 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007840 else
7841 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007842 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007843 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007844 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007845
7846 /* Avoid NUL bytes, it terminates the hash key. */
7847 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007848 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007849 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007850 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007851 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007852 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007853 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007854 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7855 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007856
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007857 /* Check for CTRL-C pressed now and then. */
7858 fast_breakcheck();
7859
Bram Moolenaar51485f02005-06-04 21:55:20 +00007860 return compressed;
7861}
7862
7863/*
7864 * Return TRUE when two nodes have identical siblings and children.
7865 */
7866 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007867node_equal(wordnode_T *n1, wordnode_T *n2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007868{
7869 wordnode_T *p1;
7870 wordnode_T *p2;
7871
7872 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7873 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7874 if (p1->wn_byte != p2->wn_byte
7875 || (p1->wn_byte == NUL
7876 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007877 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007878 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007879 : (p1->wn_child != p2->wn_child)))
7880 break;
7881
7882 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007883}
7884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007885static int
7886#ifdef __BORLANDC__
7887_RTLENTRYF
7888#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007889rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007890
7891/*
7892 * Function given to qsort() to sort the REP items on "from" string.
7893 */
7894 static int
7895#ifdef __BORLANDC__
7896_RTLENTRYF
7897#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007898rep_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007899{
7900 fromto_T *p1 = (fromto_T *)s1;
7901 fromto_T *p2 = (fromto_T *)s2;
7902
7903 return STRCMP(p1->ft_from, p2->ft_from);
7904}
7905
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007906/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007907 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007908 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007909 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007910 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007911write_vim_spell(spellinfo_T *spin, char_u *fname)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007912{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007913 FILE *fd;
7914 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007915 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007916 wordnode_T *tree;
7917 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 int i;
7919 int l;
7920 garray_T *gap;
7921 fromto_T *ftp;
7922 char_u *p;
7923 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007924 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007925 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007926 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007927
Bram Moolenaarb765d632005-06-07 21:00:02 +00007928 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007929 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007930 {
7931 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007932 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007933 }
7934
Bram Moolenaar5195e452005-08-19 20:32:47 +00007935 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007936 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007937 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007938 if (fwv != (size_t)1)
7939 /* Catch first write error, don't try writing more. */
7940 goto theend;
7941
Bram Moolenaar5195e452005-08-19 20:32:47 +00007942 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007943
Bram Moolenaar5195e452005-08-19 20:32:47 +00007944 /*
7945 * <SECTIONS>: <section> ... <sectionend>
7946 */
7947
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007948 /* SN_INFO: <infotext> */
7949 if (spin->si_info != NULL)
7950 {
7951 putc(SN_INFO, fd); /* <sectionID> */
7952 putc(0, fd); /* <sectionflags> */
7953
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007954 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007955 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007956 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007957 }
7958
Bram Moolenaar5195e452005-08-19 20:32:47 +00007959 /* SN_REGION: <regionname> ...
7960 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007961 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007962 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007963 putc(SN_REGION, fd); /* <sectionID> */
7964 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7965 l = spin->si_region_count * 2;
7966 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007967 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007968 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007969 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007970 }
7971 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007972 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007973
Bram Moolenaar5195e452005-08-19 20:32:47 +00007974 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7975 *
7976 * The table with character flags and the table for case folding.
7977 * This makes sure the same characters are recognized as word characters
7978 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007979 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007980 * 'encoding'.
7981 * Also skip this for an .add.spl file, the main spell file must contain
7982 * the table (avoids that it conflicts). File is shorter too.
7983 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007984 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007985 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007986 char_u folchars[128 * 8];
7987 int flags;
7988
Bram Moolenaard12a1322005-08-21 22:08:24 +00007989 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007990 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7991
7992 /* Form the <folchars> string first, we need to know its length. */
7993 l = 0;
7994 for (i = 128; i < 256; ++i)
7995 {
7996#ifdef FEAT_MBYTE
7997 if (has_mbyte)
7998 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7999 else
8000#endif
8001 folchars[l++] = spelltab.st_fold[i];
8002 }
8003 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8004
8005 fputc(128, fd); /* <charflagslen> */
8006 for (i = 128; i < 256; ++i)
8007 {
8008 flags = 0;
8009 if (spelltab.st_isw[i])
8010 flags |= CF_WORD;
8011 if (spelltab.st_isu[i])
8012 flags |= CF_UPPER;
8013 fputc(flags, fd); /* <charflags> */
8014 }
8015
8016 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008017 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008018 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008019
Bram Moolenaar5195e452005-08-19 20:32:47 +00008020 /* SN_MIDWORD: <midword> */
8021 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008022 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008023 putc(SN_MIDWORD, fd); /* <sectionID> */
8024 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8025
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008026 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008027 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008028 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8029 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008030 }
8031
Bram Moolenaar5195e452005-08-19 20:32:47 +00008032 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8033 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008034 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008035 putc(SN_PREFCOND, fd); /* <sectionID> */
8036 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8037
8038 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8039 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8040
8041 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008042 }
8043
Bram Moolenaar5195e452005-08-19 20:32:47 +00008044 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008045 * SN_SAL: <salflags> <salcount> <sal> ...
8046 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008047
Bram Moolenaar5195e452005-08-19 20:32:47 +00008048 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008049 * round 2: SN_SAL section (unless SN_SOFO is used)
8050 * round 3: SN_REPSAL section */
8051 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 {
8053 if (round == 1)
8054 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008055 else if (round == 2)
8056 {
8057 /* Don't write SN_SAL when using a SN_SOFO section */
8058 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8059 continue;
8060 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008061 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008062 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008063 gap = &spin->si_repsal;
8064
8065 /* Don't write the section if there are no items. */
8066 if (gap->ga_len == 0)
8067 continue;
8068
8069 /* Sort the REP/REPSAL items. */
8070 if (round != 2)
8071 qsort(gap->ga_data, (size_t)gap->ga_len,
8072 sizeof(fromto_T), rep_compare);
8073
8074 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8075 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008076
Bram Moolenaar5195e452005-08-19 20:32:47 +00008077 /* This is for making suggestions, section is not required. */
8078 putc(0, fd); /* <sectionflags> */
8079
8080 /* Compute the length of what follows. */
8081 l = 2; /* count <repcount> or <salcount> */
8082 for (i = 0; i < gap->ga_len; ++i)
8083 {
8084 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008085 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8086 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008087 }
8088 if (round == 2)
8089 ++l; /* count <salflags> */
8090 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8091
8092 if (round == 2)
8093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008094 i = 0;
8095 if (spin->si_followup)
8096 i |= SAL_F0LLOWUP;
8097 if (spin->si_collapse)
8098 i |= SAL_COLLAPSE;
8099 if (spin->si_rem_accents)
8100 i |= SAL_REM_ACCENTS;
8101 putc(i, fd); /* <salflags> */
8102 }
8103
8104 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8105 for (i = 0; i < gap->ga_len; ++i)
8106 {
8107 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8108 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8109 ftp = &((fromto_T *)gap->ga_data)[i];
8110 for (rr = 1; rr <= 2; ++rr)
8111 {
8112 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008113 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008114 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008115 if (l > 0)
8116 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008117 }
8118 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008120 }
8121
Bram Moolenaar5195e452005-08-19 20:32:47 +00008122 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8123 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008124 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8125 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008126 putc(SN_SOFO, fd); /* <sectionID> */
8127 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008129 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008130 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8131 /* <sectionlen> */
8132
8133 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008134 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008135
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008136 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008137 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008138 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008139 }
8140
Bram Moolenaar4770d092006-01-12 23:22:24 +00008141 /* SN_WORDS: <word> ...
8142 * This is for making suggestions, section is not required. */
8143 if (spin->si_commonwords.ht_used > 0)
8144 {
8145 putc(SN_WORDS, fd); /* <sectionID> */
8146 putc(0, fd); /* <sectionflags> */
8147
8148 /* round 1: count the bytes
8149 * round 2: write the bytes */
8150 for (round = 1; round <= 2; ++round)
8151 {
8152 int todo;
8153 int len = 0;
8154 hashitem_T *hi;
8155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008156 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008157 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8158 if (!HASHITEM_EMPTY(hi))
8159 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008160 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008161 len += l;
8162 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008163 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008164 --todo;
8165 }
8166 if (round == 1)
8167 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8168 }
8169 }
8170
Bram Moolenaar5195e452005-08-19 20:32:47 +00008171 /* SN_MAP: <mapstr>
8172 * This is for making suggestions, section is not required. */
8173 if (spin->si_map.ga_len > 0)
8174 {
8175 putc(SN_MAP, fd); /* <sectionID> */
8176 putc(0, fd); /* <sectionflags> */
8177 l = spin->si_map.ga_len;
8178 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008179 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008180 /* <mapstr> */
8181 }
8182
Bram Moolenaar4770d092006-01-12 23:22:24 +00008183 /* SN_SUGFILE: <timestamp>
8184 * This is used to notify that a .sug file may be available and at the
8185 * same time allows for checking that a .sug file that is found matches
8186 * with this .spl file. That's because the word numbers must be exactly
8187 * right. */
8188 if (!spin->si_nosugfile
8189 && (spin->si_sal.ga_len > 0
8190 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8191 {
8192 putc(SN_SUGFILE, fd); /* <sectionID> */
8193 putc(0, fd); /* <sectionflags> */
8194 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8195
8196 /* Set si_sugtime and write it to the file. */
8197 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008198 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008199 }
8200
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008201 /* SN_NOSPLITSUGS: nothing
8202 * This is used to notify that no suggestions with word splits are to be
8203 * made. */
8204 if (spin->si_nosplitsugs)
8205 {
8206 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8207 putc(0, fd); /* <sectionflags> */
8208 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8209 }
8210
Bram Moolenaar7b877b32016-01-09 13:51:34 +01008211 /* SN_NOCOMPUNDSUGS: nothing
8212 * This is used to notify that no suggestions with compounds are to be
8213 * made. */
8214 if (spin->si_nocompoundsugs)
8215 {
8216 putc(SN_NOCOMPOUNDSUGS, fd); /* <sectionID> */
8217 putc(0, fd); /* <sectionflags> */
8218 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8219 }
8220
Bram Moolenaar5195e452005-08-19 20:32:47 +00008221 /* SN_COMPOUND: compound info.
8222 * We don't mark it required, when not supported all compound words will
8223 * be bad words. */
8224 if (spin->si_compflags != NULL)
8225 {
8226 putc(SN_COMPOUND, fd); /* <sectionID> */
8227 putc(0, fd); /* <sectionflags> */
8228
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008229 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008230 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008231 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008232 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8233
Bram Moolenaar5195e452005-08-19 20:32:47 +00008234 putc(spin->si_compmax, fd); /* <compmax> */
8235 putc(spin->si_compminlen, fd); /* <compminlen> */
8236 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008237 putc(0, fd); /* for Vim 7.0b compatibility */
8238 putc(spin->si_compoptions, fd); /* <compoptions> */
8239 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8240 /* <comppatcount> */
8241 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8242 {
8243 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008244 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008245 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8246 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008247 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008248 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008249 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008250 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008251 }
8252
Bram Moolenaar78622822005-08-23 21:00:13 +00008253 /* SN_NOBREAK: NOBREAK flag */
8254 if (spin->si_nobreak)
8255 {
8256 putc(SN_NOBREAK, fd); /* <sectionID> */
8257 putc(0, fd); /* <sectionflags> */
8258
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008259 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008260 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8261 }
8262
Bram Moolenaar5195e452005-08-19 20:32:47 +00008263 /* SN_SYLLABLE: syllable info.
8264 * We don't mark it required, when not supported syllables will not be
8265 * counted. */
8266 if (spin->si_syllable != NULL)
8267 {
8268 putc(SN_SYLLABLE, fd); /* <sectionID> */
8269 putc(0, fd); /* <sectionflags> */
8270
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008271 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008272 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008273 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8274 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008275 }
8276
8277 /* end of <SECTIONS> */
8278 putc(SN_END, fd); /* <sectionend> */
8279
Bram Moolenaar50cde822005-06-05 21:54:54 +00008280
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008281 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008282 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008283 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008284 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008285 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008286 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008287 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008288 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008289 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008290 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008291 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008292 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008293
Bram Moolenaar0c405862005-06-22 22:26:26 +00008294 /* Clear the index and wnode fields in the tree. */
8295 clear_node(tree);
8296
Bram Moolenaar51485f02005-06-04 21:55:20 +00008297 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008298 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008299 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008300 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008301
Bram Moolenaar51485f02005-06-04 21:55:20 +00008302 /* number of nodes in 4 bytes */
8303 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008304 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008305
Bram Moolenaar51485f02005-06-04 21:55:20 +00008306 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008307 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008308 }
8309
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008310 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008311 if (putc(0, fd) == EOF)
8312 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008313theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008314 if (fclose(fd) == EOF)
8315 retval = FAIL;
8316
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008317 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008318 retval = FAIL;
8319 if (retval == FAIL)
8320 EMSG(_(e_write));
8321
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008322 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008323}
8324
8325/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008326 * Clear the index and wnode fields of "node", it siblings and its
8327 * children. This is needed because they are a union with other items to save
8328 * space.
8329 */
8330 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008331clear_node(wordnode_T *node)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008332{
8333 wordnode_T *np;
8334
8335 if (node != NULL)
8336 for (np = node; np != NULL; np = np->wn_sibling)
8337 {
8338 np->wn_u1.index = 0;
8339 np->wn_u2.wnode = NULL;
8340
8341 if (np->wn_byte != NUL)
8342 clear_node(np->wn_child);
8343 }
8344}
8345
8346
8347/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008348 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008349 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008350 * This first writes the list of possible bytes (siblings). Then for each
8351 * byte recursively write the children.
8352 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008353 * NOTE: The code here must match the code in read_tree_node(), since
8354 * assumptions are made about the indexes (so that we don't have to write them
8355 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008356 *
8357 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008359 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008360put_node(
8361 FILE *fd, /* NULL when only counting */
8362 wordnode_T *node,
8363 int idx,
8364 int regionmask,
8365 int prefixtree) /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008366{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008367 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008368 int siblingcount = 0;
8369 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008370 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008371
Bram Moolenaar51485f02005-06-04 21:55:20 +00008372 /* If "node" is zero the tree is empty. */
8373 if (node == NULL)
8374 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008375
Bram Moolenaar51485f02005-06-04 21:55:20 +00008376 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008377 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008378
8379 /* Count the number of siblings. */
8380 for (np = node; np != NULL; np = np->wn_sibling)
8381 ++siblingcount;
8382
8383 /* Write the sibling count. */
8384 if (fd != NULL)
8385 putc(siblingcount, fd); /* <siblingcount> */
8386
8387 /* Write each sibling byte and optionally extra info. */
8388 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008389 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008390 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008391 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008392 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008393 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008394 /* For a NUL byte (end of word) write the flags etc. */
8395 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008396 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008397 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008398 * associated condition nr (stored in wn_region). The
8399 * byte value is misused to store the "rare" and "not
8400 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008401 if (np->wn_flags == (short_u)PFX_FLAGS)
8402 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008403 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008404 {
8405 putc(BY_FLAGS, fd); /* <byte> */
8406 putc(np->wn_flags, fd); /* <pflags> */
8407 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008408 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008409 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008410 }
8411 else
8412 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008413 /* For word trees we write the flag/region items. */
8414 flags = np->wn_flags;
8415 if (regionmask != 0 && np->wn_region != regionmask)
8416 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008417 if (np->wn_affixID != 0)
8418 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008419 if (flags == 0)
8420 {
8421 /* word without flags or region */
8422 putc(BY_NOFLAGS, fd); /* <byte> */
8423 }
8424 else
8425 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008426 if (np->wn_flags >= 0x100)
8427 {
8428 putc(BY_FLAGS2, fd); /* <byte> */
8429 putc(flags, fd); /* <flags> */
8430 putc((unsigned)flags >> 8, fd); /* <flags2> */
8431 }
8432 else
8433 {
8434 putc(BY_FLAGS, fd); /* <byte> */
8435 putc(flags, fd); /* <flags> */
8436 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008437 if (flags & WF_REGION)
8438 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008439 if (flags & WF_AFX)
8440 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008441 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008442 }
8443 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008444 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008445 else
8446 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008447 if (np->wn_child->wn_u1.index != 0
8448 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008449 {
8450 /* The child is written elsewhere, write the reference. */
8451 if (fd != NULL)
8452 {
8453 putc(BY_INDEX, fd); /* <byte> */
8454 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008455 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008456 }
8457 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008458 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008459 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008460 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008461
Bram Moolenaar51485f02005-06-04 21:55:20 +00008462 if (fd != NULL)
8463 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8464 {
8465 EMSG(_(e_write));
8466 return 0;
8467 }
8468 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008469 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008470
8471 /* Space used in the array when reading: one for each sibling and one for
8472 * the count. */
8473 newindex += siblingcount + 1;
8474
8475 /* Recursively dump the children of each sibling. */
8476 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008477 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8478 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008479 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008480
8481 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008482}
8483
8484
8485/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008486 * ":mkspell [-ascii] outfile infile ..."
8487 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008488 */
8489 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008490ex_mkspell(exarg_T *eap)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008491{
8492 int fcount;
8493 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008494 char_u *arg = eap->arg;
8495 int ascii = FALSE;
8496
8497 if (STRNCMP(arg, "-ascii", 6) == 0)
8498 {
8499 ascii = TRUE;
8500 arg = skipwhite(arg + 6);
8501 }
8502
8503 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02008504 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008507 FreeWild(fcount, fnames);
8508 }
8509}
8510
8511/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008512 * Create the .sug file.
8513 * Uses the soundfold info in "spin".
8514 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8515 */
8516 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008517spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008518{
Bram Moolenaard9462e32011-04-11 21:35:11 +02008519 char_u *fname = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008520 int len;
8521 slang_T *slang;
8522 int free_slang = FALSE;
8523
8524 /*
8525 * Read back the .spl file that was written. This fills the required
8526 * info for soundfolding. This also uses less memory than the
8527 * pointer-linked version of the trie. And it avoids having two versions
8528 * of the code for the soundfolding stuff.
8529 * It might have been done already by spell_reload_one().
8530 */
8531 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8532 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8533 break;
8534 if (slang == NULL)
8535 {
8536 spell_message(spin, (char_u *)_("Reading back spell file..."));
8537 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8538 if (slang == NULL)
8539 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008540 free_slang = TRUE;
8541 }
8542
8543 /*
8544 * Clear the info in "spin" that is used.
8545 */
8546 spin->si_blocks = NULL;
8547 spin->si_blocks_cnt = 0;
8548 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8549 spin->si_free_count = 0;
8550 spin->si_first_free = NULL;
8551 spin->si_foldwcount = 0;
8552
8553 /*
8554 * Go through the trie of good words, soundfold each word and add it to
8555 * the soundfold trie.
8556 */
8557 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8558 if (sug_filltree(spin, slang) == FAIL)
8559 goto theend;
8560
8561 /*
8562 * Create the table which links each soundfold word with a list of the
8563 * good words it may come from. Creates buffer "spin->si_spellbuf".
8564 * This also removes the wordnr from the NUL byte entries to make
8565 * compression possible.
8566 */
8567 if (sug_maketable(spin) == FAIL)
8568 goto theend;
8569
8570 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8571 (long)spin->si_spellbuf->b_ml.ml_line_count);
8572
8573 /*
8574 * Compress the soundfold trie.
8575 */
8576 spell_message(spin, (char_u *)_(msg_compressing));
8577 wordtree_compress(spin, spin->si_foldroot);
8578
8579 /*
8580 * Write the .sug file.
8581 * Make the file name by changing ".spl" to ".sug".
8582 */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008583 fname = alloc(MAXPATHL);
8584 if (fname == NULL)
8585 goto theend;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008586 vim_strncpy(fname, wfname, MAXPATHL - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008587 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008588 fname[len - 2] = 'u';
8589 fname[len - 1] = 'g';
8590 sug_write(spin, fname);
8591
8592theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02008593 vim_free(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008594 if (free_slang)
8595 slang_free(slang);
8596 free_blocks(spin->si_blocks);
8597 close_spellbuf(spin->si_spellbuf);
8598}
8599
8600/*
8601 * Build the soundfold trie for language "slang".
8602 */
8603 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008604sug_filltree(spellinfo_T *spin, slang_T *slang)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008605{
8606 char_u *byts;
8607 idx_T *idxs;
8608 int depth;
8609 idx_T arridx[MAXWLEN];
8610 int curi[MAXWLEN];
8611 char_u tword[MAXWLEN];
8612 char_u tsalword[MAXWLEN];
8613 int c;
8614 idx_T n;
8615 unsigned words_done = 0;
8616 int wordcount[MAXWLEN];
8617
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008618 /* We use si_foldroot for the soundfolded trie. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008619 spin->si_foldroot = wordtree_alloc(spin);
8620 if (spin->si_foldroot == NULL)
8621 return FAIL;
8622
8623 /* let tree_add_word() know we're adding to the soundfolded tree */
8624 spin->si_sugtree = TRUE;
8625
8626 /*
8627 * Go through the whole case-folded tree, soundfold each word and put it
8628 * in the trie.
8629 */
8630 byts = slang->sl_fbyts;
8631 idxs = slang->sl_fidxs;
8632
8633 arridx[0] = 0;
8634 curi[0] = 1;
8635 wordcount[0] = 0;
8636
8637 depth = 0;
8638 while (depth >= 0 && !got_int)
8639 {
8640 if (curi[depth] > byts[arridx[depth]])
8641 {
8642 /* Done all bytes at this node, go up one level. */
8643 idxs[arridx[depth]] = wordcount[depth];
8644 if (depth > 0)
8645 wordcount[depth - 1] += wordcount[depth];
8646
8647 --depth;
8648 line_breakcheck();
8649 }
8650 else
8651 {
8652
8653 /* Do one more byte at this node. */
8654 n = arridx[depth] + curi[depth];
8655 ++curi[depth];
8656
8657 c = byts[n];
8658 if (c == 0)
8659 {
8660 /* Sound-fold the word. */
8661 tword[depth] = NUL;
8662 spell_soundfold(slang, tword, TRUE, tsalword);
8663
8664 /* We use the "flags" field for the MSB of the wordnr,
8665 * "region" for the LSB of the wordnr. */
8666 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8667 words_done >> 16, words_done & 0xffff,
8668 0) == FAIL)
8669 return FAIL;
8670
8671 ++words_done;
8672 ++wordcount[depth];
8673
8674 /* Reset the block count each time to avoid compression
8675 * kicking in. */
8676 spin->si_blocks_cnt = 0;
8677
8678 /* Skip over any other NUL bytes (same word with different
8679 * flags). */
8680 while (byts[n + 1] == 0)
8681 {
8682 ++n;
8683 ++curi[depth];
8684 }
8685 }
8686 else
8687 {
8688 /* Normal char, go one level deeper. */
8689 tword[depth++] = c;
8690 arridx[depth] = idxs[n];
8691 curi[depth] = 1;
8692 wordcount[depth] = 0;
8693 }
8694 }
8695 }
8696
8697 smsg((char_u *)_("Total number of words: %d"), words_done);
8698
8699 return OK;
8700}
8701
8702/*
8703 * Make the table that links each word in the soundfold trie to the words it
8704 * can be produced from.
8705 * This is not unlike lines in a file, thus use a memfile to be able to access
8706 * the table efficiently.
8707 * Returns FAIL when out of memory.
8708 */
8709 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008710sug_maketable(spellinfo_T *spin)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008711{
8712 garray_T ga;
8713 int res = OK;
8714
8715 /* Allocate a buffer, open a memline for it and create the swap file
8716 * (uses a temp file, not a .swp file). */
8717 spin->si_spellbuf = open_spellbuf();
8718 if (spin->si_spellbuf == NULL)
8719 return FAIL;
8720
8721 /* Use a buffer to store the line info, avoids allocating many small
8722 * pieces of memory. */
8723 ga_init2(&ga, 1, 100);
8724
8725 /* recursively go through the tree */
8726 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8727 res = FAIL;
8728
8729 ga_clear(&ga);
8730 return res;
8731}
8732
8733/*
8734 * Fill the table for one node and its children.
8735 * Returns the wordnr at the start of the node.
8736 * Returns -1 when out of memory.
8737 */
8738 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008739sug_filltable(
8740 spellinfo_T *spin,
8741 wordnode_T *node,
8742 int startwordnr,
8743 garray_T *gap) /* place to store line of numbers */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008744{
8745 wordnode_T *p, *np;
8746 int wordnr = startwordnr;
8747 int nr;
8748 int prev_nr;
8749
8750 for (p = node; p != NULL; p = p->wn_sibling)
8751 {
8752 if (p->wn_byte == NUL)
8753 {
8754 gap->ga_len = 0;
8755 prev_nr = 0;
8756 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8757 {
8758 if (ga_grow(gap, 10) == FAIL)
8759 return -1;
8760
8761 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8762 /* Compute the offset from the previous nr and store the
8763 * offset in a way that it takes a minimum number of bytes.
8764 * It's a bit like utf-8, but without the need to mark
8765 * following bytes. */
8766 nr -= prev_nr;
8767 prev_nr += nr;
8768 gap->ga_len += offset2bytes(nr,
8769 (char_u *)gap->ga_data + gap->ga_len);
8770 }
8771
8772 /* add the NUL byte */
8773 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8774
8775 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8776 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8777 return -1;
8778 ++wordnr;
8779
8780 /* Remove extra NUL entries, we no longer need them. We don't
8781 * bother freeing the nodes, the won't be reused anyway. */
8782 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8783 p->wn_sibling = p->wn_sibling->wn_sibling;
8784
8785 /* Clear the flags on the remaining NUL node, so that compression
8786 * works a lot better. */
8787 p->wn_flags = 0;
8788 p->wn_region = 0;
8789 }
8790 else
8791 {
8792 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8793 if (wordnr == -1)
8794 return -1;
8795 }
8796 }
8797 return wordnr;
8798}
8799
8800/*
8801 * Convert an offset into a minimal number of bytes.
8802 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8803 * bytes.
8804 */
8805 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008806offset2bytes(int nr, char_u *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008807{
8808 int rem;
8809 int b1, b2, b3, b4;
8810
8811 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8812 b1 = nr % 255 + 1;
8813 rem = nr / 255;
8814 b2 = rem % 255 + 1;
8815 rem = rem / 255;
8816 b3 = rem % 255 + 1;
8817 b4 = rem / 255 + 1;
8818
8819 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8820 {
8821 buf[0] = 0xe0 + b4;
8822 buf[1] = b3;
8823 buf[2] = b2;
8824 buf[3] = b1;
8825 return 4;
8826 }
8827 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8828 {
8829 buf[0] = 0xc0 + b3;
8830 buf[1] = b2;
8831 buf[2] = b1;
8832 return 3;
8833 }
8834 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8835 {
8836 buf[0] = 0x80 + b2;
8837 buf[1] = b1;
8838 return 2;
8839 }
8840 /* 1 byte */
8841 buf[0] = b1;
8842 return 1;
8843}
8844
8845/*
8846 * Opposite of offset2bytes().
8847 * "pp" points to the bytes and is advanced over it.
8848 * Returns the offset.
8849 */
8850 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008851bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008852{
8853 char_u *p = *pp;
8854 int nr;
8855 int c;
8856
8857 c = *p++;
8858 if ((c & 0x80) == 0x00) /* 1 byte */
8859 {
8860 nr = c - 1;
8861 }
8862 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8863 {
8864 nr = (c & 0x3f) - 1;
8865 nr = nr * 255 + (*p++ - 1);
8866 }
8867 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8868 {
8869 nr = (c & 0x1f) - 1;
8870 nr = nr * 255 + (*p++ - 1);
8871 nr = nr * 255 + (*p++ - 1);
8872 }
8873 else /* 4 bytes */
8874 {
8875 nr = (c & 0x0f) - 1;
8876 nr = nr * 255 + (*p++ - 1);
8877 nr = nr * 255 + (*p++ - 1);
8878 nr = nr * 255 + (*p++ - 1);
8879 }
8880
8881 *pp = p;
8882 return nr;
8883}
8884
8885/*
8886 * Write the .sug file in "fname".
8887 */
8888 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008889sug_write(spellinfo_T *spin, char_u *fname)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008890{
8891 FILE *fd;
8892 wordnode_T *tree;
8893 int nodecount;
8894 int wcount;
8895 char_u *line;
8896 linenr_T lnum;
8897 int len;
8898
8899 /* Create the file. Note that an existing file is silently overwritten! */
8900 fd = mch_fopen((char *)fname, "w");
8901 if (fd == NULL)
8902 {
8903 EMSG2(_(e_notopen), fname);
8904 return;
8905 }
8906
8907 vim_snprintf((char *)IObuff, IOSIZE,
8908 _("Writing suggestion file %s ..."), fname);
8909 spell_message(spin, IObuff);
8910
8911 /*
8912 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8913 */
8914 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8915 {
8916 EMSG(_(e_write));
8917 goto theend;
8918 }
8919 putc(VIMSUGVERSION, fd); /* <versionnr> */
8920
8921 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008922 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008923
8924 /*
8925 * <SUGWORDTREE>
8926 */
8927 spin->si_memtot = 0;
8928 tree = spin->si_foldroot->wn_sibling;
8929
8930 /* Clear the index and wnode fields in the tree. */
8931 clear_node(tree);
8932
8933 /* Count the number of nodes. Needed to be able to allocate the
8934 * memory when reading the nodes. Also fills in index for shared
8935 * nodes. */
8936 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8937
8938 /* number of nodes in 4 bytes */
8939 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8940 spin->si_memtot += nodecount + nodecount * sizeof(int);
8941
8942 /* Write the nodes. */
8943 (void)put_node(fd, tree, 0, 0, FALSE);
8944
8945 /*
8946 * <SUGTABLE>: <sugwcount> <sugline> ...
8947 */
8948 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8949 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8950
8951 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8952 {
8953 /* <sugline>: <sugnr> ... NUL */
8954 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008955 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008956 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8957 {
8958 EMSG(_(e_write));
8959 goto theend;
8960 }
8961 spin->si_memtot += len;
8962 }
8963
8964 /* Write another byte to check for errors. */
8965 if (putc(0, fd) == EOF)
8966 EMSG(_(e_write));
8967
8968 vim_snprintf((char *)IObuff, IOSIZE,
8969 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8970 spell_message(spin, IObuff);
8971
8972theend:
8973 /* close the file */
8974 fclose(fd);
8975}
8976
8977/*
8978 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8979 * list and only contains text lines. Can use a swapfile to reduce memory
8980 * use.
8981 * Most other fields are invalid! Esp. watch out for string options being
8982 * NULL and there is no undo info.
8983 * Returns NULL when out of memory.
8984 */
8985 static buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008986open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008987{
8988 buf_T *buf;
8989
8990 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8991 if (buf != NULL)
8992 {
8993 buf->b_spell = TRUE;
8994 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02008995#ifdef FEAT_CRYPT
8996 buf->b_p_key = empty_option;
8997#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00008998 ml_open(buf);
8999 ml_open_file(buf); /* create swap file now */
9000 }
9001 return buf;
9002}
9003
9004/*
9005 * Close the buffer used for spell info.
9006 */
9007 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009008close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009009{
9010 if (buf != NULL)
9011 {
9012 ml_close(buf, TRUE);
9013 vim_free(buf);
9014 }
9015}
9016
9017
9018/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009019 * Create a Vim spell file from one or more word lists.
9020 * "fnames[0]" is the output file name.
9021 * "fnames[fcount - 1]" is the last input file name.
9022 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9023 * and ".spl" is appended to make the output file name.
9024 */
9025 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009026mkspell(
9027 int fcount,
9028 char_u **fnames,
9029 int ascii, /* -ascii argument given */
9030 int over_write, /* overwrite existing output file */
9031 int added_word) /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009032{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009033 char_u *fname = NULL;
9034 char_u *wfname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009035 char_u **innames;
9036 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009037 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009038 int i;
9039 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009040 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009041 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009042 spellinfo_T spin;
9043
9044 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009046 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 spin.si_followup = TRUE;
9048 spin.si_rem_accents = TRUE;
9049 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009050 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9052 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009053 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009054 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009055 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009056 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009057
Bram Moolenaarb765d632005-06-07 21:00:02 +00009058 /* default: fnames[0] is output file, following are input files */
9059 innames = &fnames[1];
9060 incount = fcount - 1;
9061
Bram Moolenaard9462e32011-04-11 21:35:11 +02009062 wfname = alloc(MAXPATHL);
9063 if (wfname == NULL)
9064 return;
9065
Bram Moolenaarb765d632005-06-07 21:00:02 +00009066 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009067 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009068 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009069 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9070 {
9071 /* For ":mkspell path/en.latin1.add" output file is
9072 * "path/en.latin1.add.spl". */
9073 innames = &fnames[0];
9074 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009075 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009076 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009077 else if (fcount == 1)
9078 {
9079 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9080 innames = &fnames[0];
9081 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009082 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009083 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009084 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009085 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9086 {
9087 /* Name ends in ".spl", use as the file name. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009088 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009089 }
9090 else
9091 /* Name should be language, make the file name from it. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009092 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009093 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009094
9095 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009096 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009097 spin.si_ascii = TRUE;
9098
9099 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009100 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009101 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009102 }
9103
Bram Moolenaarb765d632005-06-07 21:00:02 +00009104 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009105 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009106 else if (vim_strchr(gettail(wfname), '_') != NULL)
9107 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009108 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109 EMSG(_("E754: Only up to 8 regions supported"));
9110 else
9111 {
9112 /* Check for overwriting before doing things that may take a lot of
9113 * time. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009114 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009115 {
9116 EMSG(_(e_exists));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009117 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009118 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009119 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009120 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009121 EMSG2(_(e_isadir2), wfname);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009122 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009123 }
9124
Bram Moolenaard9462e32011-04-11 21:35:11 +02009125 fname = alloc(MAXPATHL);
9126 if (fname == NULL)
9127 goto theend;
9128
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009129 /*
9130 * Init the aff and dic pointers.
9131 * Get the region names if there are more than 2 arguments.
9132 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009133 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009135 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009136
Bram Moolenaar3982c542005-06-08 21:56:31 +00009137 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009139 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009140 if (STRLEN(gettail(innames[i])) < 5
9141 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009142 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009143 EMSG2(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009144 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009145 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009146 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9147 spin.si_region_name[i * 2 + 1] =
9148 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009149 }
9150 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009151 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009152
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009153 spin.si_foldroot = wordtree_alloc(&spin);
9154 spin.si_keeproot = wordtree_alloc(&spin);
9155 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009156 if (spin.si_foldroot == NULL
9157 || spin.si_keeproot == NULL
9158 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009159 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009160 free_blocks(spin.si_blocks);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009161 goto theend;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009162 }
9163
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009164 /* When not producing a .add.spl file clear the character table when
9165 * we encounter one in the .aff file. This means we dump the current
9166 * one in the .spl file if the .aff file doesn't define one. That's
9167 * better than guessing the contents, the table will match a
9168 * previously loaded spell file. */
9169 if (!spin.si_add)
9170 spin.si_clear_chartab = TRUE;
9171
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009172 /*
9173 * Read all the .aff and .dic files.
9174 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009175 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009176 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009177 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009178 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009179 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009180 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009181
Bram Moolenaard9462e32011-04-11 21:35:11 +02009182 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009183 if (mch_stat((char *)fname, &st) >= 0)
9184 {
9185 /* Read the .aff file. Will init "spin->si_conv" based on the
9186 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009187 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009188 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009189 error = TRUE;
9190 else
9191 {
9192 /* Read the .dic file and store the words in the trees. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009193 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009194 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009195 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009196 error = TRUE;
9197 }
9198 }
9199 else
9200 {
9201 /* No .aff file, try reading the file as a word list. Store
9202 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009203 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009204 error = TRUE;
9205 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009206
Bram Moolenaarb765d632005-06-07 21:00:02 +00009207#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009208 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009209 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009210#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009211 }
9212
Bram Moolenaar78622822005-08-23 21:00:13 +00009213 if (spin.si_compflags != NULL && spin.si_nobreak)
9214 MSG(_("Warning: both compounding and NOBREAK specified"));
9215
Bram Moolenaar4770d092006-01-12 23:22:24 +00009216 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009217 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009218 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009219 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009220 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009221 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009222 wordtree_compress(&spin, spin.si_foldroot);
9223 wordtree_compress(&spin, spin.si_keeproot);
9224 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009225 }
9226
Bram Moolenaar4770d092006-01-12 23:22:24 +00009227 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009228 {
9229 /*
9230 * Write the info in the spell file.
9231 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009232 vim_snprintf((char *)IObuff, IOSIZE,
9233 _("Writing spell file %s ..."), wfname);
9234 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009235
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009236 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009237
Bram Moolenaar4770d092006-01-12 23:22:24 +00009238 spell_message(&spin, (char_u *)_("Done!"));
9239 vim_snprintf((char *)IObuff, IOSIZE,
9240 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9241 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009242
Bram Moolenaar4770d092006-01-12 23:22:24 +00009243 /*
9244 * If the file is loaded need to reload it.
9245 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009246 if (!error)
9247 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009248 }
9249
9250 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009252 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 ga_clear(&spin.si_sal);
9254 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009255 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009256 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009257 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009258
9259 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009260 for (i = 0; i < incount; ++i)
9261 if (afile[i] != NULL)
9262 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009263
9264 /* Free all the bits and pieces at once. */
9265 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009266
9267 /*
9268 * If there is soundfolding info and no NOSUGFILE item create the
9269 * .sug file with the soundfolded word trie.
9270 */
9271 if (spin.si_sugtime != 0 && !error && !got_int)
9272 spell_make_sugfile(&spin, wfname);
9273
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009274 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009275
9276theend:
9277 vim_free(fname);
9278 vim_free(wfname);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009279}
9280
Bram Moolenaar4770d092006-01-12 23:22:24 +00009281/*
9282 * Display a message for spell file processing when 'verbose' is set or using
9283 * ":mkspell". "str" can be IObuff.
9284 */
9285 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009286spell_message(spellinfo_T *spin, char_u *str)
Bram Moolenaar4770d092006-01-12 23:22:24 +00009287{
9288 if (spin->si_verbose || p_verbose > 2)
9289 {
9290 if (!spin->si_verbose)
9291 verbose_enter();
9292 MSG(str);
9293 out_flush();
9294 if (!spin->si_verbose)
9295 verbose_leave();
9296 }
9297}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009298
9299/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009300 * ":[count]spellgood {word}"
9301 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009302 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009303 */
9304 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009305ex_spell(exarg_T *eap)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009306{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009307 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009308 eap->forceit ? 0 : (int)eap->line2,
9309 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009310}
9311
9312/*
9313 * Add "word[len]" to 'spellfile' as a good or bad word.
9314 */
9315 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009316spell_add_word(
9317 char_u *word,
9318 int len,
9319 int bad,
9320 int idx, /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009321 'spellfile' */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009322 int undo) /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009323{
Bram Moolenaara3917072006-09-14 08:48:14 +00009324 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009325 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009326 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009327 char_u *fname;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009328 char_u *fnamebuf = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009329 char_u line[MAXWLEN * 2];
9330 long fpos, fpos_next = 0;
9331 int i;
9332 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009333
Bram Moolenaar89d40322006-08-29 15:30:07 +00009334 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009335 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009336 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009337 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +02009338 int_wordlist = vim_tempname('s', FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009339 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009340 return;
9341 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009342 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009343 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009344 else
9345 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009346 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009347 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009348 {
9349 init_spellfile();
9350 new_spf = TRUE;
9351 }
9352
Bram Moolenaar860cae12010-06-05 23:22:07 +02009353 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009354 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009355 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009356 return;
9357 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009358 fnamebuf = alloc(MAXPATHL);
9359 if (fnamebuf == NULL)
9360 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009361
Bram Moolenaar860cae12010-06-05 23:22:07 +02009362 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009363 {
9364 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009365 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009366 break;
9367 if (*spf == NUL)
9368 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009369 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009370 vim_free(fnamebuf);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009371 return;
9372 }
9373 }
9374
Bram Moolenaarb765d632005-06-07 21:00:02 +00009375 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009376 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009377 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9378 buf = NULL;
9379 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009380 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009381 EMSG(_(e_bufloaded));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009382 vim_free(fnamebuf);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009383 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009384 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009385
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009386 fname = fnamebuf;
9387 }
9388
Bram Moolenaard0131a82006-03-04 21:46:13 +00009389 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009390 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009391 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009392 * since its flags sort before the one with WF_BANNED. */
9393 fd = mch_fopen((char *)fname, "r");
9394 if (fd != NULL)
9395 {
9396 while (!vim_fgets(line, MAXWLEN * 2, fd))
9397 {
9398 fpos = fpos_next;
9399 fpos_next = ftell(fd);
9400 if (STRNCMP(word, line, len) == 0
9401 && (line[len] == '/' || line[len] < ' '))
9402 {
9403 /* Found duplicate word. Remove it by writing a '#' at
9404 * the start of the line. Mixing reading and writing
9405 * doesn't work for all systems, close the file first. */
9406 fclose(fd);
9407 fd = mch_fopen((char *)fname, "r+");
9408 if (fd == NULL)
9409 break;
9410 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009411 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009412 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009413 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009414 {
9415 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009416 smsg((char_u *)_("Word '%.*s' removed from %s"),
9417 len, word, NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009418 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009419 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009420 fseek(fd, fpos_next, SEEK_SET);
9421 }
9422 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009423 if (fd != NULL)
9424 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009425 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009426 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009427
9428 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009429 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009430 fd = mch_fopen((char *)fname, "a");
9431 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009432 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009433 char_u *p;
9434
Bram Moolenaard0131a82006-03-04 21:46:13 +00009435 /* We just initialized the 'spellfile' option and can't open the
9436 * file. We may need to create the "spell" directory first. We
9437 * already checked the runtime directory is writable in
9438 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009439 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009440 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009441 int c = *p;
9442
Bram Moolenaard0131a82006-03-04 21:46:13 +00009443 /* The directory doesn't exist. Try creating it and opening
9444 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009445 *p = NUL;
9446 vim_mkdir(fname, 0755);
9447 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009448 fd = mch_fopen((char *)fname, "a");
9449 }
9450 }
9451
9452 if (fd == NULL)
9453 EMSG2(_(e_notopen), fname);
9454 else
9455 {
9456 if (bad)
9457 fprintf(fd, "%.*s/!\n", len, word);
9458 else
9459 fprintf(fd, "%.*s\n", len, word);
9460 fclose(fd);
9461
9462 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009463 smsg((char_u *)_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009464 }
9465 }
9466
Bram Moolenaard0131a82006-03-04 21:46:13 +00009467 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009468 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009469 /* Update the .add.spl file. */
9470 mkspell(1, &fname, FALSE, TRUE, TRUE);
9471
9472 /* If the .add file is edited somewhere, reload it. */
9473 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009474 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009475
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009476 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009477 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009478 vim_free(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009479}
9480
9481/*
9482 * Initialize 'spellfile' for the current buffer.
9483 */
9484 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009485init_spellfile(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009486{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009487 char_u *buf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009488 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009489 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009490 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009491 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009492 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009493 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009494
Bram Moolenaar860cae12010-06-05 23:22:07 +02009495 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009496 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009497 buf = alloc(MAXPATHL);
9498 if (buf == NULL)
9499 return;
9500
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009501 /* Find the end of the language name. Exclude the region. If there
9502 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009503 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009504 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009505 if (vim_ispathsep(*lend))
9506 {
9507 aspath = TRUE;
9508 lstart = lend + 1;
9509 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009510
9511 /* Loop over all entries in 'runtimepath'. Use the first one where we
9512 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009513 rtp = p_rtp;
9514 while (*rtp != NUL)
9515 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009516 if (aspath)
9517 /* Use directory of an entry with path, e.g., for
9518 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009519 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9520 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009521 else
9522 /* Copy the path from 'runtimepath' to buf[]. */
9523 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009524 if (filewritable(buf) == 2)
9525 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009526 /* Use the first language name from 'spelllang' and the
9527 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009528 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009529 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9530 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009531 else
9532 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009533 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009534 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009535 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009536 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009537 vim_mkdir(buf, 0755);
9538
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009539 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009540 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009541 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009542 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009543 l = (int)STRLEN(buf);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009544 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9545 ->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009546 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9547 fname != NULL
9548 && strstr((char *)gettail(fname), ".ascii.") != NULL
9549 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009550 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9551 break;
9552 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009553 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009554 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009555
9556 vim_free(buf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009557 }
9558}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009559
Bram Moolenaar51485f02005-06-04 21:55:20 +00009560
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009561/*
9562 * Init the chartab used for spelling for ASCII.
9563 * EBCDIC is not supported!
9564 */
9565 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009566clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009567{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009568 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009569
9570 /* Init everything to FALSE. */
9571 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9572 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9573 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009574 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009575 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009576 sp->st_upper[i] = i;
9577 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009578
9579 /* We include digits. A word shouldn't start with a digit, but handling
9580 * that is done separately. */
9581 for (i = '0'; i <= '9'; ++i)
9582 sp->st_isw[i] = TRUE;
9583 for (i = 'A'; i <= 'Z'; ++i)
9584 {
9585 sp->st_isw[i] = TRUE;
9586 sp->st_isu[i] = TRUE;
9587 sp->st_fold[i] = i + 0x20;
9588 }
9589 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009590 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009591 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009592 sp->st_upper[i] = i - 0x20;
9593 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009594}
9595
9596/*
9597 * Init the chartab used for spelling. Only depends on 'encoding'.
9598 * Called once while starting up and when 'encoding' changes.
9599 * The default is to use isalpha(), but the spell file should define the word
9600 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009601 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009602 */
9603 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009604init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009605{
9606 int i;
9607
9608 did_set_spelltab = FALSE;
9609 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009610#ifdef FEAT_MBYTE
9611 if (enc_dbcs)
9612 {
9613 /* DBCS: assume double-wide characters are word characters. */
9614 for (i = 128; i <= 255; ++i)
9615 if (MB_BYTE2LEN(i) == 2)
9616 spelltab.st_isw[i] = TRUE;
9617 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009618 else if (enc_utf8)
9619 {
9620 for (i = 128; i < 256; ++i)
9621 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009622 int f = utf_fold(i);
9623 int u = utf_toupper(i);
9624
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009625 spelltab.st_isu[i] = utf_isupper(i);
9626 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009627 /* The folded/upper-cased value is different between latin1 and
9628 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9629 * value for utf-8 to avoid this. */
9630 spelltab.st_fold[i] = (f < 256) ? f : i;
9631 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009632 }
9633 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009634 else
9635#endif
9636 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009637 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009638 for (i = 128; i < 256; ++i)
9639 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009640 if (MB_ISUPPER(i))
9641 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009642 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009643 spelltab.st_isu[i] = TRUE;
9644 spelltab.st_fold[i] = MB_TOLOWER(i);
9645 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009646 else if (MB_ISLOWER(i))
9647 {
9648 spelltab.st_isw[i] = TRUE;
9649 spelltab.st_upper[i] = MB_TOUPPER(i);
9650 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009651 }
9652 }
9653}
9654
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009655/*
9656 * Set the spell character tables from strings in the affix file.
9657 */
9658 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009659set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660{
9661 /* We build the new tables here first, so that we can compare with the
9662 * previous one. */
9663 spelltab_T new_st;
9664 char_u *pf = fol, *pl = low, *pu = upp;
9665 int f, l, u;
9666
9667 clear_spell_chartab(&new_st);
9668
9669 while (*pf != NUL)
9670 {
9671 if (*pl == NUL || *pu == NUL)
9672 {
9673 EMSG(_(e_affform));
9674 return FAIL;
9675 }
9676#ifdef FEAT_MBYTE
9677 f = mb_ptr2char_adv(&pf);
9678 l = mb_ptr2char_adv(&pl);
9679 u = mb_ptr2char_adv(&pu);
9680#else
9681 f = *pf++;
9682 l = *pl++;
9683 u = *pu++;
9684#endif
9685 /* Every character that appears is a word character. */
9686 if (f < 256)
9687 new_st.st_isw[f] = TRUE;
9688 if (l < 256)
9689 new_st.st_isw[l] = TRUE;
9690 if (u < 256)
9691 new_st.st_isw[u] = TRUE;
9692
9693 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9694 * case-folding */
9695 if (l < 256 && l != f)
9696 {
9697 if (f >= 256)
9698 {
9699 EMSG(_(e_affrange));
9700 return FAIL;
9701 }
9702 new_st.st_fold[l] = f;
9703 }
9704
9705 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009706 * case-folding, it's upper case and the "UPP" is the upper case of
9707 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009708 if (u < 256 && u != f)
9709 {
9710 if (f >= 256)
9711 {
9712 EMSG(_(e_affrange));
9713 return FAIL;
9714 }
9715 new_st.st_fold[u] = f;
9716 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009717 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009718 }
9719 }
9720
9721 if (*pl != NUL || *pu != NUL)
9722 {
9723 EMSG(_(e_affform));
9724 return FAIL;
9725 }
9726
9727 return set_spell_finish(&new_st);
9728}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009729
9730/*
9731 * Set the spell character tables from strings in the .spl file.
9732 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009733 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009734set_spell_charflags(
9735 char_u *flags,
9736 int cnt, /* length of "flags" */
9737 char_u *fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009738{
9739 /* We build the new tables here first, so that we can compare with the
9740 * previous one. */
9741 spelltab_T new_st;
9742 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009743 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009744 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009745
9746 clear_spell_chartab(&new_st);
9747
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009748 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009749 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009750 if (i < cnt)
9751 {
9752 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9753 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9754 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009755
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009756 if (*p != NUL)
9757 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009758#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009759 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009760#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009761 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009762#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009763 new_st.st_fold[i + 128] = c;
9764 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9765 new_st.st_upper[c] = i + 128;
9766 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009767 }
9768
Bram Moolenaar5195e452005-08-19 20:32:47 +00009769 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009770}
9771
9772 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009773set_spell_finish(spelltab_T *new_st)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009774{
9775 int i;
9776
9777 if (did_set_spelltab)
9778 {
9779 /* check that it's the same table */
9780 for (i = 0; i < 256; ++i)
9781 {
9782 if (spelltab.st_isw[i] != new_st->st_isw[i]
9783 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009784 || spelltab.st_fold[i] != new_st->st_fold[i]
9785 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009786 {
9787 EMSG(_("E763: Word characters differ between spell files"));
9788 return FAIL;
9789 }
9790 }
9791 }
9792 else
9793 {
9794 /* copy the new spelltab into the one being used */
9795 spelltab = *new_st;
9796 did_set_spelltab = TRUE;
9797 }
9798
9799 return OK;
9800}
9801
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009802/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009803 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009804 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009805 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009806 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009807 */
9808 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009809spell_iswordp(
9810 char_u *p,
9811 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009812{
Bram Moolenaarea408852005-06-25 22:49:46 +00009813#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009814 char_u *s;
9815 int l;
9816 int c;
9817
9818 if (has_mbyte)
9819 {
9820 l = MB_BYTE2LEN(*p);
9821 s = p;
9822 if (l == 1)
9823 {
9824 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009825 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009826 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009827 }
9828 else
9829 {
9830 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009831 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9832 : (wp->w_s->b_spell_ismw_mb != NULL
9833 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009834 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009835 }
9836
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009837 c = mb_ptr2char(s);
9838 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009839 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009840 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009841 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009842#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009843
Bram Moolenaar860cae12010-06-05 23:22:07 +02009844 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009845}
9846
9847/*
9848 * Return TRUE if "p" points to a word character.
9849 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9850 */
9851 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009852spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009853{
9854#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009855 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009856
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009857 if (has_mbyte)
9858 {
9859 c = mb_ptr2char(p);
9860 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009861 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009862 return spelltab.st_isw[c];
9863 }
9864#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009865 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009866}
9867
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009868#ifdef FEAT_MBYTE
9869/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009870 * Return TRUE if word class indicates a word character.
9871 * Only for characters above 255.
9872 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009873 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009874 */
9875 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009876spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009877{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009878 if (wp->w_s->b_cjk)
9879 /* East Asian characters are not considered word characters. */
9880 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009881 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9882}
9883
9884/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009885 * Return TRUE if "p" points to a word character.
9886 * Wide version of spell_iswordp().
9887 */
9888 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009889spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009890{
9891 int *s;
9892
Bram Moolenaar860cae12010-06-05 23:22:07 +02009893 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9894 : (wp->w_s->b_spell_ismw_mb != NULL
9895 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009896 s = p + 1;
9897 else
9898 s = p;
9899
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009900 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009901 {
9902 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009903 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009904 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009905 return spell_mb_isword_class(
9906 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009907 return 0;
9908 }
9909 return spelltab.st_isw[*s];
9910}
9911#endif
9912
Bram Moolenaarea408852005-06-25 22:49:46 +00009913/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009914 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009915 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009916 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009917 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009918write_spell_prefcond(FILE *fd, garray_T *gap)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009919{
9920 int i;
9921 char_u *p;
9922 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009923 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009924 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009925
Bram Moolenaar5195e452005-08-19 20:32:47 +00009926 if (fd != NULL)
9927 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9928
9929 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009930
9931 for (i = 0; i < gap->ga_len; ++i)
9932 {
9933 /* <prefcond> : <condlen> <condstr> */
9934 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009935 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009936 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009937 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009938 if (fd != NULL)
9939 {
9940 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009941 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009942 }
9943 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009944 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009945 else if (fd != NULL)
9946 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009947 }
9948
Bram Moolenaar5195e452005-08-19 20:32:47 +00009949 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009950}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009951
9952/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009953 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9954 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009955 * When using a multi-byte 'encoding' the length may change!
9956 * Returns FAIL when something wrong.
9957 */
9958 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009959spell_casefold(
9960 char_u *str,
9961 int len,
9962 char_u *buf,
9963 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009964{
9965 int i;
9966
9967 if (len >= buflen)
9968 {
9969 buf[0] = NUL;
9970 return FAIL; /* result will not fit */
9971 }
9972
9973#ifdef FEAT_MBYTE
9974 if (has_mbyte)
9975 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009976 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009977 char_u *p;
9978 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009979
9980 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009981 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009982 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009983 if (outi + MB_MAXBYTES > buflen)
9984 {
9985 buf[outi] = NUL;
9986 return FAIL;
9987 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009988 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009989 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009990 }
9991 buf[outi] = NUL;
9992 }
9993 else
9994#endif
9995 {
9996 /* Be quick for non-multibyte encodings. */
9997 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009998 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009999 buf[i] = NUL;
10000 }
10001
10002 return OK;
10003}
10004
Bram Moolenaar4770d092006-01-12 23:22:24 +000010005/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010006#define SPS_BEST 1
10007#define SPS_FAST 2
10008#define SPS_DOUBLE 4
10009
Bram Moolenaar4770d092006-01-12 23:22:24 +000010010static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10011static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010012
10013/*
10014 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010015 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010016 */
10017 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010018spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010019{
10020 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010021 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010022 char_u buf[MAXPATHL];
10023 int f;
10024
10025 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010026 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010027
10028 for (p = p_sps; *p != NUL; )
10029 {
10030 copy_option_part(&p, buf, MAXPATHL, ",");
10031
10032 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010033 if (VIM_ISDIGIT(*buf))
10034 {
10035 s = buf;
10036 sps_limit = getdigits(&s);
10037 if (*s != NUL && !VIM_ISDIGIT(*s))
10038 f = -1;
10039 }
10040 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010041 f = SPS_BEST;
10042 else if (STRCMP(buf, "fast") == 0)
10043 f = SPS_FAST;
10044 else if (STRCMP(buf, "double") == 0)
10045 f = SPS_DOUBLE;
10046 else if (STRNCMP(buf, "expr:", 5) != 0
10047 && STRNCMP(buf, "file:", 5) != 0)
10048 f = -1;
10049
10050 if (f == -1 || (sps_flags != 0 && f != 0))
10051 {
10052 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010053 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010054 return FAIL;
10055 }
10056 if (f != 0)
10057 sps_flags = f;
10058 }
10059
10060 if (sps_flags == 0)
10061 sps_flags = SPS_BEST;
10062
10063 return OK;
10064}
10065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066/*
Bram Moolenaar134bf072013-09-25 18:54:24 +020010067 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010069 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010070 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010071 */
10072 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010073spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074{
10075 char_u *line;
10076 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 char_u wcopy[MAXWLEN + 2];
10078 char_u *p;
10079 int i;
10080 int c;
10081 suginfo_T sug;
10082 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010083 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010084 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010085 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010086 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010087 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010088 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010090 if (no_spell_checking(curwin))
10091 return;
10092
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010093 if (VIsual_active)
10094 {
10095 /* Use the Visually selected text as the bad word. But reject
10096 * a multi-line selection. */
10097 if (curwin->w_cursor.lnum != VIsual.lnum)
10098 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010099 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010100 return;
10101 }
10102 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10103 if (badlen < 0)
10104 badlen = -badlen;
10105 else
10106 curwin->w_cursor.col = VIsual.col;
10107 ++badlen;
10108 end_visual_mode();
10109 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010110 /* Find the start of the badly spelled word. */
10111 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010112 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010114 /* No bad word or it starts after the cursor: use the word under the
10115 * cursor. */
10116 curwin->w_cursor = prev_cursor;
10117 line = ml_get_curline();
10118 p = line + curwin->w_cursor.col;
10119 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010120 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010121 mb_ptr_back(line, p);
10122 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010123 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010124 mb_ptr_adv(p);
10125
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010126 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010127 {
10128 beep_flush();
10129 return;
10130 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010131 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 }
10133
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010134 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010136 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010137 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010138
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010139 /* Make a copy of current line since autocommands may free the line. */
10140 line = vim_strsave(ml_get_curline());
10141 if (line == NULL)
10142 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010143
Bram Moolenaar5195e452005-08-19 20:32:47 +000010144 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10145 * 'spellsuggest', whatever is smaller. */
10146 if (sps_limit > (int)Rows - 2)
10147 limit = (int)Rows - 2;
10148 else
10149 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010150 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010151 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152
10153 if (sug.su_ga.ga_len == 0)
10154 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010155 else if (count > 0)
10156 {
10157 if (count > sug.su_ga.ga_len)
10158 smsg((char_u *)_("Sorry, only %ld suggestions"),
10159 (long)sug.su_ga.ga_len);
10160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010161 else
10162 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010163 vim_free(repl_from);
10164 repl_from = NULL;
10165 vim_free(repl_to);
10166 repl_to = NULL;
10167
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010168#ifdef FEAT_RIGHTLEFT
10169 /* When 'rightleft' is set the list is drawn right-left. */
10170 cmdmsg_rl = curwin->w_p_rl;
10171 if (cmdmsg_rl)
10172 msg_col = Columns - 1;
10173#endif
10174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175 /* List the suggestions. */
10176 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010177 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010178 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10180 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010181#ifdef FEAT_RIGHTLEFT
10182 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10183 {
10184 /* And now the rabbit from the high hat: Avoid showing the
10185 * untranslated message rightleft. */
10186 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10187 sug.su_badlen, sug.su_badptr);
10188 }
10189#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 msg_puts(IObuff);
10191 msg_clr_eos();
10192 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 msg_scroll = TRUE;
10195 for (i = 0; i < sug.su_ga.ga_len; ++i)
10196 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010197 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198
10199 /* The suggested word may replace only part of the bad word, add
10200 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020010201 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010203 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 sug.su_badptr + stp->st_orglen,
10205 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010206 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10207#ifdef FEAT_RIGHTLEFT
10208 if (cmdmsg_rl)
10209 rl_mirror(IObuff);
10210#endif
10211 msg_puts(IObuff);
10212
10213 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010214 msg_puts(IObuff);
10215
10216 /* The word may replace more than "su_badlen". */
10217 if (sug.su_badlen < stp->st_orglen)
10218 {
10219 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10220 stp->st_orglen, sug.su_badptr);
10221 msg_puts(IObuff);
10222 }
10223
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010224 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010225 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010226 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010227 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010228 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010229 stp->st_salscore ? "s " : "",
10230 stp->st_score, stp->st_altscore);
10231 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010232 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010233 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010234#ifdef FEAT_RIGHTLEFT
10235 if (cmdmsg_rl)
10236 /* Mirror the numbers, but keep the leading space. */
10237 rl_mirror(IObuff + 1);
10238#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010239 msg_advance(30);
10240 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 msg_putchar('\n');
10243 }
10244
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010245#ifdef FEAT_RIGHTLEFT
10246 cmdmsg_rl = FALSE;
10247 msg_col = 0;
10248#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010250 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010251 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010252 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010253 lines_left = Rows; /* avoid more prompt */
10254 /* don't delay for 'smd' in normal_cmd() */
10255 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 }
10257
Bram Moolenaard12a1322005-08-21 22:08:24 +000010258 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10259 {
10260 /* Save the from and to text for :spellrepall. */
10261 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010262 if (sug.su_badlen > stp->st_orglen)
10263 {
10264 /* Replacing less than "su_badlen", append the remainder to
10265 * repl_to. */
10266 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10267 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10268 sug.su_badlen - stp->st_orglen,
10269 sug.su_badptr + stp->st_orglen);
10270 repl_to = vim_strsave(IObuff);
10271 }
10272 else
10273 {
10274 /* Replacing su_badlen or more, use the whole word. */
10275 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10276 repl_to = vim_strsave(stp->st_word);
10277 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010278
10279 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010280 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10281 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010282 if (p != NULL)
10283 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010284 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010285 mch_memmove(p, line, c);
10286 STRCPY(p + c, stp->st_word);
10287 STRCAT(p, sug.su_badptr + stp->st_orglen);
10288 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10289 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010290
10291 /* For redo we use a change-word command. */
10292 ResetRedobuff();
10293 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010294 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010295 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010296 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010297
10298 /* After this "p" may be invalid. */
10299 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010300 }
10301 }
10302 else
10303 curwin->w_cursor = prev_cursor;
10304
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010305 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010306skip:
10307 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010308}
10309
10310/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010311 * Check if the word at line "lnum" column "col" is required to start with a
10312 * capital. This uses 'spellcapcheck' of the current buffer.
10313 */
10314 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010315check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010316{
10317 int need_cap = FALSE;
10318 char_u *line;
10319 char_u *line_copy = NULL;
10320 char_u *p;
10321 colnr_T endcol;
10322 regmatch_T regmatch;
10323
Bram Moolenaar860cae12010-06-05 23:22:07 +020010324 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010325 return FALSE;
10326
10327 line = ml_get_curline();
10328 endcol = 0;
10329 if ((int)(skipwhite(line) - line) >= (int)col)
10330 {
10331 /* At start of line, check if previous line is empty or sentence
10332 * ends there. */
10333 if (lnum == 1)
10334 need_cap = TRUE;
10335 else
10336 {
10337 line = ml_get(lnum - 1);
10338 if (*skipwhite(line) == NUL)
10339 need_cap = TRUE;
10340 else
10341 {
10342 /* Append a space in place of the line break. */
10343 line_copy = concat_str(line, (char_u *)" ");
10344 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010345 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010346 }
10347 }
10348 }
10349 else
10350 endcol = col;
10351
10352 if (endcol > 0)
10353 {
10354 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010355 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010356 regmatch.rm_ic = FALSE;
10357 p = line + endcol;
10358 for (;;)
10359 {
10360 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010361 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010362 break;
10363 if (vim_regexec(&regmatch, p, 0)
10364 && regmatch.endp[0] == line + endcol)
10365 {
10366 need_cap = TRUE;
10367 break;
10368 }
10369 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +010010370 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010371 }
10372
10373 vim_free(line_copy);
10374
10375 return need_cap;
10376}
10377
10378
10379/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010380 * ":spellrepall"
10381 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010382 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010383ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010384{
10385 pos_T pos = curwin->w_cursor;
10386 char_u *frompat;
10387 int addlen;
10388 char_u *line;
10389 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010390 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010391 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010392
10393 if (repl_from == NULL || repl_to == NULL)
10394 {
10395 EMSG(_("E752: No previous spell replacement"));
10396 return;
10397 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010398 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010399
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010400 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010401 if (frompat == NULL)
10402 return;
10403 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10404 p_ws = FALSE;
10405
Bram Moolenaar5195e452005-08-19 20:32:47 +000010406 sub_nsubs = 0;
10407 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010408 curwin->w_cursor.lnum = 0;
10409 while (!got_int)
10410 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010411 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010412 || u_save_cursor() == FAIL)
10413 break;
10414
10415 /* Only replace when the right word isn't there yet. This happens
10416 * when changing "etc" to "etc.". */
10417 line = ml_get_curline();
10418 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10419 repl_to, STRLEN(repl_to)) != 0)
10420 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010421 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010422 if (p == NULL)
10423 break;
10424 mch_memmove(p, line, curwin->w_cursor.col);
10425 STRCPY(p + curwin->w_cursor.col, repl_to);
10426 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10427 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10428 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010429
10430 if (curwin->w_cursor.lnum != prev_lnum)
10431 {
10432 ++sub_nlines;
10433 prev_lnum = curwin->w_cursor.lnum;
10434 }
10435 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010436 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010437 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010438 }
10439
10440 p_ws = save_ws;
10441 curwin->w_cursor = pos;
10442 vim_free(frompat);
10443
Bram Moolenaar5195e452005-08-19 20:32:47 +000010444 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010445 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010446 else
10447 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010448}
10449
10450/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010451 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10452 * a list of allocated strings.
10453 */
10454 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010455spell_suggest_list(
10456 garray_T *gap,
10457 char_u *word,
10458 int maxcount, /* maximum nr of suggestions */
10459 int need_cap, /* 'spellcapcheck' matched */
10460 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010461{
10462 suginfo_T sug;
10463 int i;
10464 suggest_T *stp;
10465 char_u *wcopy;
10466
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010467 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010468
10469 /* Make room in "gap". */
10470 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010471 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010472 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010473 for (i = 0; i < sug.su_ga.ga_len; ++i)
10474 {
10475 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010476
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010477 /* The suggested word may replace only part of "word", add the not
10478 * replaced part. */
10479 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010480 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010481 if (wcopy == NULL)
10482 break;
10483 STRCPY(wcopy, stp->st_word);
10484 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10485 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10486 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010487 }
10488
10489 spell_find_cleanup(&sug);
10490}
10491
10492/*
10493 * Find spell suggestions for the word at the start of "badptr".
10494 * Return the suggestions in "su->su_ga".
10495 * The maximum number of suggestions is "maxcount".
10496 * Note: does use info for the current window.
10497 * This is based on the mechanisms of Aspell, but completely reimplemented.
10498 */
10499 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010500spell_find_suggest(
10501 char_u *badptr,
10502 int badlen, /* length of bad word or 0 if unknown */
10503 suginfo_T *su,
10504 int maxcount,
10505 int banbadword, /* don't include badword in suggestions */
10506 int need_cap, /* word should start with capital */
10507 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010508{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010509 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010510 char_u buf[MAXPATHL];
10511 char_u *p;
10512 int do_combine = FALSE;
10513 char_u *sps_copy;
10514#ifdef FEAT_EVAL
10515 static int expr_busy = FALSE;
10516#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010517 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010518 int i;
10519 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010520
10521 /*
10522 * Set the info in "*su".
10523 */
10524 vim_memset(su, 0, sizeof(suginfo_T));
10525 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10526 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010527 if (*badptr == NUL)
10528 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010529 hash_init(&su->su_banned);
10530
10531 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010532 if (badlen != 0)
10533 su->su_badlen = badlen;
10534 else
10535 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010536 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010537 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010538
10539 if (su->su_badlen >= MAXWLEN)
10540 su->su_badlen = MAXWLEN - 1; /* just in case */
10541 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10542 (void)spell_casefold(su->su_badptr, su->su_badlen,
10543 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010544 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010545 su->su_badflags = badword_captype(su->su_badptr,
10546 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010547 if (need_cap)
10548 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010549
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010550 /* Find the default language for sound folding. We simply use the first
10551 * one in 'spelllang' that supports sound folding. That's good for when
10552 * using multiple files for one language, it's not that bad when mixing
10553 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010554 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010555 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010556 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010557 if (lp->lp_sallang != NULL)
10558 {
10559 su->su_sallang = lp->lp_sallang;
10560 break;
10561 }
10562 }
10563
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010564 /* Soundfold the bad word with the default sound folding, so that we don't
10565 * have to do this many times. */
10566 if (su->su_sallang != NULL)
10567 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10568 su->su_sal_badword);
10569
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010570 /* If the word is not capitalised and spell_check() doesn't consider the
10571 * word to be bad then it might need to be capitalised. Add a suggestion
10572 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010573 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010574 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010575 {
10576 make_case_word(su->su_badword, buf, WF_ONECAP);
10577 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010578 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010579 }
10580
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010581 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010582 if (banbadword)
10583 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010584
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010585 /* Make a copy of 'spellsuggest', because the expression may change it. */
10586 sps_copy = vim_strsave(p_sps);
10587 if (sps_copy == NULL)
10588 return;
10589
10590 /* Loop over the items in 'spellsuggest'. */
10591 for (p = sps_copy; *p != NUL; )
10592 {
10593 copy_option_part(&p, buf, MAXPATHL, ",");
10594
10595 if (STRNCMP(buf, "expr:", 5) == 0)
10596 {
10597#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010598 /* Evaluate an expression. Skip this when called recursively,
10599 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010600 if (!expr_busy)
10601 {
10602 expr_busy = TRUE;
10603 spell_suggest_expr(su, buf + 5);
10604 expr_busy = FALSE;
10605 }
10606#endif
10607 }
10608 else if (STRNCMP(buf, "file:", 5) == 0)
10609 /* Use list of suggestions in a file. */
10610 spell_suggest_file(su, buf + 5);
10611 else
10612 {
10613 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010614 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010615 if (sps_flags & SPS_DOUBLE)
10616 do_combine = TRUE;
10617 }
10618 }
10619
10620 vim_free(sps_copy);
10621
10622 if (do_combine)
10623 /* Combine the two list of suggestions. This must be done last,
10624 * because sorting changes the order again. */
10625 score_combine(su);
10626}
10627
10628#ifdef FEAT_EVAL
10629/*
10630 * Find suggestions by evaluating expression "expr".
10631 */
10632 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010633spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010634{
10635 list_T *list;
10636 listitem_T *li;
10637 int score;
10638 char_u *p;
10639
10640 /* The work is split up in a few parts to avoid having to export
10641 * suginfo_T.
10642 * First evaluate the expression and get the resulting list. */
10643 list = eval_spell_expr(su->su_badword, expr);
10644 if (list != NULL)
10645 {
10646 /* Loop over the items in the list. */
10647 for (li = list->lv_first; li != NULL; li = li->li_next)
10648 if (li->li_tv.v_type == VAR_LIST)
10649 {
10650 /* Get the word and the score from the items. */
10651 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010652 if (score >= 0 && score <= su->su_maxscore)
10653 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10654 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010655 }
10656 list_unref(list);
10657 }
10658
Bram Moolenaar4770d092006-01-12 23:22:24 +000010659 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10660 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010661 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10662}
10663#endif
10664
10665/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010666 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010667 */
10668 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010669spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010670{
10671 FILE *fd;
10672 char_u line[MAXWLEN * 2];
10673 char_u *p;
10674 int len;
10675 char_u cword[MAXWLEN];
10676
10677 /* Open the file. */
10678 fd = mch_fopen((char *)fname, "r");
10679 if (fd == NULL)
10680 {
10681 EMSG2(_(e_notopen), fname);
10682 return;
10683 }
10684
10685 /* Read it line by line. */
10686 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10687 {
10688 line_breakcheck();
10689
10690 p = vim_strchr(line, '/');
10691 if (p == NULL)
10692 continue; /* No Tab found, just skip the line. */
10693 *p++ = NUL;
10694 if (STRICMP(su->su_badword, line) == 0)
10695 {
10696 /* Match! Isolate the good word, until CR or NL. */
10697 for (len = 0; p[len] >= ' '; ++len)
10698 ;
10699 p[len] = NUL;
10700
10701 /* If the suggestion doesn't have specific case duplicate the case
10702 * of the bad word. */
10703 if (captype(p, NULL) == 0)
10704 {
10705 make_case_word(p, cword, su->su_badflags);
10706 p = cword;
10707 }
10708
10709 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010710 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010711 }
10712 }
10713
10714 fclose(fd);
10715
Bram Moolenaar4770d092006-01-12 23:22:24 +000010716 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10717 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010718 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10719}
10720
10721/*
10722 * Find suggestions for the internal method indicated by "sps_flags".
10723 */
10724 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010725spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010726{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010727 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010728 * Load the .sug file(s) that are available and not done yet.
10729 */
10730 suggest_load_files();
10731
10732 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010733 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010734 *
10735 * Set a maximum score to limit the combination of operations that is
10736 * tried.
10737 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010738 suggest_try_special(su);
10739
10740 /*
10741 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10742 * from the .aff file and inserting a space (split the word).
10743 */
10744 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010745
10746 /* For the resulting top-scorers compute the sound-a-like score. */
10747 if (sps_flags & SPS_DOUBLE)
10748 score_comp_sal(su);
10749
10750 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010751 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010752 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010753 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010754 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010755 if (sps_flags & SPS_BEST)
10756 /* Adjust the word score for the suggestions found so far for how
10757 * they sounds like. */
10758 rescore_suggestions(su);
10759
10760 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010761 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010762 * for the soundfold word, limits the changes that are being tried,
10763 * and "su_sfmaxscore" the rescored score, which is set by
10764 * cleanup_suggestions().
10765 * First find words with a small edit distance, because this is much
10766 * faster and often already finds the top-N suggestions. If we didn't
10767 * find many suggestions try again with a higher edit distance.
10768 * "sl_sounddone" is used to avoid doing the same word twice.
10769 */
10770 suggest_try_soundalike_prep();
10771 su->su_maxscore = SCORE_SFMAX1;
10772 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010773 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010774 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10775 {
10776 /* We didn't find enough matches, try again, allowing more
10777 * changes to the soundfold word. */
10778 su->su_maxscore = SCORE_SFMAX2;
10779 suggest_try_soundalike(su);
10780 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10781 {
10782 /* Still didn't find enough matches, try again, allowing even
10783 * more changes to the soundfold word. */
10784 su->su_maxscore = SCORE_SFMAX3;
10785 suggest_try_soundalike(su);
10786 }
10787 }
10788 su->su_maxscore = su->su_sfmaxscore;
10789 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010790 }
10791
Bram Moolenaar4770d092006-01-12 23:22:24 +000010792 /* When CTRL-C was hit while searching do show the results. Only clear
10793 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010794 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010795 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010796 {
10797 (void)vgetc();
10798 got_int = FALSE;
10799 }
10800
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010801 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010802 {
10803 if (sps_flags & SPS_BEST)
10804 /* Adjust the word score for how it sounds like. */
10805 rescore_suggestions(su);
10806
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10808 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010809 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010810 }
10811}
10812
10813/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010814 * Load the .sug files for languages that have one and weren't loaded yet.
10815 */
10816 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010817suggest_load_files(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010818{
10819 langp_T *lp;
10820 int lpi;
10821 slang_T *slang;
10822 char_u *dotp;
10823 FILE *fd;
10824 char_u buf[MAXWLEN];
10825 int i;
10826 time_t timestamp;
10827 int wcount;
10828 int wordnr;
10829 garray_T ga;
10830 int c;
10831
10832 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010833 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010834 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010835 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010836 slang = lp->lp_slang;
10837 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10838 {
10839 /* Change ".spl" to ".sug" and open the file. When the file isn't
10840 * found silently skip it. Do set "sl_sugloaded" so that we
10841 * don't try again and again. */
10842 slang->sl_sugloaded = TRUE;
10843
10844 dotp = vim_strrchr(slang->sl_fname, '.');
10845 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10846 continue;
10847 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010848 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010849 if (fd == NULL)
10850 goto nextone;
10851
10852 /*
10853 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10854 */
10855 for (i = 0; i < VIMSUGMAGICL; ++i)
10856 buf[i] = getc(fd); /* <fileID> */
10857 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10858 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010859 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010860 slang->sl_fname);
10861 goto nextone;
10862 }
10863 c = getc(fd); /* <versionnr> */
10864 if (c < VIMSUGVERSION)
10865 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010866 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010867 slang->sl_fname);
10868 goto nextone;
10869 }
10870 else if (c > VIMSUGVERSION)
10871 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010872 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010873 slang->sl_fname);
10874 goto nextone;
10875 }
10876
10877 /* Check the timestamp, it must be exactly the same as the one in
10878 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010879 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010880 if (timestamp != slang->sl_sugtime)
10881 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010882 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010883 slang->sl_fname);
10884 goto nextone;
10885 }
10886
10887 /*
10888 * <SUGWORDTREE>: <wordtree>
10889 * Read the trie with the soundfolded words.
10890 */
10891 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10892 FALSE, 0) != 0)
10893 {
10894someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010895 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010896 slang->sl_fname);
10897 slang_clear_sug(slang);
10898 goto nextone;
10899 }
10900
10901 /*
10902 * <SUGTABLE>: <sugwcount> <sugline> ...
10903 *
10904 * Read the table with word numbers. We use a file buffer for
10905 * this, because it's so much like a file with lines. Makes it
10906 * possible to swap the info and save on memory use.
10907 */
10908 slang->sl_sugbuf = open_spellbuf();
10909 if (slang->sl_sugbuf == NULL)
10910 goto someerror;
10911 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010912 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010913 if (wcount < 0)
10914 goto someerror;
10915
10916 /* Read all the wordnr lists into the buffer, one NUL terminated
10917 * list per line. */
10918 ga_init2(&ga, 1, 100);
10919 for (wordnr = 0; wordnr < wcount; ++wordnr)
10920 {
10921 ga.ga_len = 0;
10922 for (;;)
10923 {
10924 c = getc(fd); /* <sugline> */
10925 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10926 goto someerror;
10927 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10928 if (c == NUL)
10929 break;
10930 }
10931 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10932 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10933 goto someerror;
10934 }
10935 ga_clear(&ga);
10936
10937 /*
10938 * Need to put word counts in the word tries, so that we can find
10939 * a word by its number.
10940 */
10941 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10942 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10943
10944nextone:
10945 if (fd != NULL)
10946 fclose(fd);
10947 STRCPY(dotp, ".spl");
10948 }
10949 }
10950}
10951
10952
10953/*
10954 * Fill in the wordcount fields for a trie.
10955 * Returns the total number of words.
10956 */
10957 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010010958tree_count_words(char_u *byts, idx_T *idxs)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010959{
10960 int depth;
10961 idx_T arridx[MAXWLEN];
10962 int curi[MAXWLEN];
10963 int c;
10964 idx_T n;
10965 int wordcount[MAXWLEN];
10966
10967 arridx[0] = 0;
10968 curi[0] = 1;
10969 wordcount[0] = 0;
10970 depth = 0;
10971 while (depth >= 0 && !got_int)
10972 {
10973 if (curi[depth] > byts[arridx[depth]])
10974 {
10975 /* Done all bytes at this node, go up one level. */
10976 idxs[arridx[depth]] = wordcount[depth];
10977 if (depth > 0)
10978 wordcount[depth - 1] += wordcount[depth];
10979
10980 --depth;
10981 fast_breakcheck();
10982 }
10983 else
10984 {
10985 /* Do one more byte at this node. */
10986 n = arridx[depth] + curi[depth];
10987 ++curi[depth];
10988
10989 c = byts[n];
10990 if (c == 0)
10991 {
10992 /* End of word, count it. */
10993 ++wordcount[depth];
10994
10995 /* Skip over any other NUL bytes (same word with different
10996 * flags). */
10997 while (byts[n + 1] == 0)
10998 {
10999 ++n;
11000 ++curi[depth];
11001 }
11002 }
11003 else
11004 {
11005 /* Normal char, go one level deeper to count the words. */
11006 ++depth;
11007 arridx[depth] = idxs[n];
11008 curi[depth] = 1;
11009 wordcount[depth] = 0;
11010 }
11011 }
11012 }
11013}
11014
11015/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011016 * Free the info put in "*su" by spell_find_suggest().
11017 */
11018 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011019spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011020{
11021 int i;
11022
11023 /* Free the suggestions. */
11024 for (i = 0; i < su->su_ga.ga_len; ++i)
11025 vim_free(SUG(su->su_ga, i).st_word);
11026 ga_clear(&su->su_ga);
11027 for (i = 0; i < su->su_sga.ga_len; ++i)
11028 vim_free(SUG(su->su_sga, i).st_word);
11029 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030
11031 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011032 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033}
11034
11035/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011036 * Make a copy of "word", with the first letter upper or lower cased, to
11037 * "wcopy[MAXWLEN]". "word" must not be empty.
11038 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039 */
11040 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011041onecap_copy(
11042 char_u *word,
11043 char_u *wcopy,
11044 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045{
11046 char_u *p;
11047 int c;
11048 int l;
11049
11050 p = word;
11051#ifdef FEAT_MBYTE
11052 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011053 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 else
11055#endif
11056 c = *p++;
11057 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011058 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011060 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061#ifdef FEAT_MBYTE
11062 if (has_mbyte)
11063 l = mb_char2bytes(c, wcopy);
11064 else
11065#endif
11066 {
11067 l = 1;
11068 wcopy[0] = c;
11069 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011070 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071}
11072
11073/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011074 * Make a copy of "word" with all the letters upper cased into
11075 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 */
11077 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011078allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079{
11080 char_u *s;
11081 char_u *d;
11082 int c;
11083
11084 d = wcopy;
11085 for (s = word; *s != NUL; )
11086 {
11087#ifdef FEAT_MBYTE
11088 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011089 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 else
11091#endif
11092 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011093
11094#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +020011095 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +000011096 * would cause weird errors in other 8-bit encodings. */
11097 if (enc_latin1like && c == 0xdf)
11098 {
11099 c = 'S';
11100 if (d - wcopy >= MAXWLEN - 1)
11101 break;
11102 *d++ = c;
11103 }
11104 else
11105#endif
11106 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107
11108#ifdef FEAT_MBYTE
11109 if (has_mbyte)
11110 {
11111 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11112 break;
11113 d += mb_char2bytes(c, d);
11114 }
11115 else
11116#endif
11117 {
11118 if (d - wcopy >= MAXWLEN - 1)
11119 break;
11120 *d++ = c;
11121 }
11122 }
11123 *d = NUL;
11124}
11125
11126/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011127 * Try finding suggestions by recognizing specific situations.
11128 */
11129 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011130suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011131{
11132 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011133 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011134 int c;
11135 char_u word[MAXWLEN];
11136
11137 /*
11138 * Recognize a word that is repeated: "the the".
11139 */
11140 p = skiptowhite(su->su_fbadword);
11141 len = p - su->su_fbadword;
11142 p = skipwhite(p);
11143 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11144 {
11145 /* Include badflags: if the badword is onecap or allcap
11146 * use that for the goodword too: "The the" -> "The". */
11147 c = su->su_fbadword[len];
11148 su->su_fbadword[len] = NUL;
11149 make_case_word(su->su_fbadword, word, su->su_badflags);
11150 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011151
11152 /* Give a soundalike score of 0, compute the score as if deleting one
11153 * character. */
11154 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011155 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011156 }
11157}
11158
11159/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011160 * Change the 0 to 1 to measure how much time is spent in each state.
11161 * Output is dumped in "suggestprof".
11162 */
11163#if 0
11164# define SUGGEST_PROFILE
11165proftime_T current;
11166proftime_T total;
11167proftime_T times[STATE_FINAL + 1];
11168long counts[STATE_FINAL + 1];
11169
11170 static void
11171prof_init(void)
11172{
11173 for (int i = 0; i <= STATE_FINAL; ++i)
11174 {
11175 profile_zero(&times[i]);
11176 counts[i] = 0;
11177 }
11178 profile_start(&current);
11179 profile_start(&total);
11180}
11181
11182/* call before changing state */
11183 static void
11184prof_store(state_T state)
11185{
11186 profile_end(&current);
11187 profile_add(&times[state], &current);
11188 ++counts[state];
11189 profile_start(&current);
11190}
11191# define PROF_STORE(state) prof_store(state);
11192
11193 static void
11194prof_report(char *name)
11195{
11196 FILE *fd = fopen("suggestprof", "a");
11197
11198 profile_end(&total);
11199 fprintf(fd, "-----------------------\n");
11200 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
11201 for (int i = 0; i <= STATE_FINAL; ++i)
11202 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
11203 fclose(fd);
11204}
11205#else
11206# define PROF_STORE(state)
11207#endif
11208
11209/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 * Try finding suggestions by adding/removing/swapping letters.
11211 */
11212 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011213suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214{
11215 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011216 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011218 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011219 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220
11221 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011222 * to find matches (esp. REP items). Append some more text, changing
11223 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011225 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011226 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011227 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228
Bram Moolenaar860cae12010-06-05 23:22:07 +020011229 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011231 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011232
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011233 /* If reloading a spell file fails it's still in the list but
11234 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011235 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011236 continue;
11237
Bram Moolenaar4770d092006-01-12 23:22:24 +000011238 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011239#ifdef SUGGEST_PROFILE
11240 prof_init();
11241#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011242 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011243#ifdef SUGGEST_PROFILE
11244 prof_report("try_change");
11245#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011246 }
11247}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011248
Bram Moolenaar4770d092006-01-12 23:22:24 +000011249/* Check the maximum score, if we go over it we won't try this change. */
11250#define TRY_DEEPER(su, stack, depth, add) \
11251 (stack[depth].ts_score + (add) < su->su_maxscore)
11252
11253/*
11254 * Try finding suggestions by adding/removing/swapping letters.
11255 *
11256 * This uses a state machine. At each node in the tree we try various
11257 * operations. When trying if an operation works "depth" is increased and the
11258 * stack[] is used to store info. This allows combinations, thus insert one
11259 * character, replace one and delete another. The number of changes is
11260 * limited by su->su_maxscore.
11261 *
11262 * After implementing this I noticed an article by Kemal Oflazer that
11263 * describes something similar: "Error-tolerant Finite State Recognition with
11264 * Applications to Morphological Analysis and Spelling Correction" (1996).
11265 * The implementation in the article is simplified and requires a stack of
11266 * unknown depth. The implementation here only needs a stack depth equal to
11267 * the length of the word.
11268 *
11269 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11270 * The mechanism is the same, but we find a match with a sound-folded word
11271 * that comes from one or more original words. Each of these words may be
11272 * added, this is done by add_sound_suggest().
11273 * Don't use:
11274 * the prefix tree or the keep-case tree
11275 * "su->su_badlen"
11276 * anything to do with upper and lower case
11277 * anything to do with word or non-word characters ("spell_iswordp()")
11278 * banned words
11279 * word flags (rare, region, compounding)
11280 * word splitting for now
11281 * "similar_chars()"
11282 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11283 */
11284 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010011285suggest_trie_walk(
11286 suginfo_T *su,
11287 langp_T *lp,
11288 char_u *fword,
11289 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011290{
11291 char_u tword[MAXWLEN]; /* good word collected so far */
11292 trystate_T stack[MAXWLEN];
11293 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011294 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011295 * words and split word. NUL terminated
11296 * when going deeper but not when coming
11297 * back. */
11298 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11299 trystate_T *sp;
11300 int newscore;
11301 int score;
11302 char_u *byts, *fbyts, *pbyts;
11303 idx_T *idxs, *fidxs, *pidxs;
11304 int depth;
11305 int c, c2, c3;
11306 int n = 0;
11307 int flags;
11308 garray_T *gap;
11309 idx_T arridx;
11310 int len;
11311 char_u *p;
11312 fromto_T *ftp;
11313 int fl = 0, tl;
11314 int repextra = 0; /* extra bytes in fword[] from REP item */
11315 slang_T *slang = lp->lp_slang;
11316 int fword_ends;
11317 int goodword_ends;
11318#ifdef DEBUG_TRIEWALK
11319 /* Stores the name of the change made at each level. */
11320 char_u changename[MAXWLEN][80];
11321#endif
11322 int breakcheckcount = 1000;
11323 int compound_ok;
11324
11325 /*
11326 * Go through the whole case-fold tree, try changes at each node.
11327 * "tword[]" contains the word collected from nodes in the tree.
11328 * "fword[]" the word we are trying to match with (initially the bad
11329 * word).
11330 */
11331 depth = 0;
11332 sp = &stack[0];
11333 vim_memset(sp, 0, sizeof(trystate_T));
11334 sp->ts_curi = 1;
11335
11336 if (soundfold)
11337 {
11338 /* Going through the soundfold tree. */
11339 byts = fbyts = slang->sl_sbyts;
11340 idxs = fidxs = slang->sl_sidxs;
11341 pbyts = NULL;
11342 pidxs = NULL;
11343 sp->ts_prefixdepth = PFD_NOPREFIX;
11344 sp->ts_state = STATE_START;
11345 }
11346 else
11347 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011348 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011349 * When there are postponed prefixes we need to use these first. At
11350 * the end of the prefix we continue in the case-fold tree.
11351 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011352 fbyts = slang->sl_fbyts;
11353 fidxs = slang->sl_fidxs;
11354 pbyts = slang->sl_pbyts;
11355 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011356 if (pbyts != NULL)
11357 {
11358 byts = pbyts;
11359 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011360 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011361 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11362 }
11363 else
11364 {
11365 byts = fbyts;
11366 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011367 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011368 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011369 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011370 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011371
Bram Moolenaar4770d092006-01-12 23:22:24 +000011372 /*
11373 * Loop to find all suggestions. At each round we either:
11374 * - For the current state try one operation, advance "ts_curi",
11375 * increase "depth".
11376 * - When a state is done go to the next, set "ts_state".
11377 * - When all states are tried decrease "depth".
11378 */
11379 while (depth >= 0 && !got_int)
11380 {
11381 sp = &stack[depth];
11382 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011384 case STATE_START:
11385 case STATE_NOPREFIX:
11386 /*
11387 * Start of node: Deal with NUL bytes, which means
11388 * tword[] may end here.
11389 */
11390 arridx = sp->ts_arridx; /* current node in the tree */
11391 len = byts[arridx]; /* bytes in this node */
11392 arridx += sp->ts_curi; /* index of current byte */
11393
11394 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011396 /* Skip over the NUL bytes, we use them later. */
11397 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11398 ;
11399 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400
Bram Moolenaar4770d092006-01-12 23:22:24 +000011401 /* Always past NUL bytes now. */
11402 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011403 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011404 sp->ts_state = STATE_ENDNUL;
11405 sp->ts_save_badflags = su->su_badflags;
11406
11407 /* At end of a prefix or at start of prefixtree: check for
11408 * following word. */
11409 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011410 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011411 /* Set su->su_badflags to the caps type at this position.
11412 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011413#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011414 if (has_mbyte)
11415 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11416 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011417#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011418 n = sp->ts_fidx;
11419 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11420 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011421 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011422#ifdef DEBUG_TRIEWALK
11423 sprintf(changename[depth], "prefix");
11424#endif
11425 go_deeper(stack, depth, 0);
11426 ++depth;
11427 sp = &stack[depth];
11428 sp->ts_prefixdepth = depth - 1;
11429 byts = fbyts;
11430 idxs = fidxs;
11431 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011432
Bram Moolenaar4770d092006-01-12 23:22:24 +000011433 /* Move the prefix to preword[] with the right case
11434 * and make find_keepcap_word() works. */
11435 tword[sp->ts_twordlen] = NUL;
11436 make_case_word(tword + sp->ts_splitoff,
11437 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011438 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011439 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011440 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011441 break;
11442 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011443
Bram Moolenaar4770d092006-01-12 23:22:24 +000011444 if (sp->ts_curi > len || byts[arridx] != 0)
11445 {
11446 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011447 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011448 sp->ts_state = STATE_ENDNUL;
11449 sp->ts_save_badflags = su->su_badflags;
11450 break;
11451 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011452
Bram Moolenaar4770d092006-01-12 23:22:24 +000011453 /*
11454 * End of word in tree.
11455 */
11456 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457
Bram Moolenaar4770d092006-01-12 23:22:24 +000011458 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011459
11460 /* Skip words with the NOSUGGEST flag. */
11461 if (flags & WF_NOSUGGEST)
11462 break;
11463
Bram Moolenaar4770d092006-01-12 23:22:24 +000011464 fword_ends = (fword[sp->ts_fidx] == NUL
11465 || (soundfold
11466 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011467 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011468 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469
Bram Moolenaar4770d092006-01-12 23:22:24 +000011470 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011471 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011472 {
11473 /* There was a prefix before the word. Check that the prefix
11474 * can be used with this word. */
11475 /* Count the length of the NULs in the prefix. If there are
11476 * none this must be the first try without a prefix. */
11477 n = stack[sp->ts_prefixdepth].ts_arridx;
11478 len = pbyts[n++];
11479 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11480 ;
11481 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011482 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011483 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011484 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011485 if (c == 0)
11486 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011487
Bram Moolenaar4770d092006-01-12 23:22:24 +000011488 /* Use the WF_RARE flag for a rare prefix. */
11489 if (c & WF_RAREPFX)
11490 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011491
Bram Moolenaar4770d092006-01-12 23:22:24 +000011492 /* Tricky: when checking for both prefix and compounding
11493 * we run into the prefix flag first.
11494 * Remember that it's OK, so that we accept the prefix
11495 * when arriving at a compound flag. */
11496 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011497 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011498 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011499
Bram Moolenaar4770d092006-01-12 23:22:24 +000011500 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11501 * appending another compound word below. */
11502 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011503 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 goodword_ends = FALSE;
11505 else
11506 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011507
Bram Moolenaar4770d092006-01-12 23:22:24 +000011508 p = NULL;
11509 compound_ok = TRUE;
11510 if (sp->ts_complen > sp->ts_compsplit)
11511 {
11512 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011513 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011514 /* There was a word before this word. When there was no
11515 * change in this word (it was correct) add the first word
11516 * as a suggestion. If this word was corrected too, we
11517 * need to check if a correct word follows. */
11518 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011519 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011520 && STRNCMP(fword + sp->ts_splitfidx,
11521 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011522 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011523 {
11524 preword[sp->ts_prewordlen] = NUL;
11525 newscore = score_wordcount_adj(slang, sp->ts_score,
11526 preword + sp->ts_prewordlen,
11527 sp->ts_prewordlen > 0);
11528 /* Add the suggestion if the score isn't too bad. */
11529 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011530 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011531 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011532 newscore, 0, FALSE,
11533 lp->lp_sallang, FALSE);
11534 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011535 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011536 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011537 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011538 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011539 /* There was a compound word before this word. If this
11540 * word does not support compounding then give up
11541 * (splitting is tried for the word without compound
11542 * flag). */
11543 if (((unsigned)flags >> 24) == 0
11544 || sp->ts_twordlen - sp->ts_splitoff
11545 < slang->sl_compminlen)
11546 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011547#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011548 /* For multi-byte chars check character length against
11549 * COMPOUNDMIN. */
11550 if (has_mbyte
11551 && slang->sl_compminlen > 0
11552 && mb_charlen(tword + sp->ts_splitoff)
11553 < slang->sl_compminlen)
11554 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011555#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011556
Bram Moolenaar4770d092006-01-12 23:22:24 +000011557 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11558 compflags[sp->ts_complen + 1] = NUL;
11559 vim_strncpy(preword + sp->ts_prewordlen,
11560 tword + sp->ts_splitoff,
11561 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011562
11563 /* Verify CHECKCOMPOUNDPATTERN rules. */
11564 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11565 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011566 compound_ok = FALSE;
11567
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011568 if (compound_ok)
11569 {
11570 p = preword;
11571 while (*skiptowhite(p) != NUL)
11572 p = skipwhite(skiptowhite(p));
11573 if (fword_ends && !can_compound(slang, p,
11574 compflags + sp->ts_compsplit))
11575 /* Compound is not allowed. But it may still be
11576 * possible if we add another (short) word. */
11577 compound_ok = FALSE;
11578 }
11579
Bram Moolenaar4770d092006-01-12 23:22:24 +000011580 /* Get pointer to last char of previous word. */
11581 p = preword + sp->ts_prewordlen;
11582 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011583 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011584 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585
Bram Moolenaar4770d092006-01-12 23:22:24 +000011586 /*
11587 * Form the word with proper case in preword.
11588 * If there is a word from a previous split, append.
11589 * For the soundfold tree don't change the case, simply append.
11590 */
11591 if (soundfold)
11592 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11593 else if (flags & WF_KEEPCAP)
11594 /* Must find the word in the keep-case tree. */
11595 find_keepcap_word(slang, tword + sp->ts_splitoff,
11596 preword + sp->ts_prewordlen);
11597 else
11598 {
11599 /* Include badflags: If the badword is onecap or allcap
11600 * use that for the goodword too. But if the badword is
11601 * allcap and it's only one char long use onecap. */
11602 c = su->su_badflags;
11603 if ((c & WF_ALLCAP)
11604#ifdef FEAT_MBYTE
11605 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11606#else
11607 && su->su_badlen == 1
11608#endif
11609 )
11610 c = WF_ONECAP;
11611 c |= flags;
11612
11613 /* When appending a compound word after a word character don't
11614 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011615 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011616 c &= ~WF_ONECAP;
11617 make_case_word(tword + sp->ts_splitoff,
11618 preword + sp->ts_prewordlen, c);
11619 }
11620
11621 if (!soundfold)
11622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 /* Don't use a banned word. It may appear again as a good
11624 * word, thus remember it. */
11625 if (flags & WF_BANNED)
11626 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011627 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011628 break;
11629 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011630 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011631 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11632 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011633 {
11634 if (slang->sl_compprog == NULL)
11635 break;
11636 /* the word so far was banned but we may try compounding */
11637 goodword_ends = FALSE;
11638 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011639 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011640
Bram Moolenaar4770d092006-01-12 23:22:24 +000011641 newscore = 0;
11642 if (!soundfold) /* soundfold words don't have flags */
11643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011645 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 newscore += SCORE_REGION;
11647 if (flags & WF_RARE)
11648 newscore += SCORE_RARE;
11649
Bram Moolenaar0c405862005-06-22 22:26:26 +000011650 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011651 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011652 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654
Bram Moolenaar4770d092006-01-12 23:22:24 +000011655 /* TODO: how about splitting in the soundfold tree? */
11656 if (fword_ends
11657 && goodword_ends
11658 && sp->ts_fidx >= sp->ts_fidxtry
11659 && compound_ok)
11660 {
11661 /* The badword also ends: add suggestions. */
11662#ifdef DEBUG_TRIEWALK
11663 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011665 int j;
11666
11667 /* print the stack of changes that brought us here */
11668 smsg("------ %s -------", fword);
11669 for (j = 0; j < depth; ++j)
11670 smsg("%s", changename[j]);
11671 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011672#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011673 if (soundfold)
11674 {
11675 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011676 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011677 add_sound_suggest(su, preword, sp->ts_score, lp);
11678 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011679 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011680 {
11681 /* Give a penalty when changing non-word char to word
11682 * char, e.g., "thes," -> "these". */
11683 p = fword + sp->ts_fidx;
11684 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011685 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011686 {
11687 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011688 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011689 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011690 newscore += SCORE_NONWORD;
11691 }
11692
Bram Moolenaar4770d092006-01-12 23:22:24 +000011693 /* Give a bonus to words seen before. */
11694 score = score_wordcount_adj(slang,
11695 sp->ts_score + newscore,
11696 preword + sp->ts_prewordlen,
11697 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011698
Bram Moolenaar4770d092006-01-12 23:22:24 +000011699 /* Add the suggestion if the score isn't too bad. */
11700 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011701 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011702 add_suggestion(su, &su->su_ga, preword,
11703 sp->ts_fidx - repextra,
11704 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011705
11706 if (su->su_badflags & WF_MIXCAP)
11707 {
11708 /* We really don't know if the word should be
11709 * upper or lower case, add both. */
11710 c = captype(preword, NULL);
11711 if (c == 0 || c == WF_ALLCAP)
11712 {
11713 make_case_word(tword + sp->ts_splitoff,
11714 preword + sp->ts_prewordlen,
11715 c == 0 ? WF_ALLCAP : 0);
11716
11717 add_suggestion(su, &su->su_ga, preword,
11718 sp->ts_fidx - repextra,
11719 score + SCORE_ICASE, 0, FALSE,
11720 lp->lp_sallang, FALSE);
11721 }
11722 }
11723 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011725 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011726
Bram Moolenaar4770d092006-01-12 23:22:24 +000011727 /*
11728 * Try word split and/or compounding.
11729 */
11730 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011731#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011732 /* Don't split halfway a character. */
11733 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011734#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011735 )
11736 {
11737 int try_compound;
11738 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011739
Bram Moolenaar4770d092006-01-12 23:22:24 +000011740 /* If past the end of the bad word don't try a split.
11741 * Otherwise try changing the next word. E.g., find
11742 * suggestions for "the the" where the second "the" is
11743 * different. It's done like a split.
11744 * TODO: word split for soundfold words */
11745 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11746 && !soundfold;
11747
11748 /* Get here in several situations:
11749 * 1. The word in the tree ends:
11750 * If the word allows compounding try that. Otherwise try
11751 * a split by inserting a space. For both check that a
11752 * valid words starts at fword[sp->ts_fidx].
11753 * For NOBREAK do like compounding to be able to check if
11754 * the next word is valid.
11755 * 2. The badword does end, but it was due to a change (e.g.,
11756 * a swap). No need to split, but do check that the
11757 * following word is valid.
11758 * 3. The badword and the word in the tree end. It may still
11759 * be possible to compound another (short) word.
11760 */
11761 try_compound = FALSE;
11762 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +010011763 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +000011764 && slang->sl_compprog != NULL
11765 && ((unsigned)flags >> 24) != 0
11766 && sp->ts_twordlen - sp->ts_splitoff
11767 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011768#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011769 && (!has_mbyte
11770 || slang->sl_compminlen == 0
11771 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011772 >= slang->sl_compminlen)
11773#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011774 && (slang->sl_compsylmax < MAXWLEN
11775 || sp->ts_complen + 1 - sp->ts_compsplit
11776 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011777 && (can_be_compound(sp, slang,
11778 compflags, ((unsigned)flags >> 24))))
11779
Bram Moolenaar4770d092006-01-12 23:22:24 +000011780 {
11781 try_compound = TRUE;
11782 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11783 compflags[sp->ts_complen + 1] = NUL;
11784 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011785
Bram Moolenaar4770d092006-01-12 23:22:24 +000011786 /* For NOBREAK we never try splitting, it won't make any word
11787 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +010011788 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011789 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011790
Bram Moolenaar4770d092006-01-12 23:22:24 +000011791 /* If we could add a compound word, and it's also possible to
11792 * split at this point, do the split first and set
11793 * TSF_DIDSPLIT to avoid doing it again. */
11794 else if (!fword_ends
11795 && try_compound
11796 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11797 {
11798 try_compound = FALSE;
11799 sp->ts_flags |= TSF_DIDSPLIT;
11800 --sp->ts_curi; /* do the same NUL again */
11801 compflags[sp->ts_complen] = NUL;
11802 }
11803 else
11804 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011805
Bram Moolenaar4770d092006-01-12 23:22:24 +000011806 if (try_split || try_compound)
11807 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011808 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011809 {
11810 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011811 * words so far are valid for compounding. If there
11812 * is only one word it must not have the NEEDCOMPOUND
11813 * flag. */
11814 if (sp->ts_complen == sp->ts_compsplit
11815 && (flags & WF_NEEDCOMP))
11816 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011817 p = preword;
11818 while (*skiptowhite(p) != NUL)
11819 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011820 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011821 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011822 compflags + sp->ts_compsplit))
11823 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011824
11825 if (slang->sl_nosplitsugs)
11826 newscore += SCORE_SPLIT_NO;
11827 else
11828 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011829
11830 /* Give a bonus to words seen before. */
11831 newscore = score_wordcount_adj(slang, newscore,
11832 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011833 }
11834
Bram Moolenaar4770d092006-01-12 23:22:24 +000011835 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011837 go_deeper(stack, depth, newscore);
11838#ifdef DEBUG_TRIEWALK
11839 if (!try_compound && !fword_ends)
11840 sprintf(changename[depth], "%.*s-%s: split",
11841 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11842 else
11843 sprintf(changename[depth], "%.*s-%s: compound",
11844 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11845#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011846 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011847 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011848 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011849 sp->ts_state = STATE_SPLITUNDO;
11850
11851 ++depth;
11852 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011854 /* Append a space to preword when splitting. */
11855 if (!try_compound && !fword_ends)
11856 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011857 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011858 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011859 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011860
11861 /* If the badword has a non-word character at this
11862 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011863 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011864 * character when the word ends. But only when the
11865 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011866 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011867 + sp->ts_fidx,
11868 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011869 || fword_ends)
11870 && fword[sp->ts_fidx] != NUL
11871 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011872 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011873 int l;
11874
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011875#ifdef FEAT_MBYTE
11876 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011877 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011878 else
11879#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011880 l = 1;
11881 if (fword_ends)
11882 {
11883 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011884 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011885 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011886 sp->ts_prewordlen += l;
11887 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011888 }
11889 else
11890 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11891 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011892 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011893
Bram Moolenaard12a1322005-08-21 22:08:24 +000011894 /* When compounding include compound flag in
11895 * compflags[] (already set above). When splitting we
11896 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011897 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011898 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011899 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011900 sp->ts_compsplit = sp->ts_complen;
11901 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011902
Bram Moolenaar53805d12005-08-01 07:08:33 +000011903 /* set su->su_badflags to the caps type at this
11904 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011905#ifdef FEAT_MBYTE
11906 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011907 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011908 else
11909#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011910 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011911 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011912 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011915 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011916
11917 /* If there are postponed prefixes, try these too. */
11918 if (pbyts != NULL)
11919 {
11920 byts = pbyts;
11921 idxs = pidxs;
11922 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011923 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011924 sp->ts_state = STATE_NOPREFIX;
11925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926 }
11927 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011928 }
11929 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930
Bram Moolenaar4770d092006-01-12 23:22:24 +000011931 case STATE_SPLITUNDO:
11932 /* Undo the changes done for word split or compound word. */
11933 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934
Bram Moolenaar4770d092006-01-12 23:22:24 +000011935 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011936 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011937 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011938
Bram Moolenaar4770d092006-01-12 23:22:24 +000011939 /* In case we went into the prefix tree. */
11940 byts = fbyts;
11941 idxs = fidxs;
11942 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011943
Bram Moolenaar4770d092006-01-12 23:22:24 +000011944 case STATE_ENDNUL:
11945 /* Past the NUL bytes in the node. */
11946 su->su_badflags = sp->ts_save_badflags;
11947 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011948#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011949 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011950#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011951 )
11952 {
11953 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011954 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011955 sp->ts_state = STATE_DEL;
11956 break;
11957 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011958 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011959 sp->ts_state = STATE_PLAIN;
11960 /*FALLTHROUGH*/
11961
11962 case STATE_PLAIN:
11963 /*
11964 * Go over all possible bytes at this node, add each to tword[]
11965 * and use child node. "ts_curi" is the index.
11966 */
11967 arridx = sp->ts_arridx;
11968 if (sp->ts_curi > byts[arridx])
11969 {
11970 /* Done all bytes at this node, do next state. When still at
11971 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010011972 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011973 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011976 sp->ts_state = STATE_FINAL;
11977 }
11978 else
11979 {
11980 arridx += sp->ts_curi++;
11981 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011982
Bram Moolenaar4770d092006-01-12 23:22:24 +000011983 /* Normal byte, go one level deeper. If it's not equal to the
11984 * byte in the bad word adjust the score. But don't even try
11985 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +010011986 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +000011987 * delete + substitute. */
11988 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011989#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011990 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011991#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011992 )
11993 newscore = 0;
11994 else
11995 newscore = SCORE_SUBST;
11996 if ((newscore == 0
11997 || (sp->ts_fidx >= sp->ts_fidxtry
11998 && ((sp->ts_flags & TSF_DIDDEL) == 0
11999 || c != fword[sp->ts_delidx])))
12000 && TRY_DEEPER(su, stack, depth, newscore))
12001 {
12002 go_deeper(stack, depth, newscore);
12003#ifdef DEBUG_TRIEWALK
12004 if (newscore > 0)
12005 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12006 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12007 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012009 sprintf(changename[depth], "%.*s-%s: accept %c",
12010 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12011 fword[sp->ts_fidx]);
12012#endif
12013 ++depth;
12014 sp = &stack[depth];
12015 ++sp->ts_fidx;
12016 tword[sp->ts_twordlen++] = c;
12017 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012018#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012019 if (newscore == SCORE_SUBST)
12020 sp->ts_isdiff = DIFF_YES;
12021 if (has_mbyte)
12022 {
12023 /* Multi-byte characters are a bit complicated to
12024 * handle: They differ when any of the bytes differ
12025 * and then their length may also differ. */
12026 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012027 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012028 /* First byte. */
12029 sp->ts_tcharidx = 0;
12030 sp->ts_tcharlen = MB_BYTE2LEN(c);
12031 sp->ts_fcharstart = sp->ts_fidx - 1;
12032 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012033 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012034 }
12035 else if (sp->ts_isdiff == DIFF_INSERT)
12036 /* When inserting trail bytes don't advance in the
12037 * bad word. */
12038 --sp->ts_fidx;
12039 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12040 {
12041 /* Last byte of character. */
12042 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012043 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012044 /* Correct ts_fidx for the byte length of the
12045 * character (we didn't check that before). */
12046 sp->ts_fidx = sp->ts_fcharstart
12047 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012048 fword[sp->ts_fcharstart]);
12049
Bram Moolenaar4770d092006-01-12 23:22:24 +000012050 /* For changing a composing character adjust
12051 * the score from SCORE_SUBST to
12052 * SCORE_SUBCOMP. */
12053 if (enc_utf8
12054 && utf_iscomposing(
12055 mb_ptr2char(tword
12056 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012057 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012058 && utf_iscomposing(
12059 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012060 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012061 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012062 SCORE_SUBST - SCORE_SUBCOMP;
12063
Bram Moolenaar4770d092006-01-12 23:22:24 +000012064 /* For a similar character adjust score from
12065 * SCORE_SUBST to SCORE_SIMILAR. */
12066 else if (!soundfold
12067 && slang->sl_has_map
12068 && similar_chars(slang,
12069 mb_ptr2char(tword
12070 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012071 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012072 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012073 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012074 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012075 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012076 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012077 else if (sp->ts_isdiff == DIFF_INSERT
12078 && sp->ts_twordlen > sp->ts_tcharlen)
12079 {
12080 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12081 c = mb_ptr2char(p);
12082 if (enc_utf8 && utf_iscomposing(c))
12083 {
12084 /* Inserting a composing char doesn't
12085 * count that much. */
12086 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12087 }
12088 else
12089 {
12090 /* If the previous character was the same,
12091 * thus doubling a character, give a bonus
12092 * to the score. Also for the soundfold
12093 * tree (might seem illogical but does
12094 * give better scores). */
12095 mb_ptr_back(tword, p);
12096 if (c == mb_ptr2char(p))
12097 sp->ts_score -= SCORE_INS
12098 - SCORE_INSDUP;
12099 }
12100 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012101
Bram Moolenaar4770d092006-01-12 23:22:24 +000012102 /* Starting a new char, reset the length. */
12103 sp->ts_tcharlen = 0;
12104 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012105 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012106 else
12107#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012108 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012109 /* If we found a similar char adjust the score.
12110 * We do this after calling go_deeper() because
12111 * it's slow. */
12112 if (newscore != 0
12113 && !soundfold
12114 && slang->sl_has_map
12115 && similar_chars(slang,
12116 c, fword[sp->ts_fidx - 1]))
12117 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012119 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012120 }
12121 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012122
Bram Moolenaar4770d092006-01-12 23:22:24 +000012123 case STATE_DEL:
12124#ifdef FEAT_MBYTE
12125 /* When past the first byte of a multi-byte char don't try
12126 * delete/insert/swap a character. */
12127 if (has_mbyte && sp->ts_tcharlen > 0)
12128 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012129 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012130 sp->ts_state = STATE_FINAL;
12131 break;
12132 }
12133#endif
12134 /*
12135 * Try skipping one character in the bad word (delete it).
12136 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012137 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012138 sp->ts_state = STATE_INS_PREP;
12139 sp->ts_curi = 1;
12140 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12141 /* Deleting a vowel at the start of a word counts less, see
12142 * soundalike_score(). */
12143 newscore = 2 * SCORE_DEL / 3;
12144 else
12145 newscore = SCORE_DEL;
12146 if (fword[sp->ts_fidx] != NUL
12147 && TRY_DEEPER(su, stack, depth, newscore))
12148 {
12149 go_deeper(stack, depth, newscore);
12150#ifdef DEBUG_TRIEWALK
12151 sprintf(changename[depth], "%.*s-%s: delete %c",
12152 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12153 fword[sp->ts_fidx]);
12154#endif
12155 ++depth;
12156
12157 /* Remember what character we deleted, so that we can avoid
12158 * inserting it again. */
12159 stack[depth].ts_flags |= TSF_DIDDEL;
12160 stack[depth].ts_delidx = sp->ts_fidx;
12161
12162 /* Advance over the character in fword[]. Give a bonus to the
12163 * score if the same character is following "nn" -> "n". It's
12164 * a bit illogical for soundfold tree but it does give better
12165 * results. */
12166#ifdef FEAT_MBYTE
12167 if (has_mbyte)
12168 {
12169 c = mb_ptr2char(fword + sp->ts_fidx);
12170 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12171 if (enc_utf8 && utf_iscomposing(c))
12172 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12173 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12174 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12175 }
12176 else
12177#endif
12178 {
12179 ++stack[depth].ts_fidx;
12180 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12181 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12182 }
12183 break;
12184 }
12185 /*FALLTHROUGH*/
12186
12187 case STATE_INS_PREP:
12188 if (sp->ts_flags & TSF_DIDDEL)
12189 {
12190 /* If we just deleted a byte then inserting won't make sense,
12191 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012192 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012193 sp->ts_state = STATE_SWAP;
12194 break;
12195 }
12196
12197 /* skip over NUL bytes */
12198 n = sp->ts_arridx;
12199 for (;;)
12200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012201 if (sp->ts_curi > byts[n])
12202 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012203 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012204 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012205 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012206 break;
12207 }
12208 if (byts[n + sp->ts_curi] != NUL)
12209 {
12210 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012211 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012212 sp->ts_state = STATE_INS;
12213 break;
12214 }
12215 ++sp->ts_curi;
12216 }
12217 break;
12218
12219 /*FALLTHROUGH*/
12220
12221 case STATE_INS:
12222 /* Insert one byte. Repeat this for each possible byte at this
12223 * node. */
12224 n = sp->ts_arridx;
12225 if (sp->ts_curi > byts[n])
12226 {
12227 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012228 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012229 sp->ts_state = STATE_SWAP;
12230 break;
12231 }
12232
12233 /* Do one more byte at this node, but:
12234 * - Skip NUL bytes.
12235 * - Skip the byte if it's equal to the byte in the word,
12236 * accepting that byte is always better.
12237 */
12238 n += sp->ts_curi++;
12239 c = byts[n];
12240 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12241 /* Inserting a vowel at the start of a word counts less,
12242 * see soundalike_score(). */
12243 newscore = 2 * SCORE_INS / 3;
12244 else
12245 newscore = SCORE_INS;
12246 if (c != fword[sp->ts_fidx]
12247 && TRY_DEEPER(su, stack, depth, newscore))
12248 {
12249 go_deeper(stack, depth, newscore);
12250#ifdef DEBUG_TRIEWALK
12251 sprintf(changename[depth], "%.*s-%s: insert %c",
12252 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12253 c);
12254#endif
12255 ++depth;
12256 sp = &stack[depth];
12257 tword[sp->ts_twordlen++] = c;
12258 sp->ts_arridx = idxs[n];
12259#ifdef FEAT_MBYTE
12260 if (has_mbyte)
12261 {
12262 fl = MB_BYTE2LEN(c);
12263 if (fl > 1)
12264 {
12265 /* There are following bytes for the same character.
12266 * We must find all bytes before trying
12267 * delete/insert/swap/etc. */
12268 sp->ts_tcharlen = fl;
12269 sp->ts_tcharidx = 1;
12270 sp->ts_isdiff = DIFF_INSERT;
12271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012272 }
12273 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012274 fl = 1;
12275 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012276#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012277 {
12278 /* If the previous character was the same, thus doubling a
12279 * character, give a bonus to the score. Also for
12280 * soundfold words (illogical but does give a better
12281 * score). */
12282 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012283 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012284 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012286 }
12287 break;
12288
12289 case STATE_SWAP:
12290 /*
12291 * Swap two bytes in the bad word: "12" -> "21".
12292 * We change "fword" here, it's changed back afterwards at
12293 * STATE_UNSWAP.
12294 */
12295 p = fword + sp->ts_fidx;
12296 c = *p;
12297 if (c == NUL)
12298 {
12299 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012300 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012301 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012302 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012303 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012304
Bram Moolenaar4770d092006-01-12 23:22:24 +000012305 /* Don't swap if the first character is not a word character.
12306 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012307 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012308 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012309 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012310 sp->ts_state = STATE_REP_INI;
12311 break;
12312 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012313
Bram Moolenaar4770d092006-01-12 23:22:24 +000012314#ifdef FEAT_MBYTE
12315 if (has_mbyte)
12316 {
12317 n = mb_cptr2len(p);
12318 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012319 if (p[n] == NUL)
12320 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012321 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012322 c2 = c; /* don't swap non-word char */
12323 else
12324 c2 = mb_ptr2char(p + n);
12325 }
12326 else
12327#endif
12328 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012329 if (p[1] == NUL)
12330 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012331 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012332 c2 = c; /* don't swap non-word char */
12333 else
12334 c2 = p[1];
12335 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012336
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012337 /* When the second character is NUL we can't swap. */
12338 if (c2 == NUL)
12339 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012340 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012341 sp->ts_state = STATE_REP_INI;
12342 break;
12343 }
12344
Bram Moolenaar4770d092006-01-12 23:22:24 +000012345 /* When characters are identical, swap won't do anything.
12346 * Also get here if the second char is not a word character. */
12347 if (c == c2)
12348 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012349 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012350 sp->ts_state = STATE_SWAP3;
12351 break;
12352 }
12353 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12354 {
12355 go_deeper(stack, depth, SCORE_SWAP);
12356#ifdef DEBUG_TRIEWALK
12357 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12358 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12359 c, c2);
12360#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012361 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012362 sp->ts_state = STATE_UNSWAP;
12363 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012364#ifdef FEAT_MBYTE
12365 if (has_mbyte)
12366 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012367 fl = mb_char2len(c2);
12368 mch_memmove(p, p + n, fl);
12369 mb_char2bytes(c, p + fl);
12370 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012371 }
12372 else
12373#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012374 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012375 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012376 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012377 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012378 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012379 }
12380 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012381 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012382 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012383 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012384 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012385 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012386 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012387
Bram Moolenaar4770d092006-01-12 23:22:24 +000012388 case STATE_UNSWAP:
12389 /* Undo the STATE_SWAP swap: "21" -> "12". */
12390 p = fword + sp->ts_fidx;
12391#ifdef FEAT_MBYTE
12392 if (has_mbyte)
12393 {
12394 n = MB_BYTE2LEN(*p);
12395 c = mb_ptr2char(p + n);
12396 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12397 mb_char2bytes(c, p);
12398 }
12399 else
12400#endif
12401 {
12402 c = *p;
12403 *p = p[1];
12404 p[1] = c;
12405 }
12406 /*FALLTHROUGH*/
12407
12408 case STATE_SWAP3:
12409 /* Swap two bytes, skipping one: "123" -> "321". We change
12410 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12411 p = fword + sp->ts_fidx;
12412#ifdef FEAT_MBYTE
12413 if (has_mbyte)
12414 {
12415 n = mb_cptr2len(p);
12416 c = mb_ptr2char(p);
12417 fl = mb_cptr2len(p + n);
12418 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012419 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012420 c3 = c; /* don't swap non-word char */
12421 else
12422 c3 = mb_ptr2char(p + n + fl);
12423 }
12424 else
12425#endif
12426 {
12427 c = *p;
12428 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012429 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012430 c3 = c; /* don't swap non-word char */
12431 else
12432 c3 = p[2];
12433 }
12434
12435 /* When characters are identical: "121" then SWAP3 result is
12436 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12437 * same as SWAP on next char: "112". Thus skip all swapping.
12438 * Also skip when c3 is NUL.
12439 * Also get here when the third character is not a word character.
12440 * Second character may any char: "a.b" -> "b.a" */
12441 if (c == c3 || c3 == NUL)
12442 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012443 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012444 sp->ts_state = STATE_REP_INI;
12445 break;
12446 }
12447 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12448 {
12449 go_deeper(stack, depth, SCORE_SWAP3);
12450#ifdef DEBUG_TRIEWALK
12451 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12452 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12453 c, c3);
12454#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012455 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012456 sp->ts_state = STATE_UNSWAP3;
12457 ++depth;
12458#ifdef FEAT_MBYTE
12459 if (has_mbyte)
12460 {
12461 tl = mb_char2len(c3);
12462 mch_memmove(p, p + n + fl, tl);
12463 mb_char2bytes(c2, p + tl);
12464 mb_char2bytes(c, p + fl + tl);
12465 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12466 }
12467 else
12468#endif
12469 {
12470 p[0] = p[2];
12471 p[2] = c;
12472 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12473 }
12474 }
12475 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012476 {
12477 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012478 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012479 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012480 break;
12481
12482 case STATE_UNSWAP3:
12483 /* Undo STATE_SWAP3: "321" -> "123" */
12484 p = fword + sp->ts_fidx;
12485#ifdef FEAT_MBYTE
12486 if (has_mbyte)
12487 {
12488 n = MB_BYTE2LEN(*p);
12489 c2 = mb_ptr2char(p + n);
12490 fl = MB_BYTE2LEN(p[n]);
12491 c = mb_ptr2char(p + n + fl);
12492 tl = MB_BYTE2LEN(p[n + fl]);
12493 mch_memmove(p + fl + tl, p, n);
12494 mb_char2bytes(c, p);
12495 mb_char2bytes(c2, p + tl);
12496 p = p + tl;
12497 }
12498 else
12499#endif
12500 {
12501 c = *p;
12502 *p = p[2];
12503 p[2] = c;
12504 ++p;
12505 }
12506
Bram Moolenaar860cae12010-06-05 23:22:07 +020012507 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012508 {
12509 /* Middle char is not a word char, skip the rotate. First and
12510 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012511 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012512 sp->ts_state = STATE_REP_INI;
12513 break;
12514 }
12515
12516 /* Rotate three characters left: "123" -> "231". We change
12517 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12518 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12519 {
12520 go_deeper(stack, depth, SCORE_SWAP3);
12521#ifdef DEBUG_TRIEWALK
12522 p = fword + sp->ts_fidx;
12523 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12524 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12525 p[0], p[1], p[2]);
12526#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012527 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012528 sp->ts_state = STATE_UNROT3L;
12529 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012530 p = fword + sp->ts_fidx;
12531#ifdef FEAT_MBYTE
12532 if (has_mbyte)
12533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012534 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012535 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012536 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012537 fl += mb_cptr2len(p + n + fl);
12538 mch_memmove(p, p + n, fl);
12539 mb_char2bytes(c, p + fl);
12540 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012541 }
12542 else
12543#endif
12544 {
12545 c = *p;
12546 *p = p[1];
12547 p[1] = p[2];
12548 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012549 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012550 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012551 }
12552 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012553 {
12554 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012555 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012556 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012557 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558
Bram Moolenaar4770d092006-01-12 23:22:24 +000012559 case STATE_UNROT3L:
12560 /* Undo ROT3L: "231" -> "123" */
12561 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012562#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012563 if (has_mbyte)
12564 {
12565 n = MB_BYTE2LEN(*p);
12566 n += MB_BYTE2LEN(p[n]);
12567 c = mb_ptr2char(p + n);
12568 tl = MB_BYTE2LEN(p[n]);
12569 mch_memmove(p + tl, p, n);
12570 mb_char2bytes(c, p);
12571 }
12572 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012573#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012574 {
12575 c = p[2];
12576 p[2] = p[1];
12577 p[1] = *p;
12578 *p = c;
12579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580
Bram Moolenaar4770d092006-01-12 23:22:24 +000012581 /* Rotate three bytes right: "123" -> "312". We change "fword"
12582 * here, it's changed back afterwards at STATE_UNROT3R. */
12583 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12584 {
12585 go_deeper(stack, depth, SCORE_SWAP3);
12586#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012587 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012588 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12589 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12590 p[0], p[1], p[2]);
12591#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012592 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012593 sp->ts_state = STATE_UNROT3R;
12594 ++depth;
12595 p = fword + sp->ts_fidx;
12596#ifdef FEAT_MBYTE
12597 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012598 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012599 n = mb_cptr2len(p);
12600 n += mb_cptr2len(p + n);
12601 c = mb_ptr2char(p + n);
12602 tl = mb_cptr2len(p + n);
12603 mch_memmove(p + tl, p, n);
12604 mb_char2bytes(c, p);
12605 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012606 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012607 else
12608#endif
12609 {
12610 c = p[2];
12611 p[2] = p[1];
12612 p[1] = *p;
12613 *p = c;
12614 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12615 }
12616 }
12617 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012618 {
12619 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012620 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012621 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012622 break;
12623
12624 case STATE_UNROT3R:
12625 /* Undo ROT3R: "312" -> "123" */
12626 p = fword + sp->ts_fidx;
12627#ifdef FEAT_MBYTE
12628 if (has_mbyte)
12629 {
12630 c = mb_ptr2char(p);
12631 tl = MB_BYTE2LEN(*p);
12632 n = MB_BYTE2LEN(p[tl]);
12633 n += MB_BYTE2LEN(p[tl + n]);
12634 mch_memmove(p, p + tl, n);
12635 mb_char2bytes(c, p + n);
12636 }
12637 else
12638#endif
12639 {
12640 c = *p;
12641 *p = p[1];
12642 p[1] = p[2];
12643 p[2] = c;
12644 }
12645 /*FALLTHROUGH*/
12646
12647 case STATE_REP_INI:
12648 /* Check if matching with REP items from the .aff file would work.
12649 * Quickly skip if:
12650 * - there are no REP items and we are not in the soundfold trie
12651 * - the score is going to be too high anyway
12652 * - already applied a REP item or swapped here */
12653 if ((lp->lp_replang == NULL && !soundfold)
12654 || sp->ts_score + SCORE_REP >= su->su_maxscore
12655 || sp->ts_fidx < sp->ts_fidxtry)
12656 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012657 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012658 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012659 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012660 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661
Bram Moolenaar4770d092006-01-12 23:22:24 +000012662 /* Use the first byte to quickly find the first entry that may
12663 * match. If the index is -1 there is none. */
12664 if (soundfold)
12665 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12666 else
12667 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668
Bram Moolenaar4770d092006-01-12 23:22:24 +000012669 if (sp->ts_curi < 0)
12670 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012671 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012672 sp->ts_state = STATE_FINAL;
12673 break;
12674 }
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_REP;
12678 /*FALLTHROUGH*/
12679
12680 case STATE_REP:
12681 /* Try matching with REP items from the .aff file. For each match
12682 * replace the characters and check if the resulting word is
12683 * valid. */
12684 p = fword + sp->ts_fidx;
12685
12686 if (soundfold)
12687 gap = &slang->sl_repsal;
12688 else
12689 gap = &lp->lp_replang->sl_rep;
12690 while (sp->ts_curi < gap->ga_len)
12691 {
12692 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12693 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012694 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012695 /* past possible matching entries */
12696 sp->ts_curi = gap->ga_len;
12697 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012698 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012699 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12700 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12701 {
12702 go_deeper(stack, depth, SCORE_REP);
12703#ifdef DEBUG_TRIEWALK
12704 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12705 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12706 ftp->ft_from, ftp->ft_to);
12707#endif
12708 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012709 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012710 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012711
Bram Moolenaar4770d092006-01-12 23:22:24 +000012712 /* Change the "from" to the "to" string. */
12713 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012714 fl = (int)STRLEN(ftp->ft_from);
12715 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012716 if (fl != tl)
12717 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012718 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012719 repextra += tl - fl;
12720 }
12721 mch_memmove(p, ftp->ft_to, tl);
12722 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12723#ifdef FEAT_MBYTE
12724 stack[depth].ts_tcharlen = 0;
12725#endif
12726 break;
12727 }
12728 }
12729
12730 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012731 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012732 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012733 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012734 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012735 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012736
12737 break;
12738
12739 case STATE_REP_UNDO:
12740 /* Undo a REP replacement and continue with the next one. */
12741 if (soundfold)
12742 gap = &slang->sl_repsal;
12743 else
12744 gap = &lp->lp_replang->sl_rep;
12745 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012746 fl = (int)STRLEN(ftp->ft_from);
12747 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012748 p = fword + sp->ts_fidx;
12749 if (fl != tl)
12750 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012751 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012752 repextra -= tl - fl;
12753 }
12754 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010012755 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012756 sp->ts_state = STATE_REP;
12757 break;
12758
12759 default:
12760 /* Did all possible states at this level, go up one level. */
12761 --depth;
12762
12763 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12764 {
12765 /* Continue in or go back to the prefix tree. */
12766 byts = pbyts;
12767 idxs = pidxs;
12768 }
12769
12770 /* Don't check for CTRL-C too often, it takes time. */
12771 if (--breakcheckcount == 0)
12772 {
12773 ui_breakcheck();
12774 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012775 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 }
12777 }
12778}
12779
Bram Moolenaar4770d092006-01-12 23:22:24 +000012780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012781/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012782 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012784 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012785go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786{
Bram Moolenaarea424162005-06-16 21:51:00 +000012787 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012789 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012791 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012792}
12793
Bram Moolenaar53805d12005-08-01 07:08:33 +000012794#ifdef FEAT_MBYTE
12795/*
12796 * Case-folding may change the number of bytes: Count nr of chars in
12797 * fword[flen] and return the byte length of that many chars in "word".
12798 */
12799 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012800nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +000012801{
12802 char_u *p;
12803 int i = 0;
12804
12805 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12806 ++i;
12807 for (p = word; i > 0; mb_ptr_adv(p))
12808 --i;
12809 return (int)(p - word);
12810}
12811#endif
12812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813/*
12814 * "fword" is a good word with case folded. Find the matching keep-case
12815 * words and put it in "kword".
12816 * Theoretically there could be several keep-case words that result in the
12817 * same case-folded word, but we only find one...
12818 */
12819 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012820find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012821{
12822 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12823 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012824 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012825
12826 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012827 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 int round[MAXWLEN];
12829 int fwordidx[MAXWLEN];
12830 int uwordidx[MAXWLEN];
12831 int kwordlen[MAXWLEN];
12832
12833 int flen, ulen;
12834 int l;
12835 int len;
12836 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012837 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012838 char_u *p;
12839 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012840 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841
12842 if (byts == NULL)
12843 {
12844 /* array is empty: "cannot happen" */
12845 *kword = NUL;
12846 return;
12847 }
12848
12849 /* Make an all-cap version of "fword". */
12850 allcap_copy(fword, uword);
12851
12852 /*
12853 * Each character needs to be tried both case-folded and upper-case.
12854 * All this gets very complicated if we keep in mind that changing case
12855 * may change the byte length of a multi-byte character...
12856 */
12857 depth = 0;
12858 arridx[0] = 0;
12859 round[0] = 0;
12860 fwordidx[0] = 0;
12861 uwordidx[0] = 0;
12862 kwordlen[0] = 0;
12863 while (depth >= 0)
12864 {
12865 if (fword[fwordidx[depth]] == NUL)
12866 {
12867 /* We are at the end of "fword". If the tree allows a word to end
12868 * here we have found a match. */
12869 if (byts[arridx[depth] + 1] == 0)
12870 {
12871 kword[kwordlen[depth]] = NUL;
12872 return;
12873 }
12874
12875 /* kword is getting too long, continue one level up */
12876 --depth;
12877 }
12878 else if (++round[depth] > 2)
12879 {
12880 /* tried both fold-case and upper-case character, continue one
12881 * level up */
12882 --depth;
12883 }
12884 else
12885 {
12886 /*
12887 * round[depth] == 1: Try using the folded-case character.
12888 * round[depth] == 2: Try using the upper-case character.
12889 */
12890#ifdef FEAT_MBYTE
12891 if (has_mbyte)
12892 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012893 flen = mb_cptr2len(fword + fwordidx[depth]);
12894 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012895 }
12896 else
12897#endif
12898 ulen = flen = 1;
12899 if (round[depth] == 1)
12900 {
12901 p = fword + fwordidx[depth];
12902 l = flen;
12903 }
12904 else
12905 {
12906 p = uword + uwordidx[depth];
12907 l = ulen;
12908 }
12909
12910 for (tryidx = arridx[depth]; l > 0; --l)
12911 {
12912 /* Perform a binary search in the list of accepted bytes. */
12913 len = byts[tryidx++];
12914 c = *p++;
12915 lo = tryidx;
12916 hi = tryidx + len - 1;
12917 while (lo < hi)
12918 {
12919 m = (lo + hi) / 2;
12920 if (byts[m] > c)
12921 hi = m - 1;
12922 else if (byts[m] < c)
12923 lo = m + 1;
12924 else
12925 {
12926 lo = hi = m;
12927 break;
12928 }
12929 }
12930
12931 /* Stop if there is no matching byte. */
12932 if (hi < lo || byts[lo] != c)
12933 break;
12934
12935 /* Continue at the child (if there is one). */
12936 tryidx = idxs[lo];
12937 }
12938
12939 if (l == 0)
12940 {
12941 /*
12942 * Found the matching char. Copy it to "kword" and go a
12943 * level deeper.
12944 */
12945 if (round[depth] == 1)
12946 {
12947 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12948 flen);
12949 kwordlen[depth + 1] = kwordlen[depth] + flen;
12950 }
12951 else
12952 {
12953 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12954 ulen);
12955 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12956 }
12957 fwordidx[depth + 1] = fwordidx[depth] + flen;
12958 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12959
12960 ++depth;
12961 arridx[depth] = tryidx;
12962 round[depth] = 0;
12963 }
12964 }
12965 }
12966
12967 /* Didn't find it: "cannot happen". */
12968 *kword = NUL;
12969}
12970
12971/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012972 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12973 * su->su_sga.
12974 */
12975 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010012976score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012977{
12978 langp_T *lp;
12979 char_u badsound[MAXWLEN];
12980 int i;
12981 suggest_T *stp;
12982 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012983 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012984 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012985
12986 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12987 return;
12988
12989 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012990 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012991 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012992 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012993 if (lp->lp_slang->sl_sal.ga_len > 0)
12994 {
12995 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012996 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012997
12998 for (i = 0; i < su->su_ga.ga_len; ++i)
12999 {
13000 stp = &SUG(su->su_ga, i);
13001
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013002 /* Case-fold the suggested word, sound-fold it and compute the
13003 * sound-a-like score. */
13004 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013005 if (score < SCORE_MAXMAX)
13006 {
13007 /* Add the suggestion. */
13008 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13009 sstp->st_word = vim_strsave(stp->st_word);
13010 if (sstp->st_word != NULL)
13011 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013012 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013013 sstp->st_score = score;
13014 sstp->st_altscore = 0;
13015 sstp->st_orglen = stp->st_orglen;
13016 ++su->su_sga.ga_len;
13017 }
13018 }
13019 }
13020 break;
13021 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013022 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013023}
13024
13025/*
13026 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013027 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013028 */
13029 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013030score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013031{
13032 int i;
13033 int j;
13034 garray_T ga;
13035 garray_T *gap;
13036 langp_T *lp;
13037 suggest_T *stp;
13038 char_u *p;
13039 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013040 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013041 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013042 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013043
13044 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013045 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013046 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013047 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013048 if (lp->lp_slang->sl_sal.ga_len > 0)
13049 {
13050 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013051 slang = lp->lp_slang;
13052 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013053
13054 for (i = 0; i < su->su_ga.ga_len; ++i)
13055 {
13056 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013057 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013058 if (stp->st_altscore == SCORE_MAXMAX)
13059 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13060 else
13061 stp->st_score = (stp->st_score * 3
13062 + stp->st_altscore) / 4;
13063 stp->st_salscore = FALSE;
13064 }
13065 break;
13066 }
13067 }
13068
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013069 if (slang == NULL) /* Using "double" without sound folding. */
13070 {
13071 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13072 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013073 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013074 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013075
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013076 /* Add the alternate score to su_sga. */
13077 for (i = 0; i < su->su_sga.ga_len; ++i)
13078 {
13079 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013080 stp->st_altscore = spell_edit_score(slang,
13081 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013082 if (stp->st_score == SCORE_MAXMAX)
13083 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13084 else
13085 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13086 stp->st_salscore = TRUE;
13087 }
13088
Bram Moolenaar4770d092006-01-12 23:22:24 +000013089 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13090 * for both lists. */
13091 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013092 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013093 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013094 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13095
13096 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13097 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13098 return;
13099
13100 stp = &SUG(ga, 0);
13101 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13102 {
13103 /* round 1: get a suggestion from su_ga
13104 * round 2: get a suggestion from su_sga */
13105 for (round = 1; round <= 2; ++round)
13106 {
13107 gap = round == 1 ? &su->su_ga : &su->su_sga;
13108 if (i < gap->ga_len)
13109 {
13110 /* Don't add a word if it's already there. */
13111 p = SUG(*gap, i).st_word;
13112 for (j = 0; j < ga.ga_len; ++j)
13113 if (STRCMP(stp[j].st_word, p) == 0)
13114 break;
13115 if (j == ga.ga_len)
13116 stp[ga.ga_len++] = SUG(*gap, i);
13117 else
13118 vim_free(p);
13119 }
13120 }
13121 }
13122
13123 ga_clear(&su->su_ga);
13124 ga_clear(&su->su_sga);
13125
13126 /* Truncate the list to the number of suggestions that will be displayed. */
13127 if (ga.ga_len > su->su_maxcount)
13128 {
13129 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13130 vim_free(stp[i].st_word);
13131 ga.ga_len = su->su_maxcount;
13132 }
13133
13134 su->su_ga = ga;
13135}
13136
13137/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013138 * For the goodword in "stp" compute the soundalike score compared to the
13139 * badword.
13140 */
13141 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013142stp_sal_score(
13143 suggest_T *stp,
13144 suginfo_T *su,
13145 slang_T *slang,
13146 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013147{
13148 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013149 char_u *pbad;
13150 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013151 char_u badsound2[MAXWLEN];
13152 char_u fword[MAXWLEN];
13153 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013154 char_u goodword[MAXWLEN];
13155 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013156
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013157 lendiff = (int)(su->su_badlen - stp->st_orglen);
13158 if (lendiff >= 0)
13159 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013160 else
13161 {
13162 /* soundfold the bad word with more characters following */
13163 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13164
13165 /* When joining two words the sound often changes a lot. E.g., "t he"
13166 * sounds like "t h" while "the" sounds like "@". Avoid that by
13167 * removing the space. Don't do it when the good word also contains a
13168 * space. */
13169 if (vim_iswhite(su->su_badptr[su->su_badlen])
13170 && *skiptowhite(stp->st_word) == NUL)
13171 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013172 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013173
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013174 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013175 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013176 }
13177
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013178 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013179 {
13180 /* Add part of the bad word to the good word, so that we soundfold
13181 * what replaces the bad word. */
13182 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013183 vim_strncpy(goodword + stp->st_wordlen,
13184 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013185 pgood = goodword;
13186 }
13187 else
13188 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013189
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013190 /* Sound-fold the word and compute the score for the difference. */
13191 spell_soundfold(slang, pgood, FALSE, goodsound);
13192
13193 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013194}
13195
Bram Moolenaar4770d092006-01-12 23:22:24 +000013196/* structure used to store soundfolded words that add_sound_suggest() has
13197 * handled already. */
13198typedef struct
13199{
13200 short sft_score; /* lowest score used */
13201 char_u sft_word[1]; /* soundfolded word, actually longer */
13202} sftword_T;
13203
13204static sftword_T dumsft;
13205#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13206#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13207
13208/*
13209 * Prepare for calling suggest_try_soundalike().
13210 */
13211 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013212suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013213{
13214 langp_T *lp;
13215 int lpi;
13216 slang_T *slang;
13217
13218 /* Do this for all languages that support sound folding and for which a
13219 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013220 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013221 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013222 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013223 slang = lp->lp_slang;
13224 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13225 /* prepare the hashtable used by add_sound_suggest() */
13226 hash_init(&slang->sl_sounddone);
13227 }
13228}
13229
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013230/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013231 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013232 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 */
13234 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013235suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236{
13237 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013238 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013239 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013240 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013241
Bram Moolenaar4770d092006-01-12 23:22:24 +000013242 /* Do this for all languages that support sound folding and for which a
13243 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013244 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013245 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013246 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013247 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013248 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 {
13250 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013251 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013252
Bram Moolenaar4770d092006-01-12 23:22:24 +000013253 /* try all kinds of inserts/deletes/swaps/etc. */
13254 /* TODO: also soundfold the next words, so that we can try joining
13255 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +010013256#ifdef SUGGEST_PROFILE
13257 prof_init();
13258#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000013259 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +010013260#ifdef SUGGEST_PROFILE
13261 prof_report("soundalike");
13262#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000013263 }
13264 }
13265}
13266
13267/*
13268 * Finish up after calling suggest_try_soundalike().
13269 */
13270 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013271suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013272{
13273 langp_T *lp;
13274 int lpi;
13275 slang_T *slang;
13276 int todo;
13277 hashitem_T *hi;
13278
13279 /* Do this for all languages that support sound folding and for which a
13280 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013281 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013282 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013283 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013284 slang = lp->lp_slang;
13285 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13286 {
13287 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013288 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013289 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13290 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013292 vim_free(HI2SFT(hi));
13293 --todo;
13294 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013295
13296 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013297 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013298 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013299 }
13300 }
13301}
13302
13303/*
13304 * A match with a soundfolded word is found. Add the good word(s) that
13305 * produce this soundfolded word.
13306 */
13307 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013308add_sound_suggest(
13309 suginfo_T *su,
13310 char_u *goodword,
13311 int score, /* soundfold score */
13312 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013313{
13314 slang_T *slang = lp->lp_slang; /* language for sound folding */
13315 int sfwordnr;
13316 char_u *nrline;
13317 int orgnr;
13318 char_u theword[MAXWLEN];
13319 int i;
13320 int wlen;
13321 char_u *byts;
13322 idx_T *idxs;
13323 int n;
13324 int wordcount;
13325 int wc;
13326 int goodscore;
13327 hash_T hash;
13328 hashitem_T *hi;
13329 sftword_T *sft;
13330 int bc, gc;
13331 int limit;
13332
13333 /*
13334 * It's very well possible that the same soundfold word is found several
13335 * times with different scores. Since the following is quite slow only do
13336 * the words that have a better score than before. Use a hashtable to
13337 * remember the words that have been done.
13338 */
13339 hash = hash_hash(goodword);
13340 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13341 if (HASHITEM_EMPTY(hi))
13342 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013343 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13344 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013345 if (sft != NULL)
13346 {
13347 sft->sft_score = score;
13348 STRCPY(sft->sft_word, goodword);
13349 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13350 }
13351 }
13352 else
13353 {
13354 sft = HI2SFT(hi);
13355 if (score >= sft->sft_score)
13356 return;
13357 sft->sft_score = score;
13358 }
13359
13360 /*
13361 * Find the word nr in the soundfold tree.
13362 */
13363 sfwordnr = soundfold_find(slang, goodword);
13364 if (sfwordnr < 0)
13365 {
13366 EMSG2(_(e_intern2), "add_sound_suggest()");
13367 return;
13368 }
13369
13370 /*
13371 * go over the list of good words that produce this soundfold word
13372 */
13373 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13374 orgnr = 0;
13375 while (*nrline != NUL)
13376 {
13377 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13378 * previous wordnr. */
13379 orgnr += bytes2offset(&nrline);
13380
13381 byts = slang->sl_fbyts;
13382 idxs = slang->sl_fidxs;
13383
13384 /* Lookup the word "orgnr" one of the two tries. */
13385 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013386 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013387 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013388 {
13389 i = 1;
13390 if (wordcount == orgnr && byts[n + 1] == NUL)
13391 break; /* found end of word */
13392
13393 if (byts[n + 1] == NUL)
13394 ++wordcount;
13395
13396 /* skip over the NUL bytes */
13397 for ( ; byts[n + i] == NUL; ++i)
13398 if (i > byts[n]) /* safety check */
13399 {
13400 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013401 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013402 goto badword;
13403 }
13404
13405 /* One of the siblings must have the word. */
13406 for ( ; i < byts[n]; ++i)
13407 {
13408 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13409 if (wordcount + wc > orgnr)
13410 break;
13411 wordcount += wc;
13412 }
13413
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013414 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +000013415 n = idxs[n + i];
13416 }
13417badword:
13418 theword[wlen] = NUL;
13419
13420 /* Go over the possible flags and regions. */
13421 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13422 {
13423 char_u cword[MAXWLEN];
13424 char_u *p;
13425 int flags = (int)idxs[n + i];
13426
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013427 /* Skip words with the NOSUGGEST flag */
13428 if (flags & WF_NOSUGGEST)
13429 continue;
13430
Bram Moolenaar4770d092006-01-12 23:22:24 +000013431 if (flags & WF_KEEPCAP)
13432 {
13433 /* Must find the word in the keep-case tree. */
13434 find_keepcap_word(slang, theword, cword);
13435 p = cword;
13436 }
13437 else
13438 {
13439 flags |= su->su_badflags;
13440 if ((flags & WF_CAPMASK) != 0)
13441 {
13442 /* Need to fix case according to "flags". */
13443 make_case_word(theword, cword, flags);
13444 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013445 }
13446 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013447 p = theword;
13448 }
13449
13450 /* Add the suggestion. */
13451 if (sps_flags & SPS_DOUBLE)
13452 {
13453 /* Add the suggestion if the score isn't too bad. */
13454 if (score <= su->su_maxscore)
13455 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13456 score, 0, FALSE, slang, FALSE);
13457 }
13458 else
13459 {
13460 /* Add a penalty for words in another region. */
13461 if ((flags & WF_REGION)
13462 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13463 goodscore = SCORE_REGION;
13464 else
13465 goodscore = 0;
13466
13467 /* Add a small penalty for changing the first letter from
13468 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013469 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +000013470 * letter is the same, that has already been counted. */
13471 gc = PTR2CHAR(p);
13472 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013474 bc = PTR2CHAR(su->su_badword);
13475 if (!SPELL_ISUPPER(bc)
13476 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13477 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 }
13479
Bram Moolenaar4770d092006-01-12 23:22:24 +000013480 /* Compute the score for the good word. This only does letter
13481 * insert/delete/swap/replace. REP items are not considered,
13482 * which may make the score a bit higher.
13483 * Use a limit for the score to make it work faster. Use
13484 * MAXSCORE(), because RESCORE() will change the score.
13485 * If the limit is very high then the iterative method is
13486 * inefficient, using an array is quicker. */
13487 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13488 if (limit > SCORE_LIMITMAX)
13489 goodscore += spell_edit_score(slang, su->su_badword, p);
13490 else
13491 goodscore += spell_edit_score_limit(slang, su->su_badword,
13492 p, limit);
13493
13494 /* When going over the limit don't bother to do the rest. */
13495 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013497 /* Give a bonus to words seen before. */
13498 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499
Bram Moolenaar4770d092006-01-12 23:22:24 +000013500 /* Add the suggestion if the score isn't too bad. */
13501 goodscore = RESCORE(goodscore, score);
13502 if (goodscore <= su->su_sfmaxscore)
13503 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13504 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 }
13507 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013508 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 }
13510}
13511
13512/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013513 * Find word "word" in fold-case tree for "slang" and return the word number.
13514 */
13515 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013516soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013517{
13518 idx_T arridx = 0;
13519 int len;
13520 int wlen = 0;
13521 int c;
13522 char_u *ptr = word;
13523 char_u *byts;
13524 idx_T *idxs;
13525 int wordnr = 0;
13526
13527 byts = slang->sl_sbyts;
13528 idxs = slang->sl_sidxs;
13529
13530 for (;;)
13531 {
13532 /* First byte is the number of possible bytes. */
13533 len = byts[arridx++];
13534
13535 /* If the first possible byte is a zero the word could end here.
13536 * If the word ends we found the word. If not skip the NUL bytes. */
13537 c = ptr[wlen];
13538 if (byts[arridx] == NUL)
13539 {
13540 if (c == NUL)
13541 break;
13542
13543 /* Skip over the zeros, there can be several. */
13544 while (len > 0 && byts[arridx] == NUL)
13545 {
13546 ++arridx;
13547 --len;
13548 }
13549 if (len == 0)
13550 return -1; /* no children, word should have ended here */
13551 ++wordnr;
13552 }
13553
13554 /* If the word ends we didn't find it. */
13555 if (c == NUL)
13556 return -1;
13557
13558 /* Perform a binary search in the list of accepted bytes. */
13559 if (c == TAB) /* <Tab> is handled like <Space> */
13560 c = ' ';
13561 while (byts[arridx] < c)
13562 {
13563 /* The word count is in the first idxs[] entry of the child. */
13564 wordnr += idxs[idxs[arridx]];
13565 ++arridx;
13566 if (--len == 0) /* end of the bytes, didn't find it */
13567 return -1;
13568 }
13569 if (byts[arridx] != c) /* didn't find the byte */
13570 return -1;
13571
13572 /* Continue at the child (if there is one). */
13573 arridx = idxs[arridx];
13574 ++wlen;
13575
13576 /* One space in the good word may stand for several spaces in the
13577 * checked word. */
13578 if (c == ' ')
13579 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13580 ++wlen;
13581 }
13582
13583 return wordnr;
13584}
13585
13586/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013587 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 */
13589 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013590make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591{
13592 if (flags & WF_ALLCAP)
13593 /* Make it all upper-case */
13594 allcap_copy(fword, cword);
13595 else if (flags & WF_ONECAP)
13596 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013597 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 else
13599 /* Use goodword as-is. */
13600 STRCPY(cword, fword);
13601}
13602
Bram Moolenaarea424162005-06-16 21:51:00 +000013603/*
13604 * Use map string "map" for languages "lp".
13605 */
13606 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013607set_map_str(slang_T *lp, char_u *map)
Bram Moolenaarea424162005-06-16 21:51:00 +000013608{
13609 char_u *p;
13610 int headc = 0;
13611 int c;
13612 int i;
13613
13614 if (*map == NUL)
13615 {
13616 lp->sl_has_map = FALSE;
13617 return;
13618 }
13619 lp->sl_has_map = TRUE;
13620
Bram Moolenaar4770d092006-01-12 23:22:24 +000013621 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013622 for (i = 0; i < 256; ++i)
13623 lp->sl_map_array[i] = 0;
13624#ifdef FEAT_MBYTE
13625 hash_init(&lp->sl_map_hash);
13626#endif
13627
13628 /*
13629 * The similar characters are stored separated with slashes:
13630 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13631 * before the same slash. For characters above 255 sl_map_hash is used.
13632 */
13633 for (p = map; *p != NUL; )
13634 {
13635#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013636 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013637#else
13638 c = *p++;
13639#endif
13640 if (c == '/')
13641 headc = 0;
13642 else
13643 {
13644 if (headc == 0)
13645 headc = c;
13646
13647#ifdef FEAT_MBYTE
13648 /* Characters above 255 don't fit in sl_map_array[], put them in
13649 * the hash table. Each entry is the char, a NUL the headchar and
13650 * a NUL. */
13651 if (c >= 256)
13652 {
13653 int cl = mb_char2len(c);
13654 int headcl = mb_char2len(headc);
13655 char_u *b;
13656 hash_T hash;
13657 hashitem_T *hi;
13658
13659 b = alloc((unsigned)(cl + headcl + 2));
13660 if (b == NULL)
13661 return;
13662 mb_char2bytes(c, b);
13663 b[cl] = NUL;
13664 mb_char2bytes(headc, b + cl + 1);
13665 b[cl + 1 + headcl] = NUL;
13666 hash = hash_hash(b);
13667 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13668 if (HASHITEM_EMPTY(hi))
13669 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13670 else
13671 {
13672 /* This should have been checked when generating the .spl
13673 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013674 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013675 vim_free(b);
13676 }
13677 }
13678 else
13679#endif
13680 lp->sl_map_array[c] = headc;
13681 }
13682 }
13683}
13684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685/*
13686 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13687 * lines in the .aff file.
13688 */
13689 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013690similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691{
Bram Moolenaarea424162005-06-16 21:51:00 +000013692 int m1, m2;
13693#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +020013694 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +000013695 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696
Bram Moolenaarea424162005-06-16 21:51:00 +000013697 if (c1 >= 256)
13698 {
13699 buf[mb_char2bytes(c1, buf)] = 0;
13700 hi = hash_find(&slang->sl_map_hash, buf);
13701 if (HASHITEM_EMPTY(hi))
13702 m1 = 0;
13703 else
13704 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13705 }
13706 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013707#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013708 m1 = slang->sl_map_array[c1];
13709 if (m1 == 0)
13710 return FALSE;
13711
13712
13713#ifdef FEAT_MBYTE
13714 if (c2 >= 256)
13715 {
13716 buf[mb_char2bytes(c2, buf)] = 0;
13717 hi = hash_find(&slang->sl_map_hash, buf);
13718 if (HASHITEM_EMPTY(hi))
13719 m2 = 0;
13720 else
13721 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13722 }
13723 else
13724#endif
13725 m2 = slang->sl_map_array[c2];
13726
13727 return m1 == m2;
13728}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729
13730/*
13731 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013732 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013733 */
13734 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013735add_suggestion(
13736 suginfo_T *su,
13737 garray_T *gap, /* either su_ga or su_sga */
13738 char_u *goodword,
13739 int badlenarg, /* len of bad word replaced with "goodword" */
13740 int score,
13741 int altscore,
13742 int had_bonus, /* value for st_had_bonus */
13743 slang_T *slang, /* language for sound folding */
13744 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +000013745 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013747 int goodlen; /* len of goodword changed */
13748 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013750 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013752 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013754 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13755 * "thee the" is added next to changing the first "the" the "thee". */
13756 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013757 pbad = su->su_badptr + badlenarg;
13758 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013759 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013760 goodlen = (int)(pgood - goodword);
13761 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013762 if (goodlen <= 0 || badlen <= 0)
13763 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013764 mb_ptr_back(goodword, pgood);
13765 mb_ptr_back(su->su_badptr, pbad);
13766#ifdef FEAT_MBYTE
13767 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013768 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013769 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13770 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013771 }
13772 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013773#endif
13774 if (*pgood != *pbad)
13775 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013776 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013777
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013778 if (badlen == 0 && goodlen == 0)
13779 /* goodword doesn't change anything; may happen for "the the" changing
13780 * the first "the" to itself. */
13781 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013782
Bram Moolenaar89d40322006-08-29 15:30:07 +000013783 if (gap->ga_len == 0)
13784 i = -1;
13785 else
13786 {
13787 /* Check if the word is already there. Also check the length that is
13788 * being replaced "thes," -> "these" is a different suggestion from
13789 * "thes" -> "these". */
13790 stp = &SUG(*gap, 0);
13791 for (i = gap->ga_len; --i >= 0; ++stp)
13792 if (stp->st_wordlen == goodlen
13793 && stp->st_orglen == badlen
13794 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013796 /*
13797 * Found it. Remember the word with the lowest score.
13798 */
13799 if (stp->st_slang == NULL)
13800 stp->st_slang = slang;
13801
13802 new_sug.st_score = score;
13803 new_sug.st_altscore = altscore;
13804 new_sug.st_had_bonus = had_bonus;
13805
13806 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013807 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013808 /* Only one of the two had the soundalike score computed.
13809 * Need to do that for the other one now, otherwise the
13810 * scores can't be compared. This happens because
13811 * suggest_try_change() doesn't compute the soundalike
13812 * word to keep it fast, while some special methods set
13813 * the soundalike score to zero. */
13814 if (had_bonus)
13815 rescore_one(su, stp);
13816 else
13817 {
13818 new_sug.st_word = stp->st_word;
13819 new_sug.st_wordlen = stp->st_wordlen;
13820 new_sug.st_slang = stp->st_slang;
13821 new_sug.st_orglen = badlen;
13822 rescore_one(su, &new_sug);
13823 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013824 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825
Bram Moolenaar89d40322006-08-29 15:30:07 +000013826 if (stp->st_score > new_sug.st_score)
13827 {
13828 stp->st_score = new_sug.st_score;
13829 stp->st_altscore = new_sug.st_altscore;
13830 stp->st_had_bonus = new_sug.st_had_bonus;
13831 }
13832 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013833 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835
Bram Moolenaar4770d092006-01-12 23:22:24 +000013836 if (i < 0 && ga_grow(gap, 1) == OK)
13837 {
13838 /* Add a suggestion. */
13839 stp = &SUG(*gap, gap->ga_len);
13840 stp->st_word = vim_strnsave(goodword, goodlen);
13841 if (stp->st_word != NULL)
13842 {
13843 stp->st_wordlen = goodlen;
13844 stp->st_score = score;
13845 stp->st_altscore = altscore;
13846 stp->st_had_bonus = had_bonus;
13847 stp->st_orglen = badlen;
13848 stp->st_slang = slang;
13849 ++gap->ga_len;
13850
13851 /* If we have too many suggestions now, sort the list and keep
13852 * the best suggestions. */
13853 if (gap->ga_len > SUG_MAX_COUNT(su))
13854 {
13855 if (maxsf)
13856 su->su_sfmaxscore = cleanup_suggestions(gap,
13857 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13858 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013859 su->su_maxscore = cleanup_suggestions(gap,
13860 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 }
13862 }
13863 }
13864}
13865
13866/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013867 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13868 * for split words, such as "the the". Remove these from the list here.
13869 */
13870 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013871check_suggestions(
13872 suginfo_T *su,
13873 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013874{
13875 suggest_T *stp;
13876 int i;
13877 char_u longword[MAXWLEN + 1];
13878 int len;
13879 hlf_T attr;
13880
13881 stp = &SUG(*gap, 0);
13882 for (i = gap->ga_len - 1; i >= 0; --i)
13883 {
13884 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013885 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013886 len = stp[i].st_wordlen;
13887 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13888 MAXWLEN - len);
13889 attr = HLF_COUNT;
13890 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13891 if (attr != HLF_COUNT)
13892 {
13893 /* Remove this entry. */
13894 vim_free(stp[i].st_word);
13895 --gap->ga_len;
13896 if (i < gap->ga_len)
13897 mch_memmove(stp + i, stp + i + 1,
13898 sizeof(suggest_T) * (gap->ga_len - i));
13899 }
13900 }
13901}
13902
13903
13904/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 * Add a word to be banned.
13906 */
13907 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013908add_banned(
13909 suginfo_T *su,
13910 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013912 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013913 hash_T hash;
13914 hashitem_T *hi;
13915
Bram Moolenaar4770d092006-01-12 23:22:24 +000013916 hash = hash_hash(word);
13917 hi = hash_lookup(&su->su_banned, word, hash);
13918 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013920 s = vim_strsave(word);
13921 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 hash_add_item(&su->su_banned, hi, s, hash);
13923 }
13924}
13925
13926/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013927 * Recompute the score for all suggestions if sound-folding is possible. This
13928 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013929 */
13930 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013931rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013932{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013933 int i;
13934
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013935 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013936 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013937 rescore_one(su, &SUG(su->su_ga, i));
13938}
13939
13940/*
13941 * Recompute the score for one suggestion if sound-folding is possible.
13942 */
13943 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013944rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013945{
13946 slang_T *slang = stp->st_slang;
13947 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013948 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013949
13950 /* Only rescore suggestions that have no sal score yet and do have a
13951 * language. */
13952 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13953 {
13954 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013955 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013956 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013957 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013958 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013959 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013960 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013961
13962 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013963 if (stp->st_altscore == SCORE_MAXMAX)
13964 stp->st_altscore = SCORE_BIG;
13965 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13966 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013967 }
13968}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970static int
13971#ifdef __BORLANDC__
13972_RTLENTRYF
13973#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010013974sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975
13976/*
13977 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013978 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013979 */
13980 static int
13981#ifdef __BORLANDC__
13982_RTLENTRYF
13983#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +010013984sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985{
13986 suggest_T *p1 = (suggest_T *)s1;
13987 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013988 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013989
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013990 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013991 {
13992 n = p1->st_altscore - p2->st_altscore;
13993 if (n == 0)
13994 n = STRICMP(p1->st_word, p2->st_word);
13995 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013996 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997}
13998
13999/*
14000 * Cleanup the suggestions:
14001 * - Sort on score.
14002 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014003 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014005 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014006cleanup_suggestions(
14007 garray_T *gap,
14008 int maxscore,
14009 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014011 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014012 int i;
14013
14014 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014015 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016
14017 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014018 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014019 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014020 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014022 gap->ga_len = keep;
14023 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014025 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026}
14027
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014028#if defined(FEAT_EVAL) || defined(PROTO)
14029/*
14030 * Soundfold a string, for soundfold().
14031 * Result is in allocated memory, NULL for an error.
14032 */
14033 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014034eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014035{
14036 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014037 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014038 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014039
Bram Moolenaar860cae12010-06-05 23:22:07 +020014040 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014041 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014042 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014043 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014044 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014045 if (lp->lp_slang->sl_sal.ga_len > 0)
14046 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014047 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014048 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014049 return vim_strsave(sound);
14050 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014051 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014052
14053 /* No language with sound folding, return word as-is. */
14054 return vim_strsave(word);
14055}
14056#endif
14057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058/*
14059 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014060 *
14061 * There are many ways to turn a word into a sound-a-like representation. The
14062 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14063 * swedish name matching - survey and test of different algorithms" by Klas
14064 * Erikson.
14065 *
14066 * We support two methods:
14067 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14068 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 */
14070 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014071spell_soundfold(
14072 slang_T *slang,
14073 char_u *inword,
14074 int folded, /* "inword" is already case-folded */
14075 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014076{
14077 char_u fword[MAXWLEN];
14078 char_u *word;
14079
14080 if (slang->sl_sofo)
14081 /* SOFOFROM and SOFOTO used */
14082 spell_soundfold_sofo(slang, inword, res);
14083 else
14084 {
14085 /* SAL items used. Requires the word to be case-folded. */
14086 if (folded)
14087 word = inword;
14088 else
14089 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014090 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014091 word = fword;
14092 }
14093
14094#ifdef FEAT_MBYTE
14095 if (has_mbyte)
14096 spell_soundfold_wsal(slang, word, res);
14097 else
14098#endif
14099 spell_soundfold_sal(slang, word, res);
14100 }
14101}
14102
14103/*
14104 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14105 * SOFOTO lines.
14106 */
14107 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014108spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014109{
14110 char_u *s;
14111 int ri = 0;
14112 int c;
14113
14114#ifdef FEAT_MBYTE
14115 if (has_mbyte)
14116 {
14117 int prevc = 0;
14118 int *ip;
14119
14120 /* The sl_sal_first[] table contains the translation for chars up to
14121 * 255, sl_sal the rest. */
14122 for (s = inword; *s != NUL; )
14123 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014124 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014125 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14126 c = ' ';
14127 else if (c < 256)
14128 c = slang->sl_sal_first[c];
14129 else
14130 {
14131 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14132 if (ip == NULL) /* empty list, can't match */
14133 c = NUL;
14134 else
14135 for (;;) /* find "c" in the list */
14136 {
14137 if (*ip == 0) /* not found */
14138 {
14139 c = NUL;
14140 break;
14141 }
14142 if (*ip == c) /* match! */
14143 {
14144 c = ip[1];
14145 break;
14146 }
14147 ip += 2;
14148 }
14149 }
14150
14151 if (c != NUL && c != prevc)
14152 {
14153 ri += mb_char2bytes(c, res + ri);
14154 if (ri + MB_MAXBYTES > MAXWLEN)
14155 break;
14156 prevc = c;
14157 }
14158 }
14159 }
14160 else
14161#endif
14162 {
14163 /* The sl_sal_first[] table contains the translation. */
14164 for (s = inword; (c = *s) != NUL; ++s)
14165 {
14166 if (vim_iswhite(c))
14167 c = ' ';
14168 else
14169 c = slang->sl_sal_first[c];
14170 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14171 res[ri++] = c;
14172 }
14173 }
14174
14175 res[ri] = NUL;
14176}
14177
14178 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014179spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014181 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014183 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014185 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014187 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 int n, k = 0;
14189 int z0;
14190 int k0;
14191 int n0;
14192 int c;
14193 int pri;
14194 int p0 = -333;
14195 int c0;
14196
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014197 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014198 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 if (slang->sl_rem_accents)
14200 {
14201 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014202 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014204 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014205 {
14206 *t++ = ' ';
14207 s = skipwhite(s);
14208 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014209 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014211 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 *t++ = *s;
14213 ++s;
14214 }
14215 }
14216 *t = NUL;
14217 }
14218 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014219 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014221 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222
14223 /*
14224 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014225 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014227 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 while ((c = word[i]) != NUL)
14229 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014230 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 n = slang->sl_sal_first[c];
14232 z0 = 0;
14233
14234 if (n >= 0)
14235 {
14236 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014237 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014239 /* Quickly skip entries that don't match the word. Most
14240 * entries are less then three chars, optimize for that. */
14241 k = smp[n].sm_leadlen;
14242 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014244 if (word[i + 1] != s[1])
14245 continue;
14246 if (k > 2)
14247 {
14248 for (j = 2; j < k; ++j)
14249 if (word[i + j] != s[j])
14250 break;
14251 if (j < k)
14252 continue;
14253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 }
14255
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014256 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014258 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014259 while (*pf != NUL && *pf != word[i + k])
14260 ++pf;
14261 if (*pf == NUL)
14262 continue;
14263 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014265 s = smp[n].sm_rules;
14266 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267
14268 p0 = *s;
14269 k0 = k;
14270 while (*s == '-' && k > 1)
14271 {
14272 k--;
14273 s++;
14274 }
14275 if (*s == '<')
14276 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014277 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 {
14279 /* determine priority */
14280 pri = *s - '0';
14281 s++;
14282 }
14283 if (*s == '^' && *(s + 1) == '^')
14284 s++;
14285
14286 if (*s == NUL
14287 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014288 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014289 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014291 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014293 && spell_iswordp(word + i - 1, curwin)
14294 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 {
14296 /* search for followup rules, if: */
14297 /* followup and k > 1 and NO '-' in searchstring */
14298 c0 = word[i + k - 1];
14299 n0 = slang->sl_sal_first[c0];
14300
14301 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014302 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 {
14304 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014305 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307 /* Quickly skip entries that don't match the word.
14308 * */
14309 k0 = smp[n0].sm_leadlen;
14310 if (k0 > 1)
14311 {
14312 if (word[i + k] != s[1])
14313 continue;
14314 if (k0 > 2)
14315 {
14316 pf = word + i + k + 1;
14317 for (j = 2; j < k0; ++j)
14318 if (*pf++ != s[j])
14319 break;
14320 if (j < k0)
14321 continue;
14322 }
14323 }
14324 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014326 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014327 {
14328 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014329 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014330 while (*pf != NUL && *pf != word[i + k0])
14331 ++pf;
14332 if (*pf == NUL)
14333 continue;
14334 ++k0;
14335 }
14336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014338 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 while (*s == '-')
14340 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014341 /* "k0" gets NOT reduced because
14342 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 s++;
14344 }
14345 if (*s == '<')
14346 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014347 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 {
14349 p0 = *s - '0';
14350 s++;
14351 }
14352
14353 if (*s == NUL
14354 /* *s == '^' cuts */
14355 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014356 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014357 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014358 {
14359 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362
14363 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014365 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 /* rule fits; stop search */
14367 break;
14368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 }
14370
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014371 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 }
14374
14375 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014377 if (s == NULL)
14378 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014379 pf = smp[n].sm_rules;
14380 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 if (p0 == 1 && z == 0)
14382 {
14383 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014384 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14385 || res[reslen - 1] == *s))
14386 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 z0 = 1;
14388 z = 1;
14389 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014390 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 {
14392 word[i + k0] = *s;
14393 k0++;
14394 s++;
14395 }
14396 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014397 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014398
14399 /* new "actual letter" */
14400 c = word[i];
14401 }
14402 else
14403 {
14404 /* no '<' rule used */
14405 i += k - 1;
14406 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014407 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014409 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014410 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 s++;
14412 }
14413 /* new "actual letter" */
14414 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014415 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 {
14417 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014418 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014419 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 i = 0;
14421 z0 = 1;
14422 }
14423 }
14424 break;
14425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 }
14427 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014428 else if (vim_iswhite(c))
14429 {
14430 c = ' ';
14431 k = 1;
14432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433
14434 if (z0 == 0)
14435 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014436 if (k && !p0 && reslen < MAXWLEN && c != NUL
14437 && (!slang->sl_collapse || reslen == 0
14438 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014440 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441
14442 i++;
14443 z = 0;
14444 k = 0;
14445 }
14446 }
14447
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014448 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449}
14450
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014451#ifdef FEAT_MBYTE
14452/*
14453 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14454 * Multi-byte version of spell_soundfold().
14455 */
14456 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014457spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014458{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014459 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014460 int word[MAXWLEN];
14461 int wres[MAXWLEN];
14462 int l;
14463 char_u *s;
14464 int *ws;
14465 char_u *t;
14466 int *pf;
14467 int i, j, z;
14468 int reslen;
14469 int n, k = 0;
14470 int z0;
14471 int k0;
14472 int n0;
14473 int c;
14474 int pri;
14475 int p0 = -333;
14476 int c0;
14477 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014478 int wordlen;
14479
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014480
14481 /*
14482 * Convert the multi-byte string to a wide-character string.
14483 * Remove accents, if wanted. We actually remove all non-word characters.
14484 * But keep white space.
14485 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014486 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014487 for (s = inword; *s != NUL; )
14488 {
14489 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014490 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014491 if (slang->sl_rem_accents)
14492 {
14493 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14494 {
14495 if (did_white)
14496 continue;
14497 c = ' ';
14498 did_white = TRUE;
14499 }
14500 else
14501 {
14502 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014503 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014504 continue;
14505 }
14506 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014507 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014508 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014509 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014510
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014511 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014512 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014513 * Converted from C++ to C. Added support for multi-byte chars.
14514 * Changed to keep spaces.
14515 */
14516 i = reslen = z = 0;
14517 while ((c = word[i]) != NUL)
14518 {
14519 /* Start with the first rule that has the character in the word. */
14520 n = slang->sl_sal_first[c & 0xff];
14521 z0 = 0;
14522
14523 if (n >= 0)
14524 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014525 /* Check all rules for the same index byte.
14526 * If c is 0x300 need extra check for the end of the array, as
14527 * (c & 0xff) is NUL. */
14528 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14529 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014530 {
14531 /* Quickly skip entries that don't match the word. Most
14532 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014533 if (c != ws[0])
14534 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014535 k = smp[n].sm_leadlen;
14536 if (k > 1)
14537 {
14538 if (word[i + 1] != ws[1])
14539 continue;
14540 if (k > 2)
14541 {
14542 for (j = 2; j < k; ++j)
14543 if (word[i + j] != ws[j])
14544 break;
14545 if (j < k)
14546 continue;
14547 }
14548 }
14549
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014550 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014551 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014552 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014553 while (*pf != NUL && *pf != word[i + k])
14554 ++pf;
14555 if (*pf == NUL)
14556 continue;
14557 ++k;
14558 }
14559 s = smp[n].sm_rules;
14560 pri = 5; /* default priority */
14561
14562 p0 = *s;
14563 k0 = k;
14564 while (*s == '-' && k > 1)
14565 {
14566 k--;
14567 s++;
14568 }
14569 if (*s == '<')
14570 s++;
14571 if (VIM_ISDIGIT(*s))
14572 {
14573 /* determine priority */
14574 pri = *s - '0';
14575 s++;
14576 }
14577 if (*s == '^' && *(s + 1) == '^')
14578 s++;
14579
14580 if (*s == NUL
14581 || (*s == '^'
14582 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014583 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014584 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014585 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014586 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014587 && spell_iswordp_w(word + i - 1, curwin)
14588 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014589 {
14590 /* search for followup rules, if: */
14591 /* followup and k > 1 and NO '-' in searchstring */
14592 c0 = word[i + k - 1];
14593 n0 = slang->sl_sal_first[c0 & 0xff];
14594
14595 if (slang->sl_followup && k > 1 && n0 >= 0
14596 && p0 != '-' && word[i + k] != NUL)
14597 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014598 /* Test follow-up rule for "word[i + k]"; loop over
14599 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014600 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14601 == (c0 & 0xff); ++n0)
14602 {
14603 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014604 */
14605 if (c0 != ws[0])
14606 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014607 k0 = smp[n0].sm_leadlen;
14608 if (k0 > 1)
14609 {
14610 if (word[i + k] != ws[1])
14611 continue;
14612 if (k0 > 2)
14613 {
14614 pf = word + i + k + 1;
14615 for (j = 2; j < k0; ++j)
14616 if (*pf++ != ws[j])
14617 break;
14618 if (j < k0)
14619 continue;
14620 }
14621 }
14622 k0 += k - 1;
14623
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014624 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014625 {
14626 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014627 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014628 while (*pf != NUL && *pf != word[i + k0])
14629 ++pf;
14630 if (*pf == NUL)
14631 continue;
14632 ++k0;
14633 }
14634
14635 p0 = 5;
14636 s = smp[n0].sm_rules;
14637 while (*s == '-')
14638 {
14639 /* "k0" gets NOT reduced because
14640 * "if (k0 == k)" */
14641 s++;
14642 }
14643 if (*s == '<')
14644 s++;
14645 if (VIM_ISDIGIT(*s))
14646 {
14647 p0 = *s - '0';
14648 s++;
14649 }
14650
14651 if (*s == NUL
14652 /* *s == '^' cuts */
14653 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014654 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014655 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014656 {
14657 if (k0 == k)
14658 /* this is just a piece of the string */
14659 continue;
14660
14661 if (p0 < pri)
14662 /* priority too low */
14663 continue;
14664 /* rule fits; stop search */
14665 break;
14666 }
14667 }
14668
14669 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14670 == (c0 & 0xff))
14671 continue;
14672 }
14673
14674 /* replace string */
14675 ws = smp[n].sm_to_w;
14676 s = smp[n].sm_rules;
14677 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14678 if (p0 == 1 && z == 0)
14679 {
14680 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014681 if (reslen > 0 && ws != NULL && *ws != NUL
14682 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014683 || wres[reslen - 1] == *ws))
14684 reslen--;
14685 z0 = 1;
14686 z = 1;
14687 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014688 if (ws != NULL)
14689 while (*ws != NUL && word[i + k0] != NUL)
14690 {
14691 word[i + k0] = *ws;
14692 k0++;
14693 ws++;
14694 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014695 if (k > k0)
14696 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014697 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014698
14699 /* new "actual letter" */
14700 c = word[i];
14701 }
14702 else
14703 {
14704 /* no '<' rule used */
14705 i += k - 1;
14706 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014707 if (ws != NULL)
14708 while (*ws != NUL && ws[1] != NUL
14709 && reslen < MAXWLEN)
14710 {
14711 if (reslen == 0 || wres[reslen - 1] != *ws)
14712 wres[reslen++] = *ws;
14713 ws++;
14714 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014715 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014716 if (ws == NULL)
14717 c = NUL;
14718 else
14719 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014720 if (strstr((char *)s, "^^") != NULL)
14721 {
14722 if (c != NUL)
14723 wres[reslen++] = c;
14724 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014725 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014726 i = 0;
14727 z0 = 1;
14728 }
14729 }
14730 break;
14731 }
14732 }
14733 }
14734 else if (vim_iswhite(c))
14735 {
14736 c = ' ';
14737 k = 1;
14738 }
14739
14740 if (z0 == 0)
14741 {
14742 if (k && !p0 && reslen < MAXWLEN && c != NUL
14743 && (!slang->sl_collapse || reslen == 0
14744 || wres[reslen - 1] != c))
14745 /* condense only double letters */
14746 wres[reslen++] = c;
14747
14748 i++;
14749 z = 0;
14750 k = 0;
14751 }
14752 }
14753
14754 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14755 l = 0;
14756 for (n = 0; n < reslen; ++n)
14757 {
14758 l += mb_char2bytes(wres[n], res + l);
14759 if (l + MB_MAXBYTES > MAXWLEN)
14760 break;
14761 }
14762 res[l] = NUL;
14763}
14764#endif
14765
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014766/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014767 * Compute a score for two sound-a-like words.
14768 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14769 * Instead of a generic loop we write out the code. That keeps it fast by
14770 * avoiding checks that will not be possible.
14771 */
14772 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010014773soundalike_score(
14774 char_u *goodstart, /* sound-folded good word */
14775 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014776{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014777 char_u *goodsound = goodstart;
14778 char_u *badsound = badstart;
14779 int goodlen;
14780 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781 int n;
14782 char_u *pl, *ps;
14783 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014784 int score = 0;
14785
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014786 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014787 * counted so much, vowels halfway the word aren't counted at all. */
14788 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14789 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014790 if ((badsound[0] == NUL && goodsound[1] == NUL)
14791 || (goodsound[0] == NUL && badsound[1] == NUL))
14792 /* changing word with vowel to word without a sound */
14793 return SCORE_DEL;
14794 if (badsound[0] == NUL || goodsound[0] == NUL)
14795 /* more than two changes */
14796 return SCORE_MAXMAX;
14797
Bram Moolenaar4770d092006-01-12 23:22:24 +000014798 if (badsound[1] == goodsound[1]
14799 || (badsound[1] != NUL
14800 && goodsound[1] != NUL
14801 && badsound[2] == goodsound[2]))
14802 {
14803 /* handle like a substitute */
14804 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014805 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014806 {
14807 score = 2 * SCORE_DEL / 3;
14808 if (*badsound == '*')
14809 ++badsound;
14810 else
14811 ++goodsound;
14812 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014813 }
14814
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014815 goodlen = (int)STRLEN(goodsound);
14816 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014817
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014818 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014819 * changes. */
14820 n = goodlen - badlen;
14821 if (n < -2 || n > 2)
14822 return SCORE_MAXMAX;
14823
14824 if (n > 0)
14825 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014826 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014827 ps = badsound;
14828 }
14829 else
14830 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014831 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014832 ps = goodsound;
14833 }
14834
14835 /* Skip over the identical part. */
14836 while (*pl == *ps && *pl != NUL)
14837 {
14838 ++pl;
14839 ++ps;
14840 }
14841
14842 switch (n)
14843 {
14844 case -2:
14845 case 2:
14846 /*
14847 * Must delete two characters from "pl".
14848 */
14849 ++pl; /* first delete */
14850 while (*pl == *ps)
14851 {
14852 ++pl;
14853 ++ps;
14854 }
14855 /* strings must be equal after second delete */
14856 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014857 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014858
14859 /* Failed to compare. */
14860 break;
14861
14862 case -1:
14863 case 1:
14864 /*
14865 * Minimal one delete from "pl" required.
14866 */
14867
14868 /* 1: delete */
14869 pl2 = pl + 1;
14870 ps2 = ps;
14871 while (*pl2 == *ps2)
14872 {
14873 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014874 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014875 ++pl2;
14876 ++ps2;
14877 }
14878
14879 /* 2: delete then swap, then rest must be equal */
14880 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14881 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014882 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014883
14884 /* 3: delete then substitute, then the rest must be equal */
14885 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014886 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014887
14888 /* 4: first swap then delete */
14889 if (pl[0] == ps[1] && pl[1] == ps[0])
14890 {
14891 pl2 = pl + 2; /* swap, skip two chars */
14892 ps2 = ps + 2;
14893 while (*pl2 == *ps2)
14894 {
14895 ++pl2;
14896 ++ps2;
14897 }
14898 /* delete a char and then strings must be equal */
14899 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014900 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014901 }
14902
14903 /* 5: first substitute then delete */
14904 pl2 = pl + 1; /* substitute, skip one char */
14905 ps2 = ps + 1;
14906 while (*pl2 == *ps2)
14907 {
14908 ++pl2;
14909 ++ps2;
14910 }
14911 /* delete a char and then strings must be equal */
14912 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014913 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014914
14915 /* Failed to compare. */
14916 break;
14917
14918 case 0:
14919 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014920 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014921 * insert is only possible in combination with a delete.
14922 * 1: check if for identical strings
14923 */
14924 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014925 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014926
14927 /* 2: swap */
14928 if (pl[0] == ps[1] && pl[1] == ps[0])
14929 {
14930 pl2 = pl + 2; /* swap, skip two chars */
14931 ps2 = ps + 2;
14932 while (*pl2 == *ps2)
14933 {
14934 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014935 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014936 ++pl2;
14937 ++ps2;
14938 }
14939 /* 3: swap and swap again */
14940 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14941 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014942 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014943
14944 /* 4: swap and substitute */
14945 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014946 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014947 }
14948
14949 /* 5: substitute */
14950 pl2 = pl + 1;
14951 ps2 = ps + 1;
14952 while (*pl2 == *ps2)
14953 {
14954 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014955 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014956 ++pl2;
14957 ++ps2;
14958 }
14959
14960 /* 6: substitute and swap */
14961 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14962 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014963 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014964
14965 /* 7: substitute and substitute */
14966 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014967 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014968
14969 /* 8: insert then delete */
14970 pl2 = pl;
14971 ps2 = ps + 1;
14972 while (*pl2 == *ps2)
14973 {
14974 ++pl2;
14975 ++ps2;
14976 }
14977 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014978 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014979
14980 /* 9: delete then insert */
14981 pl2 = pl + 1;
14982 ps2 = ps;
14983 while (*pl2 == *ps2)
14984 {
14985 ++pl2;
14986 ++ps2;
14987 }
14988 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014989 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014990
14991 /* Failed to compare. */
14992 break;
14993 }
14994
14995 return SCORE_MAXMAX;
14996}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014998/*
14999 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015000 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015001 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015002 * The algorithm is described by Du and Chang, 1992.
15003 * The implementation of the algorithm comes from Aspell editdist.cpp,
15004 * edit_distance(). It has been converted from C++ to C and modified to
15005 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015006 */
15007 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015008spell_edit_score(
15009 slang_T *slang,
15010 char_u *badword,
15011 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015012{
15013 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015014 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 int j, i;
15016 int t;
15017 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015018 int pbc, pgc;
15019#ifdef FEAT_MBYTE
15020 char_u *p;
15021 int wbadword[MAXWLEN];
15022 int wgoodword[MAXWLEN];
15023
15024 if (has_mbyte)
15025 {
15026 /* Get the characters from the multi-byte strings and put them in an
15027 * int array for easy access. */
15028 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015029 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015030 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015031 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015032 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015033 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015034 }
15035 else
15036#endif
15037 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015038 badlen = (int)STRLEN(badword) + 1;
15039 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015040 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041
15042 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15043#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015044 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15045 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015046 if (cnt == NULL)
15047 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048
15049 CNT(0, 0) = 0;
15050 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015051 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015052
15053 for (i = 1; i <= badlen; ++i)
15054 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015055 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015056 for (j = 1; j <= goodlen; ++j)
15057 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015058#ifdef FEAT_MBYTE
15059 if (has_mbyte)
15060 {
15061 bc = wbadword[i - 1];
15062 gc = wgoodword[j - 1];
15063 }
15064 else
15065#endif
15066 {
15067 bc = badword[i - 1];
15068 gc = goodword[j - 1];
15069 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 if (bc == gc)
15071 CNT(i, j) = CNT(i - 1, j - 1);
15072 else
15073 {
15074 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015075 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15077 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015078 {
15079 /* For a similar character use SCORE_SIMILAR. */
15080 if (slang != NULL
15081 && slang->sl_has_map
15082 && similar_chars(slang, gc, bc))
15083 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15084 else
15085 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015088 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015090#ifdef FEAT_MBYTE
15091 if (has_mbyte)
15092 {
15093 pbc = wbadword[i - 2];
15094 pgc = wgoodword[j - 2];
15095 }
15096 else
15097#endif
15098 {
15099 pbc = badword[i - 2];
15100 pgc = goodword[j - 2];
15101 }
15102 if (bc == pgc && pbc == gc)
15103 {
15104 t = SCORE_SWAP + CNT(i - 2, j - 2);
15105 if (t < CNT(i, j))
15106 CNT(i, j) = t;
15107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108 }
15109 t = SCORE_DEL + CNT(i - 1, j);
15110 if (t < CNT(i, j))
15111 CNT(i, j) = t;
15112 t = SCORE_INS + CNT(i, j - 1);
15113 if (t < CNT(i, j))
15114 CNT(i, j) = t;
15115 }
15116 }
15117 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015118
15119 i = CNT(badlen - 1, goodlen - 1);
15120 vim_free(cnt);
15121 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015122}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015123
Bram Moolenaar4770d092006-01-12 23:22:24 +000015124typedef struct
15125{
15126 int badi;
15127 int goodi;
15128 int score;
15129} limitscore_T;
15130
15131/*
15132 * Like spell_edit_score(), but with a limit on the score to make it faster.
15133 * May return SCORE_MAXMAX when the score is higher than "limit".
15134 *
15135 * This uses a stack for the edits still to be tried.
15136 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15137 * for multi-byte characters.
15138 */
15139 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015140spell_edit_score_limit(
15141 slang_T *slang,
15142 char_u *badword,
15143 char_u *goodword,
15144 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015145{
15146 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15147 int stackidx;
15148 int bi, gi;
15149 int bi2, gi2;
15150 int bc, gc;
15151 int score;
15152 int score_off;
15153 int minscore;
15154 int round;
15155
15156#ifdef FEAT_MBYTE
15157 /* Multi-byte characters require a bit more work, use a different function
15158 * to avoid testing "has_mbyte" quite often. */
15159 if (has_mbyte)
15160 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15161#endif
15162
15163 /*
15164 * The idea is to go from start to end over the words. So long as
15165 * characters are equal just continue, this always gives the lowest score.
15166 * When there is a difference try several alternatives. Each alternative
15167 * increases "score" for the edit distance. Some of the alternatives are
15168 * pushed unto a stack and tried later, some are tried right away. At the
15169 * end of the word the score for one alternative is known. The lowest
15170 * possible score is stored in "minscore".
15171 */
15172 stackidx = 0;
15173 bi = 0;
15174 gi = 0;
15175 score = 0;
15176 minscore = limit + 1;
15177
15178 for (;;)
15179 {
15180 /* Skip over an equal part, score remains the same. */
15181 for (;;)
15182 {
15183 bc = badword[bi];
15184 gc = goodword[gi];
15185 if (bc != gc) /* stop at a char that's different */
15186 break;
15187 if (bc == NUL) /* both words end */
15188 {
15189 if (score < minscore)
15190 minscore = score;
15191 goto pop; /* do next alternative */
15192 }
15193 ++bi;
15194 ++gi;
15195 }
15196
15197 if (gc == NUL) /* goodword ends, delete badword chars */
15198 {
15199 do
15200 {
15201 if ((score += SCORE_DEL) >= minscore)
15202 goto pop; /* do next alternative */
15203 } while (badword[++bi] != NUL);
15204 minscore = score;
15205 }
15206 else if (bc == NUL) /* badword ends, insert badword chars */
15207 {
15208 do
15209 {
15210 if ((score += SCORE_INS) >= minscore)
15211 goto pop; /* do next alternative */
15212 } while (goodword[++gi] != NUL);
15213 minscore = score;
15214 }
15215 else /* both words continue */
15216 {
15217 /* If not close to the limit, perform a change. Only try changes
15218 * that may lead to a lower score than "minscore".
15219 * round 0: try deleting a char from badword
15220 * round 1: try inserting a char in badword */
15221 for (round = 0; round <= 1; ++round)
15222 {
15223 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15224 if (score_off < minscore)
15225 {
15226 if (score_off + SCORE_EDIT_MIN >= minscore)
15227 {
15228 /* Near the limit, rest of the words must match. We
15229 * can check that right now, no need to push an item
15230 * onto the stack. */
15231 bi2 = bi + 1 - round;
15232 gi2 = gi + round;
15233 while (goodword[gi2] == badword[bi2])
15234 {
15235 if (goodword[gi2] == NUL)
15236 {
15237 minscore = score_off;
15238 break;
15239 }
15240 ++bi2;
15241 ++gi2;
15242 }
15243 }
15244 else
15245 {
15246 /* try deleting/inserting a character later */
15247 stack[stackidx].badi = bi + 1 - round;
15248 stack[stackidx].goodi = gi + round;
15249 stack[stackidx].score = score_off;
15250 ++stackidx;
15251 }
15252 }
15253 }
15254
15255 if (score + SCORE_SWAP < minscore)
15256 {
15257 /* If swapping two characters makes a match then the
15258 * substitution is more expensive, thus there is no need to
15259 * try both. */
15260 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15261 {
15262 /* Swap two characters, that is: skip them. */
15263 gi += 2;
15264 bi += 2;
15265 score += SCORE_SWAP;
15266 continue;
15267 }
15268 }
15269
15270 /* Substitute one character for another which is the same
15271 * thing as deleting a character from both goodword and badword.
15272 * Use a better score when there is only a case difference. */
15273 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15274 score += SCORE_ICASE;
15275 else
15276 {
15277 /* For a similar character use SCORE_SIMILAR. */
15278 if (slang != NULL
15279 && slang->sl_has_map
15280 && similar_chars(slang, gc, bc))
15281 score += SCORE_SIMILAR;
15282 else
15283 score += SCORE_SUBST;
15284 }
15285
15286 if (score < minscore)
15287 {
15288 /* Do the substitution. */
15289 ++gi;
15290 ++bi;
15291 continue;
15292 }
15293 }
15294pop:
15295 /*
15296 * Get here to try the next alternative, pop it from the stack.
15297 */
15298 if (stackidx == 0) /* stack is empty, finished */
15299 break;
15300
15301 /* pop an item from the stack */
15302 --stackidx;
15303 gi = stack[stackidx].goodi;
15304 bi = stack[stackidx].badi;
15305 score = stack[stackidx].score;
15306 }
15307
15308 /* When the score goes over "limit" it may actually be much higher.
15309 * Return a very large number to avoid going below the limit when giving a
15310 * bonus. */
15311 if (minscore > limit)
15312 return SCORE_MAXMAX;
15313 return minscore;
15314}
15315
15316#ifdef FEAT_MBYTE
15317/*
15318 * Multi-byte version of spell_edit_score_limit().
15319 * Keep it in sync with the above!
15320 */
15321 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015322spell_edit_score_limit_w(
15323 slang_T *slang,
15324 char_u *badword,
15325 char_u *goodword,
15326 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015327{
15328 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15329 int stackidx;
15330 int bi, gi;
15331 int bi2, gi2;
15332 int bc, gc;
15333 int score;
15334 int score_off;
15335 int minscore;
15336 int round;
15337 char_u *p;
15338 int wbadword[MAXWLEN];
15339 int wgoodword[MAXWLEN];
15340
15341 /* Get the characters from the multi-byte strings and put them in an
15342 * int array for easy access. */
15343 bi = 0;
15344 for (p = badword; *p != NUL; )
15345 wbadword[bi++] = mb_cptr2char_adv(&p);
15346 wbadword[bi++] = 0;
15347 gi = 0;
15348 for (p = goodword; *p != NUL; )
15349 wgoodword[gi++] = mb_cptr2char_adv(&p);
15350 wgoodword[gi++] = 0;
15351
15352 /*
15353 * The idea is to go from start to end over the words. So long as
15354 * characters are equal just continue, this always gives the lowest score.
15355 * When there is a difference try several alternatives. Each alternative
15356 * increases "score" for the edit distance. Some of the alternatives are
15357 * pushed unto a stack and tried later, some are tried right away. At the
15358 * end of the word the score for one alternative is known. The lowest
15359 * possible score is stored in "minscore".
15360 */
15361 stackidx = 0;
15362 bi = 0;
15363 gi = 0;
15364 score = 0;
15365 minscore = limit + 1;
15366
15367 for (;;)
15368 {
15369 /* Skip over an equal part, score remains the same. */
15370 for (;;)
15371 {
15372 bc = wbadword[bi];
15373 gc = wgoodword[gi];
15374
15375 if (bc != gc) /* stop at a char that's different */
15376 break;
15377 if (bc == NUL) /* both words end */
15378 {
15379 if (score < minscore)
15380 minscore = score;
15381 goto pop; /* do next alternative */
15382 }
15383 ++bi;
15384 ++gi;
15385 }
15386
15387 if (gc == NUL) /* goodword ends, delete badword chars */
15388 {
15389 do
15390 {
15391 if ((score += SCORE_DEL) >= minscore)
15392 goto pop; /* do next alternative */
15393 } while (wbadword[++bi] != NUL);
15394 minscore = score;
15395 }
15396 else if (bc == NUL) /* badword ends, insert badword chars */
15397 {
15398 do
15399 {
15400 if ((score += SCORE_INS) >= minscore)
15401 goto pop; /* do next alternative */
15402 } while (wgoodword[++gi] != NUL);
15403 minscore = score;
15404 }
15405 else /* both words continue */
15406 {
15407 /* If not close to the limit, perform a change. Only try changes
15408 * that may lead to a lower score than "minscore".
15409 * round 0: try deleting a char from badword
15410 * round 1: try inserting a char in badword */
15411 for (round = 0; round <= 1; ++round)
15412 {
15413 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15414 if (score_off < minscore)
15415 {
15416 if (score_off + SCORE_EDIT_MIN >= minscore)
15417 {
15418 /* Near the limit, rest of the words must match. We
15419 * can check that right now, no need to push an item
15420 * onto the stack. */
15421 bi2 = bi + 1 - round;
15422 gi2 = gi + round;
15423 while (wgoodword[gi2] == wbadword[bi2])
15424 {
15425 if (wgoodword[gi2] == NUL)
15426 {
15427 minscore = score_off;
15428 break;
15429 }
15430 ++bi2;
15431 ++gi2;
15432 }
15433 }
15434 else
15435 {
15436 /* try deleting a character from badword later */
15437 stack[stackidx].badi = bi + 1 - round;
15438 stack[stackidx].goodi = gi + round;
15439 stack[stackidx].score = score_off;
15440 ++stackidx;
15441 }
15442 }
15443 }
15444
15445 if (score + SCORE_SWAP < minscore)
15446 {
15447 /* If swapping two characters makes a match then the
15448 * substitution is more expensive, thus there is no need to
15449 * try both. */
15450 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15451 {
15452 /* Swap two characters, that is: skip them. */
15453 gi += 2;
15454 bi += 2;
15455 score += SCORE_SWAP;
15456 continue;
15457 }
15458 }
15459
15460 /* Substitute one character for another which is the same
15461 * thing as deleting a character from both goodword and badword.
15462 * Use a better score when there is only a case difference. */
15463 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15464 score += SCORE_ICASE;
15465 else
15466 {
15467 /* For a similar character use SCORE_SIMILAR. */
15468 if (slang != NULL
15469 && slang->sl_has_map
15470 && similar_chars(slang, gc, bc))
15471 score += SCORE_SIMILAR;
15472 else
15473 score += SCORE_SUBST;
15474 }
15475
15476 if (score < minscore)
15477 {
15478 /* Do the substitution. */
15479 ++gi;
15480 ++bi;
15481 continue;
15482 }
15483 }
15484pop:
15485 /*
15486 * Get here to try the next alternative, pop it from the stack.
15487 */
15488 if (stackidx == 0) /* stack is empty, finished */
15489 break;
15490
15491 /* pop an item from the stack */
15492 --stackidx;
15493 gi = stack[stackidx].goodi;
15494 bi = stack[stackidx].badi;
15495 score = stack[stackidx].score;
15496 }
15497
15498 /* When the score goes over "limit" it may actually be much higher.
15499 * Return a very large number to avoid going below the limit when giving a
15500 * bonus. */
15501 if (minscore > limit)
15502 return SCORE_MAXMAX;
15503 return minscore;
15504}
15505#endif
15506
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015507/*
15508 * ":spellinfo"
15509 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015511ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015512{
15513 int lpi;
15514 langp_T *lp;
15515 char_u *p;
15516
15517 if (no_spell_checking(curwin))
15518 return;
15519
15520 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015521 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015522 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015523 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015524 msg_puts((char_u *)"file: ");
15525 msg_puts(lp->lp_slang->sl_fname);
15526 msg_putchar('\n');
15527 p = lp->lp_slang->sl_info;
15528 if (p != NULL)
15529 {
15530 msg_puts(p);
15531 msg_putchar('\n');
15532 }
15533 }
15534 msg_end();
15535}
15536
Bram Moolenaar4770d092006-01-12 23:22:24 +000015537#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15538#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015539#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015540#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15541#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015542
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015543/*
15544 * ":spelldump"
15545 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015546 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015547ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015549 char_u *spl;
15550 long dummy;
15551
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015552 if (no_spell_checking(curwin))
15553 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015554 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015555
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015556 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015557 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015558
15559 /* enable spelling locally in the new window */
15560 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010015561 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015562 vim_free(spl);
15563
Bram Moolenaar860cae12010-06-05 23:22:07 +020015564 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015565 return;
15566
Bram Moolenaar860cae12010-06-05 23:22:07 +020015567 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015568
15569 /* Delete the empty line that we started with. */
15570 if (curbuf->b_ml.ml_line_count > 1)
15571 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15572
15573 redraw_later(NOT_VALID);
15574}
15575
15576/*
15577 * Go through all possible words and:
15578 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15579 * "ic" and "dir" are not used.
15580 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15581 */
15582 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015583spell_dump_compl(
15584 char_u *pat, /* leading part of the word */
15585 int ic, /* ignore case */
15586 int *dir, /* direction for adding matches */
15587 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015588{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015589 langp_T *lp;
15590 slang_T *slang;
15591 idx_T arridx[MAXWLEN];
15592 int curi[MAXWLEN];
15593 char_u word[MAXWLEN];
15594 int c;
15595 char_u *byts;
15596 idx_T *idxs;
15597 linenr_T lnum = 0;
15598 int round;
15599 int depth;
15600 int n;
15601 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015602 char_u *region_names = NULL; /* region names being used */
15603 int do_region = TRUE; /* dump region names and numbers */
15604 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015605 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015606 int dumpflags = dumpflags_arg;
15607 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015608
Bram Moolenaard0131a82006-03-04 21:46:13 +000015609 /* When ignoring case or when the pattern starts with capital pass this on
15610 * to dump_word(). */
15611 if (pat != NULL)
15612 {
15613 if (ic)
15614 dumpflags |= DUMPFLAG_ICASE;
15615 else
15616 {
15617 n = captype(pat, NULL);
15618 if (n == WF_ONECAP)
15619 dumpflags |= DUMPFLAG_ONECAP;
15620 else if (n == WF_ALLCAP
15621#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015622 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015623#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015624 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015625#endif
15626 )
15627 dumpflags |= DUMPFLAG_ALLCAP;
15628 }
15629 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015630
Bram Moolenaar7887d882005-07-01 22:33:52 +000015631 /* Find out if we can support regions: All languages must support the same
15632 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015633 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015634 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015635 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015636 p = lp->lp_slang->sl_regions;
15637 if (p[0] != 0)
15638 {
15639 if (region_names == NULL) /* first language with regions */
15640 region_names = p;
15641 else if (STRCMP(region_names, p) != 0)
15642 {
15643 do_region = FALSE; /* region names are different */
15644 break;
15645 }
15646 }
15647 }
15648
15649 if (do_region && region_names != NULL)
15650 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015651 if (pat == NULL)
15652 {
15653 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15654 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15655 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015656 }
15657 else
15658 do_region = FALSE;
15659
15660 /*
15661 * Loop over all files loaded for the entries in 'spelllang'.
15662 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015663 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015664 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015665 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015666 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015667 if (slang->sl_fbyts == NULL) /* reloading failed */
15668 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015669
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015670 if (pat == NULL)
15671 {
15672 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15673 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15674 }
15675
15676 /* When matching with a pattern and there are no prefixes only use
15677 * parts of the tree that match "pat". */
15678 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015679 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015680 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015681 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015682
15683 /* round 1: case-folded tree
15684 * round 2: keep-case tree */
15685 for (round = 1; round <= 2; ++round)
15686 {
15687 if (round == 1)
15688 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015689 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015690 byts = slang->sl_fbyts;
15691 idxs = slang->sl_fidxs;
15692 }
15693 else
15694 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015695 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015696 byts = slang->sl_kbyts;
15697 idxs = slang->sl_kidxs;
15698 }
15699 if (byts == NULL)
15700 continue; /* array is empty */
15701
15702 depth = 0;
15703 arridx[0] = 0;
15704 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015705 while (depth >= 0 && !got_int
15706 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015707 {
15708 if (curi[depth] > byts[arridx[depth]])
15709 {
15710 /* Done all bytes at this node, go up one level. */
15711 --depth;
15712 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015713 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015714 }
15715 else
15716 {
15717 /* Do one more byte at this node. */
15718 n = arridx[depth] + curi[depth];
15719 ++curi[depth];
15720 c = byts[n];
15721 if (c == 0)
15722 {
15723 /* End of word, deal with the word.
15724 * Don't use keep-case words in the fold-case tree,
15725 * they will appear in the keep-case tree.
15726 * Only use the word when the region matches. */
15727 flags = (int)idxs[n];
15728 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015729 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015730 && (do_region
15731 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015732 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015733 & lp->lp_region) != 0))
15734 {
15735 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015736 if (!do_region)
15737 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015738
15739 /* Dump the basic word if there is no prefix or
15740 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015741 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015742 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015743 {
15744 dump_word(slang, word, pat, dir,
15745 dumpflags, flags, lnum);
15746 if (pat == NULL)
15747 ++lnum;
15748 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015749
15750 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015751 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015752 lnum = dump_prefixes(slang, word, pat, dir,
15753 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015754 }
15755 }
15756 else
15757 {
15758 /* Normal char, go one level deeper. */
15759 word[depth++] = c;
15760 arridx[depth] = idxs[n];
15761 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015762
15763 /* Check if this characters matches with the pattern.
15764 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015765 * Always ignore case here, dump_word() will check
15766 * proper case later. This isn't exactly right when
15767 * length changes for multi-byte characters with
15768 * ignore case... */
15769 if (depth <= patlen
15770 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015771 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015772 }
15773 }
15774 }
15775 }
15776 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015777}
15778
15779/*
15780 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015781 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015782 */
15783 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015784dump_word(
15785 slang_T *slang,
15786 char_u *word,
15787 char_u *pat,
15788 int *dir,
15789 int dumpflags,
15790 int wordflags,
15791 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015792{
15793 int keepcap = FALSE;
15794 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015795 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015796 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015797 char_u badword[MAXWLEN + 10];
15798 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015799 int flags = wordflags;
15800
15801 if (dumpflags & DUMPFLAG_ONECAP)
15802 flags |= WF_ONECAP;
15803 if (dumpflags & DUMPFLAG_ALLCAP)
15804 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015805
Bram Moolenaar4770d092006-01-12 23:22:24 +000015806 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015807 {
15808 /* Need to fix case according to "flags". */
15809 make_case_word(word, cword, flags);
15810 p = cword;
15811 }
15812 else
15813 {
15814 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015815 if ((dumpflags & DUMPFLAG_KEEPCASE)
15816 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015817 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015818 keepcap = TRUE;
15819 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015820 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015821
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015822 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015823 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015824 /* Add flags and regions after a slash. */
15825 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015826 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015827 STRCPY(badword, p);
15828 STRCAT(badword, "/");
15829 if (keepcap)
15830 STRCAT(badword, "=");
15831 if (flags & WF_BANNED)
15832 STRCAT(badword, "!");
15833 else if (flags & WF_RARE)
15834 STRCAT(badword, "?");
15835 if (flags & WF_REGION)
15836 for (i = 0; i < 7; ++i)
15837 if (flags & (0x10000 << i))
15838 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15839 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015840 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015841
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015842 if (dumpflags & DUMPFLAG_COUNT)
15843 {
15844 hashitem_T *hi;
15845
15846 /* Include the word count for ":spelldump!". */
15847 hi = hash_find(&slang->sl_wordcount, tw);
15848 if (!HASHITEM_EMPTY(hi))
15849 {
15850 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15851 tw, HI2WC(hi)->wc_count);
15852 p = IObuff;
15853 }
15854 }
15855
15856 ml_append(lnum, p, (colnr_T)0, FALSE);
15857 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015858 else if (((dumpflags & DUMPFLAG_ICASE)
15859 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15860 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015861 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015862 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015863 /* if dir was BACKWARD then honor it just once */
15864 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015865}
15866
15867/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015868 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15869 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015870 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015871 * Return the updated line number.
15872 */
15873 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015874dump_prefixes(
15875 slang_T *slang,
15876 char_u *word, /* case-folded word */
15877 char_u *pat,
15878 int *dir,
15879 int dumpflags,
15880 int flags, /* flags with prefix ID */
15881 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015882{
15883 idx_T arridx[MAXWLEN];
15884 int curi[MAXWLEN];
15885 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015886 char_u word_up[MAXWLEN];
15887 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015888 int c;
15889 char_u *byts;
15890 idx_T *idxs;
15891 linenr_T lnum = startlnum;
15892 int depth;
15893 int n;
15894 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015895 int i;
15896
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015897 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015898 * upper-case letter in word_up[]. */
15899 c = PTR2CHAR(word);
15900 if (SPELL_TOUPPER(c) != c)
15901 {
15902 onecap_copy(word, word_up, TRUE);
15903 has_word_up = TRUE;
15904 }
15905
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015906 byts = slang->sl_pbyts;
15907 idxs = slang->sl_pidxs;
15908 if (byts != NULL) /* array not is empty */
15909 {
15910 /*
15911 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015912 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015913 */
15914 depth = 0;
15915 arridx[0] = 0;
15916 curi[0] = 1;
15917 while (depth >= 0 && !got_int)
15918 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015919 n = arridx[depth];
15920 len = byts[n];
15921 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015922 {
15923 /* Done all bytes at this node, go up one level. */
15924 --depth;
15925 line_breakcheck();
15926 }
15927 else
15928 {
15929 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015930 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015931 ++curi[depth];
15932 c = byts[n];
15933 if (c == 0)
15934 {
15935 /* End of prefix, find out how many IDs there are. */
15936 for (i = 1; i < len; ++i)
15937 if (byts[n + i] != 0)
15938 break;
15939 curi[depth] += i - 1;
15940
Bram Moolenaar53805d12005-08-01 07:08:33 +000015941 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15942 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015943 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015944 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015945 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015946 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015947 : flags, lnum);
15948 if (lnum != 0)
15949 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015950 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015951
15952 /* Check for prefix that matches the word when the
15953 * first letter is upper-case, but only if the prefix has
15954 * a condition. */
15955 if (has_word_up)
15956 {
15957 c = valid_word_prefix(i, n, flags, word_up, slang,
15958 TRUE);
15959 if (c != 0)
15960 {
15961 vim_strncpy(prefix + depth, word_up,
15962 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015963 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015964 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015965 : flags, lnum);
15966 if (lnum != 0)
15967 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015968 }
15969 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015970 }
15971 else
15972 {
15973 /* Normal char, go one level deeper. */
15974 prefix[depth++] = c;
15975 arridx[depth] = idxs[n];
15976 curi[depth] = 1;
15977 }
15978 }
15979 }
15980 }
15981
15982 return lnum;
15983}
15984
Bram Moolenaar95529562005-08-25 21:21:38 +000015985/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015986 * Move "p" to the end of word "start".
15987 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015988 */
15989 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +010015990spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015991{
15992 char_u *p = start;
15993
Bram Moolenaar860cae12010-06-05 23:22:07 +020015994 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000015995 mb_ptr_adv(p);
15996 return p;
15997}
15998
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015999#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016000/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016001 * For Insert mode completion CTRL-X s:
16002 * Find start of the word in front of column "startcol".
16003 * We don't check if it is badly spelled, with completion we can only change
16004 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016005 * Returns the column number of the word.
16006 */
16007 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010016008spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016009{
16010 char_u *line;
16011 char_u *p;
16012 int col = 0;
16013
Bram Moolenaar95529562005-08-25 21:21:38 +000016014 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016015 return startcol;
16016
16017 /* Find a word character before "startcol". */
16018 line = ml_get_curline();
16019 for (p = line + startcol; p > line; )
16020 {
16021 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010016022 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016023 break;
16024 }
16025
16026 /* Go back to start of the word. */
16027 while (p > line)
16028 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016029 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016030 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016031 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016032 break;
16033 col = 0;
16034 }
16035
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016036 return col;
16037}
16038
16039/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016040 * Need to check for 'spellcapcheck' now, the word is removed before
16041 * expand_spelling() is called. Therefore the ugly global variable.
16042 */
16043static int spell_expand_need_cap;
16044
16045 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +010016046spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +000016047{
16048 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16049}
16050
16051/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016052 * Get list of spelling suggestions.
16053 * Used for Insert mode completion CTRL-X ?.
16054 * Returns the number of matches. The matches are in "matchp[]", array of
16055 * allocated strings.
16056 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016057 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +010016058expand_spelling(
16059 linenr_T lnum UNUSED,
16060 char_u *pat,
16061 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016062{
16063 garray_T ga;
16064
Bram Moolenaar4770d092006-01-12 23:22:24 +000016065 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016066 *matchp = ga.ga_data;
16067 return ga.ga_len;
16068}
16069#endif
16070
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016071#endif /* FEAT_SPELL */