blob: 9d7f933a265c5b99f882b93091227804fe497d20 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000080 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#include "vim.h"
307
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000308#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000309
Bram Moolenaar4770d092006-01-12 23:22:24 +0000310#ifndef UNIX /* it's in os_unix.h for Unix */
311# include <time.h> /* for time_t */
312#endif
313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000314#define MAXWLEN 250 /* Assume max. word len is this many bytes.
315 Some places assume a word length fits in a
316 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000317
Bram Moolenaare52325c2005-08-22 22:54:29 +0000318/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000319 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100320#if VIM_SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000321typedef int idx_T;
322#else
323typedef long idx_T;
324#endif
325
Bram Moolenaar56f78042010-12-08 17:09:32 +0100326#ifdef VMS
327# define SPL_FNAME_TMPL "%s_%s.spl"
328# define SPL_FNAME_ADD "_add."
329# define SPL_FNAME_ASCII "_ascii."
330#else
331# define SPL_FNAME_TMPL "%s.%s.spl"
332# define SPL_FNAME_ADD ".add."
333# define SPL_FNAME_ASCII ".ascii."
334#endif
335
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000336/* Flags used for a word. Only the lowest byte can be used, the region byte
337 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000338#define WF_REGION 0x01 /* region byte follows */
339#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
340#define WF_ALLCAP 0x04 /* word must be all capitals */
341#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000342#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000343#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000344#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000345#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000347/* for <flags2>, shifted up one byte to be used in wn_flags */
348#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000349#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000350#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000351#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000352#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
353#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000354
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000355/* only used for su_badflags */
356#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
357
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000358#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000359
Bram Moolenaar53805d12005-08-01 07:08:33 +0000360/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000361#define WFP_RARE 0x01 /* rare prefix */
362#define WFP_NC 0x02 /* prefix is not combining */
363#define WFP_UP 0x04 /* to-upper prefix */
364#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
365#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000366
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000367/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
368 * byte) and prefcondnr (two bytes). */
369#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
370#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
371#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
372#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
373 * COMPOUNDPERMITFLAG */
374#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
375 * COMPOUNDFORBIDFLAG */
376
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000377
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000378/* flags for <compoptions> */
379#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
380#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
381#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
382#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
383
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000384/* Special byte values for <byte>. Some are only used in the tree for
385 * postponed prefixes, some only in the other trees. This is a bit messy... */
386#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000387 * postponed prefix: no <pflags> */
388#define BY_INDEX 1 /* child is shared, index follows */
389#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
390 * postponed prefix: <pflags> follows */
391#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
392 * follow; never used in prefix tree */
393#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000394
Bram Moolenaar4770d092006-01-12 23:22:24 +0000395/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
396 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000397 * One replacement: from "ft_from" to "ft_to". */
398typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000399{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400 char_u *ft_from;
401 char_u *ft_to;
402} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000403
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000404/* Info from "SAL" entries in ".aff" file used in sl_sal.
405 * The info is split for quick processing by spell_soundfold().
406 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
407typedef struct salitem_S
408{
409 char_u *sm_lead; /* leading letters */
410 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000411 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000412 char_u *sm_rules; /* rules like ^, $, priority */
413 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000414#ifdef FEAT_MBYTE
415 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000416 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000417 int *sm_to_w; /* wide character copy of "sm_to" */
418#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000419} salitem_T;
420
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000421#ifdef FEAT_MBYTE
422typedef int salfirst_T;
423#else
424typedef short salfirst_T;
425#endif
426
Bram Moolenaar5195e452005-08-19 20:32:47 +0000427/* Values for SP_*ERROR are negative, positive values are used by
428 * read_cnt_string(). */
429#define SP_TRUNCERROR -1 /* spell file truncated error */
430#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000431#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000432
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000433/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000434 * Structure used to store words and other info for one language, loaded from
435 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000436 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
437 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
438 *
439 * The "byts" array stores the possible bytes in each tree node, preceded by
440 * the number of possible bytes, sorted on byte value:
441 * <len> <byte1> <byte2> ...
442 * The "idxs" array stores the index of the child node corresponding to the
443 * byte in "byts".
444 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000445 * the flags, region mask and affixID for the word. There may be several
446 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000447 */
448typedef struct slang_S slang_T;
449struct slang_S
450{
451 slang_T *sl_next; /* next language */
452 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000453 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000455
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000458 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000459 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000460 char_u *sl_pbyts; /* prefix tree word bytes */
461 idx_T *sl_pidxs; /* prefix tree word indexes */
462
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000463 char_u *sl_info; /* infotext string or NULL */
464
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000465 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000466
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000467 char_u *sl_midword; /* MIDWORD string or NULL */
468
Bram Moolenaar4770d092006-01-12 23:22:24 +0000469 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
470
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000471 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000472 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000473 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000474 int sl_compoptions; /* COMP_* flags */
475 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000476 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000477 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000478 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000480 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000481 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000482 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
483 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000484
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000485 int sl_prefixcnt; /* number of items in "sl_prefprog" */
486 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488 garray_T sl_rep; /* list of fromto_T entries from REP lines */
489 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
490 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000492 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493 there is none */
494 int sl_followup; /* SAL followup */
495 int sl_collapse; /* SAL collapse_result */
496 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000497 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
498 * "sl_sal_first" maps chars, when has_mbyte
499 * "sl_sal" is a list of wide char lists. */
500 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
501 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000502 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000503
504 /* Info from the .sug file. Loaded on demand. */
505 time_t sl_sugtime; /* timestamp for .sug file */
506 char_u *sl_sbyts; /* soundfolded word bytes */
507 idx_T *sl_sidxs; /* soundfolded word indexes */
508 buf_T *sl_sugbuf; /* buffer with word number table */
509 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
510 load */
511
Bram Moolenaarea424162005-06-16 21:51:00 +0000512 int sl_has_map; /* TRUE if there is a MAP line */
513#ifdef FEAT_MBYTE
514 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
515 int sl_map_array[256]; /* MAP for first 256 chars */
516#else
517 char_u sl_map_array[256]; /* MAP for first 256 chars */
518#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000519 hashtab_T sl_sounddone; /* table with soundfolded words that have
520 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000521};
522
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000523/* First language that is loaded, start of the linked list of loaded
524 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525static slang_T *first_lang = NULL;
526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527/* Flags used in .spl file for soundsalike flags. */
528#define SAL_F0LLOWUP 1
529#define SAL_COLLAPSE 2
530#define SAL_REM_ACCENTS 4
531
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000532/*
533 * Structure used in "b_langp", filled from 'spelllang'.
534 */
535typedef struct langp_S
536{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000537 slang_T *lp_slang; /* info for this language */
538 slang_T *lp_sallang; /* language used for sound folding or NULL */
539 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000540 int lp_region; /* bitmask for region or REGION_ALL */
541} langp_T;
542
543#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
544
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000545#define REGION_ALL 0xff /* word valid in all regions */
546
Bram Moolenaar5195e452005-08-19 20:32:47 +0000547#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
548#define VIMSPELLMAGICL 8
549#define VIMSPELLVERSION 50
550
Bram Moolenaar4770d092006-01-12 23:22:24 +0000551#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
552#define VIMSUGMAGICL 6
553#define VIMSUGVERSION 1
554
Bram Moolenaar5195e452005-08-19 20:32:47 +0000555/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
556#define SN_REGION 0 /* <regionname> section */
557#define SN_CHARFLAGS 1 /* charflags section */
558#define SN_MIDWORD 2 /* <midword> section */
559#define SN_PREFCOND 3 /* <prefcond> section */
560#define SN_REP 4 /* REP items section */
561#define SN_SAL 5 /* SAL items section */
562#define SN_SOFO 6 /* soundfolding section */
563#define SN_MAP 7 /* MAP items section */
564#define SN_COMPOUND 8 /* compound words section */
565#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000566#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000567#define SN_SUGFILE 11 /* timestamp for .sug file */
568#define SN_REPSAL 12 /* REPSAL items section */
569#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000570#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000571#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000572#define SN_END 255 /* end of sections */
573
574#define SNF_REQUIRED 1 /* <sectionflags>: required section */
575
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576/* Result values. Lower number is accepted over higher one. */
577#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000578#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000579#define SP_RARE 1
580#define SP_LOCAL 2
581#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000582
Bram Moolenaar7887d882005-07-01 22:33:52 +0000583/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000584static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000585
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586typedef struct wordcount_S
587{
588 short_u wc_count; /* nr of times word was seen */
589 char_u wc_word[1]; /* word, actually longer */
590} wordcount_T;
591
592static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000593#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000594#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
595#define MAXWORDCOUNT 0xffff
596
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000597/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598 * Information used when looking for suggestions.
599 */
600typedef struct suginfo_S
601{
602 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000605 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u *su_badptr; /* start of bad word in line */
608 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000609 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
611 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000612 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000613 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000614 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615} suginfo_T;
616
617/* One word suggestion. Used in "si_ga". */
618typedef struct suggest_S
619{
620 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000621 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622 int st_orglen; /* length of replaced text */
623 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624 int st_altscore; /* used when st_score compares equal */
625 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000626 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000627 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628} suggest_T;
629
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000631
Bram Moolenaar4770d092006-01-12 23:22:24 +0000632/* TRUE if a word appears in the list of banned words. */
633#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
634
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000635/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000636 * what is displayed, because when rescore_suggestions() is called the score
637 * may change and wrong suggestions may be removed later. */
638#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639
640/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
641 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000642#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643
644/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000645#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000646#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000648#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000650#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000651#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000652#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653#define SCORE_SUBST 93 /* substitute a character */
654#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000658#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000659#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000660#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000661#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000662#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000664#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000665#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
666 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000667
Bram Moolenaar4770d092006-01-12 23:22:24 +0000668#define SCORE_COMMON1 30 /* subtracted for words seen before */
669#define SCORE_COMMON2 40 /* subtracted for words often seen */
670#define SCORE_COMMON3 50 /* subtracted for words very often seen */
671#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
672#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
673
674/* When trying changed soundfold words it becomes slow when trying more than
675 * two changes. With less then two changes it's slightly faster but we miss a
676 * few good suggestions. In rare cases we need to try three of four changes.
677 */
678#define SCORE_SFMAX1 200 /* maximum score for first try */
679#define SCORE_SFMAX2 300 /* maximum score for second try */
680#define SCORE_SFMAX3 400 /* maximum score for third try */
681
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000683#define SCORE_MAXMAX 999999 /* accept any score */
684#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
685
686/* for spell_edit_score_limit() we need to know the minimum value of
687 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
688#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689
690/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691 * Structure to store info for word matching.
692 */
693typedef struct matchinf_S
694{
695 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000696
697 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000698 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000699 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000701 char_u *mi_cend; /* char after what was used for
702 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000703
704 /* case-folded text */
705 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000706 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000707
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 /* for when checking word after a prefix */
709 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000710 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000711 int mi_prefcnt; /* number of entries at mi_prefarridx */
712 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000713#ifdef FEAT_MBYTE
714 int mi_cprefixlen; /* byte length of prefix in original
715 case */
716#else
717# define mi_cprefixlen mi_prefixlen /* it's the same value */
718#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000719
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000720 /* for when checking a compound word */
721 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000722 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
723 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000724 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000725
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000726 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000727 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000728 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200729 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000730
731 /* for NOBREAK */
732 int mi_result2; /* "mi_resul" without following word */
733 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000734} matchinf_T;
735
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000736/*
737 * The tables used for recognizing word characters according to spelling.
738 * These are only used for the first 256 characters of 'encoding'.
739 */
740typedef struct spelltab_S
741{
742 char_u st_isw[256]; /* flags: is word char */
743 char_u st_isu[256]; /* flags: is uppercase char */
744 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000746} spelltab_T;
747
748static spelltab_T spelltab;
749static int did_set_spelltab;
750
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000751#define CF_WORD 0x01
752#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000753
754static void clear_spell_chartab __ARGS((spelltab_T *sp));
755static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200756static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100757static int spell_iswordp_nmw __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000758#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100759static int spell_mb_isword_class __ARGS((int cl, win_T *wp));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000761#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000762static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000763
764/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000766 */
767typedef enum
768{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_START = 0, /* At start of node check for NUL bytes (goodword
770 * ends); if badword ends there is a match, otherwise
771 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000772 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000773 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
775 STATE_PLAIN, /* Use each byte of the node. */
776 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000777 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000778 STATE_INS, /* Insert a byte in the bad word. */
779 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNSWAP, /* Undo swap two characters. */
781 STATE_SWAP3, /* Swap two characters over three. */
782 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000784 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000785 STATE_REP_INI, /* Prepare for using REP items. */
786 STATE_REP, /* Use matching REP items from the .aff file. */
787 STATE_REP_UNDO, /* Undo a REP item replacement. */
788 STATE_FINAL /* End of this node. */
789} state_T;
790
791/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000792 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 */
794typedef struct trystate_S
795{
Bram Moolenaarea424162005-06-16 21:51:00 +0000796 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000797 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000798 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000799 short ts_curi; /* index in list of child nodes */
800 char_u ts_fidx; /* index in fword[], case-folded bad word */
801 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
802 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000803 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000804 * PFD_PREFIXTREE or PFD_NOPREFIX */
805 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000806#ifdef FEAT_MBYTE
807 char_u ts_tcharlen; /* number of bytes in tword character */
808 char_u ts_tcharidx; /* current byte index in tword character */
809 char_u ts_isdiff; /* DIFF_ values */
810 char_u ts_fcharstart; /* index in fword where badword char started */
811#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000812 char_u ts_prewordlen; /* length of word in "preword[]" */
813 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000814 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000815 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000816 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000817 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000818 char_u ts_delidx; /* index in fword for char that was deleted,
819 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000820} trystate_T;
821
Bram Moolenaarea424162005-06-16 21:51:00 +0000822/* values for ts_isdiff */
823#define DIFF_NONE 0 /* no different byte (yet) */
824#define DIFF_YES 1 /* different byte found */
825#define DIFF_INSERT 2 /* inserting character */
826
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827/* values for ts_flags */
828#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
829#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000830#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000831
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000833#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000834#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000835#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000836
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000837/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000838#define FIND_FOLDWORD 0 /* find word case-folded */
839#define FIND_KEEPWORD 1 /* find keep-case word */
840#define FIND_PREFIX 2 /* find word after prefix */
841#define FIND_COMPOUND 3 /* find case-folded compound word */
842#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000843
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000844static slang_T *slang_alloc __ARGS((char_u *lang));
845static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000846static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000847static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000849static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000850static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000851static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
852static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000853static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000854static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000855static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000856static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000857static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000858static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000859static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000860static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000861static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000862static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000863static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000864static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
865static int read_charflags_section __ARGS((FILE *fd));
866static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000868static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000869static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
870static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
871static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000872static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
873static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000874static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000875static int init_syl_tab __ARGS((slang_T *slang));
876static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000877static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
878static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000879#ifdef FEAT_MBYTE
880static int *mb_str2wide __ARGS((char_u *s));
881#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000882static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
884static void clear_midword __ARGS((win_T *buf));
885static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000886static int find_region __ARGS((char_u *rp, char_u *region));
887static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000888static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000889static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000890static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000891static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000892static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000893static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000894static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000895#ifdef FEAT_EVAL
896static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
897#endif
898static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000899static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
900static void suggest_load_files __ARGS((void));
901static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000903static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000904static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000905static void suggest_try_special __ARGS((suginfo_T *su));
906static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000907static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
908static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000909#ifdef FEAT_MBYTE
910static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
911#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000912static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913static void score_comp_sal __ARGS((suginfo_T *su));
914static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000915static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000917static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static void suggest_try_soundalike_finish __ARGS((void));
919static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
920static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000922static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000923static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000924static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
925static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000926static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000927static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000928static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000929static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000930static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
931static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
932static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000934static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000935#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000937static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
938static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#ifdef FEAT_MBYTE
940static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
941#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000942static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
943static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000944static buf_T *open_spellbuf __ARGS((void));
945static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000946
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000947/*
948 * Use our own character-case definitions, because the current locale may
949 * differ from what the .spl file uses.
950 * These must not be called with negative number!
951 */
952#ifndef FEAT_MBYTE
953/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000954# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
955# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000956# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
957#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000958# if defined(HAVE_WCHAR_H)
959# include <wchar.h> /* for towupper() and towlower() */
960# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000961/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
962 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
963 * the "w" library function for characters above 255 if available. */
964# ifdef HAVE_TOWLOWER
965# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000966 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000967# else
968# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000969 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000970# endif
971
972# ifdef HAVE_TOWUPPER
973# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000974 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000975# else
976# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000977 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000978# endif
979
980# ifdef HAVE_ISWUPPER
981# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
982 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
983# else
984# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000985 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000986# endif
987#endif
988
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000989
990static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000991static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000992static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000993static char *e_affname = N_("Affix name too long in %s line %d: %s");
994static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
995static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000996static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000998/* Remember what "z?" replaced. */
999static char_u *repl_from = NULL;
1000static char_u *repl_to = NULL;
1001
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001002/*
1003 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001004 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001005 * "*attrp" is set to the highlight index for a badly spelled word. For a
1006 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001007 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001009 * "capcol" is used to check for a Capitalised word after the end of a
1010 * sentence. If it's zero then perform the check. Return the column where to
1011 * check next, or -1 when no sentence end was found. If it's NULL then don't
1012 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001013 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 * Returns the length of the word in bytes, also when it's OK, so that the
1015 * caller can skip over the word.
1016 */
1017 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001018spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001020 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001021 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001023 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001024{
1025 matchinf_T mi; /* Most things are put in "mi" so that it can
1026 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001027 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001028 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001029 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001030 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001031 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001032
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001033 /* A word never starts at a space or a control character. Return quickly
1034 * then, skipping over the character. */
1035 if (*ptr <= ' ')
1036 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001037
1038 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001039 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001040 return 1;
1041
Bram Moolenaar5195e452005-08-19 20:32:47 +00001042 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001043
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001044 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001045 * 0X99FF. But always do check spelling to find "3GPP" and "11
1046 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001049 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1050 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001051 else
1052 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001053 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055
Bram Moolenaar0c405862005-06-22 22:26:26 +00001056 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001057 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001058 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001059 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001061 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001062 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001063 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001064 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001065
Bram Moolenaar860cae12010-06-05 23:22:07 +02001066 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001067 {
1068 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001069 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001071 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001073 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001074 if (capcol != NULL)
1075 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001076
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001077 /* We always use the characters up to the next non-word character,
1078 * also for bad words. */
1079 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001081 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001082 mi.mi_capflags = 0;
1083 mi.mi_cend = NULL;
1084 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085
Bram Moolenaar5195e452005-08-19 20:32:47 +00001086 /* case-fold the word with one non-word character, so that we can check
1087 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001088 if (*mi.mi_fend != NUL)
1089 mb_ptr_adv(mi.mi_fend);
1090
1091 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1092 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001093 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /* The word is bad unless we recognize it. */
1096 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001097 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001098
1099 /*
1100 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001101 * We check them all, because a word may be matched longer in another
1102 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001103 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001104 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001105 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001106 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001107
1108 /* If reloading fails the language is still in the list but everything
1109 * has been cleared. */
1110 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1111 continue;
1112
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001113 /* Check for a matching word in case-folded words. */
1114 find_word(&mi, FIND_FOLDWORD);
1115
1116 /* Check for a matching word in keep-case words. */
1117 find_word(&mi, FIND_KEEPWORD);
1118
1119 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001120 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001121
1122 /* For a NOBREAK language, may want to use a word without a following
1123 * word as a backup. */
1124 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1125 && mi.mi_result2 != SP_BAD)
1126 {
1127 mi.mi_result = mi.mi_result2;
1128 mi.mi_end = mi.mi_end2;
1129 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001130
1131 /* Count the word in the first language where it's found to be OK. */
1132 if (count_word && mi.mi_result == SP_OK)
1133 {
1134 count_common_word(mi.mi_lp->lp_slang, ptr,
1135 (int)(mi.mi_end - ptr), 1);
1136 count_word = FALSE;
1137 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001138 }
1139
1140 if (mi.mi_result != SP_OK)
1141 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001142 /* If we found a number skip over it. Allows for "42nd". Do flag
1143 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001144 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001145 {
1146 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1147 return nrlen;
1148 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001149
1150 /* When we are at a non-word character there is no error, just
1151 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001152 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001153 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001154 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001155 {
1156 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001157 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001158
1159 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001160 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001161 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001162 r = vim_regexec(&regmatch, ptr, 0);
1163 wp->w_s->b_cap_prog = regmatch.regprog;
1164 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001165 *capcol = (int)(regmatch.endp[0] - ptr);
1166 }
1167
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001168#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001169 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001170 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001171#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001172 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001173 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001174 else if (mi.mi_end == ptr)
1175 /* Always include at least one character. Required for when there
1176 * is a mixup in "midword". */
1177 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001178 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001179 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001180 {
1181 char_u *p, *fp;
1182 int save_result = mi.mi_result;
1183
1184 /* First language in 'spelllang' is NOBREAK. Find first position
1185 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001186 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001187 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001188 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001189 p = mi.mi_word;
1190 fp = mi.mi_fword;
1191 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001192 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001193 mb_ptr_adv(p);
1194 mb_ptr_adv(fp);
1195 if (p >= mi.mi_end)
1196 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001197 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001198 find_word(&mi, FIND_COMPOUND);
1199 if (mi.mi_result != SP_BAD)
1200 {
1201 mi.mi_end = p;
1202 break;
1203 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001204 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001205 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001206 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001207 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001208
1209 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001210 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001211 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001212 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001213 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001214 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215 }
1216
Bram Moolenaar5195e452005-08-19 20:32:47 +00001217 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1218 {
1219 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001220 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001221 return wrongcaplen;
1222 }
1223
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 return (int)(mi.mi_end - ptr);
1225}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001226
Bram Moolenaar51485f02005-06-04 21:55:20 +00001227/*
1228 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001229 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1230 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1231 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1232 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 *
1234 * For a match mip->mi_result is updated.
1235 */
1236 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001237find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001238 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001239 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001240{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001241 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001243 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 int endidxcnt = 0;
1245 int len;
1246 int wlen = 0;
1247 int flen;
1248 int c;
1249 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001250 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001251#ifdef FEAT_MBYTE
1252 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001253#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001254 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001255 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256 slang_T *slang = mip->mi_lp->lp_slang;
1257 unsigned flags;
1258 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001259 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001260 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001261 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001262 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001263
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001264 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001265 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001266 /* Check for word with matching case in keep-case tree. */
1267 ptr = mip->mi_word;
1268 flen = 9999; /* no case folding, always enough bytes */
1269 byts = slang->sl_kbyts;
1270 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001271
1272 if (mode == FIND_KEEPCOMPOUND)
1273 /* Skip over the previously found word(s). */
1274 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001275 }
1276 else
1277 {
1278 /* Check for case-folded in case-folded tree. */
1279 ptr = mip->mi_fword;
1280 flen = mip->mi_fwordlen; /* available case-folded bytes */
1281 byts = slang->sl_fbyts;
1282 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001283
1284 if (mode == FIND_PREFIX)
1285 {
1286 /* Skip over the prefix. */
1287 wlen = mip->mi_prefixlen;
1288 flen -= mip->mi_prefixlen;
1289 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001290 else if (mode == FIND_COMPOUND)
1291 {
1292 /* Skip over the previously found word(s). */
1293 wlen = mip->mi_compoff;
1294 flen -= mip->mi_compoff;
1295 }
1296
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001297 }
1298
Bram Moolenaar51485f02005-06-04 21:55:20 +00001299 if (byts == NULL)
1300 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001301
Bram Moolenaar51485f02005-06-04 21:55:20 +00001302 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001303 * Repeat advancing in the tree until:
1304 * - there is a byte that doesn't match,
1305 * - we reach the end of the tree,
1306 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001307 */
1308 for (;;)
1309 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001310 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001311 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001312
1313 len = byts[arridx++];
1314
1315 /* If the first possible byte is a zero the word could end here.
1316 * Remember this index, we first check for the longest word. */
1317 if (byts[arridx] == 0)
1318 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001319 if (endidxcnt == MAXWLEN)
1320 {
1321 /* Must be a corrupted spell file. */
1322 EMSG(_(e_format));
1323 return;
1324 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001325 endlen[endidxcnt] = wlen;
1326 endidx[endidxcnt++] = arridx++;
1327 --len;
1328
1329 /* Skip over the zeros, there can be several flag/region
1330 * combinations. */
1331 while (len > 0 && byts[arridx] == 0)
1332 {
1333 ++arridx;
1334 --len;
1335 }
1336 if (len == 0)
1337 break; /* no children, word must end here */
1338 }
1339
1340 /* Stop looking at end of the line. */
1341 if (ptr[wlen] == NUL)
1342 break;
1343
1344 /* Perform a binary search in the list of accepted bytes. */
1345 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001346 if (c == TAB) /* <Tab> is handled like <Space> */
1347 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001348 lo = arridx;
1349 hi = arridx + len - 1;
1350 while (lo < hi)
1351 {
1352 m = (lo + hi) / 2;
1353 if (byts[m] > c)
1354 hi = m - 1;
1355 else if (byts[m] < c)
1356 lo = m + 1;
1357 else
1358 {
1359 lo = hi = m;
1360 break;
1361 }
1362 }
1363
1364 /* Stop if there is no matching byte. */
1365 if (hi < lo || byts[lo] != c)
1366 break;
1367
1368 /* Continue at the child (if there is one). */
1369 arridx = idxs[lo];
1370 ++wlen;
1371 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001372
1373 /* One space in the good word may stand for several spaces in the
1374 * checked word. */
1375 if (c == ' ')
1376 {
1377 for (;;)
1378 {
1379 if (flen <= 0 && *mip->mi_fend != NUL)
1380 flen = fold_more(mip);
1381 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1382 break;
1383 ++wlen;
1384 --flen;
1385 }
1386 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001387 }
1388
1389 /*
1390 * Verify that one of the possible endings is valid. Try the longest
1391 * first.
1392 */
1393 while (endidxcnt > 0)
1394 {
1395 --endidxcnt;
1396 arridx = endidx[endidxcnt];
1397 wlen = endlen[endidxcnt];
1398
1399#ifdef FEAT_MBYTE
1400 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1401 continue; /* not at first byte of character */
1402#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001403 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001404 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001405 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001406 continue; /* next char is a word character */
1407 word_ends = FALSE;
1408 }
1409 else
1410 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001411 /* The prefix flag is before compound flags. Once a valid prefix flag
1412 * has been found we try compound flags. */
1413 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414
1415#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001416 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001417 {
1418 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001419 * when folding case. This can be slow, take a shortcut when the
1420 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001421 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001422 if (STRNCMP(ptr, p, wlen) != 0)
1423 {
1424 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1425 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001426 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001427 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428 }
1429#endif
1430
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001431 /* Check flags and region. For FIND_PREFIX check the condition and
1432 * prefix ID.
1433 * Repeat this if there are more flags/region alternatives until there
1434 * is a match. */
1435 res = SP_BAD;
1436 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1437 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001438 {
1439 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001440
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001441 /* For the fold-case tree check that the case of the checked word
1442 * matches with what the word in the tree requires.
1443 * For keep-case tree the case is always right. For prefixes we
1444 * don't bother to check. */
1445 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001447 if (mip->mi_cend != mip->mi_word + wlen)
1448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 /* mi_capflags was set for a different word length, need
1450 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001451 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001452 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001453 }
1454
Bram Moolenaar0c405862005-06-22 22:26:26 +00001455 if (mip->mi_capflags == WF_KEEPCAP
1456 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001457 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458 }
1459
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460 /* When mode is FIND_PREFIX the word must support the prefix:
1461 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001463 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001464 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001465 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001466 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001467 mip->mi_word + mip->mi_cprefixlen, slang,
1468 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001469 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001470 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001471
1472 /* Use the WF_RARE flag for a rare prefix. */
1473 if (c & WF_RAREPFX)
1474 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001475 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001476 }
1477
Bram Moolenaar78622822005-08-23 21:00:13 +00001478 if (slang->sl_nobreak)
1479 {
1480 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1481 && (flags & WF_BANNED) == 0)
1482 {
1483 /* NOBREAK: found a valid following word. That's all we
1484 * need to know, so return. */
1485 mip->mi_result = SP_OK;
1486 break;
1487 }
1488 }
1489
1490 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1491 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001492 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001493 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001494 * COMPOUNDMIN reject it quickly.
1495 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001496 * that's too short... Myspell compatibility requires this
1497 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001498 if (((unsigned)flags >> 24) == 0
1499 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001500 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001501#ifdef FEAT_MBYTE
1502 /* For multi-byte chars check character length against
1503 * COMPOUNDMIN. */
1504 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001505 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001506 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1507 wlen - mip->mi_compoff) < slang->sl_compminlen)
1508 continue;
1509#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001510
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001511 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001512 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001513 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1514 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001515 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001516 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001517
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001518 /* Don't allow compounding on a side where an affix was added,
1519 * unless COMPOUNDPERMITFLAG was used. */
1520 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1521 continue;
1522 if (!word_ends && (flags & WF_NOCOMPAFT))
1523 continue;
1524
Bram Moolenaard12a1322005-08-21 22:08:24 +00001525 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001526 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001527 ? slang->sl_compstartflags
1528 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001529 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001530 continue;
1531
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001532 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1533 * discard the compound word. */
1534 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1535 continue;
1536
Bram Moolenaare52325c2005-08-22 22:54:29 +00001537 if (mode == FIND_COMPOUND)
1538 {
1539 int capflags;
1540
1541 /* Need to check the caps type of the appended compound
1542 * word. */
1543#ifdef FEAT_MBYTE
1544 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1545 mip->mi_compoff) != 0)
1546 {
1547 /* case folding may have changed the length */
1548 p = mip->mi_word;
1549 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1550 mb_ptr_adv(p);
1551 }
1552 else
1553#endif
1554 p = mip->mi_word + mip->mi_compoff;
1555 capflags = captype(p, mip->mi_word + wlen);
1556 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1557 && (flags & WF_FIXCAP) != 0))
1558 continue;
1559
1560 if (capflags != WF_ALLCAP)
1561 {
1562 /* When the character before the word is a word
1563 * character we do not accept a Onecap word. We do
1564 * accept a no-caps word, even when the dictionary
1565 * word specifies ONECAP. */
1566 mb_ptr_back(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001567 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +00001568 ? capflags == WF_ONECAP
1569 : (flags & WF_ONECAP) != 0
1570 && capflags != WF_ONECAP)
1571 continue;
1572 }
1573 }
1574
Bram Moolenaar5195e452005-08-19 20:32:47 +00001575 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001576 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001577 * the number of syllables must not be too large. */
1578 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1579 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1580 if (word_ends)
1581 {
1582 char_u fword[MAXWLEN];
1583
1584 if (slang->sl_compsylmax < MAXWLEN)
1585 {
1586 /* "fword" is only needed for checking syllables. */
1587 if (ptr == mip->mi_word)
1588 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1589 else
1590 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1591 }
1592 if (!can_compound(slang, fword, mip->mi_compflags))
1593 continue;
1594 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001595 else if (slang->sl_comprules != NULL
1596 && !match_compoundrule(slang, mip->mi_compflags))
1597 /* The compound flags collected so far do not match any
1598 * COMPOUNDRULE, discard the compounded word. */
1599 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001600 }
1601
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001602 /* Check NEEDCOMPOUND: can't use word without compounding. */
1603 else if (flags & WF_NEEDCOMP)
1604 continue;
1605
Bram Moolenaar78622822005-08-23 21:00:13 +00001606 nobreak_result = SP_OK;
1607
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001608 if (!word_ends)
1609 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001610 int save_result = mip->mi_result;
1611 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001612 langp_T *save_lp = mip->mi_lp;
1613 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001614
1615 /* Check that a valid word follows. If there is one and we
1616 * are compounding, it will set "mi_result", thus we are
1617 * always finished here. For NOBREAK we only check that a
1618 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001619 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001620 if (slang->sl_nobreak)
1621 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001622
1623 /* Find following word in case-folded tree. */
1624 mip->mi_compoff = endlen[endidxcnt];
1625#ifdef FEAT_MBYTE
1626 if (has_mbyte && mode == FIND_KEEPWORD)
1627 {
1628 /* Compute byte length in case-folded word from "wlen":
1629 * byte length in keep-case word. Length may change when
1630 * folding case. This can be slow, take a shortcut when
1631 * the case-folded word is equal to the keep-case word. */
1632 p = mip->mi_fword;
1633 if (STRNCMP(ptr, p, wlen) != 0)
1634 {
1635 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1636 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001637 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001638 }
1639 }
1640#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001641 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001642 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001643 if (flags & WF_COMPROOT)
1644 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001645
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001646 /* For NOBREAK we need to try all NOBREAK languages, at least
1647 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001648 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001649 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001650 if (slang->sl_nobreak)
1651 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001652 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001653 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1654 || !mip->mi_lp->lp_slang->sl_nobreak)
1655 continue;
1656 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001657
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001658 find_word(mip, FIND_COMPOUND);
1659
1660 /* When NOBREAK any word that matches is OK. Otherwise we
1661 * need to find the longest match, thus try with keep-case
1662 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001663 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1664 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001665 /* Find following word in keep-case tree. */
1666 mip->mi_compoff = wlen;
1667 find_word(mip, FIND_KEEPCOMPOUND);
1668
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001669#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1670 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1671 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001672 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1673 {
1674 /* Check for following word with prefix. */
1675 mip->mi_compoff = c;
1676 find_prefix(mip, FIND_COMPOUND);
1677 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001678#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001679 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001680
1681 if (!slang->sl_nobreak)
1682 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001683 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001684 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001685 if (flags & WF_COMPROOT)
1686 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001687 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001688
Bram Moolenaar78622822005-08-23 21:00:13 +00001689 if (slang->sl_nobreak)
1690 {
1691 nobreak_result = mip->mi_result;
1692 mip->mi_result = save_result;
1693 mip->mi_end = save_end;
1694 }
1695 else
1696 {
1697 if (mip->mi_result == SP_OK)
1698 break;
1699 continue;
1700 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001701 }
1702
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001703 if (flags & WF_BANNED)
1704 res = SP_BANNED;
1705 else if (flags & WF_REGION)
1706 {
1707 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001708 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001709 res = SP_OK;
1710 else
1711 res = SP_LOCAL;
1712 }
1713 else if (flags & WF_RARE)
1714 res = SP_RARE;
1715 else
1716 res = SP_OK;
1717
Bram Moolenaar78622822005-08-23 21:00:13 +00001718 /* Always use the longest match and the best result. For NOBREAK
1719 * we separately keep the longest match without a following good
1720 * word as a fall-back. */
1721 if (nobreak_result == SP_BAD)
1722 {
1723 if (mip->mi_result2 > res)
1724 {
1725 mip->mi_result2 = res;
1726 mip->mi_end2 = mip->mi_word + wlen;
1727 }
1728 else if (mip->mi_result2 == res
1729 && mip->mi_end2 < mip->mi_word + wlen)
1730 mip->mi_end2 = mip->mi_word + wlen;
1731 }
1732 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001733 {
1734 mip->mi_result = res;
1735 mip->mi_end = mip->mi_word + wlen;
1736 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001737 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001738 mip->mi_end = mip->mi_word + wlen;
1739
Bram Moolenaar78622822005-08-23 21:00:13 +00001740 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001741 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001742 }
1743
Bram Moolenaar78622822005-08-23 21:00:13 +00001744 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001745 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001746 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001747}
1748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001749/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001750 * Return TRUE if there is a match between the word ptr[wlen] and
1751 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1752 * word.
1753 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1754 * end of ptr[wlen] and the second part matches after it.
1755 */
1756 static int
1757match_checkcompoundpattern(ptr, wlen, gap)
1758 char_u *ptr;
1759 int wlen;
1760 garray_T *gap; /* &sl_comppat */
1761{
1762 int i;
1763 char_u *p;
1764 int len;
1765
1766 for (i = 0; i + 1 < gap->ga_len; i += 2)
1767 {
1768 p = ((char_u **)gap->ga_data)[i + 1];
1769 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1770 {
1771 /* Second part matches at start of following compound word, now
1772 * check if first part matches at end of previous word. */
1773 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001774 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001775 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1776 return TRUE;
1777 }
1778 }
1779 return FALSE;
1780}
1781
1782/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001783 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1784 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001785 */
1786 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001787can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001788 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001789 char_u *word;
1790 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001791{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001792#ifdef FEAT_MBYTE
1793 char_u uflags[MAXWLEN * 2];
1794 int i;
1795#endif
1796 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001797
1798 if (slang->sl_compprog == NULL)
1799 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001800#ifdef FEAT_MBYTE
1801 if (enc_utf8)
1802 {
1803 /* Need to convert the single byte flags to utf8 characters. */
1804 p = uflags;
1805 for (i = 0; flags[i] != NUL; ++i)
1806 p += mb_char2bytes(flags[i], p);
1807 *p = NUL;
1808 p = uflags;
1809 }
1810 else
1811#endif
1812 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001813 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001814 return FALSE;
1815
Bram Moolenaare52325c2005-08-22 22:54:29 +00001816 /* Count the number of syllables. This may be slow, do it last. If there
1817 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001818 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001819 if (slang->sl_compsylmax < MAXWLEN
1820 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001821 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001822 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001823}
1824
1825/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001826 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1827 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1828 * lines if they don't contain wildcards.
1829 */
1830 static int
1831can_be_compound(sp, slang, compflags, flag)
1832 trystate_T *sp;
1833 slang_T *slang;
1834 char_u *compflags;
1835 int flag;
1836{
1837 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1838 * then it can't possibly compound. */
1839 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1840 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1841 return FALSE;
1842
1843 /* If there are no wildcards, we can check if the flags collected so far
1844 * possibly can form a match with COMPOUNDRULE patterns. This only
1845 * makes sense when we have two or more words. */
1846 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1847 {
1848 int v;
1849
1850 compflags[sp->ts_complen] = flag;
1851 compflags[sp->ts_complen + 1] = NUL;
1852 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1853 compflags[sp->ts_complen] = NUL;
1854 return v;
1855 }
1856
1857 return TRUE;
1858}
1859
1860
1861/*
1862 * Return TRUE if the compound flags in compflags[] match the start of any
1863 * compound rule. This is used to stop trying a compound if the flags
1864 * collected so far can't possibly match any compound rule.
1865 * Caller must check that slang->sl_comprules is not NULL.
1866 */
1867 static int
1868match_compoundrule(slang, compflags)
1869 slang_T *slang;
1870 char_u *compflags;
1871{
1872 char_u *p;
1873 int i;
1874 int c;
1875
1876 /* loop over all the COMPOUNDRULE entries */
1877 for (p = slang->sl_comprules; *p != NUL; ++p)
1878 {
1879 /* loop over the flags in the compound word we have made, match
1880 * them against the current rule entry */
1881 for (i = 0; ; ++i)
1882 {
1883 c = compflags[i];
1884 if (c == NUL)
1885 /* found a rule that matches for the flags we have so far */
1886 return TRUE;
1887 if (*p == '/' || *p == NUL)
1888 break; /* end of rule, it's too short */
1889 if (*p == '[')
1890 {
1891 int match = FALSE;
1892
1893 /* compare against all the flags in [] */
1894 ++p;
1895 while (*p != ']' && *p != NUL)
1896 if (*p++ == c)
1897 match = TRUE;
1898 if (!match)
1899 break; /* none matches */
1900 }
1901 else if (*p != c)
1902 break; /* flag of word doesn't match flag in pattern */
1903 ++p;
1904 }
1905
1906 /* Skip to the next "/", where the next pattern starts. */
1907 p = vim_strchr(p, '/');
1908 if (p == NULL)
1909 break;
1910 }
1911
1912 /* Checked all the rules and none of them match the flags, so there
1913 * can't possibly be a compound starting with these flags. */
1914 return FALSE;
1915}
1916
1917/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001918 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1919 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001920 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001921 */
1922 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001923valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001924 int totprefcnt; /* nr of prefix IDs */
1925 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001926 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001927 char_u *word;
1928 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001929 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001930{
1931 int prefcnt;
1932 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001933 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001934 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001935
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001936 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001937 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1938 {
1939 pidx = slang->sl_pidxs[arridx + prefcnt];
1940
1941 /* Check the prefix ID. */
1942 if (prefid != (pidx & 0xff))
1943 continue;
1944
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001945 /* Check if the prefix doesn't combine and the word already has a
1946 * suffix. */
1947 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1948 continue;
1949
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001950 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001951 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001952 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1953 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001954 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001955 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001956 continue;
1957 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001958 else if (cond_req)
1959 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001960
Bram Moolenaar53805d12005-08-01 07:08:33 +00001961 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001962 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001964 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001965}
1966
1967/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001968 * Check if the word at "mip->mi_word" has a matching prefix.
1969 * If it does, then check the following word.
1970 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001971 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1972 * prefix in a compound word.
1973 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001974 * For a match mip->mi_result is updated.
1975 */
1976 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001977find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001978 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001979 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001980{
1981 idx_T arridx = 0;
1982 int len;
1983 int wlen = 0;
1984 int flen;
1985 int c;
1986 char_u *ptr;
1987 idx_T lo, hi, m;
1988 slang_T *slang = mip->mi_lp->lp_slang;
1989 char_u *byts;
1990 idx_T *idxs;
1991
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001992 byts = slang->sl_pbyts;
1993 if (byts == NULL)
1994 return; /* array is empty */
1995
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001996 /* We use the case-folded word here, since prefixes are always
1997 * case-folded. */
1998 ptr = mip->mi_fword;
1999 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002000 if (mode == FIND_COMPOUND)
2001 {
2002 /* Skip over the previously found word(s). */
2003 ptr += mip->mi_compoff;
2004 flen -= mip->mi_compoff;
2005 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002006 idxs = slang->sl_pidxs;
2007
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002008 /*
2009 * Repeat advancing in the tree until:
2010 * - there is a byte that doesn't match,
2011 * - we reach the end of the tree,
2012 * - or we reach the end of the line.
2013 */
2014 for (;;)
2015 {
2016 if (flen == 0 && *mip->mi_fend != NUL)
2017 flen = fold_more(mip);
2018
2019 len = byts[arridx++];
2020
2021 /* If the first possible byte is a zero the prefix could end here.
2022 * Check if the following word matches and supports the prefix. */
2023 if (byts[arridx] == 0)
2024 {
2025 /* There can be several prefixes with different conditions. We
2026 * try them all, since we don't know which one will give the
2027 * longest match. The word is the same each time, pass the list
2028 * of possible prefixes to find_word(). */
2029 mip->mi_prefarridx = arridx;
2030 mip->mi_prefcnt = len;
2031 while (len > 0 && byts[arridx] == 0)
2032 {
2033 ++arridx;
2034 --len;
2035 }
2036 mip->mi_prefcnt -= len;
2037
2038 /* Find the word that comes after the prefix. */
2039 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002040 if (mode == FIND_COMPOUND)
2041 /* Skip over the previously found word(s). */
2042 mip->mi_prefixlen += mip->mi_compoff;
2043
Bram Moolenaar53805d12005-08-01 07:08:33 +00002044#ifdef FEAT_MBYTE
2045 if (has_mbyte)
2046 {
2047 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002048 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2049 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002050 }
2051 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002052 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002053#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002054 find_word(mip, FIND_PREFIX);
2055
2056
2057 if (len == 0)
2058 break; /* no children, word must end here */
2059 }
2060
2061 /* Stop looking at end of the line. */
2062 if (ptr[wlen] == NUL)
2063 break;
2064
2065 /* Perform a binary search in the list of accepted bytes. */
2066 c = ptr[wlen];
2067 lo = arridx;
2068 hi = arridx + len - 1;
2069 while (lo < hi)
2070 {
2071 m = (lo + hi) / 2;
2072 if (byts[m] > c)
2073 hi = m - 1;
2074 else if (byts[m] < c)
2075 lo = m + 1;
2076 else
2077 {
2078 lo = hi = m;
2079 break;
2080 }
2081 }
2082
2083 /* Stop if there is no matching byte. */
2084 if (hi < lo || byts[lo] != c)
2085 break;
2086
2087 /* Continue at the child (if there is one). */
2088 arridx = idxs[lo];
2089 ++wlen;
2090 --flen;
2091 }
2092}
2093
2094/*
2095 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002096 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002097 * Return the length of the folded chars in bytes.
2098 */
2099 static int
2100fold_more(mip)
2101 matchinf_T *mip;
2102{
2103 int flen;
2104 char_u *p;
2105
2106 p = mip->mi_fend;
2107 do
2108 {
2109 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002110 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002111
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002112 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002113 if (*mip->mi_fend != NUL)
2114 mb_ptr_adv(mip->mi_fend);
2115
2116 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2117 mip->mi_fword + mip->mi_fwordlen,
2118 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002119 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002120 mip->mi_fwordlen += flen;
2121 return flen;
2122}
2123
2124/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002125 * Check case flags for a word. Return TRUE if the word has the requested
2126 * case.
2127 */
2128 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002129spell_valid_case(wordflags, treeflags)
2130 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002131 int treeflags; /* flags for the word in the spell tree */
2132{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002133 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002135 && ((treeflags & WF_ONECAP) == 0
2136 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137}
2138
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002139/*
2140 * Return TRUE if spell checking is not enabled.
2141 */
2142 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002143no_spell_checking(wp)
2144 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002145{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002146 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2147 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002148 {
2149 EMSG(_("E756: Spell checking is not enabled"));
2150 return TRUE;
2151 }
2152 return FALSE;
2153}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002154
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002155/*
2156 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002157 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2158 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002159 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2160 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002161 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002162 */
2163 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002164spell_move_to(wp, dir, allwords, curline, attrp)
2165 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002166 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002167 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002169 hlf_T *attrp; /* return: attributes of bad word or NULL
2170 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002171{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002172 linenr_T lnum;
2173 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002174 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002175 char_u *line;
2176 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002177 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002178 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002179 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002180#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002181 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002182#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002183 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002184 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002185 char_u *buf = NULL;
2186 int buflen = 0;
2187 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002188 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002189 int found_one = FALSE;
2190 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002191
Bram Moolenaar95529562005-08-25 21:21:38 +00002192 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002193 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002195 /*
2196 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002197 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002198 *
2199 * When searching backwards, we continue in the line to find the last
2200 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002201 *
2202 * We concatenate the start of the next line, so that wrapped words work
2203 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2204 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002205 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002206 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002207 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002208
2209 while (!got_int)
2210 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002211 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002213 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002214 if (buflen < len + MAXWLEN + 2)
2215 {
2216 vim_free(buf);
2217 buflen = len + MAXWLEN + 2;
2218 buf = alloc(buflen);
2219 if (buf == NULL)
2220 break;
2221 }
2222
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002223 /* In first line check first word for Capital. */
2224 if (lnum == 1)
2225 capcol = 0;
2226
2227 /* For checking first word with a capital skip white space. */
2228 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002229 capcol = (int)(skipwhite(line) - line);
2230 else if (curline && wp == curwin)
2231 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002232 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002233 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002234 if (check_need_cap(lnum, col))
2235 capcol = col;
2236
2237 /* Need to get the line again, may have looked at the previous
2238 * one. */
2239 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2240 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002241
Bram Moolenaar0c405862005-06-22 22:26:26 +00002242 /* Copy the line into "buf" and append the start of the next line if
2243 * possible. */
2244 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002245 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002246 spell_cat_line(buf + STRLEN(buf),
2247 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002248
2249 p = buf + skip;
2250 endp = buf + len;
2251 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002252 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002253 /* When searching backward don't search after the cursor. Unless
2254 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002255 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002256 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002257 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002258 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002259 break;
2260
2261 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002262 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002263 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002264
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002266 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002268 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002270 /* When searching forward only accept a bad word after
2271 * the cursor. */
2272 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002273 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002274 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002275 && (wrapped
2276 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002277 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002278 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002280#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002281 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002282 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002283 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002284 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002285 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002286 if (!can_spell)
2287 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002288 }
2289 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002290#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002291 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002292
Bram Moolenaar51485f02005-06-04 21:55:20 +00002293 if (can_spell)
2294 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002295 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002297 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002298#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002299 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002300#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002301 if (dir == FORWARD)
2302 {
2303 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002304 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002305 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002306 if (attrp != NULL)
2307 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002308 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002309 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002310 else if (curline)
2311 /* Insert mode completion: put cursor after
2312 * the bad word. */
2313 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002314 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002315 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002317 else
2318 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 }
2321
Bram Moolenaar51485f02005-06-04 21:55:20 +00002322 /* advance to character after the word */
2323 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002324 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325 }
2326
Bram Moolenaar5195e452005-08-19 20:32:47 +00002327 if (dir == BACKWARD && found_pos.lnum != 0)
2328 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002329 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002330 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002331 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002332 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002333 }
2334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002335 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002336 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002338 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002339 if (dir == BACKWARD)
2340 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002341 /* If we are back at the starting line and searched it again there
2342 * is no match, give up. */
2343 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002344 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002345
2346 if (lnum > 1)
2347 --lnum;
2348 else if (!p_ws)
2349 break; /* at first line and 'nowrapscan' */
2350 else
2351 {
2352 /* Wrap around to the end of the buffer. May search the
2353 * starting line again and accept the last match. */
2354 lnum = wp->w_buffer->b_ml.ml_line_count;
2355 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002356 if (!shortmess(SHM_SEARCH))
2357 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002358 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002359 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002360 }
2361 else
2362 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002363 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2364 ++lnum;
2365 else if (!p_ws)
2366 break; /* at first line and 'nowrapscan' */
2367 else
2368 {
2369 /* Wrap around to the start of the buffer. May search the
2370 * starting line again and accept the first match. */
2371 lnum = 1;
2372 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002373 if (!shortmess(SHM_SEARCH))
2374 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002375 }
2376
2377 /* If we are back at the starting line and there is no match then
2378 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002379 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002380 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002381
2382 /* Skip the characters at the start of the next line that were
2383 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002384 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002385 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002386 else
2387 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002388
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002389 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002390 --capcol;
2391
2392 /* But after empty line check first word in next line */
2393 if (*skipwhite(line) == NUL)
2394 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002395 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396
2397 line_breakcheck();
2398 }
2399
Bram Moolenaar0c405862005-06-22 22:26:26 +00002400 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002401 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002402}
2403
2404/*
2405 * For spell checking: concatenate the start of the following line "line" into
2406 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002407 * Keep the blanks at the start of the next line, this is used in win_line()
2408 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002409 */
2410 void
2411spell_cat_line(buf, line, maxlen)
2412 char_u *buf;
2413 char_u *line;
2414 int maxlen;
2415{
2416 char_u *p;
2417 int n;
2418
2419 p = skipwhite(line);
2420 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2421 p = skipwhite(p + 1);
2422
2423 if (*p != NUL)
2424 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002425 /* Only worth concatenating if there is something else than spaces to
2426 * concatenate. */
2427 n = (int)(p - line) + 1;
2428 if (n < maxlen - 1)
2429 {
2430 vim_memset(buf, ' ', n);
2431 vim_strncpy(buf + n, p, maxlen - 1 - n);
2432 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002433 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002434}
2435
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002436/*
2437 * Structure used for the cookie argument of do_in_runtimepath().
2438 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002439typedef struct spelload_S
2440{
2441 char_u sl_lang[MAXWLEN + 1]; /* language name */
2442 slang_T *sl_slang; /* resulting slang_T struct */
2443 int sl_nobreak; /* NOBREAK language found */
2444} spelload_T;
2445
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002447 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002448 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002450 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451spell_load_lang(lang)
2452 char_u *lang;
2453{
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 Moolenaarda2303d2005-08-30 21:55:26 +00002483 r = do_in_runtimepath(fname_enc, FALSE, 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 Moolenaarb8a7b562006-02-01 21:47:16 +00002495 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2496
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 Moolenaarda2303d2005-08-30 21:55:26 +00002524 do_in_runtimepath(fname_enc, TRUE, 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 *
2533spell_enc()
2534{
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
2548int_wordlist_spl(fname)
2549 char_u *fname;
2550{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002551 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002552 int_wordlist, spell_enc());
2553}
2554
2555/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002556 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 * Caller must fill "sl_next".
2558 */
2559 static slang_T *
2560slang_alloc(lang)
2561 char_u *lang;
2562{
2563 slang_T *lp;
2564
Bram Moolenaar51485f02005-06-04 21:55:20 +00002565 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002566 if (lp != NULL)
2567 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002568 if (lang != NULL)
2569 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002571 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002572 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002573 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002576
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002577 return lp;
2578}
2579
2580/*
2581 * Free the contents of an slang_T and the structure itself.
2582 */
2583 static void
2584slang_free(lp)
2585 slang_T *lp;
2586{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002587 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002588 vim_free(lp->sl_fname);
2589 slang_clear(lp);
2590 vim_free(lp);
2591}
2592
2593/*
2594 * Clear an slang_T so that the file can be reloaded.
2595 */
2596 static void
2597slang_clear(lp)
2598 slang_T *lp;
2599{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002600 garray_T *gap;
2601 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002602 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002603 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002604 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002605
Bram Moolenaar51485f02005-06-04 21:55:20 +00002606 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002607 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002608 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002609 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002610 vim_free(lp->sl_pbyts);
2611 lp->sl_pbyts = NULL;
2612
Bram Moolenaar51485f02005-06-04 21:55:20 +00002613 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002614 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002615 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002616 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002617 vim_free(lp->sl_pidxs);
2618 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619
Bram Moolenaar4770d092006-01-12 23:22:24 +00002620 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002621 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002622 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2623 while (gap->ga_len > 0)
2624 {
2625 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2626 vim_free(ftp->ft_from);
2627 vim_free(ftp->ft_to);
2628 }
2629 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002630 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002631
2632 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002633 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002634 {
2635 /* "ga_len" is set to 1 without adding an item for latin1 */
2636 if (gap->ga_data != NULL)
2637 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2638 for (i = 0; i < gap->ga_len; ++i)
2639 vim_free(((int **)gap->ga_data)[i]);
2640 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002641 else
2642 /* SAL items: free salitem_T items */
2643 while (gap->ga_len > 0)
2644 {
2645 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2646 vim_free(smp->sm_lead);
2647 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2648 vim_free(smp->sm_to);
2649#ifdef FEAT_MBYTE
2650 vim_free(smp->sm_lead_w);
2651 vim_free(smp->sm_oneof_w);
2652 vim_free(smp->sm_to_w);
2653#endif
2654 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002655 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002656
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002657 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002658 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002659 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002660 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002661 lp->sl_prefprog = NULL;
2662
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002663 vim_free(lp->sl_info);
2664 lp->sl_info = NULL;
2665
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002666 vim_free(lp->sl_midword);
2667 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002668
Bram Moolenaar473de612013-06-08 18:19:48 +02002669 vim_regfree(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002670 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002671 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002672 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002673 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002674 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002675 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002676 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002677
2678 vim_free(lp->sl_syllable);
2679 lp->sl_syllable = NULL;
2680 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002681
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002682 ga_clear_strings(&lp->sl_comppat);
2683
Bram Moolenaar4770d092006-01-12 23:22:24 +00002684 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2685 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002686
Bram Moolenaar4770d092006-01-12 23:22:24 +00002687#ifdef FEAT_MBYTE
2688 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002689#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690
Bram Moolenaar4770d092006-01-12 23:22:24 +00002691 /* Clear info from .sug file. */
2692 slang_clear_sug(lp);
2693
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002695 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002696 lp->sl_compsylmax = MAXWLEN;
2697 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002698}
2699
2700/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002701 * Clear the info from the .sug file in "lp".
2702 */
2703 static void
2704slang_clear_sug(lp)
2705 slang_T *lp;
2706{
2707 vim_free(lp->sl_sbyts);
2708 lp->sl_sbyts = NULL;
2709 vim_free(lp->sl_sidxs);
2710 lp->sl_sidxs = NULL;
2711 close_spellbuf(lp->sl_sugbuf);
2712 lp->sl_sugbuf = NULL;
2713 lp->sl_sugloaded = FALSE;
2714 lp->sl_sugtime = 0;
2715}
2716
2717/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002718 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002719 * Invoked through do_in_runtimepath().
2720 */
2721 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002722spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002724 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002725{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002726 spelload_T *slp = (spelload_T *)cookie;
2727 slang_T *slang;
2728
2729 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2730 if (slang != NULL)
2731 {
2732 /* When a previously loaded file has NOBREAK also use it for the
2733 * ".add" files. */
2734 if (slp->sl_nobreak && slang->sl_add)
2735 slang->sl_nobreak = TRUE;
2736 else if (slang->sl_nobreak)
2737 slp->sl_nobreak = TRUE;
2738
2739 slp->sl_slang = slang;
2740 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002741}
2742
2743/*
2744 * Load one spell file and store the info into a slang_T.
2745 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002746 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002747 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2748 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2749 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2750 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002751 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002752 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2753 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002754 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002755 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 static slang_T *
2757spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002758 char_u *fname;
2759 char_u *lang;
2760 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002762{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002763 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002764 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002765 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002767 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002768 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 char_u *save_sourcing_name = sourcing_name;
2770 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002771 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002772 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002773 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002774
Bram Moolenaarb765d632005-06-07 21:00:02 +00002775 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776 if (fd == NULL)
2777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002778 if (!silent)
2779 EMSG2(_(e_notopen), fname);
2780 else if (p_verbose > 2)
2781 {
2782 verbose_enter();
2783 smsg((char_u *)e_notopen, fname);
2784 verbose_leave();
2785 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002786 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002787 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002788 if (p_verbose > 2)
2789 {
2790 verbose_enter();
2791 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2792 verbose_leave();
2793 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794
Bram Moolenaarb765d632005-06-07 21:00:02 +00002795 if (old_lp == NULL)
2796 {
2797 lp = slang_alloc(lang);
2798 if (lp == NULL)
2799 goto endFAIL;
2800
2801 /* Remember the file name, used to reload the file when it's updated. */
2802 lp->sl_fname = vim_strsave(fname);
2803 if (lp->sl_fname == NULL)
2804 goto endFAIL;
2805
Bram Moolenaar56f78042010-12-08 17:09:32 +01002806 /* Check for .add.spl (_add.spl for VMS). */
2807 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002808 }
2809 else
2810 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002811
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002812 /* Set sourcing_name, so that error messages mention the file name. */
2813 sourcing_name = fname;
2814 sourcing_lnum = 0;
2815
Bram Moolenaar4770d092006-01-12 23:22:24 +00002816 /*
2817 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002818 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002819 for (i = 0; i < VIMSPELLMAGICL; ++i)
2820 buf[i] = getc(fd); /* <fileID> */
2821 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2822 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002823 EMSG(_("E757: This does not look like a spell file"));
2824 goto endFAIL;
2825 }
2826 c = getc(fd); /* <versionnr> */
2827 if (c < VIMSPELLVERSION)
2828 {
2829 EMSG(_("E771: Old spell file, needs to be updated"));
2830 goto endFAIL;
2831 }
2832 else if (c > VIMSPELLVERSION)
2833 {
2834 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002835 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002836 }
2837
Bram Moolenaar5195e452005-08-19 20:32:47 +00002838
2839 /*
2840 * <SECTIONS>: <section> ... <sectionend>
2841 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2842 */
2843 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002844 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002845 n = getc(fd); /* <sectionID> or <sectionend> */
2846 if (n == SN_END)
2847 break;
2848 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002849 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002850 if (len < 0)
2851 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002852
Bram Moolenaar5195e452005-08-19 20:32:47 +00002853 res = 0;
2854 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002855 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002856 case SN_INFO:
2857 lp->sl_info = read_string(fd, len); /* <infotext> */
2858 if (lp->sl_info == NULL)
2859 goto endFAIL;
2860 break;
2861
Bram Moolenaar5195e452005-08-19 20:32:47 +00002862 case SN_REGION:
2863 res = read_region_section(fd, lp, len);
2864 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002865
Bram Moolenaar5195e452005-08-19 20:32:47 +00002866 case SN_CHARFLAGS:
2867 res = read_charflags_section(fd);
2868 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002869
Bram Moolenaar5195e452005-08-19 20:32:47 +00002870 case SN_MIDWORD:
2871 lp->sl_midword = read_string(fd, len); /* <midword> */
2872 if (lp->sl_midword == NULL)
2873 goto endFAIL;
2874 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002875
Bram Moolenaar5195e452005-08-19 20:32:47 +00002876 case SN_PREFCOND:
2877 res = read_prefcond_section(fd, lp);
2878 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002879
Bram Moolenaar5195e452005-08-19 20:32:47 +00002880 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002881 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2882 break;
2883
2884 case SN_REPSAL:
2885 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002886 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002887
Bram Moolenaar5195e452005-08-19 20:32:47 +00002888 case SN_SAL:
2889 res = read_sal_section(fd, lp);
2890 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002891
Bram Moolenaar5195e452005-08-19 20:32:47 +00002892 case SN_SOFO:
2893 res = read_sofo_section(fd, lp);
2894 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002895
Bram Moolenaar5195e452005-08-19 20:32:47 +00002896 case SN_MAP:
2897 p = read_string(fd, len); /* <mapstr> */
2898 if (p == NULL)
2899 goto endFAIL;
2900 set_map_str(lp, p);
2901 vim_free(p);
2902 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002903
Bram Moolenaar4770d092006-01-12 23:22:24 +00002904 case SN_WORDS:
2905 res = read_words_section(fd, lp, len);
2906 break;
2907
2908 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002909 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002910 break;
2911
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002912 case SN_NOSPLITSUGS:
2913 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2914 break;
2915
Bram Moolenaar5195e452005-08-19 20:32:47 +00002916 case SN_COMPOUND:
2917 res = read_compound(fd, lp, len);
2918 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002919
Bram Moolenaar78622822005-08-23 21:00:13 +00002920 case SN_NOBREAK:
2921 lp->sl_nobreak = TRUE;
2922 break;
2923
Bram Moolenaar5195e452005-08-19 20:32:47 +00002924 case SN_SYLLABLE:
2925 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2926 if (lp->sl_syllable == NULL)
2927 goto endFAIL;
2928 if (init_syl_tab(lp) == FAIL)
2929 goto endFAIL;
2930 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002931
Bram Moolenaar5195e452005-08-19 20:32:47 +00002932 default:
2933 /* Unsupported section. When it's required give an error
2934 * message. When it's not required skip the contents. */
2935 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002936 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002937 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002938 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002939 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 while (--len >= 0)
2941 if (getc(fd) < 0)
2942 goto truncerr;
2943 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002944 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002945someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002946 if (res == SP_FORMERROR)
2947 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002948 EMSG(_(e_format));
2949 goto endFAIL;
2950 }
2951 if (res == SP_TRUNCERROR)
2952 {
2953truncerr:
2954 EMSG(_(e_spell_trunc));
2955 goto endFAIL;
2956 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002957 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002958 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002959 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002960
Bram Moolenaar4770d092006-01-12 23:22:24 +00002961 /* <LWORDTREE> */
2962 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2963 if (res != 0)
2964 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002965
Bram Moolenaar4770d092006-01-12 23:22:24 +00002966 /* <KWORDTREE> */
2967 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2968 if (res != 0)
2969 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002970
Bram Moolenaar4770d092006-01-12 23:22:24 +00002971 /* <PREFIXTREE> */
2972 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2973 lp->sl_prefixcnt);
2974 if (res != 0)
2975 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002976
Bram Moolenaarb765d632005-06-07 21:00:02 +00002977 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002978 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002979 {
2980 lp->sl_next = first_lang;
2981 first_lang = lp;
2982 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002983
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002984 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002985
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002986endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002987 if (lang != NULL)
2988 /* truncating the name signals the error to spell_load_lang() */
2989 *lang = NUL;
2990 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002991 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002992 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002993
2994endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002995 if (fd != NULL)
2996 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002997 sourcing_name = save_sourcing_name;
2998 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002999
3000 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003001}
3002
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003003/*
3004 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003005 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003006 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003007 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3008 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003009 */
3010 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003011read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003012 FILE *fd;
3013 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003014 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003015{
3016 int cnt = 0;
3017 int i;
3018 char_u *str;
3019
3020 /* read the length bytes, MSB first */
3021 for (i = 0; i < cnt_bytes; ++i)
3022 cnt = (cnt << 8) + getc(fd);
3023 if (cnt < 0)
3024 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003025 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003026 return NULL;
3027 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003028 *cntp = cnt;
3029 if (cnt == 0)
3030 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003031
Bram Moolenaar5195e452005-08-19 20:32:47 +00003032 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003033 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003034 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003035 return str;
3036}
3037
Bram Moolenaar7887d882005-07-01 22:33:52 +00003038/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003039 * Read SN_REGION: <regionname> ...
3040 * Return SP_*ERROR flags.
3041 */
3042 static int
3043read_region_section(fd, lp, len)
3044 FILE *fd;
3045 slang_T *lp;
3046 int len;
3047{
3048 int i;
3049
3050 if (len > 16)
3051 return SP_FORMERROR;
3052 for (i = 0; i < len; ++i)
3053 lp->sl_regions[i] = getc(fd); /* <regionname> */
3054 lp->sl_regions[len] = NUL;
3055 return 0;
3056}
3057
3058/*
3059 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3060 * <folcharslen> <folchars>
3061 * Return SP_*ERROR flags.
3062 */
3063 static int
3064read_charflags_section(fd)
3065 FILE *fd;
3066{
3067 char_u *flags;
3068 char_u *fol;
3069 int flagslen, follen;
3070
3071 /* <charflagslen> <charflags> */
3072 flags = read_cnt_string(fd, 1, &flagslen);
3073 if (flagslen < 0)
3074 return flagslen;
3075
3076 /* <folcharslen> <folchars> */
3077 fol = read_cnt_string(fd, 2, &follen);
3078 if (follen < 0)
3079 {
3080 vim_free(flags);
3081 return follen;
3082 }
3083
3084 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3085 if (flags != NULL && fol != NULL)
3086 set_spell_charflags(flags, flagslen, fol);
3087
3088 vim_free(flags);
3089 vim_free(fol);
3090
3091 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3092 if ((flags == NULL) != (fol == NULL))
3093 return SP_FORMERROR;
3094 return 0;
3095}
3096
3097/*
3098 * Read SN_PREFCOND section.
3099 * Return SP_*ERROR flags.
3100 */
3101 static int
3102read_prefcond_section(fd, lp)
3103 FILE *fd;
3104 slang_T *lp;
3105{
3106 int cnt;
3107 int i;
3108 int n;
3109 char_u *p;
3110 char_u buf[MAXWLEN + 1];
3111
3112 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003113 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003114 if (cnt <= 0)
3115 return SP_FORMERROR;
3116
3117 lp->sl_prefprog = (regprog_T **)alloc_clear(
3118 (unsigned)sizeof(regprog_T *) * cnt);
3119 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003120 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003121 lp->sl_prefixcnt = cnt;
3122
3123 for (i = 0; i < cnt; ++i)
3124 {
3125 /* <prefcond> : <condlen> <condstr> */
3126 n = getc(fd); /* <condlen> */
3127 if (n < 0 || n >= MAXWLEN)
3128 return SP_FORMERROR;
3129
3130 /* When <condlen> is zero we have an empty condition. Otherwise
3131 * compile the regexp program used to check for the condition. */
3132 if (n > 0)
3133 {
3134 buf[0] = '^'; /* always match at one position only */
3135 p = buf + 1;
3136 while (n-- > 0)
3137 *p++ = getc(fd); /* <condstr> */
3138 *p = NUL;
3139 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3140 }
3141 }
3142 return 0;
3143}
3144
3145/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003146 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003147 * Return SP_*ERROR flags.
3148 */
3149 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003150read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003151 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003152 garray_T *gap;
3153 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154{
3155 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003156 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003157 int i;
3158
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003159 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003160 if (cnt < 0)
3161 return SP_TRUNCERROR;
3162
Bram Moolenaar5195e452005-08-19 20:32:47 +00003163 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003164 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003165
3166 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3167 for (; gap->ga_len < cnt; ++gap->ga_len)
3168 {
3169 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3170 ftp->ft_from = read_cnt_string(fd, 1, &i);
3171 if (i < 0)
3172 return i;
3173 if (i == 0)
3174 return SP_FORMERROR;
3175 ftp->ft_to = read_cnt_string(fd, 1, &i);
3176 if (i <= 0)
3177 {
3178 vim_free(ftp->ft_from);
3179 if (i < 0)
3180 return i;
3181 return SP_FORMERROR;
3182 }
3183 }
3184
3185 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003186 for (i = 0; i < 256; ++i)
3187 first[i] = -1;
3188 for (i = 0; i < gap->ga_len; ++i)
3189 {
3190 ftp = &((fromto_T *)gap->ga_data)[i];
3191 if (first[*ftp->ft_from] == -1)
3192 first[*ftp->ft_from] = i;
3193 }
3194 return 0;
3195}
3196
3197/*
3198 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3199 * Return SP_*ERROR flags.
3200 */
3201 static int
3202read_sal_section(fd, slang)
3203 FILE *fd;
3204 slang_T *slang;
3205{
3206 int i;
3207 int cnt;
3208 garray_T *gap;
3209 salitem_T *smp;
3210 int ccnt;
3211 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003212 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003213
3214 slang->sl_sofo = FALSE;
3215
3216 i = getc(fd); /* <salflags> */
3217 if (i & SAL_F0LLOWUP)
3218 slang->sl_followup = TRUE;
3219 if (i & SAL_COLLAPSE)
3220 slang->sl_collapse = TRUE;
3221 if (i & SAL_REM_ACCENTS)
3222 slang->sl_rem_accents = TRUE;
3223
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003224 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003225 if (cnt < 0)
3226 return SP_TRUNCERROR;
3227
3228 gap = &slang->sl_sal;
3229 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003230 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003231 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003232
3233 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3234 for (; gap->ga_len < cnt; ++gap->ga_len)
3235 {
3236 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3237 ccnt = getc(fd); /* <salfromlen> */
3238 if (ccnt < 0)
3239 return SP_TRUNCERROR;
3240 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003241 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003242 smp->sm_lead = p;
3243
3244 /* Read up to the first special char into sm_lead. */
3245 for (i = 0; i < ccnt; ++i)
3246 {
3247 c = getc(fd); /* <salfrom> */
3248 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3249 break;
3250 *p++ = c;
3251 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003252 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003253 *p++ = NUL;
3254
3255 /* Put (abc) chars in sm_oneof, if any. */
3256 if (c == '(')
3257 {
3258 smp->sm_oneof = p;
3259 for (++i; i < ccnt; ++i)
3260 {
3261 c = getc(fd); /* <salfrom> */
3262 if (c == ')')
3263 break;
3264 *p++ = c;
3265 }
3266 *p++ = NUL;
3267 if (++i < ccnt)
3268 c = getc(fd);
3269 }
3270 else
3271 smp->sm_oneof = NULL;
3272
3273 /* Any following chars go in sm_rules. */
3274 smp->sm_rules = p;
3275 if (i < ccnt)
3276 /* store the char we got while checking for end of sm_lead */
3277 *p++ = c;
3278 for (++i; i < ccnt; ++i)
3279 *p++ = getc(fd); /* <salfrom> */
3280 *p++ = NUL;
3281
3282 /* <saltolen> <salto> */
3283 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3284 if (ccnt < 0)
3285 {
3286 vim_free(smp->sm_lead);
3287 return ccnt;
3288 }
3289
3290#ifdef FEAT_MBYTE
3291 if (has_mbyte)
3292 {
3293 /* convert the multi-byte strings to wide char strings */
3294 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3295 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3296 if (smp->sm_oneof == NULL)
3297 smp->sm_oneof_w = NULL;
3298 else
3299 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3300 if (smp->sm_to == NULL)
3301 smp->sm_to_w = NULL;
3302 else
3303 smp->sm_to_w = mb_str2wide(smp->sm_to);
3304 if (smp->sm_lead_w == NULL
3305 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3306 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3307 {
3308 vim_free(smp->sm_lead);
3309 vim_free(smp->sm_to);
3310 vim_free(smp->sm_lead_w);
3311 vim_free(smp->sm_oneof_w);
3312 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003313 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003314 }
3315 }
3316#endif
3317 }
3318
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003319 if (gap->ga_len > 0)
3320 {
3321 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3322 * that we need to check the index every time. */
3323 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3324 if ((p = alloc(1)) == NULL)
3325 return SP_OTHERERROR;
3326 p[0] = NUL;
3327 smp->sm_lead = p;
3328 smp->sm_leadlen = 0;
3329 smp->sm_oneof = NULL;
3330 smp->sm_rules = p;
3331 smp->sm_to = NULL;
3332#ifdef FEAT_MBYTE
3333 if (has_mbyte)
3334 {
3335 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3336 smp->sm_leadlen = 0;
3337 smp->sm_oneof_w = NULL;
3338 smp->sm_to_w = NULL;
3339 }
3340#endif
3341 ++gap->ga_len;
3342 }
3343
Bram Moolenaar5195e452005-08-19 20:32:47 +00003344 /* Fill the first-index table. */
3345 set_sal_first(slang);
3346
3347 return 0;
3348}
3349
3350/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003351 * Read SN_WORDS: <word> ...
3352 * Return SP_*ERROR flags.
3353 */
3354 static int
3355read_words_section(fd, lp, len)
3356 FILE *fd;
3357 slang_T *lp;
3358 int len;
3359{
3360 int done = 0;
3361 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003362 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003363 char_u word[MAXWLEN];
3364
3365 while (done < len)
3366 {
3367 /* Read one word at a time. */
3368 for (i = 0; ; ++i)
3369 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003370 c = getc(fd);
3371 if (c == EOF)
3372 return SP_TRUNCERROR;
3373 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003374 if (word[i] == NUL)
3375 break;
3376 if (i == MAXWLEN - 1)
3377 return SP_FORMERROR;
3378 }
3379
3380 /* Init the count to 10. */
3381 count_common_word(lp, word, -1, 10);
3382 done += i + 1;
3383 }
3384 return 0;
3385}
3386
3387/*
3388 * Add a word to the hashtable of common words.
3389 * If it's already there then the counter is increased.
3390 */
3391 static void
3392count_common_word(lp, word, len, count)
3393 slang_T *lp;
3394 char_u *word;
3395 int len; /* word length, -1 for upto NUL */
3396 int count; /* 1 to count once, 10 to init */
3397{
3398 hash_T hash;
3399 hashitem_T *hi;
3400 wordcount_T *wc;
3401 char_u buf[MAXWLEN];
3402 char_u *p;
3403
3404 if (len == -1)
3405 p = word;
3406 else
3407 {
3408 vim_strncpy(buf, word, len);
3409 p = buf;
3410 }
3411
3412 hash = hash_hash(p);
3413 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3414 if (HASHITEM_EMPTY(hi))
3415 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003416 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003417 if (wc == NULL)
3418 return;
3419 STRCPY(wc->wc_word, p);
3420 wc->wc_count = count;
3421 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3422 }
3423 else
3424 {
3425 wc = HI2WC(hi);
3426 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3427 wc->wc_count = MAXWORDCOUNT;
3428 }
3429}
3430
3431/*
3432 * Adjust the score of common words.
3433 */
3434 static int
3435score_wordcount_adj(slang, score, word, split)
3436 slang_T *slang;
3437 int score;
3438 char_u *word;
3439 int split; /* word was split, less bonus */
3440{
3441 hashitem_T *hi;
3442 wordcount_T *wc;
3443 int bonus;
3444 int newscore;
3445
3446 hi = hash_find(&slang->sl_wordcount, word);
3447 if (!HASHITEM_EMPTY(hi))
3448 {
3449 wc = HI2WC(hi);
3450 if (wc->wc_count < SCORE_THRES2)
3451 bonus = SCORE_COMMON1;
3452 else if (wc->wc_count < SCORE_THRES3)
3453 bonus = SCORE_COMMON2;
3454 else
3455 bonus = SCORE_COMMON3;
3456 if (split)
3457 newscore = score - bonus / 2;
3458 else
3459 newscore = score - bonus;
3460 if (newscore < 0)
3461 return 0;
3462 return newscore;
3463 }
3464 return score;
3465}
3466
3467/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003468 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3469 * Return SP_*ERROR flags.
3470 */
3471 static int
3472read_sofo_section(fd, slang)
3473 FILE *fd;
3474 slang_T *slang;
3475{
3476 int cnt;
3477 char_u *from, *to;
3478 int res;
3479
3480 slang->sl_sofo = TRUE;
3481
3482 /* <sofofromlen> <sofofrom> */
3483 from = read_cnt_string(fd, 2, &cnt);
3484 if (cnt < 0)
3485 return cnt;
3486
3487 /* <sofotolen> <sofoto> */
3488 to = read_cnt_string(fd, 2, &cnt);
3489 if (cnt < 0)
3490 {
3491 vim_free(from);
3492 return cnt;
3493 }
3494
3495 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3496 if (from != NULL && to != NULL)
3497 res = set_sofo(slang, from, to);
3498 else if (from != NULL || to != NULL)
3499 res = SP_FORMERROR; /* only one of two strings is an error */
3500 else
3501 res = 0;
3502
3503 vim_free(from);
3504 vim_free(to);
3505 return res;
3506}
3507
3508/*
3509 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003510 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003511 * Returns SP_*ERROR flags.
3512 */
3513 static int
3514read_compound(fd, slang, len)
3515 FILE *fd;
3516 slang_T *slang;
3517 int len;
3518{
3519 int todo = len;
3520 int c;
3521 int atstart;
3522 char_u *pat;
3523 char_u *pp;
3524 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003525 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003526 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003527 int cnt;
3528 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003529
3530 if (todo < 2)
3531 return SP_FORMERROR; /* need at least two bytes */
3532
3533 --todo;
3534 c = getc(fd); /* <compmax> */
3535 if (c < 2)
3536 c = MAXWLEN;
3537 slang->sl_compmax = c;
3538
3539 --todo;
3540 c = getc(fd); /* <compminlen> */
3541 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003542 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003543 slang->sl_compminlen = c;
3544
3545 --todo;
3546 c = getc(fd); /* <compsylmax> */
3547 if (c < 1)
3548 c = MAXWLEN;
3549 slang->sl_compsylmax = c;
3550
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003551 c = getc(fd); /* <compoptions> */
3552 if (c != 0)
3553 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3554 else
3555 {
3556 --todo;
3557 c = getc(fd); /* only use the lower byte for now */
3558 --todo;
3559 slang->sl_compoptions = c;
3560
3561 gap = &slang->sl_comppat;
3562 c = get2c(fd); /* <comppatcount> */
3563 todo -= 2;
3564 ga_init2(gap, sizeof(char_u *), c);
3565 if (ga_grow(gap, c) == OK)
3566 while (--c >= 0)
3567 {
3568 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3569 read_cnt_string(fd, 1, &cnt);
3570 /* <comppatlen> <comppattext> */
3571 if (cnt < 0)
3572 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003573 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003574 }
3575 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003576 if (todo < 0)
3577 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003578
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003579 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003580 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003581 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3582 * Conversion to utf-8 may double the size. */
3583 c = todo * 2 + 7;
3584#ifdef FEAT_MBYTE
3585 if (enc_utf8)
3586 c += todo * 2;
3587#endif
3588 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003589 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003590 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003591
Bram Moolenaard12a1322005-08-21 22:08:24 +00003592 /* We also need a list of all flags that can appear at the start and one
3593 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003594 cp = alloc(todo + 1);
3595 if (cp == NULL)
3596 {
3597 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003598 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003599 }
3600 slang->sl_compstartflags = cp;
3601 *cp = NUL;
3602
Bram Moolenaard12a1322005-08-21 22:08:24 +00003603 ap = alloc(todo + 1);
3604 if (ap == NULL)
3605 {
3606 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003607 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003608 }
3609 slang->sl_compallflags = ap;
3610 *ap = NUL;
3611
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003612 /* And a list of all patterns in their original form, for checking whether
3613 * compounding may work in match_compoundrule(). This is freed when we
3614 * encounter a wildcard, the check doesn't work then. */
3615 crp = alloc(todo + 1);
3616 slang->sl_comprules = crp;
3617
Bram Moolenaar5195e452005-08-19 20:32:47 +00003618 pp = pat;
3619 *pp++ = '^';
3620 *pp++ = '\\';
3621 *pp++ = '(';
3622
3623 atstart = 1;
3624 while (todo-- > 0)
3625 {
3626 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003627 if (c == EOF)
3628 {
3629 vim_free(pat);
3630 return SP_TRUNCERROR;
3631 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003632
3633 /* Add all flags to "sl_compallflags". */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003634 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003635 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003636 {
3637 *ap++ = c;
3638 *ap = NUL;
3639 }
3640
Bram Moolenaar5195e452005-08-19 20:32:47 +00003641 if (atstart != 0)
3642 {
3643 /* At start of item: copy flags to "sl_compstartflags". For a
3644 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3645 if (c == '[')
3646 atstart = 2;
3647 else if (c == ']')
3648 atstart = 0;
3649 else
3650 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003651 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003652 {
3653 *cp++ = c;
3654 *cp = NUL;
3655 }
3656 if (atstart == 1)
3657 atstart = 0;
3658 }
3659 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003660
3661 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3662 if (crp != NULL)
3663 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003664 if (c == '?' || c == '+' || c == '*')
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003665 {
3666 vim_free(slang->sl_comprules);
3667 slang->sl_comprules = NULL;
3668 crp = NULL;
3669 }
3670 else
3671 *crp++ = c;
3672 }
3673
Bram Moolenaar5195e452005-08-19 20:32:47 +00003674 if (c == '/') /* slash separates two items */
3675 {
3676 *pp++ = '\\';
3677 *pp++ = '|';
3678 atstart = 1;
3679 }
3680 else /* normal char, "[abc]" and '*' are copied as-is */
3681 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003682 if (c == '?' || c == '+' || c == '~')
3683 *pp++ = '\\'; /* "a?" becomes "a\?", "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003684#ifdef FEAT_MBYTE
3685 if (enc_utf8)
3686 pp += mb_char2bytes(c, pp);
3687 else
3688#endif
3689 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003690 }
3691 }
3692
3693 *pp++ = '\\';
3694 *pp++ = ')';
3695 *pp++ = '$';
3696 *pp = NUL;
3697
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003698 if (crp != NULL)
3699 *crp = NUL;
3700
Bram Moolenaar5195e452005-08-19 20:32:47 +00003701 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3702 vim_free(pat);
3703 if (slang->sl_compprog == NULL)
3704 return SP_FORMERROR;
3705
3706 return 0;
3707}
3708
Bram Moolenaar6de68532005-08-24 22:08:48 +00003709/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003710 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003711 * Like strchr() but independent of locale.
3712 */
3713 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003714byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003715 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003716 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003717{
3718 char_u *p;
3719
3720 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003721 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003722 return TRUE;
3723 return FALSE;
3724}
3725
Bram Moolenaar5195e452005-08-19 20:32:47 +00003726#define SY_MAXLEN 30
3727typedef struct syl_item_S
3728{
3729 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3730 int sy_len;
3731} syl_item_T;
3732
3733/*
3734 * Truncate "slang->sl_syllable" at the first slash and put the following items
3735 * in "slang->sl_syl_items".
3736 */
3737 static int
3738init_syl_tab(slang)
3739 slang_T *slang;
3740{
3741 char_u *p;
3742 char_u *s;
3743 int l;
3744 syl_item_T *syl;
3745
3746 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3747 p = vim_strchr(slang->sl_syllable, '/');
3748 while (p != NULL)
3749 {
3750 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003751 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003752 break;
3753 s = p;
3754 p = vim_strchr(p, '/');
3755 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003756 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003757 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003758 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003759 if (l >= SY_MAXLEN)
3760 return SP_FORMERROR;
3761 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003762 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003763 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3764 + slang->sl_syl_items.ga_len++;
3765 vim_strncpy(syl->sy_chars, s, l);
3766 syl->sy_len = l;
3767 }
3768 return OK;
3769}
3770
3771/*
3772 * Count the number of syllables in "word".
3773 * When "word" contains spaces the syllables after the last space are counted.
3774 * Returns zero if syllables are not defines.
3775 */
3776 static int
3777count_syllables(slang, word)
3778 slang_T *slang;
3779 char_u *word;
3780{
3781 int cnt = 0;
3782 int skip = FALSE;
3783 char_u *p;
3784 int len;
3785 int i;
3786 syl_item_T *syl;
3787 int c;
3788
3789 if (slang->sl_syllable == NULL)
3790 return 0;
3791
3792 for (p = word; *p != NUL; p += len)
3793 {
3794 /* When running into a space reset counter. */
3795 if (*p == ' ')
3796 {
3797 len = 1;
3798 cnt = 0;
3799 continue;
3800 }
3801
3802 /* Find longest match of syllable items. */
3803 len = 0;
3804 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3805 {
3806 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3807 if (syl->sy_len > len
3808 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3809 len = syl->sy_len;
3810 }
3811 if (len != 0) /* found a match, count syllable */
3812 {
3813 ++cnt;
3814 skip = FALSE;
3815 }
3816 else
3817 {
3818 /* No recognized syllable item, at least a syllable char then? */
3819#ifdef FEAT_MBYTE
3820 c = mb_ptr2char(p);
3821 len = (*mb_ptr2len)(p);
3822#else
3823 c = *p;
3824 len = 1;
3825#endif
3826 if (vim_strchr(slang->sl_syllable, c) == NULL)
3827 skip = FALSE; /* No, search for next syllable */
3828 else if (!skip)
3829 {
3830 ++cnt; /* Yes, count it */
3831 skip = TRUE; /* don't count following syllable chars */
3832 }
3833 }
3834 }
3835 return cnt;
3836}
3837
3838/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003839 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003840 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003841 */
3842 static int
3843set_sofo(lp, from, to)
3844 slang_T *lp;
3845 char_u *from;
3846 char_u *to;
3847{
3848 int i;
3849
3850#ifdef FEAT_MBYTE
3851 garray_T *gap;
3852 char_u *s;
3853 char_u *p;
3854 int c;
3855 int *inp;
3856
3857 if (has_mbyte)
3858 {
3859 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3860 * characters. The index is the low byte of the character.
3861 * The list contains from-to pairs with a terminating NUL.
3862 * sl_sal_first[] is used for latin1 "from" characters. */
3863 gap = &lp->sl_sal;
3864 ga_init2(gap, sizeof(int *), 1);
3865 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003866 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003867 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3868 gap->ga_len = 256;
3869
3870 /* First count the number of items for each list. Temporarily use
3871 * sl_sal_first[] for this. */
3872 for (p = from, s = to; *p != NUL && *s != NUL; )
3873 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003874 c = mb_cptr2char_adv(&p);
3875 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003876 if (c >= 256)
3877 ++lp->sl_sal_first[c & 0xff];
3878 }
3879 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003880 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003881
3882 /* Allocate the lists. */
3883 for (i = 0; i < 256; ++i)
3884 if (lp->sl_sal_first[i] > 0)
3885 {
3886 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3887 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003888 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003889 ((int **)gap->ga_data)[i] = (int *)p;
3890 *(int *)p = 0;
3891 }
3892
3893 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3894 * list. */
3895 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3896 for (p = from, s = to; *p != NUL && *s != NUL; )
3897 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003898 c = mb_cptr2char_adv(&p);
3899 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003900 if (c >= 256)
3901 {
3902 /* Append the from-to chars at the end of the list with
3903 * the low byte. */
3904 inp = ((int **)gap->ga_data)[c & 0xff];
3905 while (*inp != 0)
3906 ++inp;
3907 *inp++ = c; /* from char */
3908 *inp++ = i; /* to char */
3909 *inp++ = NUL; /* NUL at the end */
3910 }
3911 else
3912 /* mapping byte to char is done in sl_sal_first[] */
3913 lp->sl_sal_first[c] = i;
3914 }
3915 }
3916 else
3917#endif
3918 {
3919 /* mapping bytes to bytes is done in sl_sal_first[] */
3920 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003921 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003922
3923 for (i = 0; to[i] != NUL; ++i)
3924 lp->sl_sal_first[from[i]] = to[i];
3925 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3926 }
3927
Bram Moolenaar5195e452005-08-19 20:32:47 +00003928 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003929}
3930
3931/*
3932 * Fill the first-index table for "lp".
3933 */
3934 static void
3935set_sal_first(lp)
3936 slang_T *lp;
3937{
3938 salfirst_T *sfirst;
3939 int i;
3940 salitem_T *smp;
3941 int c;
3942 garray_T *gap = &lp->sl_sal;
3943
3944 sfirst = lp->sl_sal_first;
3945 for (i = 0; i < 256; ++i)
3946 sfirst[i] = -1;
3947 smp = (salitem_T *)gap->ga_data;
3948 for (i = 0; i < gap->ga_len; ++i)
3949 {
3950#ifdef FEAT_MBYTE
3951 if (has_mbyte)
3952 /* Use the lowest byte of the first character. For latin1 it's
3953 * the character, for other encodings it should differ for most
3954 * characters. */
3955 c = *smp[i].sm_lead_w & 0xff;
3956 else
3957#endif
3958 c = *smp[i].sm_lead;
3959 if (sfirst[c] == -1)
3960 {
3961 sfirst[c] = i;
3962#ifdef FEAT_MBYTE
3963 if (has_mbyte)
3964 {
3965 int n;
3966
3967 /* Make sure all entries with this byte are following each
3968 * other. Move the ones that are in the wrong position. Do
3969 * keep the same ordering! */
3970 while (i + 1 < gap->ga_len
3971 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3972 /* Skip over entry with same index byte. */
3973 ++i;
3974
3975 for (n = 1; i + n < gap->ga_len; ++n)
3976 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3977 {
3978 salitem_T tsal;
3979
3980 /* Move entry with same index byte after the entries
3981 * we already found. */
3982 ++i;
3983 --n;
3984 tsal = smp[i + n];
3985 mch_memmove(smp + i + 1, smp + i,
3986 sizeof(salitem_T) * n);
3987 smp[i] = tsal;
3988 }
3989 }
3990#endif
3991 }
3992 }
3993}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003994
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003995#ifdef FEAT_MBYTE
3996/*
3997 * Turn a multi-byte string into a wide character string.
3998 * Return it in allocated memory (NULL for out-of-memory)
3999 */
4000 static int *
4001mb_str2wide(s)
4002 char_u *s;
4003{
4004 int *res;
4005 char_u *p;
4006 int i = 0;
4007
4008 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
4009 if (res != NULL)
4010 {
4011 for (p = s; *p != NUL; )
4012 res[i++] = mb_ptr2char_adv(&p);
4013 res[i] = NUL;
4014 }
4015 return res;
4016}
4017#endif
4018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004019/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004020 * Read a tree from the .spl or .sug file.
4021 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4022 * This is skipped when the tree has zero length.
4023 * Returns zero when OK, SP_ value for an error.
4024 */
4025 static int
4026spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4027 FILE *fd;
4028 char_u **bytsp;
4029 idx_T **idxsp;
4030 int prefixtree; /* TRUE for the prefix tree */
4031 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4032{
4033 int len;
4034 int idx;
4035 char_u *bp;
4036 idx_T *ip;
4037
4038 /* The tree size was computed when writing the file, so that we can
4039 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004040 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004041 if (len < 0)
4042 return SP_TRUNCERROR;
4043 if (len > 0)
4044 {
4045 /* Allocate the byte array. */
4046 bp = lalloc((long_u)len, TRUE);
4047 if (bp == NULL)
4048 return SP_OTHERERROR;
4049 *bytsp = bp;
4050
4051 /* Allocate the index array. */
4052 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4053 if (ip == NULL)
4054 return SP_OTHERERROR;
4055 *idxsp = ip;
4056
4057 /* Recursively read the tree and store it in the array. */
4058 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4059 if (idx < 0)
4060 return idx;
4061 }
4062 return 0;
4063}
4064
4065/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004066 * Read one row of siblings from the spell file and store it in the byte array
4067 * "byts" and index array "idxs". Recursively read the children.
4068 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004069 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004070 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004071 * Returns the index (>= 0) following the siblings.
4072 * Returns SP_TRUNCERROR if the file is shorter than expected.
4073 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004075 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004076read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077 FILE *fd;
4078 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004079 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004081 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004082 int prefixtree; /* TRUE for reading PREFIXTREE */
4083 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004085 int len;
4086 int i;
4087 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004088 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004089 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004090 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004091#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092
Bram Moolenaar51485f02005-06-04 21:55:20 +00004093 len = getc(fd); /* <siblingcount> */
4094 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004095 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004096
4097 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004098 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004099 byts[idx++] = len;
4100
4101 /* Read the byte values, flag/region bytes and shared indexes. */
4102 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004103 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004104 c = getc(fd); /* <byte> */
4105 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004106 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004107 if (c <= BY_SPECIAL)
4108 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004109 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004110 {
4111 /* No flags, all regions. */
4112 idxs[idx] = 0;
4113 c = 0;
4114 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004115 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004116 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004117 if (prefixtree)
4118 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004119 /* Read the optional pflags byte, the prefix ID and the
4120 * condition nr. In idxs[] store the prefix ID in the low
4121 * byte, the condition index shifted up 8 bits, the flags
4122 * shifted up 24 bits. */
4123 if (c == BY_FLAGS)
4124 c = getc(fd) << 24; /* <pflags> */
4125 else
4126 c = 0;
4127
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004128 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004129
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004130 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004131 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004132 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004133 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004134 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004135 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004136 {
4137 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004138 * idxs[] the flags go in the low two bytes, region above
4139 * that and prefix ID above the region. */
4140 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004141 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004142 if (c2 == BY_FLAGS2)
4143 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004144 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004145 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004146 if (c & WF_AFX)
4147 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004148 }
4149
Bram Moolenaar51485f02005-06-04 21:55:20 +00004150 idxs[idx] = c;
4151 c = 0;
4152 }
4153 else /* c == BY_INDEX */
4154 {
4155 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004156 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004158 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004159 idxs[idx] = n + SHARED_MASK;
4160 c = getc(fd); /* <xbyte> */
4161 }
4162 }
4163 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004164 }
4165
Bram Moolenaar51485f02005-06-04 21:55:20 +00004166 /* Recursively read the children for non-shared siblings.
4167 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4168 * remove SHARED_MASK) */
4169 for (i = 1; i <= len; ++i)
4170 if (byts[startidx + i] != 0)
4171 {
4172 if (idxs[startidx + i] & SHARED_MASK)
4173 idxs[startidx + i] &= ~SHARED_MASK;
4174 else
4175 {
4176 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004177 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004178 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004179 if (idx < 0)
4180 break;
4181 }
4182 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004183
Bram Moolenaar51485f02005-06-04 21:55:20 +00004184 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004185}
4186
4187/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004188 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004189 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004190 */
4191 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004192did_set_spelllang(wp)
4193 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004194{
4195 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004196 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004198 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004199 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004201 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004202 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004203 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004205 int len;
4206 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004207 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004208 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004209 char_u *use_region = NULL;
4210 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004211 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004212 int i, j;
4213 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004214 static int recursive = FALSE;
4215 char_u *ret_msg = NULL;
4216 char_u *spl_copy;
4217
4218 /* We don't want to do this recursively. May happen when a language is
4219 * not available and the SpellFileMissing autocommand opens a new buffer
4220 * in which 'spell' is set. */
4221 if (recursive)
4222 return NULL;
4223 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004224
4225 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004226 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004228 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004229 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004230 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004231 if (spl_copy == NULL)
4232 goto theend;
4233
Bram Moolenaar2593e032013-11-14 03:54:07 +01004234#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004235 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004236#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004237
4238 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004239 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004240 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004241 /* Get one language name. */
4242 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004243 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004244 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004245
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004246 if (STRCMP(lang, "cjk") == 0)
4247 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01004248#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004249 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004250#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004251 continue;
4252 }
4253
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004254 /* If the name ends in ".spl" use it as the name of the spell file.
4255 * If there is a region name let "region" point to it and remove it
4256 * from the name. */
4257 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4258 {
4259 filename = TRUE;
4260
Bram Moolenaarb6356332005-07-18 21:40:44 +00004261 /* Locate a region and remove it from the file name. */
4262 p = vim_strchr(gettail(lang), '_');
4263 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4264 && !ASCII_ISALPHA(p[3]))
4265 {
4266 vim_strncpy(region_cp, p + 1, 2);
4267 mch_memmove(p, p + 3, len - (p - lang) - 2);
4268 len -= 3;
4269 region = region_cp;
4270 }
4271 else
4272 dont_use_region = TRUE;
4273
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004274 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004275 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4276 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004277 break;
4278 }
4279 else
4280 {
4281 filename = FALSE;
4282 if (len > 3 && lang[len - 3] == '_')
4283 {
4284 region = lang + len - 2;
4285 len -= 3;
4286 lang[len] = NUL;
4287 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004288 else
4289 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004290
4291 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004292 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4293 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004294 break;
4295 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004296
Bram Moolenaarb6356332005-07-18 21:40:44 +00004297 if (region != NULL)
4298 {
4299 /* If the region differs from what was used before then don't
4300 * use it for 'spellfile'. */
4301 if (use_region != NULL && STRCMP(region, use_region) != 0)
4302 dont_use_region = TRUE;
4303 use_region = region;
4304 }
4305
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004306 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004308 {
4309 if (filename)
4310 (void)spell_load_file(lang, lang, NULL, FALSE);
4311 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004312 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004313 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004314#ifdef FEAT_AUTOCMD
4315 /* SpellFileMissing autocommands may do anything, including
4316 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004317 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004318 {
4319 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4320 goto theend;
4321 }
4322#endif
4323 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004324 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004326 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004327 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004328 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004329 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4330 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4331 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004332 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004333 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004334 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004335 {
4336 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004337 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004338 if (c == REGION_ALL)
4339 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004340 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004341 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004343 /* This addition file is for other regions. */
4344 region_mask = 0;
4345 }
4346 else
4347 /* This is probably an error. Give a warning and
4348 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004349 smsg((char_u *)
4350 _("Warning: region %s not supported"),
4351 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004352 }
4353 else
4354 region_mask = 1 << c;
4355 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004356
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004357 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004358 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004359 if (ga_grow(&ga, 1) == FAIL)
4360 {
4361 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004362 ret_msg = e_outofmem;
4363 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004364 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004365 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004366 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4367 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004368 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004369 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004370 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004371 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004372 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004373 }
4374
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004375 /* round 0: load int_wordlist, if possible.
4376 * round 1: load first name in 'spellfile'.
4377 * round 2: load second name in 'spellfile.
4378 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004379 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004380 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004382 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004383 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004384 /* Internal wordlist, if there is one. */
4385 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004386 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004387 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004388 }
4389 else
4390 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004391 /* One entry in 'spellfile'. */
4392 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4393 STRCAT(spf_name, ".spl");
4394
4395 /* If it was already found above then skip it. */
4396 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004397 {
4398 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4399 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004400 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004401 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004402 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004403 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004404 }
4405
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004406 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4408 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004412 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004413 * region name, the region is ignored otherwise. for int_wordlist
4414 * use an arbitrary name. */
4415 if (round == 0)
4416 STRCPY(lang, "internal wordlist");
4417 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004418 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004419 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004420 p = vim_strchr(lang, '.');
4421 if (p != NULL)
4422 *p = NUL; /* truncate at ".encoding.add" */
4423 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004424 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004425
4426 /* If one of the languages has NOBREAK we assume the addition
4427 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004428 if (slang != NULL && nobreak)
4429 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004430 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004432 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004433 region_mask = REGION_ALL;
4434 if (use_region != NULL && !dont_use_region)
4435 {
4436 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004437 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004438 if (c != REGION_ALL)
4439 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004440 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004441 /* This spell file is for other regions. */
4442 region_mask = 0;
4443 }
4444
4445 if (region_mask != 0)
4446 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004447 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4448 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4449 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004450 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4451 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004452 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004453 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004454 }
4455 }
4456
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004457 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004458 ga_clear(&wp->w_s->b_langp);
4459 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004460
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004461 /* For each language figure out what language to use for sound folding and
4462 * REP items. If the language doesn't support it itself use another one
4463 * with the same name. E.g. for "en-math" use "en". */
4464 for (i = 0; i < ga.ga_len; ++i)
4465 {
4466 lp = LANGP_ENTRY(ga, i);
4467
4468 /* sound folding */
4469 if (lp->lp_slang->sl_sal.ga_len > 0)
4470 /* language does sound folding itself */
4471 lp->lp_sallang = lp->lp_slang;
4472 else
4473 /* find first similar language that does sound folding */
4474 for (j = 0; j < ga.ga_len; ++j)
4475 {
4476 lp2 = LANGP_ENTRY(ga, j);
4477 if (lp2->lp_slang->sl_sal.ga_len > 0
4478 && STRNCMP(lp->lp_slang->sl_name,
4479 lp2->lp_slang->sl_name, 2) == 0)
4480 {
4481 lp->lp_sallang = lp2->lp_slang;
4482 break;
4483 }
4484 }
4485
4486 /* REP items */
4487 if (lp->lp_slang->sl_rep.ga_len > 0)
4488 /* language has REP items itself */
4489 lp->lp_replang = lp->lp_slang;
4490 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004491 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004492 for (j = 0; j < ga.ga_len; ++j)
4493 {
4494 lp2 = LANGP_ENTRY(ga, j);
4495 if (lp2->lp_slang->sl_rep.ga_len > 0
4496 && STRNCMP(lp->lp_slang->sl_name,
4497 lp2->lp_slang->sl_name, 2) == 0)
4498 {
4499 lp->lp_replang = lp2->lp_slang;
4500 break;
4501 }
4502 }
4503 }
4504
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004505theend:
4506 vim_free(spl_copy);
4507 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02004508 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004509 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004510}
4511
4512/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004513 * Clear the midword characters for buffer "buf".
4514 */
4515 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004516clear_midword(wp)
4517 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004518{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004520#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004521 vim_free(wp->w_s->b_spell_ismw_mb);
4522 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004523#endif
4524}
4525
4526/*
4527 * Use the "sl_midword" field of language "lp" for buffer "buf".
4528 * They add up to any currently used midword characters.
4529 */
4530 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004531use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004532 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004533 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004534{
4535 char_u *p;
4536
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004537 if (lp->sl_midword == NULL) /* there aren't any */
4538 return;
4539
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004540 for (p = lp->sl_midword; *p != NUL; )
4541#ifdef FEAT_MBYTE
4542 if (has_mbyte)
4543 {
4544 int c, l, n;
4545 char_u *bp;
4546
4547 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004548 l = (*mb_ptr2len)(p);
4549 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004550 wp->w_s->b_spell_ismw[c] = TRUE;
4551 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004552 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004553 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004554 else
4555 {
4556 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004557 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4558 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004559 if (bp != NULL)
4560 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004561 vim_free(wp->w_s->b_spell_ismw_mb);
4562 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004563 vim_strncpy(bp + n, p, l);
4564 }
4565 }
4566 p += l;
4567 }
4568 else
4569#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004570 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004571}
4572
4573/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004574 * Find the region "region[2]" in "rp" (points to "sl_regions").
4575 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004576 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 */
4578 static int
4579find_region(rp, region)
4580 char_u *rp;
4581 char_u *region;
4582{
4583 int i;
4584
4585 for (i = 0; ; i += 2)
4586 {
4587 if (rp[i] == NUL)
4588 return REGION_ALL;
4589 if (rp[i] == region[0] && rp[i + 1] == region[1])
4590 break;
4591 }
4592 return i / 2;
4593}
4594
4595/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004596 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004597 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004598 * Word WF_ONECAP
4599 * W WORD WF_ALLCAP
4600 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004601 */
4602 static int
4603captype(word, end)
4604 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606{
4607 char_u *p;
4608 int c;
4609 int firstcap;
4610 int allcap;
4611 int past_second = FALSE; /* past second word char */
4612
4613 /* find first letter */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004614 for (p = word; !spell_iswordp_nmw(p, curwin); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004615 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004616 return 0; /* only non-word characters, illegal word */
4617#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004618 if (has_mbyte)
4619 c = mb_ptr2char_adv(&p);
4620 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004621#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004622 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004623 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624
4625 /*
4626 * Need to check all letters to find a word with mixed upper/lower.
4627 * But a word with an upper char only at start is a ONECAP.
4628 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004630 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004631 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004632 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004633 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634 {
4635 /* UUl -> KEEPCAP */
4636 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004637 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004638 allcap = FALSE;
4639 }
4640 else if (!allcap)
4641 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004642 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004643 past_second = TRUE;
4644 }
4645
4646 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004647 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004648 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004649 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004650 return 0;
4651}
4652
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004653/*
4654 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4655 * capital. So that make_case_word() can turn WOrd into Word.
4656 * Add ALLCAP for "WOrD".
4657 */
4658 static int
4659badword_captype(word, end)
4660 char_u *word;
4661 char_u *end;
4662{
4663 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004664 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004665 int l, u;
4666 int first;
4667 char_u *p;
4668
4669 if (flags & WF_KEEPCAP)
4670 {
4671 /* Count the number of UPPER and lower case letters. */
4672 l = u = 0;
4673 first = FALSE;
4674 for (p = word; p < end; mb_ptr_adv(p))
4675 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004676 c = PTR2CHAR(p);
4677 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004678 {
4679 ++u;
4680 if (p == word)
4681 first = TRUE;
4682 }
4683 else
4684 ++l;
4685 }
4686
4687 /* If there are more UPPER than lower case letters suggest an
4688 * ALLCAP word. Otherwise, if the first letter is UPPER then
4689 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4690 * require three upper case letters. */
4691 if (u > l && u > 2)
4692 flags |= WF_ALLCAP;
4693 else if (first)
4694 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004695
4696 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4697 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004698 }
4699 return flags;
4700}
4701
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004702/*
4703 * Delete the internal wordlist and its .spl file.
4704 */
4705 void
4706spell_delete_wordlist()
4707{
4708 char_u fname[MAXPATHL];
4709
4710 if (int_wordlist != NULL)
4711 {
4712 mch_remove(int_wordlist);
4713 int_wordlist_spl(fname);
4714 mch_remove(fname);
4715 vim_free(int_wordlist);
4716 int_wordlist = NULL;
4717 }
4718}
4719
4720#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004721/*
4722 * Free all languages.
4723 */
4724 void
4725spell_free_all()
4726{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004727 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004728 buf_T *buf;
4729
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004730 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004731 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004732 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004733
4734 while (first_lang != NULL)
4735 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004736 slang = first_lang;
4737 first_lang = slang->sl_next;
4738 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004739 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004740
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004741 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00004742
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004743 vim_free(repl_to);
4744 repl_to = NULL;
4745 vim_free(repl_from);
4746 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004747}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004748#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004749
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004750#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004751/*
4752 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004753 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004754 */
4755 void
4756spell_reload()
4757{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004758 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004759
Bram Moolenaarea408852005-06-25 22:49:46 +00004760 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004761 init_spell_chartab();
4762
4763 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004764 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004765
4766 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004767 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004769 /* Only load the wordlists when 'spelllang' is set and there is a
4770 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004771 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004772 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004773 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004774 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004775 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004776# ifdef FEAT_WINDOWS
4777 break;
4778# endif
4779 }
4780 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004781 }
4782}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004783#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004784
Bram Moolenaarb765d632005-06-07 21:00:02 +00004785/*
4786 * Reload the spell file "fname" if it's loaded.
4787 */
4788 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004790 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004792{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004793 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004795
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004796 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004797 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004798 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004799 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004800 slang_clear(slang);
4801 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004802 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004803 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004804 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004806 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808
4809 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004810 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004812 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004813}
4814
4815
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004816/*
4817 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004818 */
4819
Bram Moolenaar51485f02005-06-04 21:55:20 +00004820#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 and .dic file. */
4822/*
4823 * Main structure to store the contents of a ".aff" file.
4824 */
4825typedef struct afffile_S
4826{
4827 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004828 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004829 unsigned af_rare; /* RARE ID for rare word */
4830 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004831 unsigned af_bad; /* BAD ID for banned word */
4832 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004833 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004834 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004835 unsigned af_comproot; /* COMPOUNDROOT ID */
4836 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4837 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004838 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004839 int af_pfxpostpone; /* postpone prefixes without chop string and
4840 without flags */
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02004841 int af_ignoreextra; /* IGNOREEXTRA present */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4843 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004844 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004845} afffile_T;
4846
Bram Moolenaar6de68532005-08-24 22:08:48 +00004847#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004848#define AFT_LONG 1 /* flags are two characters */
4849#define AFT_CAPLONG 2 /* flags are one or two characters */
4850#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004851
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004852typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004853/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4854struct affentry_S
4855{
4856 affentry_T *ae_next; /* next affix with same name/number */
4857 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4858 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004859 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004860 char_u *ae_cond; /* condition (NULL for ".") */
4861 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004862 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4863 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864};
4865
Bram Moolenaar6de68532005-08-24 22:08:48 +00004866#ifdef FEAT_MBYTE
4867# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4868#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004869# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004870#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004871
Bram Moolenaar51485f02005-06-04 21:55:20 +00004872/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4873typedef struct affheader_S
4874{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004875 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4876 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4877 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004878 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004879 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004880 affentry_T *ah_first; /* first affix entry */
4881} affheader_T;
4882
4883#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4884
Bram Moolenaar6de68532005-08-24 22:08:48 +00004885/* Flag used in compound items. */
4886typedef struct compitem_S
4887{
4888 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4889 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4890 int ci_newID; /* affix ID after renumbering. */
4891} compitem_T;
4892
4893#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4894
Bram Moolenaar51485f02005-06-04 21:55:20 +00004895/*
4896 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004897 * the need to keep track of each allocated thing, everything is freed all at
4898 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004899 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4900 * "sb_data" is correct for systems where pointers must be aligned on
4901 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004902 */
4903#define SBLOCKSIZE 16000 /* size of sb_data */
4904typedef struct sblock_S sblock_T;
4905struct sblock_S
4906{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004907 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004908 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004909 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004910};
4911
4912/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004913 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004914 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004915typedef struct wordnode_S wordnode_T;
4916struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004917{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004918 union /* shared to save space */
4919 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004920 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004921 int index; /* index in written nodes (valid after first
4922 round) */
4923 } wn_u1;
4924 union /* shared to save space */
4925 {
4926 wordnode_T *next; /* next node with same hash key */
4927 wordnode_T *wnode; /* parent node that will write this node */
4928 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004929 wordnode_T *wn_child; /* child (next byte in word) */
4930 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4931 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004932 int wn_refs; /* Nr. of references to this node. Only
4933 relevant for first node in a list of
4934 siblings, in following siblings it is
4935 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004936 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004937
4938 /* Info for when "wn_byte" is NUL.
4939 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4940 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4941 * "wn_region" the LSW of the wordnr. */
4942 char_u wn_affixID; /* supported/required prefix ID or 0 */
4943 short_u wn_flags; /* WF_ flags */
4944 short wn_region; /* region mask */
4945
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004946#ifdef SPELL_PRINTTREE
4947 int wn_nr; /* sequence nr for printing */
4948#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004949};
4950
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004951#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4952
Bram Moolenaar51485f02005-06-04 21:55:20 +00004953#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004954
Bram Moolenaar51485f02005-06-04 21:55:20 +00004955/*
4956 * Info used while reading the spell files.
4957 */
4958typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004959{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004960 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004961 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004962
Bram Moolenaar51485f02005-06-04 21:55:20 +00004963 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004964 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004965
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004966 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004967
Bram Moolenaar4770d092006-01-12 23:22:24 +00004968 long si_sugtree; /* creating the soundfolding trie */
4969
Bram Moolenaar51485f02005-06-04 21:55:20 +00004970 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004971 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004972 int si_did_emsg; /* TRUE when ran out of memory */
4973
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004974 long si_compress_cnt; /* words to add before lowering
4975 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004976 wordnode_T *si_first_free; /* List of nodes that have been freed during
4977 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004978 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004979#ifdef SPELL_PRINTTREE
4980 int si_wordnode_nr; /* sequence nr for nodes */
4981#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004982 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004983
Bram Moolenaar51485f02005-06-04 21:55:20 +00004984 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004985 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004986 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004987 int si_region; /* region mask */
4988 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004989 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004990 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004991 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004992 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004993 int si_region_count; /* number of regions supported (1 when there
4994 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004995 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004996 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004997
4998 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004999 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005000 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005001 char_u *si_sofofr; /* SOFOFROM text */
5002 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005003 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005004 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005005 int si_followup; /* soundsalike: ? */
5006 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005007 hashtab_T si_commonwords; /* hashtable for common words */
5008 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005009 int si_rem_accents; /* soundsalike: remove accents */
5010 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005011 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005012 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005013 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005014 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005015 int si_compoptions; /* COMP_ flags */
5016 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
5017 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005018 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00005019 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005020 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005021 garray_T si_prefcond; /* table with conditions for postponed
5022 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005023 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005024 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005025} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005026
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005027static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005028static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005029static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005030static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005031static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5032static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5033static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005034static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005035static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5036static void aff_check_number __ARGS((int spinval, int affval, char *name));
5037static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005038static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005039static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5040static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005041static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005042static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005043static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005044static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005045static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005046static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005047static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5048static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5049static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005050static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005051static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005052static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005053static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005054static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005055static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005056static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5057static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5058static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005059static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005060static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005061static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005062static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005063static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5064static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5065static int sug_maketable __ARGS((spellinfo_T *spin));
5066static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5067static int offset2bytes __ARGS((int nr, char_u *buf));
5068static int bytes2offset __ARGS((char_u **pp));
5069static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005070static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int over_write, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005071static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005072static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005073
Bram Moolenaar53805d12005-08-01 07:08:33 +00005074/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5075 * but it must be negative to indicate the prefix tree to tree_add_word().
5076 * Use a negative number with the lower 8 bits zero. */
5077#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005078
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005079/* flags for "condit" argument of store_aff_word() */
5080#define CONDIT_COMB 1 /* affix must combine */
5081#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5082#define CONDIT_SUF 4 /* add a suffix for matching flags */
5083#define CONDIT_AFF 8 /* word already has an affix */
5084
Bram Moolenaar5195e452005-08-19 20:32:47 +00005085/*
5086 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5087 */
5088static long compress_start = 30000; /* memory / SBLOCKSIZE */
5089static long compress_inc = 100; /* memory / SBLOCKSIZE */
5090static long compress_added = 500000; /* word count */
5091
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005092#ifdef SPELL_PRINTTREE
5093/*
5094 * For debugging the tree code: print the current tree in a (more or less)
5095 * readable format, so that we can see what happens when adding a word and/or
5096 * compressing the tree.
5097 * Based on code from Olaf Seibert.
5098 */
5099#define PRINTLINESIZE 1000
5100#define PRINTWIDTH 6
5101
5102#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5103 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5104
5105static char line1[PRINTLINESIZE];
5106static char line2[PRINTLINESIZE];
5107static char line3[PRINTLINESIZE];
5108
5109 static void
5110spell_clear_flags(wordnode_T *node)
5111{
5112 wordnode_T *np;
5113
5114 for (np = node; np != NULL; np = np->wn_sibling)
5115 {
5116 np->wn_u1.index = FALSE;
5117 spell_clear_flags(np->wn_child);
5118 }
5119}
5120
5121 static void
5122spell_print_node(wordnode_T *node, int depth)
5123{
5124 if (node->wn_u1.index)
5125 {
5126 /* Done this node before, print the reference. */
5127 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5128 PRINTSOME(line2, depth, " ", 0, 0);
5129 PRINTSOME(line3, depth, " ", 0, 0);
5130 msg(line1);
5131 msg(line2);
5132 msg(line3);
5133 }
5134 else
5135 {
5136 node->wn_u1.index = TRUE;
5137
5138 if (node->wn_byte != NUL)
5139 {
5140 if (node->wn_child != NULL)
5141 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5142 else
5143 /* Cannot happen? */
5144 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5145 }
5146 else
5147 PRINTSOME(line1, depth, " $ ", 0, 0);
5148
5149 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5150
5151 if (node->wn_sibling != NULL)
5152 PRINTSOME(line3, depth, " | ", 0, 0);
5153 else
5154 PRINTSOME(line3, depth, " ", 0, 0);
5155
5156 if (node->wn_byte == NUL)
5157 {
5158 msg(line1);
5159 msg(line2);
5160 msg(line3);
5161 }
5162
5163 /* do the children */
5164 if (node->wn_byte != NUL && node->wn_child != NULL)
5165 spell_print_node(node->wn_child, depth + 1);
5166
5167 /* do the siblings */
5168 if (node->wn_sibling != NULL)
5169 {
5170 /* get rid of all parent details except | */
5171 STRCPY(line1, line3);
5172 STRCPY(line2, line3);
5173 spell_print_node(node->wn_sibling, depth);
5174 }
5175 }
5176}
5177
5178 static void
5179spell_print_tree(wordnode_T *root)
5180{
5181 if (root != NULL)
5182 {
5183 /* Clear the "wn_u1.index" fields, used to remember what has been
5184 * done. */
5185 spell_clear_flags(root);
5186
5187 /* Recursively print the tree. */
5188 spell_print_node(root, 0);
5189 }
5190}
5191#endif /* SPELL_PRINTTREE */
5192
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005193/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005194 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005195 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196 */
5197 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005198spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005199 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005200 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005201{
5202 FILE *fd;
5203 afffile_T *aff;
5204 char_u rline[MAXLINELEN];
5205 char_u *line;
5206 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005207#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005208 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005209 int itemcnt;
5210 char_u *p;
5211 int lnum = 0;
5212 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005213 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005214 int aff_todo = 0;
5215 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005216 char_u *low = NULL;
5217 char_u *fol = NULL;
5218 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005220 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005222 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005224 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005225 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005226 int compminlen = 0; /* COMPOUNDMIN value */
5227 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005228 int compoptions = 0; /* COMP_ flags */
5229 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005230 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005231 concatenated */
5232 char_u *midword = NULL; /* MIDWORD value */
5233 char_u *syllable = NULL; /* SYLLABLE value */
5234 char_u *sofofrom = NULL; /* SOFOFROM value */
5235 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005236
Bram Moolenaar51485f02005-06-04 21:55:20 +00005237 /*
5238 * Open the file.
5239 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005240 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005241 if (fd == NULL)
5242 {
5243 EMSG2(_(e_notopen), fname);
5244 return NULL;
5245 }
5246
Bram Moolenaar4770d092006-01-12 23:22:24 +00005247 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5248 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005250 /* Only do REP lines when not done in another .aff file already. */
5251 do_rep = spin->si_rep.ga_len == 0;
5252
Bram Moolenaar4770d092006-01-12 23:22:24 +00005253 /* Only do REPSAL lines when not done in another .aff file already. */
5254 do_repsal = spin->si_repsal.ga_len == 0;
5255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 /* Only do SAL lines when not done in another .aff file already. */
5257 do_sal = spin->si_sal.ga_len == 0;
5258
5259 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005260 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005261
Bram Moolenaar51485f02005-06-04 21:55:20 +00005262 /*
5263 * Allocate and init the afffile_T structure.
5264 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005265 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005266 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005267 {
5268 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005269 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005270 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005271 hash_init(&aff->af_pref);
5272 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005273 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005274
5275 /*
5276 * Read all the lines in the file one by one.
5277 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005278 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005279 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005280 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005281 ++lnum;
5282
5283 /* Skip comment lines. */
5284 if (*rline == '#')
5285 continue;
5286
5287 /* Convert from "SET" to 'encoding' when needed. */
5288 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005289#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005290 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005291 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005292 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005293 if (pc == NULL)
5294 {
5295 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5296 fname, lnum, rline);
5297 continue;
5298 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005299 line = pc;
5300 }
5301 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005302#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005303 {
5304 pc = NULL;
5305 line = rline;
5306 }
5307
5308 /* Split the line up in white separated items. Put a NUL after each
5309 * item. */
5310 itemcnt = 0;
5311 for (p = line; ; )
5312 {
5313 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5314 ++p;
5315 if (*p == NUL)
5316 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005317 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005318 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005319 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005320 /* A few items have arbitrary text argument, don't split them. */
5321 if (itemcnt == 2 && spell_info_item(items[0]))
5322 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5323 ++p;
5324 else
5325 while (*p > ' ') /* skip until white space or CR/NL */
5326 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005327 if (*p == NUL)
5328 break;
5329 *p++ = NUL;
5330 }
5331
5332 /* Handle non-empty lines. */
5333 if (itemcnt > 0)
5334 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005335 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005336 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005337#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005338 /* Setup for conversion from "ENC" to 'encoding'. */
5339 aff->af_enc = enc_canonize(items[1]);
5340 if (aff->af_enc != NULL && !spin->si_ascii
5341 && convert_setup(&spin->si_conv, aff->af_enc,
5342 p_enc) == FAIL)
5343 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5344 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005345 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005346#else
5347 smsg((char_u *)_("Conversion in %s not supported"), fname);
5348#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005349 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005350 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005351 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005352 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005353 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005354 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005355 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005356 aff->af_flagtype = AFT_NUM;
5357 else if (STRCMP(items[1], "caplong") == 0)
5358 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005359 else
5360 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5361 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005362 if (aff->af_rare != 0
5363 || aff->af_keepcase != 0
5364 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005365 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005366 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005367 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005368 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005369 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005370 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005371 || aff->af_suff.ht_used > 0
5372 || aff->af_pref.ht_used > 0)
5373 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5374 fname, lnum, items[1]);
5375 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005376 else if (spell_info_item(items[0]))
5377 {
5378 p = (char_u *)getroom(spin,
5379 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5380 + STRLEN(items[0])
5381 + STRLEN(items[1]) + 3, FALSE);
5382 if (p != NULL)
5383 {
5384 if (spin->si_info != NULL)
5385 {
5386 STRCPY(p, spin->si_info);
5387 STRCAT(p, "\n");
5388 }
5389 STRCAT(p, items[0]);
5390 STRCAT(p, " ");
5391 STRCAT(p, items[1]);
5392 spin->si_info = p;
5393 }
5394 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005395 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005396 && midword == NULL)
5397 {
5398 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005399 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005400 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005402 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005403 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005404 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005405 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5406 || is_aff_rule(items, itemcnt, "RARE", 2))
5407 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005408 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005409 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005410 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005411 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005412 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005413 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5414 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005415 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005416 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005417 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005418 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005419 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005420 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5421 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5422 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005423 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005424 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5425 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005426 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005427 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005428 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005429 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005430 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5431 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005432 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005433 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005434 && aff->af_circumfix == 0)
5435 {
5436 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5437 fname, lnum);
5438 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005439 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005440 && aff->af_nosuggest == 0)
5441 {
5442 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5443 fname, lnum);
5444 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005445 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5446 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005447 && aff->af_needcomp == 0)
5448 {
5449 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5450 fname, lnum);
5451 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005452 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005453 && aff->af_comproot == 0)
5454 {
5455 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5456 fname, lnum);
5457 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005458 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5459 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005460 {
5461 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5462 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005463 if (aff->af_pref.ht_used > 0)
5464 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5465 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005466 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005467 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5468 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005469 {
5470 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5471 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005472 if (aff->af_pref.ht_used > 0)
5473 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5474 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005475 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005476 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005477 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005478 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005479 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005480 * "Na" into "Na+", "1234" into "1234+". */
5481 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005482 if (p != NULL)
5483 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005484 STRCPY(p, items[1]);
5485 STRCAT(p, "+");
5486 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005487 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005488 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005489 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5490 {
5491 /* We don't use the count, but do check that it's a number and
5492 * not COMPOUNDRULE mistyped. */
5493 if (atoi((char *)items[1]) == 0)
5494 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5495 fname, lnum, items[1]);
5496 }
5497 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005498 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005499 /* Don't use the first rule if it is a number. */
5500 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005501 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005502 /* Concatenate this string to previously defined ones,
5503 * using a slash to separate them. */
5504 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005505 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005506 l += (int)STRLEN(compflags) + 1;
5507 p = getroom(spin, l, FALSE);
5508 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005509 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005510 if (compflags != NULL)
5511 {
5512 STRCPY(p, compflags);
5513 STRCAT(p, "/");
5514 }
5515 STRCAT(p, items[1]);
5516 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005517 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005518 }
5519 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005520 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005521 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005522 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005523 compmax = atoi((char *)items[1]);
5524 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005525 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005526 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005527 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005528 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005529 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005530 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005531 compminlen = atoi((char *)items[1]);
5532 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005533 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5534 fname, lnum, items[1]);
5535 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005536 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005537 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005538 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005539 compsylmax = atoi((char *)items[1]);
5540 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005541 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5542 fname, lnum, items[1]);
5543 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005544 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005545 {
5546 compoptions |= COMP_CHECKDUP;
5547 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005548 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005549 {
5550 compoptions |= COMP_CHECKREP;
5551 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005552 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005553 {
5554 compoptions |= COMP_CHECKCASE;
5555 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005556 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005557 {
5558 compoptions |= COMP_CHECKTRIPLE;
5559 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005560 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005561 {
5562 if (atoi((char *)items[1]) == 0)
5563 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5564 fname, lnum, items[1]);
5565 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005566 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005567 {
5568 garray_T *gap = &spin->si_comppat;
5569 int i;
5570
5571 /* Only add the couple if it isn't already there. */
5572 for (i = 0; i < gap->ga_len - 1; i += 2)
5573 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5574 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5575 items[2]) == 0)
5576 break;
5577 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5578 {
5579 ((char_u **)(gap->ga_data))[gap->ga_len++]
5580 = getroom_save(spin, items[1]);
5581 ((char_u **)(gap->ga_data))[gap->ga_len++]
5582 = getroom_save(spin, items[2]);
5583 }
5584 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005585 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005586 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005587 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005588 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005589 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005590 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005591 {
5592 spin->si_nobreak = TRUE;
5593 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005594 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005595 {
5596 spin->si_nosplitsugs = TRUE;
5597 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005598 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005599 {
5600 spin->si_nosugfile = TRUE;
5601 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005602 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005603 {
5604 aff->af_pfxpostpone = TRUE;
5605 }
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005606 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
5607 {
5608 aff->af_ignoreextra = TRUE;
5609 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005610 else if ((STRCMP(items[0], "PFX") == 0
5611 || STRCMP(items[0], "SFX") == 0)
5612 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005613 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005614 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005615 int lasti = 4;
5616 char_u key[AH_KEY_LEN];
5617
5618 if (*items[0] == 'P')
5619 tp = &aff->af_pref;
5620 else
5621 tp = &aff->af_suff;
5622
5623 /* Myspell allows the same affix name to be used multiple
5624 * times. The affix files that do this have an undocumented
5625 * "S" flag on all but the last block, thus we check for that
5626 * and store it in ah_follows. */
5627 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5628 hi = hash_find(tp, key);
5629 if (!HASHITEM_EMPTY(hi))
5630 {
5631 cur_aff = HI2AH(hi);
5632 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5633 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5634 fname, lnum, items[1]);
5635 if (!cur_aff->ah_follows)
5636 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5637 fname, lnum, items[1]);
5638 }
5639 else
5640 {
5641 /* New affix letter. */
5642 cur_aff = (affheader_T *)getroom(spin,
5643 sizeof(affheader_T), TRUE);
5644 if (cur_aff == NULL)
5645 break;
5646 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5647 fname, lnum);
5648 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5649 break;
5650 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005651 || cur_aff->ah_flag == aff->af_rare
5652 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005653 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005654 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005655 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005656 || cur_aff->ah_flag == aff->af_needcomp
5657 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005658 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 +00005659 fname, lnum, items[1]);
5660 STRCPY(cur_aff->ah_key, items[1]);
5661 hash_add(tp, cur_aff->ah_key);
5662
5663 cur_aff->ah_combine = (*items[2] == 'Y');
5664 }
5665
5666 /* Check for the "S" flag, which apparently means that another
5667 * block with the same affix name is following. */
5668 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5669 {
5670 ++lasti;
5671 cur_aff->ah_follows = TRUE;
5672 }
5673 else
5674 cur_aff->ah_follows = FALSE;
5675
Bram Moolenaar8db73182005-06-17 21:51:16 +00005676 /* Myspell allows extra text after the item, but that might
5677 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005678 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005679 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005680
Bram Moolenaar95529562005-08-25 21:21:38 +00005681 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005682 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5683 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005684
Bram Moolenaar95529562005-08-25 21:21:38 +00005685 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005686 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005687 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005688 {
5689 /* Use a new number in the .spl file later, to be able
5690 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005691 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005692 cur_aff->ah_newID = ++spin->si_newprefID;
5693
5694 /* We only really use ah_newID if the prefix is
5695 * postponed. We know that only after handling all
5696 * the items. */
5697 did_postpone_prefix = FALSE;
5698 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005699 else
5700 /* Did use the ID in a previous block. */
5701 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005702 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005703
Bram Moolenaar51485f02005-06-04 21:55:20 +00005704 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005705 }
5706 else if ((STRCMP(items[0], "PFX") == 0
5707 || STRCMP(items[0], "SFX") == 0)
5708 && aff_todo > 0
5709 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005710 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005711 {
5712 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005713 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005714 int lasti = 5;
5715
Bram Moolenaar8db73182005-06-17 21:51:16 +00005716 /* Myspell allows extra text after the item, but that might
Bram Moolenaarb4b43bb2014-09-19 16:04:11 +02005717 * mean mistakes go unnoticed. Require a comment-starter,
5718 * unless IGNOREEXTRA is used. Hunspell uses a "-" item. */
5719 if (itemcnt > lasti
5720 && !aff->af_ignoreextra
5721 && *items[lasti] != '#'
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005722 && (STRCMP(items[lasti], "-") != 0
5723 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005724 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005725
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005726 /* New item for an affix letter. */
5727 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005728 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005729 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005730 if (aff_entry == NULL)
5731 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005732
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005733 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005734 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005735 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005736 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005737 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005738
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005739 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005740 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5741 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005742 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005743 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005744 aff_process_flags(aff, aff_entry);
5745 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005746 }
5747
Bram Moolenaar51485f02005-06-04 21:55:20 +00005748 /* Don't use an affix entry with non-ASCII characters when
5749 * "spin->si_ascii" is TRUE. */
5750 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005751 || has_non_ascii(aff_entry->ae_add)))
5752 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005753 aff_entry->ae_next = cur_aff->ah_first;
5754 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005755
5756 if (STRCMP(items[4], ".") != 0)
5757 {
5758 char_u buf[MAXLINELEN];
5759
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005760 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005761 if (*items[0] == 'P')
5762 sprintf((char *)buf, "^%s", items[4]);
5763 else
5764 sprintf((char *)buf, "%s$", items[4]);
5765 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005766 RE_MAGIC + RE_STRING + RE_STRICT);
5767 if (aff_entry->ae_prog == NULL)
5768 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5769 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005770 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005771
5772 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005773 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005774 * Can't be done for an affix with flags, ignoring
5775 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005776 if (*items[0] == 'P' && aff->af_pfxpostpone
5777 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005778 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005779 /* When the chop string is one lower-case letter and
5780 * the add string ends in the upper-case letter we set
5781 * the "upper" flag, clear "ae_chop" and remove the
5782 * letters from "ae_add". The condition must either
5783 * be empty or start with the same letter. */
5784 if (aff_entry->ae_chop != NULL
5785 && aff_entry->ae_add != NULL
5786#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005787 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005788 aff_entry->ae_chop)] == NUL
5789#else
5790 && aff_entry->ae_chop[1] == NUL
5791#endif
5792 )
5793 {
5794 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005795
Bram Moolenaar53805d12005-08-01 07:08:33 +00005796 c = PTR2CHAR(aff_entry->ae_chop);
5797 c_up = SPELL_TOUPPER(c);
5798 if (c_up != c
5799 && (aff_entry->ae_cond == NULL
5800 || PTR2CHAR(aff_entry->ae_cond) == c))
5801 {
5802 p = aff_entry->ae_add
5803 + STRLEN(aff_entry->ae_add);
5804 mb_ptr_back(aff_entry->ae_add, p);
5805 if (PTR2CHAR(p) == c_up)
5806 {
5807 upper = TRUE;
5808 aff_entry->ae_chop = NULL;
5809 *p = NUL;
5810
5811 /* The condition is matched with the
5812 * actual word, thus must check for the
5813 * upper-case letter. */
5814 if (aff_entry->ae_cond != NULL)
5815 {
5816 char_u buf[MAXLINELEN];
5817#ifdef FEAT_MBYTE
5818 if (has_mbyte)
5819 {
5820 onecap_copy(items[4], buf, TRUE);
5821 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005822 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005823 }
5824 else
5825#endif
5826 *aff_entry->ae_cond = c_up;
5827 if (aff_entry->ae_cond != NULL)
5828 {
5829 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005830 aff_entry->ae_cond);
Bram Moolenaar473de612013-06-08 18:19:48 +02005831 vim_regfree(aff_entry->ae_prog);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005832 aff_entry->ae_prog = vim_regcomp(
5833 buf, RE_MAGIC + RE_STRING);
5834 }
5835 }
5836 }
5837 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005838 }
5839
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005840 if (aff_entry->ae_chop == NULL
5841 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005842 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005843 int idx;
5844 char_u **pp;
5845 int n;
5846
Bram Moolenaar6de68532005-08-24 22:08:48 +00005847 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005848 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5849 --idx)
5850 {
5851 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5852 if (str_equal(p, aff_entry->ae_cond))
5853 break;
5854 }
5855 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5856 {
5857 /* Not found, add a new condition. */
5858 idx = spin->si_prefcond.ga_len++;
5859 pp = ((char_u **)spin->si_prefcond.ga_data)
5860 + idx;
5861 if (aff_entry->ae_cond == NULL)
5862 *pp = NULL;
5863 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005864 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005865 aff_entry->ae_cond);
5866 }
5867
5868 /* Add the prefix to the prefix tree. */
5869 if (aff_entry->ae_add == NULL)
5870 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005871 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005872 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005873
Bram Moolenaar53805d12005-08-01 07:08:33 +00005874 /* PFX_FLAGS is a negative number, so that
5875 * tree_add_word() knows this is the prefix tree. */
5876 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005877 if (!cur_aff->ah_combine)
5878 n |= WFP_NC;
5879 if (upper)
5880 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005881 if (aff_entry->ae_comppermit)
5882 n |= WFP_COMPPERMIT;
5883 if (aff_entry->ae_compforbid)
5884 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005885 tree_add_word(spin, p, spin->si_prefroot, n,
5886 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005887 did_postpone_prefix = TRUE;
5888 }
5889
5890 /* Didn't actually use ah_newID, backup si_newprefID. */
5891 if (aff_todo == 0 && !did_postpone_prefix)
5892 {
5893 --spin->si_newprefID;
5894 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005895 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005896 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005897 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005898 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005899 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005900 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005901 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005902 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005903 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005904 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005906 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005907 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005908 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005909 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005910 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005911 else if (is_aff_rule(items, itemcnt, "REP", 2)
5912 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005913 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005914 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005915 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005916 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005917 fname, lnum);
5918 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005919 else if ((STRCMP(items[0], "REP") == 0
5920 || STRCMP(items[0], "REPSAL") == 0)
5921 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005922 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005923 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005924 /* Myspell ignores extra arguments, we require it starts with
5925 * # to detect mistakes. */
5926 if (itemcnt > 3 && items[3][0] != '#')
5927 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005928 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005929 {
5930 /* Replace underscore with space (can't include a space
5931 * directly). */
5932 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5933 if (*p == '_')
5934 *p = ' ';
5935 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5936 if (*p == '_')
5937 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005938 add_fromto(spin, items[0][3] == 'S'
5939 ? &spin->si_repsal
5940 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005942 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005943 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005944 {
5945 /* MAP item or count */
5946 if (!found_map)
5947 {
5948 /* First line contains the count. */
5949 found_map = TRUE;
5950 if (!isdigit(*items[1]))
5951 smsg((char_u *)_("Expected MAP count in %s line %d"),
5952 fname, lnum);
5953 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005954 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005955 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005956 int c;
5957
5958 /* Check that every character appears only once. */
5959 for (p = items[1]; *p != NUL; )
5960 {
5961#ifdef FEAT_MBYTE
5962 c = mb_ptr2char_adv(&p);
5963#else
5964 c = *p++;
5965#endif
5966 if ((spin->si_map.ga_len > 0
5967 && vim_strchr(spin->si_map.ga_data, c)
5968 != NULL)
5969 || vim_strchr(p, c) != NULL)
5970 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5971 fname, lnum);
5972 }
5973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005974 /* We simply concatenate all the MAP strings, separated by
5975 * slashes. */
5976 ga_concat(&spin->si_map, items[1]);
5977 ga_append(&spin->si_map, '/');
5978 }
5979 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005980 /* Accept "SAL from to" and "SAL from to #comment". */
5981 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005982 {
5983 if (do_sal)
5984 {
5985 /* SAL item (sounds-a-like)
5986 * Either one of the known keys or a from-to pair. */
5987 if (STRCMP(items[1], "followup") == 0)
5988 spin->si_followup = sal_to_bool(items[2]);
5989 else if (STRCMP(items[1], "collapse_result") == 0)
5990 spin->si_collapse = sal_to_bool(items[2]);
5991 else if (STRCMP(items[1], "remove_accents") == 0)
5992 spin->si_rem_accents = sal_to_bool(items[2]);
5993 else
5994 /* when "to" is "_" it means empty */
5995 add_fromto(spin, &spin->si_sal, items[1],
5996 STRCMP(items[2], "_") == 0 ? (char_u *)""
5997 : items[2]);
5998 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005999 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006000 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006001 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006002 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006003 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006004 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006005 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006006 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006007 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006008 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006009 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006010 else if (STRCMP(items[0], "COMMON") == 0)
6011 {
6012 int i;
6013
6014 for (i = 1; i < itemcnt; ++i)
6015 {
6016 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
6017 items[i])))
6018 {
6019 p = vim_strsave(items[i]);
6020 if (p == NULL)
6021 break;
6022 hash_add(&spin->si_commonwords, p);
6023 }
6024 }
6025 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006026 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006027 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006028 fname, lnum, items[0]);
6029 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006030 }
6031
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006032 if (fol != NULL || low != NULL || upp != NULL)
6033 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006034 if (spin->si_clear_chartab)
6035 {
6036 /* Clear the char type tables, don't want to use any of the
6037 * currently used spell properties. */
6038 init_spell_chartab();
6039 spin->si_clear_chartab = FALSE;
6040 }
6041
Bram Moolenaar3982c542005-06-08 21:56:31 +00006042 /*
6043 * Don't write a word table for an ASCII file, so that we don't check
6044 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006045 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006046 * mb_get_class(), the list of chars in the file will be incomplete.
6047 */
6048 if (!spin->si_ascii
6049#ifdef FEAT_MBYTE
6050 && !enc_utf8
6051#endif
6052 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006053 {
6054 if (fol == NULL || low == NULL || upp == NULL)
6055 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6056 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006057 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006058 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006059
6060 vim_free(fol);
6061 vim_free(low);
6062 vim_free(upp);
6063 }
6064
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006065 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006066 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006067 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006068 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006069 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006070 }
6071
Bram Moolenaar6de68532005-08-24 22:08:48 +00006072 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006073 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006074 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6075 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006076 }
6077
Bram Moolenaar6de68532005-08-24 22:08:48 +00006078 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006079 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006080 if (syllable == NULL)
6081 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6082 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6083 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006084 }
6085
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006086 if (compoptions != 0)
6087 {
6088 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6089 spin->si_compoptions |= compoptions;
6090 }
6091
Bram Moolenaar6de68532005-08-24 22:08:48 +00006092 if (compflags != NULL)
6093 process_compflags(spin, aff, compflags);
6094
6095 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006096 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006097 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006098 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006099 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006100 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006101 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006102 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006103 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006104 }
6105
Bram Moolenaar6de68532005-08-24 22:08:48 +00006106 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006107 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006108 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6109 spin->si_syllable = syllable;
6110 }
6111
6112 if (sofofrom != NULL || sofoto != NULL)
6113 {
6114 if (sofofrom == NULL || sofoto == NULL)
6115 smsg((char_u *)_("Missing SOFO%s line in %s"),
6116 sofofrom == NULL ? "FROM" : "TO", fname);
6117 else if (spin->si_sal.ga_len > 0)
6118 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006119 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006120 {
6121 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6122 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6123 spin->si_sofofr = sofofrom;
6124 spin->si_sofoto = sofoto;
6125 }
6126 }
6127
6128 if (midword != NULL)
6129 {
6130 aff_check_string(spin->si_midword, midword, "MIDWORD");
6131 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006132 }
6133
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006134 vim_free(pc);
6135 fclose(fd);
6136 return aff;
6137}
6138
6139/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006140 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6141 * a comment is following after item "mincount".
6142 */
6143 static int
6144is_aff_rule(items, itemcnt, rulename, mincount)
6145 char_u **items;
6146 int itemcnt;
6147 char *rulename;
6148 int mincount;
6149{
6150 return (STRCMP(items[0], rulename) == 0
6151 && (itemcnt == mincount
6152 || (itemcnt > mincount && items[mincount][0] == '#')));
6153}
6154
6155/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006156 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6157 * ae_flags to ae_comppermit and ae_compforbid.
6158 */
6159 static void
6160aff_process_flags(affile, entry)
6161 afffile_T *affile;
6162 affentry_T *entry;
6163{
6164 char_u *p;
6165 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006166 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006167
6168 if (entry->ae_flags != NULL
6169 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6170 {
6171 for (p = entry->ae_flags; *p != NUL; )
6172 {
6173 prevp = p;
6174 flag = get_affitem(affile->af_flagtype, &p);
6175 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6176 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006177 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006178 p = prevp;
6179 if (flag == affile->af_comppermit)
6180 entry->ae_comppermit = TRUE;
6181 else
6182 entry->ae_compforbid = TRUE;
6183 }
6184 if (affile->af_flagtype == AFT_NUM && *p == ',')
6185 ++p;
6186 }
6187 if (*entry->ae_flags == NUL)
6188 entry->ae_flags = NULL; /* nothing left */
6189 }
6190}
6191
6192/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006193 * Return TRUE if "s" is the name of an info item in the affix file.
6194 */
6195 static int
6196spell_info_item(s)
6197 char_u *s;
6198{
6199 return STRCMP(s, "NAME") == 0
6200 || STRCMP(s, "HOME") == 0
6201 || STRCMP(s, "VERSION") == 0
6202 || STRCMP(s, "AUTHOR") == 0
6203 || STRCMP(s, "EMAIL") == 0
6204 || STRCMP(s, "COPYRIGHT") == 0;
6205}
6206
6207/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006208 * Turn an affix flag name into a number, according to the FLAG type.
6209 * returns zero for failure.
6210 */
6211 static unsigned
6212affitem2flag(flagtype, item, fname, lnum)
6213 int flagtype;
6214 char_u *item;
6215 char_u *fname;
6216 int lnum;
6217{
6218 unsigned res;
6219 char_u *p = item;
6220
6221 res = get_affitem(flagtype, &p);
6222 if (res == 0)
6223 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006224 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006225 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6226 fname, lnum, item);
6227 else
6228 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6229 fname, lnum, item);
6230 }
6231 if (*p != NUL)
6232 {
6233 smsg((char_u *)_(e_affname), fname, lnum, item);
6234 return 0;
6235 }
6236
6237 return res;
6238}
6239
6240/*
6241 * Get one affix name from "*pp" and advance the pointer.
6242 * Returns zero for an error, still advances the pointer then.
6243 */
6244 static unsigned
6245get_affitem(flagtype, pp)
6246 int flagtype;
6247 char_u **pp;
6248{
6249 int res;
6250
Bram Moolenaar95529562005-08-25 21:21:38 +00006251 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006252 {
6253 if (!VIM_ISDIGIT(**pp))
6254 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006255 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006256 return 0;
6257 }
6258 res = getdigits(pp);
6259 }
6260 else
6261 {
6262#ifdef FEAT_MBYTE
6263 res = mb_ptr2char_adv(pp);
6264#else
6265 res = *(*pp)++;
6266#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006267 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006268 && res >= 'A' && res <= 'Z'))
6269 {
6270 if (**pp == NUL)
6271 return 0;
6272#ifdef FEAT_MBYTE
6273 res = mb_ptr2char_adv(pp) + (res << 16);
6274#else
6275 res = *(*pp)++ + (res << 16);
6276#endif
6277 }
6278 }
6279 return res;
6280}
6281
6282/*
6283 * Process the "compflags" string used in an affix file and append it to
6284 * spin->si_compflags.
6285 * The processing involves changing the affix names to ID numbers, so that
6286 * they fit in one byte.
6287 */
6288 static void
6289process_compflags(spin, aff, compflags)
6290 spellinfo_T *spin;
6291 afffile_T *aff;
6292 char_u *compflags;
6293{
6294 char_u *p;
6295 char_u *prevp;
6296 unsigned flag;
6297 compitem_T *ci;
6298 int id;
6299 int len;
6300 char_u *tp;
6301 char_u key[AH_KEY_LEN];
6302 hashitem_T *hi;
6303
6304 /* Make room for the old and the new compflags, concatenated with a / in
6305 * between. Processing it makes it shorter, but we don't know by how
6306 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006307 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006308 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006309 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006310 p = getroom(spin, len, FALSE);
6311 if (p == NULL)
6312 return;
6313 if (spin->si_compflags != NULL)
6314 {
6315 STRCPY(p, spin->si_compflags);
6316 STRCAT(p, "/");
6317 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006318 spin->si_compflags = p;
6319 tp = p + STRLEN(p);
6320
6321 for (p = compflags; *p != NUL; )
6322 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006323 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006324 /* Copy non-flag characters directly. */
6325 *tp++ = *p++;
6326 else
6327 {
6328 /* First get the flag number, also checks validity. */
6329 prevp = p;
6330 flag = get_affitem(aff->af_flagtype, &p);
6331 if (flag != 0)
6332 {
6333 /* Find the flag in the hashtable. If it was used before, use
6334 * the existing ID. Otherwise add a new entry. */
6335 vim_strncpy(key, prevp, p - prevp);
6336 hi = hash_find(&aff->af_comp, key);
6337 if (!HASHITEM_EMPTY(hi))
6338 id = HI2CI(hi)->ci_newID;
6339 else
6340 {
6341 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6342 if (ci == NULL)
6343 break;
6344 STRCPY(ci->ci_key, key);
6345 ci->ci_flag = flag;
6346 /* Avoid using a flag ID that has a special meaning in a
6347 * regexp (also inside []). */
6348 do
6349 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006350 check_renumber(spin);
6351 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006352 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006353 ci->ci_newID = id;
6354 hash_add(&aff->af_comp, ci->ci_key);
6355 }
6356 *tp++ = id;
6357 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006358 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006359 ++p;
6360 }
6361 }
6362
6363 *tp = NUL;
6364}
6365
6366/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006367 * Check that the new IDs for postponed affixes and compounding don't overrun
6368 * each other. We have almost 255 available, but start at 0-127 to avoid
6369 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6370 * When that is used up an error message is given.
6371 */
6372 static void
6373check_renumber(spin)
6374 spellinfo_T *spin;
6375{
6376 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6377 {
6378 spin->si_newprefID = 127;
6379 spin->si_newcompID = 255;
6380 }
6381}
6382
6383/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006384 * Return TRUE if flag "flag" appears in affix list "afflist".
6385 */
6386 static int
6387flag_in_afflist(flagtype, afflist, flag)
6388 int flagtype;
6389 char_u *afflist;
6390 unsigned flag;
6391{
6392 char_u *p;
6393 unsigned n;
6394
6395 switch (flagtype)
6396 {
6397 case AFT_CHAR:
6398 return vim_strchr(afflist, flag) != NULL;
6399
Bram Moolenaar95529562005-08-25 21:21:38 +00006400 case AFT_CAPLONG:
6401 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006402 for (p = afflist; *p != NUL; )
6403 {
6404#ifdef FEAT_MBYTE
6405 n = mb_ptr2char_adv(&p);
6406#else
6407 n = *p++;
6408#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006409 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006410 && *p != NUL)
6411#ifdef FEAT_MBYTE
6412 n = mb_ptr2char_adv(&p) + (n << 16);
6413#else
6414 n = *p++ + (n << 16);
6415#endif
6416 if (n == flag)
6417 return TRUE;
6418 }
6419 break;
6420
Bram Moolenaar95529562005-08-25 21:21:38 +00006421 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006422 for (p = afflist; *p != NUL; )
6423 {
6424 n = getdigits(&p);
6425 if (n == flag)
6426 return TRUE;
6427 if (*p != NUL) /* skip over comma */
6428 ++p;
6429 }
6430 break;
6431 }
6432 return FALSE;
6433}
6434
6435/*
6436 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6437 */
6438 static void
6439aff_check_number(spinval, affval, name)
6440 int spinval;
6441 int affval;
6442 char *name;
6443{
6444 if (spinval != 0 && spinval != affval)
6445 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6446}
6447
6448/*
6449 * Give a warning when "spinval" and "affval" strings are set and not the same.
6450 */
6451 static void
6452aff_check_string(spinval, affval, name)
6453 char_u *spinval;
6454 char_u *affval;
6455 char *name;
6456{
6457 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6458 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6459}
6460
6461/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006462 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6463 * NULL as equal.
6464 */
6465 static int
6466str_equal(s1, s2)
6467 char_u *s1;
6468 char_u *s2;
6469{
6470 if (s1 == NULL || s2 == NULL)
6471 return s1 == s2;
6472 return STRCMP(s1, s2) == 0;
6473}
6474
6475/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006476 * Add a from-to item to "gap". Used for REP and SAL items.
6477 * They are stored case-folded.
6478 */
6479 static void
6480add_fromto(spin, gap, from, to)
6481 spellinfo_T *spin;
6482 garray_T *gap;
6483 char_u *from;
6484 char_u *to;
6485{
6486 fromto_T *ftp;
6487 char_u word[MAXWLEN];
6488
6489 if (ga_grow(gap, 1) == OK)
6490 {
6491 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006492 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006493 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006494 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006495 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006496 ++gap->ga_len;
6497 }
6498}
6499
6500/*
6501 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6502 */
6503 static int
6504sal_to_bool(s)
6505 char_u *s;
6506{
6507 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6508}
6509
6510/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006511 * Free the structure filled by spell_read_aff().
6512 */
6513 static void
6514spell_free_aff(aff)
6515 afffile_T *aff;
6516{
6517 hashtab_T *ht;
6518 hashitem_T *hi;
6519 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006521 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006522
6523 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006524
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006525 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006526 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006528 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006529 for (hi = ht->ht_array; todo > 0; ++hi)
6530 {
6531 if (!HASHITEM_EMPTY(hi))
6532 {
6533 --todo;
6534 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006535 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar473de612013-06-08 18:19:48 +02006536 vim_regfree(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006537 }
6538 }
6539 if (ht == &aff->af_suff)
6540 break;
6541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006542
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006543 hash_clear(&aff->af_pref);
6544 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006545 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006546}
6547
6548/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006549 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006550 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006551 */
6552 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006553spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006555 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006556 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006557{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006558 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006559 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006560 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006562 char_u store_afflist[MAXWLEN];
6563 int pfxlen;
6564 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006565 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006566 char_u *pc;
6567 char_u *w;
6568 int l;
6569 hash_T hash;
6570 hashitem_T *hi;
6571 FILE *fd;
6572 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006573 int non_ascii = 0;
6574 int retval = OK;
6575 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006576 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006577 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006578
Bram Moolenaar51485f02005-06-04 21:55:20 +00006579 /*
6580 * Open the file.
6581 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006582 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006583 if (fd == NULL)
6584 {
6585 EMSG2(_(e_notopen), fname);
6586 return FAIL;
6587 }
6588
Bram Moolenaar51485f02005-06-04 21:55:20 +00006589 /* The hashtable is only used to detect duplicated words. */
6590 hash_init(&ht);
6591
Bram Moolenaar4770d092006-01-12 23:22:24 +00006592 vim_snprintf((char *)IObuff, IOSIZE,
6593 _("Reading dictionary file %s ..."), fname);
6594 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006595
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006596 /* start with a message for the first line */
6597 spin->si_msg_count = 999999;
6598
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006599 /* Read and ignore the first line: word count. */
6600 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006601 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006602 EMSG2(_("E760: No word count in %s"), fname);
6603
6604 /*
6605 * Read all the lines in the file one by one.
6606 * The words are converted to 'encoding' here, before being added to
6607 * the hashtable.
6608 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006609 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006610 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006611 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006612 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006613 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006614 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006615
Bram Moolenaar51485f02005-06-04 21:55:20 +00006616 /* Remove CR, LF and white space from the end. White space halfway
6617 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006618 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006619 while (l > 0 && line[l - 1] <= ' ')
6620 --l;
6621 if (l == 0)
6622 continue; /* empty line */
6623 line[l] = NUL;
6624
Bram Moolenaarb765d632005-06-07 21:00:02 +00006625#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006626 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006627 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006628 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006629 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006630 if (pc == NULL)
6631 {
6632 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6633 fname, lnum, line);
6634 continue;
6635 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006636 w = pc;
6637 }
6638 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006639#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006640 {
6641 pc = NULL;
6642 w = line;
6643 }
6644
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006645 /* Truncate the word at the "/", set "afflist" to what follows.
6646 * Replace "\/" by "/" and "\\" by "\". */
6647 afflist = NULL;
6648 for (p = w; *p != NUL; mb_ptr_adv(p))
6649 {
6650 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006651 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006652 else if (*p == '/')
6653 {
6654 *p = NUL;
6655 afflist = p + 1;
6656 break;
6657 }
6658 }
6659
6660 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6661 if (spin->si_ascii && has_non_ascii(w))
6662 {
6663 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006664 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006665 continue;
6666 }
6667
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006668 /* This takes time, print a message every 10000 words. */
6669 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006670 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006671 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006672 vim_snprintf((char *)message, sizeof(message),
6673 _("line %6d, word %6d - %s"),
6674 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6675 msg_start();
6676 msg_puts_long_attr(message, 0);
6677 msg_clr_eos();
6678 msg_didout = FALSE;
6679 msg_col = 0;
6680 out_flush();
6681 }
6682
Bram Moolenaar51485f02005-06-04 21:55:20 +00006683 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006684 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006685 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006686 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006687 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006688 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006689 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006690 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006691
Bram Moolenaar51485f02005-06-04 21:55:20 +00006692 hash = hash_hash(dw);
6693 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006694 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006695 {
6696 if (p_verbose > 0)
6697 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006698 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006699 else if (duplicate == 0)
6700 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6701 fname, lnum, dw);
6702 ++duplicate;
6703 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006704 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006705 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006706
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006707 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006708 store_afflist[0] = NUL;
6709 pfxlen = 0;
6710 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006711 if (afflist != NULL)
6712 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006713 /* Extract flags from the affix list. */
6714 flags |= get_affix_flags(affile, afflist);
6715
Bram Moolenaar6de68532005-08-24 22:08:48 +00006716 if (affile->af_needaffix != 0 && flag_in_afflist(
6717 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006718 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006719
6720 if (affile->af_pfxpostpone)
6721 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006722 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006723
Bram Moolenaar5195e452005-08-19 20:32:47 +00006724 if (spin->si_compflags != NULL)
6725 /* Need to store the list of compound flags with the word.
6726 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006727 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006728 }
6729
Bram Moolenaar51485f02005-06-04 21:55:20 +00006730 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006731 if (store_word(spin, dw, flags, spin->si_region,
6732 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006733 retval = FAIL;
6734
6735 if (afflist != NULL)
6736 {
6737 /* Find all matching suffixes and add the resulting words.
6738 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006739 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006740 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006741 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006742 retval = FAIL;
6743
6744 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006745 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006746 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006747 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006748 retval = FAIL;
6749 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006750
6751 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006752 }
6753
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006754 if (duplicate > 0)
6755 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006756 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006757 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6758 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006759 hash_clear(&ht);
6760
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006761 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006762 return retval;
6763}
6764
6765/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006766 * Check for affix flags in "afflist" that are turned into word flags.
6767 * Return WF_ flags.
6768 */
6769 static int
6770get_affix_flags(affile, afflist)
6771 afffile_T *affile;
6772 char_u *afflist;
6773{
6774 int flags = 0;
6775
6776 if (affile->af_keepcase != 0 && flag_in_afflist(
6777 affile->af_flagtype, afflist, affile->af_keepcase))
6778 flags |= WF_KEEPCAP | WF_FIXCAP;
6779 if (affile->af_rare != 0 && flag_in_afflist(
6780 affile->af_flagtype, afflist, affile->af_rare))
6781 flags |= WF_RARE;
6782 if (affile->af_bad != 0 && flag_in_afflist(
6783 affile->af_flagtype, afflist, affile->af_bad))
6784 flags |= WF_BANNED;
6785 if (affile->af_needcomp != 0 && flag_in_afflist(
6786 affile->af_flagtype, afflist, affile->af_needcomp))
6787 flags |= WF_NEEDCOMP;
6788 if (affile->af_comproot != 0 && flag_in_afflist(
6789 affile->af_flagtype, afflist, affile->af_comproot))
6790 flags |= WF_COMPROOT;
6791 if (affile->af_nosuggest != 0 && flag_in_afflist(
6792 affile->af_flagtype, afflist, affile->af_nosuggest))
6793 flags |= WF_NOSUGGEST;
6794 return flags;
6795}
6796
6797/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006798 * Get the list of prefix IDs from the affix list "afflist".
6799 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006800 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6801 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006802 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006803 static int
6804get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006805 afffile_T *affile;
6806 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006807 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006808{
6809 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006810 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006811 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006812 int id;
6813 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006814 hashitem_T *hi;
6815
Bram Moolenaar6de68532005-08-24 22:08:48 +00006816 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006817 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006818 prevp = p;
6819 if (get_affitem(affile->af_flagtype, &p) != 0)
6820 {
6821 /* A flag is a postponed prefix flag if it appears in "af_pref"
6822 * and it's ID is not zero. */
6823 vim_strncpy(key, prevp, p - prevp);
6824 hi = hash_find(&affile->af_pref, key);
6825 if (!HASHITEM_EMPTY(hi))
6826 {
6827 id = HI2AH(hi)->ah_newID;
6828 if (id != 0)
6829 store_afflist[cnt++] = id;
6830 }
6831 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006832 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006833 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006834 }
6835
Bram Moolenaar5195e452005-08-19 20:32:47 +00006836 store_afflist[cnt] = NUL;
6837 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006838}
6839
6840/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006841 * Get the list of compound IDs from the affix list "afflist" that are used
6842 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006843 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006844 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006845 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006846get_compflags(affile, afflist, store_afflist)
6847 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006848 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006849 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006850{
6851 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006852 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006853 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006854 char_u key[AH_KEY_LEN];
6855 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006856
Bram Moolenaar6de68532005-08-24 22:08:48 +00006857 for (p = afflist; *p != NUL; )
6858 {
6859 prevp = p;
6860 if (get_affitem(affile->af_flagtype, &p) != 0)
6861 {
6862 /* A flag is a compound flag if it appears in "af_comp". */
6863 vim_strncpy(key, prevp, p - prevp);
6864 hi = hash_find(&affile->af_comp, key);
6865 if (!HASHITEM_EMPTY(hi))
6866 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6867 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006868 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006869 ++p;
6870 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006871
Bram Moolenaar5195e452005-08-19 20:32:47 +00006872 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006873}
6874
6875/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006876 * Apply affixes to a word and store the resulting words.
6877 * "ht" is the hashtable with affentry_T that need to be applied, either
6878 * prefixes or suffixes.
6879 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6880 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006881 *
6882 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006883 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006884 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006885store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006886 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006887 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006888 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006889 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006890 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006891 hashtab_T *ht;
6892 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006893 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006894 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006895 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006896 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6897 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006898{
6899 int todo;
6900 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006901 affheader_T *ah;
6902 affentry_T *ae;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006903 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006904 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006905 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006906 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006907 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006908 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006909 int use_pfxlen;
6910 int need_affix;
6911 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006912 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006913 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006914 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006915
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006916 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006917 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006918 {
6919 if (!HASHITEM_EMPTY(hi))
6920 {
6921 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006922 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006923
Bram Moolenaar51485f02005-06-04 21:55:20 +00006924 /* Check that the affix combines, if required, and that the word
6925 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006926 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6927 && flag_in_afflist(affile->af_flagtype, afflist,
6928 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006929 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006930 /* Loop over all affix entries with this name. */
6931 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006932 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006933 /* Check the condition. It's not logical to match case
6934 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006935 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006936 * Another requirement from Myspell is that the chop
6937 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006938 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006939 * prefixes with a chop string and/or flags.
6940 * When a previously added affix had CIRCUMFIX this one
6941 * must have it too, if it had not then this one must not
6942 * have one either. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006943 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006944 || ae->ae_chop != NULL
6945 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006946 && (ae->ae_chop == NULL
6947 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006948 && (ae->ae_prog == NULL
Bram Moolenaardffa5b82014-11-19 16:38:07 +01006949 || vim_regexec_prog(&ae->ae_prog, FALSE,
6950 word, (colnr_T)0))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006951 && (((condit & CONDIT_CFIX) == 0)
6952 == ((condit & CONDIT_AFF) == 0
6953 || ae->ae_flags == NULL
6954 || !flag_in_afflist(affile->af_flagtype,
6955 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006956 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006957 /* Match. Remove the chop and add the affix. */
6958 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006959 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006960 /* prefix: chop/add at the start of the word */
6961 if (ae->ae_add == NULL)
6962 *newword = NUL;
6963 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006964 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006965 p = word;
6966 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006967 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006969#ifdef FEAT_MBYTE
6970 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006971 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006972 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006973 for ( ; i > 0; --i)
6974 mb_ptr_adv(p);
6975 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006976 else
6977#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006978 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006979 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006980 STRCAT(newword, p);
6981 }
6982 else
6983 {
6984 /* suffix: chop/add at the end of the word */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006985 vim_strncpy(newword, word, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006986 if (ae->ae_chop != NULL)
6987 {
6988 /* Remove chop string. */
6989 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006990 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006991 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006992 mb_ptr_back(newword, p);
6993 *p = NUL;
6994 }
6995 if (ae->ae_add != NULL)
6996 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006997 }
6998
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006999 use_flags = flags;
7000 use_pfxlist = pfxlist;
7001 use_pfxlen = pfxlen;
7002 need_affix = FALSE;
7003 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
7004 if (ae->ae_flags != NULL)
7005 {
7006 /* Extract flags from the affix list. */
7007 use_flags |= get_affix_flags(affile, ae->ae_flags);
7008
7009 if (affile->af_needaffix != 0 && flag_in_afflist(
7010 affile->af_flagtype, ae->ae_flags,
7011 affile->af_needaffix))
7012 need_affix = TRUE;
7013
7014 /* When there is a CIRCUMFIX flag the other affix
7015 * must also have it and we don't add the word
7016 * with one affix. */
7017 if (affile->af_circumfix != 0 && flag_in_afflist(
7018 affile->af_flagtype, ae->ae_flags,
7019 affile->af_circumfix))
7020 {
7021 use_condit |= CONDIT_CFIX;
7022 if ((condit & CONDIT_CFIX) == 0)
7023 need_affix = TRUE;
7024 }
7025
7026 if (affile->af_pfxpostpone
7027 || spin->si_compflags != NULL)
7028 {
7029 if (affile->af_pfxpostpone)
7030 /* Get prefix IDS from the affix list. */
7031 use_pfxlen = get_pfxlist(affile,
7032 ae->ae_flags, store_afflist);
7033 else
7034 use_pfxlen = 0;
7035 use_pfxlist = store_afflist;
7036
7037 /* Combine the prefix IDs. Avoid adding the
7038 * same ID twice. */
7039 for (i = 0; i < pfxlen; ++i)
7040 {
7041 for (j = 0; j < use_pfxlen; ++j)
7042 if (pfxlist[i] == use_pfxlist[j])
7043 break;
7044 if (j == use_pfxlen)
7045 use_pfxlist[use_pfxlen++] = pfxlist[i];
7046 }
7047
7048 if (spin->si_compflags != NULL)
7049 /* Get compound IDS from the affix list. */
7050 get_compflags(affile, ae->ae_flags,
7051 use_pfxlist + use_pfxlen);
7052
7053 /* Combine the list of compound flags.
7054 * Concatenate them to the prefix IDs list.
7055 * Avoid adding the same ID twice. */
7056 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7057 {
7058 for (j = use_pfxlen;
7059 use_pfxlist[j] != NUL; ++j)
7060 if (pfxlist[i] == use_pfxlist[j])
7061 break;
7062 if (use_pfxlist[j] == NUL)
7063 {
7064 use_pfxlist[j++] = pfxlist[i];
7065 use_pfxlist[j] = NUL;
7066 }
7067 }
7068 }
7069 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007070
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007071 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007072 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007073 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007074 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007075 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007076 use_pfxlist = pfx_pfxlist;
7077 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007078
7079 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007080 if (spin->si_prefroot != NULL
7081 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007082 {
7083 /* ... add a flag to indicate an affix was used. */
7084 use_flags |= WF_HAS_AFF;
7085
7086 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007087 * affixes is not allowed. But do use the
7088 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007089 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007090 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007091 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007092
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007093 /* When compounding is supported and there is no
7094 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7095 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007096 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007097 {
7098 if (xht != NULL)
7099 use_flags |= WF_NOCOMPAFT;
7100 else
7101 use_flags |= WF_NOCOMPBEF;
7102 }
7103
Bram Moolenaar51485f02005-06-04 21:55:20 +00007104 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007105 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007106 spin->si_region, use_pfxlist,
7107 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007108 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007109
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007110 /* When added a prefix or a first suffix and the affix
7111 * has flags may add a(nother) suffix. RECURSIVE! */
7112 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7113 if (store_aff_word(spin, newword, ae->ae_flags,
7114 affile, &affile->af_suff, xht,
7115 use_condit & (xht == NULL
7116 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007117 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007118 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007119
7120 /* When added a suffix and combining is allowed also
7121 * try adding a prefix additionally. Both for the
7122 * word flags and for the affix flags. RECURSIVE! */
7123 if (xht != NULL && ah->ah_combine)
7124 {
7125 if (store_aff_word(spin, newword,
7126 afflist, affile,
7127 xht, NULL, use_condit,
7128 use_flags, use_pfxlist,
7129 pfxlen) == FAIL
7130 || (ae->ae_flags != NULL
7131 && store_aff_word(spin, newword,
7132 ae->ae_flags, affile,
7133 xht, NULL, use_condit,
7134 use_flags, use_pfxlist,
7135 pfxlen) == FAIL))
7136 retval = FAIL;
7137 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007138 }
7139 }
7140 }
7141 }
7142 }
7143
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007144 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007145}
7146
7147/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007148 * Read a file with a list of words.
7149 */
7150 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007151spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007152 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007153 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007154{
7155 FILE *fd;
7156 long lnum = 0;
7157 char_u rline[MAXLINELEN];
7158 char_u *line;
7159 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007160 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007161 int l;
7162 int retval = OK;
7163 int did_word = FALSE;
7164 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007165 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007166 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007167
7168 /*
7169 * Open the file.
7170 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007171 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007172 if (fd == NULL)
7173 {
7174 EMSG2(_(e_notopen), fname);
7175 return FAIL;
7176 }
7177
Bram Moolenaar4770d092006-01-12 23:22:24 +00007178 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7179 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007180
7181 /*
7182 * Read all the lines in the file one by one.
7183 */
7184 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7185 {
7186 line_breakcheck();
7187 ++lnum;
7188
7189 /* Skip comment lines. */
7190 if (*rline == '#')
7191 continue;
7192
7193 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007194 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007195 while (l > 0 && rline[l - 1] <= ' ')
7196 --l;
7197 if (l == 0)
7198 continue; /* empty or blank line */
7199 rline[l] = NUL;
7200
Bram Moolenaar9c102382006-05-03 21:26:49 +00007201 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007203#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007204 if (spin->si_conv.vc_type != CONV_NONE)
7205 {
7206 pc = string_convert(&spin->si_conv, rline, NULL);
7207 if (pc == NULL)
7208 {
7209 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7210 fname, lnum, rline);
7211 continue;
7212 }
7213 line = pc;
7214 }
7215 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007216#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007217 {
7218 pc = NULL;
7219 line = rline;
7220 }
7221
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007222 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007223 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007224 ++line;
7225 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007226 {
7227 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007228 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7229 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007230 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007231 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7232 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233 else
7234 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007235#ifdef FEAT_MBYTE
7236 char_u *enc;
7237
Bram Moolenaar51485f02005-06-04 21:55:20 +00007238 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007239 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007240 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007241 if (enc != NULL && !spin->si_ascii
7242 && convert_setup(&spin->si_conv, enc,
7243 p_enc) == FAIL)
7244 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007245 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007246 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007247 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007248#else
7249 smsg((char_u *)_("Conversion in %s not supported"), fname);
7250#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007251 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007252 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007253 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007254
Bram Moolenaar3982c542005-06-08 21:56:31 +00007255 if (STRNCMP(line, "regions=", 8) == 0)
7256 {
7257 if (spin->si_region_count > 1)
7258 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7259 fname, lnum, line);
7260 else
7261 {
7262 line += 8;
7263 if (STRLEN(line) > 16)
7264 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7265 fname, lnum, line);
7266 else
7267 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007268 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007269 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007270
7271 /* Adjust the mask for a word valid in all regions. */
7272 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007273 }
7274 }
7275 continue;
7276 }
7277
Bram Moolenaar7887d882005-07-01 22:33:52 +00007278 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7279 fname, lnum, line - 1);
7280 continue;
7281 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007282
Bram Moolenaar7887d882005-07-01 22:33:52 +00007283 flags = 0;
7284 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007285
Bram Moolenaar7887d882005-07-01 22:33:52 +00007286 /* Check for flags and region after a slash. */
7287 p = vim_strchr(line, '/');
7288 if (p != NULL)
7289 {
7290 *p++ = NUL;
7291 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007292 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007293 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007294 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007295 else if (*p == '!') /* Bad, bad, wicked word. */
7296 flags |= WF_BANNED;
7297 else if (*p == '?') /* Rare word. */
7298 flags |= WF_RARE;
7299 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007300 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007301 if ((flags & WF_REGION) == 0) /* first one */
7302 regionmask = 0;
7303 flags |= WF_REGION;
7304
7305 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007306 if (l > spin->si_region_count)
7307 {
7308 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007309 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007310 break;
7311 }
7312 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007313 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007314 else
7315 {
7316 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7317 fname, lnum, p);
7318 break;
7319 }
7320 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007321 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007322 }
7323
7324 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7325 if (spin->si_ascii && has_non_ascii(line))
7326 {
7327 ++non_ascii;
7328 continue;
7329 }
7330
7331 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007332 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007333 {
7334 retval = FAIL;
7335 break;
7336 }
7337 did_word = TRUE;
7338 }
7339
7340 vim_free(pc);
7341 fclose(fd);
7342
Bram Moolenaar4770d092006-01-12 23:22:24 +00007343 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007344 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007345 vim_snprintf((char *)IObuff, IOSIZE,
7346 _("Ignored %d words with non-ASCII characters"), non_ascii);
7347 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007348 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007349
Bram Moolenaar51485f02005-06-04 21:55:20 +00007350 return retval;
7351}
7352
7353/*
7354 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007355 * This avoids calling free() for every little struct we use (and keeping
7356 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007357 * The memory is cleared to all zeros.
7358 * Returns NULL when out of memory.
7359 */
7360 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007361getroom(spin, len, align)
7362 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007363 size_t len; /* length needed */
7364 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007365{
7366 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007367 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007369 if (align && bl != NULL)
7370 /* Round size up for alignment. On some systems structures need to be
7371 * aligned to the size of a pointer (e.g., SPARC). */
7372 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7373 & ~(sizeof(char *) - 1);
7374
Bram Moolenaar51485f02005-06-04 21:55:20 +00007375 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7376 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007377 if (len >= SBLOCKSIZE)
7378 bl = NULL;
7379 else
7380 /* Allocate a block of memory. It is not freed until much later. */
7381 bl = (sblock_T *)alloc_clear(
7382 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007383 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007384 {
7385 if (!spin->si_did_emsg)
7386 {
7387 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7388 spin->si_did_emsg = TRUE;
7389 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007390 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007391 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007392 bl->sb_next = spin->si_blocks;
7393 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007394 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007395 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007396 }
7397
7398 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007399 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007400
7401 return p;
7402}
7403
7404/*
7405 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007406 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007407 */
7408 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007409getroom_save(spin, s)
7410 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007411 char_u *s;
7412{
7413 char_u *sc;
7414
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007415 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007416 if (sc != NULL)
7417 STRCPY(sc, s);
7418 return sc;
7419}
7420
7421
7422/*
7423 * Free the list of allocated sblock_T.
7424 */
7425 static void
7426free_blocks(bl)
7427 sblock_T *bl;
7428{
7429 sblock_T *next;
7430
7431 while (bl != NULL)
7432 {
7433 next = bl->sb_next;
7434 vim_free(bl);
7435 bl = next;
7436 }
7437}
7438
7439/*
7440 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007441 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007442 */
7443 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007444wordtree_alloc(spin)
7445 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007446{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007447 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007448}
7449
7450/*
7451 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007452 * Always store it in the case-folded tree. For a keep-case word this is
7453 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7454 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007455 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007456 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7457 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007458 */
7459 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007460store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007461 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007462 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007463 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007464 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007465 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007466 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007467{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007468 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007469 int ct = captype(word, word + len);
7470 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007471 int res = OK;
7472 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007474 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007475 for (p = pfxlist; res == OK; ++p)
7476 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007477 if (!need_affix || (p != NULL && *p != NUL))
7478 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007479 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007480 if (p == NULL || *p == NUL)
7481 break;
7482 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007483 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007484
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007485 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007486 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007487 for (p = pfxlist; res == OK; ++p)
7488 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007489 if (!need_affix || (p != NULL && *p != NUL))
7490 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007491 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007492 if (p == NULL || *p == NUL)
7493 break;
7494 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007495 ++spin->si_keepwcount;
7496 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007497 return res;
7498}
7499
7500/*
7501 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007502 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007503 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007504 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007505 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007506 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007507tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007508 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007509 char_u *word;
7510 wordnode_T *root;
7511 int flags;
7512 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007513 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007514{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007515 wordnode_T *node = root;
7516 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007517 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007518 wordnode_T **prev = NULL;
7519 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007520
Bram Moolenaar51485f02005-06-04 21:55:20 +00007521 /* Add each byte of the word to the tree, including the NUL at the end. */
7522 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007523 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007524 /* When there is more than one reference to this node we need to make
7525 * a copy, so that we can modify it. Copy the whole list of siblings
7526 * (we don't optimize for a partly shared list of siblings). */
7527 if (node != NULL && node->wn_refs > 1)
7528 {
7529 --node->wn_refs;
7530 copyprev = prev;
7531 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7532 {
7533 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007534 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007535 if (np == NULL)
7536 return FAIL;
7537 np->wn_child = copyp->wn_child;
7538 if (np->wn_child != NULL)
7539 ++np->wn_child->wn_refs; /* child gets extra ref */
7540 np->wn_byte = copyp->wn_byte;
7541 if (np->wn_byte == NUL)
7542 {
7543 np->wn_flags = copyp->wn_flags;
7544 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007545 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007546 }
7547
7548 /* Link the new node in the list, there will be one ref. */
7549 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007550 if (copyprev != NULL)
7551 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007552 copyprev = &np->wn_sibling;
7553
7554 /* Let "node" point to the head of the copied list. */
7555 if (copyp == node)
7556 node = np;
7557 }
7558 }
7559
Bram Moolenaar51485f02005-06-04 21:55:20 +00007560 /* Look for the sibling that has the same character. They are sorted
7561 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007562 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007563 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007564 while (node != NULL
7565 && (node->wn_byte < word[i]
7566 || (node->wn_byte == NUL
7567 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007568 ? node->wn_affixID < (unsigned)affixID
7569 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007570 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007571 && (spin->si_sugtree
7572 ? (node->wn_region & 0xffff) < region
7573 : node->wn_affixID
7574 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007575 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007576 prev = &node->wn_sibling;
7577 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007578 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007579 if (node == NULL
7580 || node->wn_byte != word[i]
7581 || (word[i] == NUL
7582 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007583 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007584 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007585 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007586 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007587 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007588 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007589 if (np == NULL)
7590 return FAIL;
7591 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007592
7593 /* If "node" is NULL this is a new child or the end of the sibling
7594 * list: ref count is one. Otherwise use ref count of sibling and
7595 * make ref count of sibling one (matters when inserting in front
7596 * of the list of siblings). */
7597 if (node == NULL)
7598 np->wn_refs = 1;
7599 else
7600 {
7601 np->wn_refs = node->wn_refs;
7602 node->wn_refs = 1;
7603 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007604 if (prev != NULL)
7605 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007606 np->wn_sibling = node;
7607 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007608 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007609
Bram Moolenaar51485f02005-06-04 21:55:20 +00007610 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007611 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007612 node->wn_flags = flags;
7613 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007614 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007615 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007616 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007617 prev = &node->wn_child;
7618 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007619 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007620#ifdef SPELL_PRINTTREE
7621 smsg("Added \"%s\"", word);
7622 spell_print_tree(root->wn_sibling);
7623#endif
7624
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007625 /* count nr of words added since last message */
7626 ++spin->si_msg_count;
7627
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007628 if (spin->si_compress_cnt > 1)
7629 {
7630 if (--spin->si_compress_cnt == 1)
7631 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007632 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007633 }
7634
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007635 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007636 * When we have allocated lots of memory we need to compress the word tree
7637 * to free up some room. But compression is slow, and we might actually
7638 * need that room, thus only compress in the following situations:
7639 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007640 * "compress_start" blocks.
7641 * 2. When compressed before and used "compress_inc" blocks before
7642 * adding "compress_added" words (si_compress_cnt > 1).
7643 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007644 * (si_compress_cnt == 1) and the number of free nodes drops below the
7645 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007646 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007647#ifndef SPELL_PRINTTREE
7648 if (spin->si_compress_cnt == 1
7649 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007650 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007651#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007652 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007653 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007654 * when the freed up room has been used and another "compress_inc"
7655 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007656 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007657 spin->si_blocks_cnt -= compress_inc;
7658 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007659
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007660 if (spin->si_verbose)
7661 {
7662 msg_start();
7663 msg_puts((char_u *)_(msg_compressing));
7664 msg_clr_eos();
7665 msg_didout = FALSE;
7666 msg_col = 0;
7667 out_flush();
7668 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007669
7670 /* Compress both trees. Either they both have many nodes, which makes
7671 * compression useful, or one of them is small, which means
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007672 * compression goes fast. But when filling the soundfold word tree
Bram Moolenaar4770d092006-01-12 23:22:24 +00007673 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007674 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007675 if (affixID >= 0)
7676 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007677 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007678
7679 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007680}
7681
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007682/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007683 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7684 * Sets "sps_flags".
7685 */
7686 int
7687spell_check_msm()
7688{
7689 char_u *p = p_msm;
7690 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007691 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007692 long added = 0;
7693
7694 if (!VIM_ISDIGIT(*p))
7695 return FAIL;
7696 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7697 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7698 if (*p != ',')
7699 return FAIL;
7700 ++p;
7701 if (!VIM_ISDIGIT(*p))
7702 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007703 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007704 if (*p != ',')
7705 return FAIL;
7706 ++p;
7707 if (!VIM_ISDIGIT(*p))
7708 return FAIL;
7709 added = getdigits(&p) * 1024;
7710 if (*p != NUL)
7711 return FAIL;
7712
Bram Moolenaar89d40322006-08-29 15:30:07 +00007713 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007714 return FAIL;
7715
7716 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007717 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007718 compress_added = added;
7719 return OK;
7720}
7721
7722
7723/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007724 * Get a wordnode_T, either from the list of previously freed nodes or
7725 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007726 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007727 */
7728 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007729get_wordnode(spin)
7730 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007731{
7732 wordnode_T *n;
7733
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007734 if (spin->si_first_free == NULL)
7735 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007736 else
7737 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007738 n = spin->si_first_free;
7739 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007740 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007741 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007742 }
7743#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007744 if (n != NULL)
7745 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007746#endif
7747 return n;
7748}
7749
7750/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007751 * Decrement the reference count on a node (which is the head of a list of
7752 * siblings). If the reference count becomes zero free the node and its
7753 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007754 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007755 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007756 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007757deref_wordnode(spin, node)
7758 spellinfo_T *spin;
7759 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007760{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007761 wordnode_T *np;
7762 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007763
7764 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007765 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007766 for (np = node; np != NULL; np = np->wn_sibling)
7767 {
7768 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007769 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007770 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007771 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007772 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007773 ++cnt; /* length field */
7774 }
7775 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007776}
7777
7778/*
7779 * Free a wordnode_T for re-use later.
7780 * Only the "wn_child" field becomes invalid.
7781 */
7782 static void
7783free_wordnode(spin, n)
7784 spellinfo_T *spin;
7785 wordnode_T *n;
7786{
7787 n->wn_child = spin->si_first_free;
7788 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007789 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007790}
7791
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007792/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007793 * Compress a tree: find tails that are identical and can be shared.
7794 */
7795 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007796wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007797 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007798 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007799{
7800 hashtab_T ht;
7801 int n;
7802 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007803 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007804
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007805 /* Skip the root itself, it's not actually used. The first sibling is the
7806 * start of the tree. */
7807 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007808 {
7809 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007810 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007811
7812#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007813 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007814#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007815 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007816 if (tot > 1000000)
7817 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007818 else if (tot == 0)
7819 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007820 else
7821 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007822 vim_snprintf((char *)IObuff, IOSIZE,
7823 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7824 n, tot, tot - n, perc);
7825 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007826 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007827#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007828 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007829#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007830 hash_clear(&ht);
7831 }
7832}
7833
7834/*
7835 * Compress a node, its siblings and its children, depth first.
7836 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007837 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007838 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007839node_compress(spin, node, ht, tot)
7840 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007841 wordnode_T *node;
7842 hashtab_T *ht;
7843 int *tot; /* total count of nodes before compressing,
7844 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007845{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007846 wordnode_T *np;
7847 wordnode_T *tp;
7848 wordnode_T *child;
7849 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007850 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007851 int len = 0;
7852 unsigned nr, n;
7853 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007854
Bram Moolenaar51485f02005-06-04 21:55:20 +00007855 /*
7856 * Go through the list of siblings. Compress each child and then try
7857 * finding an identical child to replace it.
7858 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007859 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007860 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007861 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007862 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007863 ++len;
7864 if ((child = np->wn_child) != NULL)
7865 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007866 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007867 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007868
7869 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007870 hash = hash_hash(child->wn_u1.hashkey);
7871 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007872 if (!HASHITEM_EMPTY(hi))
7873 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007874 /* There are children we encountered before with a hash value
7875 * identical to the current child. Now check if there is one
7876 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007877 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007878 if (node_equal(child, tp))
7879 {
7880 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007881 * current one. This means the current child and all
7882 * its siblings is unlinked from the tree. */
7883 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007884 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007885 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007886 break;
7887 }
7888 if (tp == NULL)
7889 {
7890 /* No other child with this hash value equals the child of
7891 * the node, add it to the linked list after the first
7892 * item. */
7893 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007894 child->wn_u2.next = tp->wn_u2.next;
7895 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007896 }
7897 }
7898 else
7899 /* No other child has this hash value, add it to the
7900 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007901 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007902 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007903 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007904 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007905
7906 /*
7907 * Make a hash key for the node and its siblings, so that we can quickly
7908 * find a lookalike node. This must be done after compressing the sibling
7909 * list, otherwise the hash key would become invalid by the compression.
7910 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007911 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007912 nr = 0;
7913 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007914 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007915 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007916 /* end node: use wn_flags, wn_region and wn_affixID */
7917 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007918 else
7919 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007920 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007921 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007922 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007923
7924 /* Avoid NUL bytes, it terminates the hash key. */
7925 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007926 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007927 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007928 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007929 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007930 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007931 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007932 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7933 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007934
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007935 /* Check for CTRL-C pressed now and then. */
7936 fast_breakcheck();
7937
Bram Moolenaar51485f02005-06-04 21:55:20 +00007938 return compressed;
7939}
7940
7941/*
7942 * Return TRUE when two nodes have identical siblings and children.
7943 */
7944 static int
7945node_equal(n1, n2)
7946 wordnode_T *n1;
7947 wordnode_T *n2;
7948{
7949 wordnode_T *p1;
7950 wordnode_T *p2;
7951
7952 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7953 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7954 if (p1->wn_byte != p2->wn_byte
7955 || (p1->wn_byte == NUL
7956 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007957 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007958 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007959 : (p1->wn_child != p2->wn_child)))
7960 break;
7961
7962 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007963}
7964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007965static int
7966#ifdef __BORLANDC__
7967_RTLENTRYF
7968#endif
7969rep_compare __ARGS((const void *s1, const void *s2));
7970
7971/*
7972 * Function given to qsort() to sort the REP items on "from" string.
7973 */
7974 static int
7975#ifdef __BORLANDC__
7976_RTLENTRYF
7977#endif
7978rep_compare(s1, s2)
7979 const void *s1;
7980 const void *s2;
7981{
7982 fromto_T *p1 = (fromto_T *)s1;
7983 fromto_T *p2 = (fromto_T *)s2;
7984
7985 return STRCMP(p1->ft_from, p2->ft_from);
7986}
7987
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007988/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007989 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007990 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007991 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007992 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007993write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007994 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007995 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007996{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007997 FILE *fd;
7998 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007999 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008000 wordnode_T *tree;
8001 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 int i;
8003 int l;
8004 garray_T *gap;
8005 fromto_T *ftp;
8006 char_u *p;
8007 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008008 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008009 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008010 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008011
Bram Moolenaarb765d632005-06-07 21:00:02 +00008012 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00008013 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008014 {
8015 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008016 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008017 }
8018
Bram Moolenaar5195e452005-08-19 20:32:47 +00008019 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008020 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008021 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008022 if (fwv != (size_t)1)
8023 /* Catch first write error, don't try writing more. */
8024 goto theend;
8025
Bram Moolenaar5195e452005-08-19 20:32:47 +00008026 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008027
Bram Moolenaar5195e452005-08-19 20:32:47 +00008028 /*
8029 * <SECTIONS>: <section> ... <sectionend>
8030 */
8031
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008032 /* SN_INFO: <infotext> */
8033 if (spin->si_info != NULL)
8034 {
8035 putc(SN_INFO, fd); /* <sectionID> */
8036 putc(0, fd); /* <sectionflags> */
8037
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008038 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008039 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008040 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008041 }
8042
Bram Moolenaar5195e452005-08-19 20:32:47 +00008043 /* SN_REGION: <regionname> ...
8044 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008045 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008046 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008047 putc(SN_REGION, fd); /* <sectionID> */
8048 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8049 l = spin->si_region_count * 2;
8050 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008051 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008052 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008053 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008054 }
8055 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008056 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008057
Bram Moolenaar5195e452005-08-19 20:32:47 +00008058 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8059 *
8060 * The table with character flags and the table for case folding.
8061 * This makes sure the same characters are recognized as word characters
8062 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008063 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008064 * 'encoding'.
8065 * Also skip this for an .add.spl file, the main spell file must contain
8066 * the table (avoids that it conflicts). File is shorter too.
8067 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008068 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008069 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008070 char_u folchars[128 * 8];
8071 int flags;
8072
Bram Moolenaard12a1322005-08-21 22:08:24 +00008073 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008074 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8075
8076 /* Form the <folchars> string first, we need to know its length. */
8077 l = 0;
8078 for (i = 128; i < 256; ++i)
8079 {
8080#ifdef FEAT_MBYTE
8081 if (has_mbyte)
8082 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8083 else
8084#endif
8085 folchars[l++] = spelltab.st_fold[i];
8086 }
8087 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8088
8089 fputc(128, fd); /* <charflagslen> */
8090 for (i = 128; i < 256; ++i)
8091 {
8092 flags = 0;
8093 if (spelltab.st_isw[i])
8094 flags |= CF_WORD;
8095 if (spelltab.st_isu[i])
8096 flags |= CF_UPPER;
8097 fputc(flags, fd); /* <charflags> */
8098 }
8099
8100 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008101 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008102 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008103
Bram Moolenaar5195e452005-08-19 20:32:47 +00008104 /* SN_MIDWORD: <midword> */
8105 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008106 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008107 putc(SN_MIDWORD, fd); /* <sectionID> */
8108 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8109
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008110 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008111 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008112 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8113 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008114 }
8115
Bram Moolenaar5195e452005-08-19 20:32:47 +00008116 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8117 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008118 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008119 putc(SN_PREFCOND, fd); /* <sectionID> */
8120 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8121
8122 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8123 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8124
8125 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008126 }
8127
Bram Moolenaar5195e452005-08-19 20:32:47 +00008128 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008129 * SN_SAL: <salflags> <salcount> <sal> ...
8130 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008131
Bram Moolenaar5195e452005-08-19 20:32:47 +00008132 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008133 * round 2: SN_SAL section (unless SN_SOFO is used)
8134 * round 3: SN_REPSAL section */
8135 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008136 {
8137 if (round == 1)
8138 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008139 else if (round == 2)
8140 {
8141 /* Don't write SN_SAL when using a SN_SOFO section */
8142 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8143 continue;
8144 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008146 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008147 gap = &spin->si_repsal;
8148
8149 /* Don't write the section if there are no items. */
8150 if (gap->ga_len == 0)
8151 continue;
8152
8153 /* Sort the REP/REPSAL items. */
8154 if (round != 2)
8155 qsort(gap->ga_data, (size_t)gap->ga_len,
8156 sizeof(fromto_T), rep_compare);
8157
8158 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8159 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008160
Bram Moolenaar5195e452005-08-19 20:32:47 +00008161 /* This is for making suggestions, section is not required. */
8162 putc(0, fd); /* <sectionflags> */
8163
8164 /* Compute the length of what follows. */
8165 l = 2; /* count <repcount> or <salcount> */
8166 for (i = 0; i < gap->ga_len; ++i)
8167 {
8168 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008169 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8170 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008171 }
8172 if (round == 2)
8173 ++l; /* count <salflags> */
8174 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8175
8176 if (round == 2)
8177 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178 i = 0;
8179 if (spin->si_followup)
8180 i |= SAL_F0LLOWUP;
8181 if (spin->si_collapse)
8182 i |= SAL_COLLAPSE;
8183 if (spin->si_rem_accents)
8184 i |= SAL_REM_ACCENTS;
8185 putc(i, fd); /* <salflags> */
8186 }
8187
8188 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8189 for (i = 0; i < gap->ga_len; ++i)
8190 {
8191 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8192 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8193 ftp = &((fromto_T *)gap->ga_data)[i];
8194 for (rr = 1; rr <= 2; ++rr)
8195 {
8196 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008197 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008198 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008199 if (l > 0)
8200 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008201 }
8202 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 }
8205
Bram Moolenaar5195e452005-08-19 20:32:47 +00008206 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8207 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008208 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8209 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008210 putc(SN_SOFO, fd); /* <sectionID> */
8211 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008213 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008214 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8215 /* <sectionlen> */
8216
8217 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008218 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008220 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008221 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008222 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008223 }
8224
Bram Moolenaar4770d092006-01-12 23:22:24 +00008225 /* SN_WORDS: <word> ...
8226 * This is for making suggestions, section is not required. */
8227 if (spin->si_commonwords.ht_used > 0)
8228 {
8229 putc(SN_WORDS, fd); /* <sectionID> */
8230 putc(0, fd); /* <sectionflags> */
8231
8232 /* round 1: count the bytes
8233 * round 2: write the bytes */
8234 for (round = 1; round <= 2; ++round)
8235 {
8236 int todo;
8237 int len = 0;
8238 hashitem_T *hi;
8239
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008240 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008241 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8242 if (!HASHITEM_EMPTY(hi))
8243 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008244 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008245 len += l;
8246 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008247 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008248 --todo;
8249 }
8250 if (round == 1)
8251 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8252 }
8253 }
8254
Bram Moolenaar5195e452005-08-19 20:32:47 +00008255 /* SN_MAP: <mapstr>
8256 * This is for making suggestions, section is not required. */
8257 if (spin->si_map.ga_len > 0)
8258 {
8259 putc(SN_MAP, fd); /* <sectionID> */
8260 putc(0, fd); /* <sectionflags> */
8261 l = spin->si_map.ga_len;
8262 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008263 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008264 /* <mapstr> */
8265 }
8266
Bram Moolenaar4770d092006-01-12 23:22:24 +00008267 /* SN_SUGFILE: <timestamp>
8268 * This is used to notify that a .sug file may be available and at the
8269 * same time allows for checking that a .sug file that is found matches
8270 * with this .spl file. That's because the word numbers must be exactly
8271 * right. */
8272 if (!spin->si_nosugfile
8273 && (spin->si_sal.ga_len > 0
8274 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8275 {
8276 putc(SN_SUGFILE, fd); /* <sectionID> */
8277 putc(0, fd); /* <sectionflags> */
8278 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8279
8280 /* Set si_sugtime and write it to the file. */
8281 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008282 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008283 }
8284
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008285 /* SN_NOSPLITSUGS: nothing
8286 * This is used to notify that no suggestions with word splits are to be
8287 * made. */
8288 if (spin->si_nosplitsugs)
8289 {
8290 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8291 putc(0, fd); /* <sectionflags> */
8292 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8293 }
8294
Bram Moolenaar5195e452005-08-19 20:32:47 +00008295 /* SN_COMPOUND: compound info.
8296 * We don't mark it required, when not supported all compound words will
8297 * be bad words. */
8298 if (spin->si_compflags != NULL)
8299 {
8300 putc(SN_COMPOUND, fd); /* <sectionID> */
8301 putc(0, fd); /* <sectionflags> */
8302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008303 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008304 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008305 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008306 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8307
Bram Moolenaar5195e452005-08-19 20:32:47 +00008308 putc(spin->si_compmax, fd); /* <compmax> */
8309 putc(spin->si_compminlen, fd); /* <compminlen> */
8310 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008311 putc(0, fd); /* for Vim 7.0b compatibility */
8312 putc(spin->si_compoptions, fd); /* <compoptions> */
8313 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8314 /* <comppatcount> */
8315 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8316 {
8317 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008318 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008319 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8320 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008321 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008322 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008323 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008324 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008325 }
8326
Bram Moolenaar78622822005-08-23 21:00:13 +00008327 /* SN_NOBREAK: NOBREAK flag */
8328 if (spin->si_nobreak)
8329 {
8330 putc(SN_NOBREAK, fd); /* <sectionID> */
8331 putc(0, fd); /* <sectionflags> */
8332
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008333 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008334 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8335 }
8336
Bram Moolenaar5195e452005-08-19 20:32:47 +00008337 /* SN_SYLLABLE: syllable info.
8338 * We don't mark it required, when not supported syllables will not be
8339 * counted. */
8340 if (spin->si_syllable != NULL)
8341 {
8342 putc(SN_SYLLABLE, fd); /* <sectionID> */
8343 putc(0, fd); /* <sectionflags> */
8344
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008345 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008346 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008347 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8348 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008349 }
8350
8351 /* end of <SECTIONS> */
8352 putc(SN_END, fd); /* <sectionend> */
8353
Bram Moolenaar50cde822005-06-05 21:54:54 +00008354
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008355 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008356 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008357 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008358 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008359 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008360 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008361 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008362 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008363 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008364 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008365 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008366 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008367
Bram Moolenaar0c405862005-06-22 22:26:26 +00008368 /* Clear the index and wnode fields in the tree. */
8369 clear_node(tree);
8370
Bram Moolenaar51485f02005-06-04 21:55:20 +00008371 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008372 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008373 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008374 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008375
Bram Moolenaar51485f02005-06-04 21:55:20 +00008376 /* number of nodes in 4 bytes */
8377 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008378 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008379
Bram Moolenaar51485f02005-06-04 21:55:20 +00008380 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008381 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008382 }
8383
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008384 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008385 if (putc(0, fd) == EOF)
8386 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008387theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008388 if (fclose(fd) == EOF)
8389 retval = FAIL;
8390
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008391 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008392 retval = FAIL;
8393 if (retval == FAIL)
8394 EMSG(_(e_write));
8395
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008396 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008397}
8398
8399/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008400 * Clear the index and wnode fields of "node", it siblings and its
8401 * children. This is needed because they are a union with other items to save
8402 * space.
8403 */
8404 static void
8405clear_node(node)
8406 wordnode_T *node;
8407{
8408 wordnode_T *np;
8409
8410 if (node != NULL)
8411 for (np = node; np != NULL; np = np->wn_sibling)
8412 {
8413 np->wn_u1.index = 0;
8414 np->wn_u2.wnode = NULL;
8415
8416 if (np->wn_byte != NUL)
8417 clear_node(np->wn_child);
8418 }
8419}
8420
8421
8422/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008423 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008424 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008425 * This first writes the list of possible bytes (siblings). Then for each
8426 * byte recursively write the children.
8427 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008428 * NOTE: The code here must match the code in read_tree_node(), since
8429 * assumptions are made about the indexes (so that we don't have to write them
8430 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008431 *
8432 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008433 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008434 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008435put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008436 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008437 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008438 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008439 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008440 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008441{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008442 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008443 int siblingcount = 0;
8444 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008445 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008446
Bram Moolenaar51485f02005-06-04 21:55:20 +00008447 /* If "node" is zero the tree is empty. */
8448 if (node == NULL)
8449 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008450
Bram Moolenaar51485f02005-06-04 21:55:20 +00008451 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008452 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008453
8454 /* Count the number of siblings. */
8455 for (np = node; np != NULL; np = np->wn_sibling)
8456 ++siblingcount;
8457
8458 /* Write the sibling count. */
8459 if (fd != NULL)
8460 putc(siblingcount, fd); /* <siblingcount> */
8461
8462 /* Write each sibling byte and optionally extra info. */
8463 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008464 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008465 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008466 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008467 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008468 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008469 /* For a NUL byte (end of word) write the flags etc. */
8470 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008471 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008472 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008473 * associated condition nr (stored in wn_region). The
8474 * byte value is misused to store the "rare" and "not
8475 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008476 if (np->wn_flags == (short_u)PFX_FLAGS)
8477 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008478 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008479 {
8480 putc(BY_FLAGS, fd); /* <byte> */
8481 putc(np->wn_flags, fd); /* <pflags> */
8482 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008483 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008484 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008485 }
8486 else
8487 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008488 /* For word trees we write the flag/region items. */
8489 flags = np->wn_flags;
8490 if (regionmask != 0 && np->wn_region != regionmask)
8491 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008492 if (np->wn_affixID != 0)
8493 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008494 if (flags == 0)
8495 {
8496 /* word without flags or region */
8497 putc(BY_NOFLAGS, fd); /* <byte> */
8498 }
8499 else
8500 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008501 if (np->wn_flags >= 0x100)
8502 {
8503 putc(BY_FLAGS2, fd); /* <byte> */
8504 putc(flags, fd); /* <flags> */
8505 putc((unsigned)flags >> 8, fd); /* <flags2> */
8506 }
8507 else
8508 {
8509 putc(BY_FLAGS, fd); /* <byte> */
8510 putc(flags, fd); /* <flags> */
8511 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008512 if (flags & WF_REGION)
8513 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008514 if (flags & WF_AFX)
8515 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008516 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008517 }
8518 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008519 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008520 else
8521 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008522 if (np->wn_child->wn_u1.index != 0
8523 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008524 {
8525 /* The child is written elsewhere, write the reference. */
8526 if (fd != NULL)
8527 {
8528 putc(BY_INDEX, fd); /* <byte> */
8529 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008530 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008531 }
8532 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008533 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008534 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008535 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008536
Bram Moolenaar51485f02005-06-04 21:55:20 +00008537 if (fd != NULL)
8538 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8539 {
8540 EMSG(_(e_write));
8541 return 0;
8542 }
8543 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008544 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008545
8546 /* Space used in the array when reading: one for each sibling and one for
8547 * the count. */
8548 newindex += siblingcount + 1;
8549
8550 /* Recursively dump the children of each sibling. */
8551 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008552 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8553 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008554 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008555
8556 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008557}
8558
8559
8560/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008561 * ":mkspell [-ascii] outfile infile ..."
8562 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008563 */
8564 void
8565ex_mkspell(eap)
8566 exarg_T *eap;
8567{
8568 int fcount;
8569 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008570 char_u *arg = eap->arg;
8571 int ascii = FALSE;
8572
8573 if (STRNCMP(arg, "-ascii", 6) == 0)
8574 {
8575 ascii = TRUE;
8576 arg = skipwhite(arg + 6);
8577 }
8578
8579 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02008580 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008583 FreeWild(fcount, fnames);
8584 }
8585}
8586
8587/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008588 * Create the .sug file.
8589 * Uses the soundfold info in "spin".
8590 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8591 */
8592 static void
8593spell_make_sugfile(spin, wfname)
8594 spellinfo_T *spin;
8595 char_u *wfname;
8596{
Bram Moolenaard9462e32011-04-11 21:35:11 +02008597 char_u *fname = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008598 int len;
8599 slang_T *slang;
8600 int free_slang = FALSE;
8601
8602 /*
8603 * Read back the .spl file that was written. This fills the required
8604 * info for soundfolding. This also uses less memory than the
8605 * pointer-linked version of the trie. And it avoids having two versions
8606 * of the code for the soundfolding stuff.
8607 * It might have been done already by spell_reload_one().
8608 */
8609 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8610 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8611 break;
8612 if (slang == NULL)
8613 {
8614 spell_message(spin, (char_u *)_("Reading back spell file..."));
8615 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8616 if (slang == NULL)
8617 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008618 free_slang = TRUE;
8619 }
8620
8621 /*
8622 * Clear the info in "spin" that is used.
8623 */
8624 spin->si_blocks = NULL;
8625 spin->si_blocks_cnt = 0;
8626 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8627 spin->si_free_count = 0;
8628 spin->si_first_free = NULL;
8629 spin->si_foldwcount = 0;
8630
8631 /*
8632 * Go through the trie of good words, soundfold each word and add it to
8633 * the soundfold trie.
8634 */
8635 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8636 if (sug_filltree(spin, slang) == FAIL)
8637 goto theend;
8638
8639 /*
8640 * Create the table which links each soundfold word with a list of the
8641 * good words it may come from. Creates buffer "spin->si_spellbuf".
8642 * This also removes the wordnr from the NUL byte entries to make
8643 * compression possible.
8644 */
8645 if (sug_maketable(spin) == FAIL)
8646 goto theend;
8647
8648 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8649 (long)spin->si_spellbuf->b_ml.ml_line_count);
8650
8651 /*
8652 * Compress the soundfold trie.
8653 */
8654 spell_message(spin, (char_u *)_(msg_compressing));
8655 wordtree_compress(spin, spin->si_foldroot);
8656
8657 /*
8658 * Write the .sug file.
8659 * Make the file name by changing ".spl" to ".sug".
8660 */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008661 fname = alloc(MAXPATHL);
8662 if (fname == NULL)
8663 goto theend;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008664 vim_strncpy(fname, wfname, MAXPATHL - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008665 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008666 fname[len - 2] = 'u';
8667 fname[len - 1] = 'g';
8668 sug_write(spin, fname);
8669
8670theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02008671 vim_free(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008672 if (free_slang)
8673 slang_free(slang);
8674 free_blocks(spin->si_blocks);
8675 close_spellbuf(spin->si_spellbuf);
8676}
8677
8678/*
8679 * Build the soundfold trie for language "slang".
8680 */
8681 static int
8682sug_filltree(spin, slang)
8683 spellinfo_T *spin;
8684 slang_T *slang;
8685{
8686 char_u *byts;
8687 idx_T *idxs;
8688 int depth;
8689 idx_T arridx[MAXWLEN];
8690 int curi[MAXWLEN];
8691 char_u tword[MAXWLEN];
8692 char_u tsalword[MAXWLEN];
8693 int c;
8694 idx_T n;
8695 unsigned words_done = 0;
8696 int wordcount[MAXWLEN];
8697
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008698 /* We use si_foldroot for the soundfolded trie. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008699 spin->si_foldroot = wordtree_alloc(spin);
8700 if (spin->si_foldroot == NULL)
8701 return FAIL;
8702
8703 /* let tree_add_word() know we're adding to the soundfolded tree */
8704 spin->si_sugtree = TRUE;
8705
8706 /*
8707 * Go through the whole case-folded tree, soundfold each word and put it
8708 * in the trie.
8709 */
8710 byts = slang->sl_fbyts;
8711 idxs = slang->sl_fidxs;
8712
8713 arridx[0] = 0;
8714 curi[0] = 1;
8715 wordcount[0] = 0;
8716
8717 depth = 0;
8718 while (depth >= 0 && !got_int)
8719 {
8720 if (curi[depth] > byts[arridx[depth]])
8721 {
8722 /* Done all bytes at this node, go up one level. */
8723 idxs[arridx[depth]] = wordcount[depth];
8724 if (depth > 0)
8725 wordcount[depth - 1] += wordcount[depth];
8726
8727 --depth;
8728 line_breakcheck();
8729 }
8730 else
8731 {
8732
8733 /* Do one more byte at this node. */
8734 n = arridx[depth] + curi[depth];
8735 ++curi[depth];
8736
8737 c = byts[n];
8738 if (c == 0)
8739 {
8740 /* Sound-fold the word. */
8741 tword[depth] = NUL;
8742 spell_soundfold(slang, tword, TRUE, tsalword);
8743
8744 /* We use the "flags" field for the MSB of the wordnr,
8745 * "region" for the LSB of the wordnr. */
8746 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8747 words_done >> 16, words_done & 0xffff,
8748 0) == FAIL)
8749 return FAIL;
8750
8751 ++words_done;
8752 ++wordcount[depth];
8753
8754 /* Reset the block count each time to avoid compression
8755 * kicking in. */
8756 spin->si_blocks_cnt = 0;
8757
8758 /* Skip over any other NUL bytes (same word with different
8759 * flags). */
8760 while (byts[n + 1] == 0)
8761 {
8762 ++n;
8763 ++curi[depth];
8764 }
8765 }
8766 else
8767 {
8768 /* Normal char, go one level deeper. */
8769 tword[depth++] = c;
8770 arridx[depth] = idxs[n];
8771 curi[depth] = 1;
8772 wordcount[depth] = 0;
8773 }
8774 }
8775 }
8776
8777 smsg((char_u *)_("Total number of words: %d"), words_done);
8778
8779 return OK;
8780}
8781
8782/*
8783 * Make the table that links each word in the soundfold trie to the words it
8784 * can be produced from.
8785 * This is not unlike lines in a file, thus use a memfile to be able to access
8786 * the table efficiently.
8787 * Returns FAIL when out of memory.
8788 */
8789 static int
8790sug_maketable(spin)
8791 spellinfo_T *spin;
8792{
8793 garray_T ga;
8794 int res = OK;
8795
8796 /* Allocate a buffer, open a memline for it and create the swap file
8797 * (uses a temp file, not a .swp file). */
8798 spin->si_spellbuf = open_spellbuf();
8799 if (spin->si_spellbuf == NULL)
8800 return FAIL;
8801
8802 /* Use a buffer to store the line info, avoids allocating many small
8803 * pieces of memory. */
8804 ga_init2(&ga, 1, 100);
8805
8806 /* recursively go through the tree */
8807 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8808 res = FAIL;
8809
8810 ga_clear(&ga);
8811 return res;
8812}
8813
8814/*
8815 * Fill the table for one node and its children.
8816 * Returns the wordnr at the start of the node.
8817 * Returns -1 when out of memory.
8818 */
8819 static int
8820sug_filltable(spin, node, startwordnr, gap)
8821 spellinfo_T *spin;
8822 wordnode_T *node;
8823 int startwordnr;
8824 garray_T *gap; /* place to store line of numbers */
8825{
8826 wordnode_T *p, *np;
8827 int wordnr = startwordnr;
8828 int nr;
8829 int prev_nr;
8830
8831 for (p = node; p != NULL; p = p->wn_sibling)
8832 {
8833 if (p->wn_byte == NUL)
8834 {
8835 gap->ga_len = 0;
8836 prev_nr = 0;
8837 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8838 {
8839 if (ga_grow(gap, 10) == FAIL)
8840 return -1;
8841
8842 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8843 /* Compute the offset from the previous nr and store the
8844 * offset in a way that it takes a minimum number of bytes.
8845 * It's a bit like utf-8, but without the need to mark
8846 * following bytes. */
8847 nr -= prev_nr;
8848 prev_nr += nr;
8849 gap->ga_len += offset2bytes(nr,
8850 (char_u *)gap->ga_data + gap->ga_len);
8851 }
8852
8853 /* add the NUL byte */
8854 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8855
8856 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8857 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8858 return -1;
8859 ++wordnr;
8860
8861 /* Remove extra NUL entries, we no longer need them. We don't
8862 * bother freeing the nodes, the won't be reused anyway. */
8863 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8864 p->wn_sibling = p->wn_sibling->wn_sibling;
8865
8866 /* Clear the flags on the remaining NUL node, so that compression
8867 * works a lot better. */
8868 p->wn_flags = 0;
8869 p->wn_region = 0;
8870 }
8871 else
8872 {
8873 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8874 if (wordnr == -1)
8875 return -1;
8876 }
8877 }
8878 return wordnr;
8879}
8880
8881/*
8882 * Convert an offset into a minimal number of bytes.
8883 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8884 * bytes.
8885 */
8886 static int
8887offset2bytes(nr, buf)
8888 int nr;
8889 char_u *buf;
8890{
8891 int rem;
8892 int b1, b2, b3, b4;
8893
8894 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8895 b1 = nr % 255 + 1;
8896 rem = nr / 255;
8897 b2 = rem % 255 + 1;
8898 rem = rem / 255;
8899 b3 = rem % 255 + 1;
8900 b4 = rem / 255 + 1;
8901
8902 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8903 {
8904 buf[0] = 0xe0 + b4;
8905 buf[1] = b3;
8906 buf[2] = b2;
8907 buf[3] = b1;
8908 return 4;
8909 }
8910 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8911 {
8912 buf[0] = 0xc0 + b3;
8913 buf[1] = b2;
8914 buf[2] = b1;
8915 return 3;
8916 }
8917 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8918 {
8919 buf[0] = 0x80 + b2;
8920 buf[1] = b1;
8921 return 2;
8922 }
8923 /* 1 byte */
8924 buf[0] = b1;
8925 return 1;
8926}
8927
8928/*
8929 * Opposite of offset2bytes().
8930 * "pp" points to the bytes and is advanced over it.
8931 * Returns the offset.
8932 */
8933 static int
8934bytes2offset(pp)
8935 char_u **pp;
8936{
8937 char_u *p = *pp;
8938 int nr;
8939 int c;
8940
8941 c = *p++;
8942 if ((c & 0x80) == 0x00) /* 1 byte */
8943 {
8944 nr = c - 1;
8945 }
8946 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8947 {
8948 nr = (c & 0x3f) - 1;
8949 nr = nr * 255 + (*p++ - 1);
8950 }
8951 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8952 {
8953 nr = (c & 0x1f) - 1;
8954 nr = nr * 255 + (*p++ - 1);
8955 nr = nr * 255 + (*p++ - 1);
8956 }
8957 else /* 4 bytes */
8958 {
8959 nr = (c & 0x0f) - 1;
8960 nr = nr * 255 + (*p++ - 1);
8961 nr = nr * 255 + (*p++ - 1);
8962 nr = nr * 255 + (*p++ - 1);
8963 }
8964
8965 *pp = p;
8966 return nr;
8967}
8968
8969/*
8970 * Write the .sug file in "fname".
8971 */
8972 static void
8973sug_write(spin, fname)
8974 spellinfo_T *spin;
8975 char_u *fname;
8976{
8977 FILE *fd;
8978 wordnode_T *tree;
8979 int nodecount;
8980 int wcount;
8981 char_u *line;
8982 linenr_T lnum;
8983 int len;
8984
8985 /* Create the file. Note that an existing file is silently overwritten! */
8986 fd = mch_fopen((char *)fname, "w");
8987 if (fd == NULL)
8988 {
8989 EMSG2(_(e_notopen), fname);
8990 return;
8991 }
8992
8993 vim_snprintf((char *)IObuff, IOSIZE,
8994 _("Writing suggestion file %s ..."), fname);
8995 spell_message(spin, IObuff);
8996
8997 /*
8998 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8999 */
9000 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
9001 {
9002 EMSG(_(e_write));
9003 goto theend;
9004 }
9005 putc(VIMSUGVERSION, fd); /* <versionnr> */
9006
9007 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02009008 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009009
9010 /*
9011 * <SUGWORDTREE>
9012 */
9013 spin->si_memtot = 0;
9014 tree = spin->si_foldroot->wn_sibling;
9015
9016 /* Clear the index and wnode fields in the tree. */
9017 clear_node(tree);
9018
9019 /* Count the number of nodes. Needed to be able to allocate the
9020 * memory when reading the nodes. Also fills in index for shared
9021 * nodes. */
9022 nodecount = put_node(NULL, tree, 0, 0, FALSE);
9023
9024 /* number of nodes in 4 bytes */
9025 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9026 spin->si_memtot += nodecount + nodecount * sizeof(int);
9027
9028 /* Write the nodes. */
9029 (void)put_node(fd, tree, 0, 0, FALSE);
9030
9031 /*
9032 * <SUGTABLE>: <sugwcount> <sugline> ...
9033 */
9034 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9035 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9036
9037 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9038 {
9039 /* <sugline>: <sugnr> ... NUL */
9040 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009041 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009042 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9043 {
9044 EMSG(_(e_write));
9045 goto theend;
9046 }
9047 spin->si_memtot += len;
9048 }
9049
9050 /* Write another byte to check for errors. */
9051 if (putc(0, fd) == EOF)
9052 EMSG(_(e_write));
9053
9054 vim_snprintf((char *)IObuff, IOSIZE,
9055 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9056 spell_message(spin, IObuff);
9057
9058theend:
9059 /* close the file */
9060 fclose(fd);
9061}
9062
9063/*
9064 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9065 * list and only contains text lines. Can use a swapfile to reduce memory
9066 * use.
9067 * Most other fields are invalid! Esp. watch out for string options being
9068 * NULL and there is no undo info.
9069 * Returns NULL when out of memory.
9070 */
9071 static buf_T *
9072open_spellbuf()
9073{
9074 buf_T *buf;
9075
9076 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9077 if (buf != NULL)
9078 {
9079 buf->b_spell = TRUE;
9080 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02009081#ifdef FEAT_CRYPT
9082 buf->b_p_key = empty_option;
9083#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00009084 ml_open(buf);
9085 ml_open_file(buf); /* create swap file now */
9086 }
9087 return buf;
9088}
9089
9090/*
9091 * Close the buffer used for spell info.
9092 */
9093 static void
9094close_spellbuf(buf)
9095 buf_T *buf;
9096{
9097 if (buf != NULL)
9098 {
9099 ml_close(buf, TRUE);
9100 vim_free(buf);
9101 }
9102}
9103
9104
9105/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009106 * Create a Vim spell file from one or more word lists.
9107 * "fnames[0]" is the output file name.
9108 * "fnames[fcount - 1]" is the last input file name.
9109 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9110 * and ".spl" is appended to make the output file name.
9111 */
9112 static void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009113mkspell(fcount, fnames, ascii, over_write, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009114 int fcount;
9115 char_u **fnames;
9116 int ascii; /* -ascii argument given */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009117 int over_write; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009119{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009120 char_u *fname = NULL;
9121 char_u *wfname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009122 char_u **innames;
9123 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009124 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009125 int i;
9126 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009127 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009128 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009129 spellinfo_T spin;
9130
9131 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009133 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 spin.si_followup = TRUE;
9135 spin.si_rem_accents = TRUE;
9136 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009137 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9139 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009140 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009141 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009142 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009143 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009144
Bram Moolenaarb765d632005-06-07 21:00:02 +00009145 /* default: fnames[0] is output file, following are input files */
9146 innames = &fnames[1];
9147 incount = fcount - 1;
9148
Bram Moolenaard9462e32011-04-11 21:35:11 +02009149 wfname = alloc(MAXPATHL);
9150 if (wfname == NULL)
9151 return;
9152
Bram Moolenaarb765d632005-06-07 21:00:02 +00009153 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009154 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009155 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009156 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9157 {
9158 /* For ":mkspell path/en.latin1.add" output file is
9159 * "path/en.latin1.add.spl". */
9160 innames = &fnames[0];
9161 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009162 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009163 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009164 else if (fcount == 1)
9165 {
9166 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9167 innames = &fnames[0];
9168 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009169 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009170 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009171 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009172 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9173 {
9174 /* Name ends in ".spl", use as the file name. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009175 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009176 }
9177 else
9178 /* Name should be language, make the file name from it. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009179 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009180 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009181
9182 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009183 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009184 spin.si_ascii = TRUE;
9185
9186 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009187 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009188 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009189 }
9190
Bram Moolenaarb765d632005-06-07 21:00:02 +00009191 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009192 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009193 else if (vim_strchr(gettail(wfname), '_') != NULL)
9194 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009195 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009196 EMSG(_("E754: Only up to 8 regions supported"));
9197 else
9198 {
9199 /* Check for overwriting before doing things that may take a lot of
9200 * time. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009201 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009202 {
9203 EMSG(_(e_exists));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009204 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009205 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009206 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009207 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009208 EMSG2(_(e_isadir2), wfname);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009209 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009210 }
9211
Bram Moolenaard9462e32011-04-11 21:35:11 +02009212 fname = alloc(MAXPATHL);
9213 if (fname == NULL)
9214 goto theend;
9215
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009216 /*
9217 * Init the aff and dic pointers.
9218 * Get the region names if there are more than 2 arguments.
9219 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009220 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009221 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009222 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009223
Bram Moolenaar3982c542005-06-08 21:56:31 +00009224 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009225 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009226 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009227 if (STRLEN(gettail(innames[i])) < 5
9228 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009229 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009230 EMSG2(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009231 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009232 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009233 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9234 spin.si_region_name[i * 2 + 1] =
9235 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009236 }
9237 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009238 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009239
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009240 spin.si_foldroot = wordtree_alloc(&spin);
9241 spin.si_keeproot = wordtree_alloc(&spin);
9242 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009243 if (spin.si_foldroot == NULL
9244 || spin.si_keeproot == NULL
9245 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009246 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009247 free_blocks(spin.si_blocks);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009248 goto theend;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009249 }
9250
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009251 /* When not producing a .add.spl file clear the character table when
9252 * we encounter one in the .aff file. This means we dump the current
9253 * one in the .spl file if the .aff file doesn't define one. That's
9254 * better than guessing the contents, the table will match a
9255 * previously loaded spell file. */
9256 if (!spin.si_add)
9257 spin.si_clear_chartab = TRUE;
9258
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009259 /*
9260 * Read all the .aff and .dic files.
9261 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009262 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009263 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009264 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009265 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009266 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009267 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009268
Bram Moolenaard9462e32011-04-11 21:35:11 +02009269 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009270 if (mch_stat((char *)fname, &st) >= 0)
9271 {
9272 /* Read the .aff file. Will init "spin->si_conv" based on the
9273 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009274 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009275 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009276 error = TRUE;
9277 else
9278 {
9279 /* Read the .dic file and store the words in the trees. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009280 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009281 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009282 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009283 error = TRUE;
9284 }
9285 }
9286 else
9287 {
9288 /* No .aff file, try reading the file as a word list. Store
9289 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009290 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009291 error = TRUE;
9292 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009293
Bram Moolenaarb765d632005-06-07 21:00:02 +00009294#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009295 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009296 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009297#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009298 }
9299
Bram Moolenaar78622822005-08-23 21:00:13 +00009300 if (spin.si_compflags != NULL && spin.si_nobreak)
9301 MSG(_("Warning: both compounding and NOBREAK specified"));
9302
Bram Moolenaar4770d092006-01-12 23:22:24 +00009303 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009304 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009305 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009306 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009307 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009308 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009309 wordtree_compress(&spin, spin.si_foldroot);
9310 wordtree_compress(&spin, spin.si_keeproot);
9311 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009312 }
9313
Bram Moolenaar4770d092006-01-12 23:22:24 +00009314 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009315 {
9316 /*
9317 * Write the info in the spell file.
9318 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009319 vim_snprintf((char *)IObuff, IOSIZE,
9320 _("Writing spell file %s ..."), wfname);
9321 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009322
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009323 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009324
Bram Moolenaar4770d092006-01-12 23:22:24 +00009325 spell_message(&spin, (char_u *)_("Done!"));
9326 vim_snprintf((char *)IObuff, IOSIZE,
9327 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9328 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009329
Bram Moolenaar4770d092006-01-12 23:22:24 +00009330 /*
9331 * If the file is loaded need to reload it.
9332 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009333 if (!error)
9334 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009335 }
9336
9337 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009339 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009340 ga_clear(&spin.si_sal);
9341 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009342 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009343 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009344 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009345
9346 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009347 for (i = 0; i < incount; ++i)
9348 if (afile[i] != NULL)
9349 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009350
9351 /* Free all the bits and pieces at once. */
9352 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009353
9354 /*
9355 * If there is soundfolding info and no NOSUGFILE item create the
9356 * .sug file with the soundfolded word trie.
9357 */
9358 if (spin.si_sugtime != 0 && !error && !got_int)
9359 spell_make_sugfile(&spin, wfname);
9360
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009361 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009362
9363theend:
9364 vim_free(fname);
9365 vim_free(wfname);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009366}
9367
Bram Moolenaar4770d092006-01-12 23:22:24 +00009368/*
9369 * Display a message for spell file processing when 'verbose' is set or using
9370 * ":mkspell". "str" can be IObuff.
9371 */
9372 static void
9373spell_message(spin, str)
9374 spellinfo_T *spin;
9375 char_u *str;
9376{
9377 if (spin->si_verbose || p_verbose > 2)
9378 {
9379 if (!spin->si_verbose)
9380 verbose_enter();
9381 MSG(str);
9382 out_flush();
9383 if (!spin->si_verbose)
9384 verbose_leave();
9385 }
9386}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009387
9388/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009389 * ":[count]spellgood {word}"
9390 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009391 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009392 */
9393 void
9394ex_spell(eap)
9395 exarg_T *eap;
9396{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009397 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009398 eap->forceit ? 0 : (int)eap->line2,
9399 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009400}
9401
9402/*
9403 * Add "word[len]" to 'spellfile' as a good or bad word.
9404 */
9405 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009406spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009407 char_u *word;
9408 int len;
9409 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009410 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009411 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009412 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009413{
Bram Moolenaara3917072006-09-14 08:48:14 +00009414 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009415 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009416 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009417 char_u *fname;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009418 char_u *fnamebuf = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009419 char_u line[MAXWLEN * 2];
9420 long fpos, fpos_next = 0;
9421 int i;
9422 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009423
Bram Moolenaar89d40322006-08-29 15:30:07 +00009424 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009425 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009426 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009427 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009428 int_wordlist = vim_tempname('s');
9429 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009430 return;
9431 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009432 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009433 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009434 else
9435 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009436 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009437 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009438 {
9439 init_spellfile();
9440 new_spf = TRUE;
9441 }
9442
Bram Moolenaar860cae12010-06-05 23:22:07 +02009443 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009444 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009445 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009446 return;
9447 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009448 fnamebuf = alloc(MAXPATHL);
9449 if (fnamebuf == NULL)
9450 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009451
Bram Moolenaar860cae12010-06-05 23:22:07 +02009452 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009453 {
9454 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009455 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009456 break;
9457 if (*spf == NUL)
9458 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009459 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009460 vim_free(fnamebuf);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009461 return;
9462 }
9463 }
9464
Bram Moolenaarb765d632005-06-07 21:00:02 +00009465 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009466 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009467 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9468 buf = NULL;
9469 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009470 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009471 EMSG(_(e_bufloaded));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009472 vim_free(fnamebuf);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009473 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009474 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009475
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009476 fname = fnamebuf;
9477 }
9478
Bram Moolenaard0131a82006-03-04 21:46:13 +00009479 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009480 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009481 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009482 * since its flags sort before the one with WF_BANNED. */
9483 fd = mch_fopen((char *)fname, "r");
9484 if (fd != NULL)
9485 {
9486 while (!vim_fgets(line, MAXWLEN * 2, fd))
9487 {
9488 fpos = fpos_next;
9489 fpos_next = ftell(fd);
9490 if (STRNCMP(word, line, len) == 0
9491 && (line[len] == '/' || line[len] < ' '))
9492 {
9493 /* Found duplicate word. Remove it by writing a '#' at
9494 * the start of the line. Mixing reading and writing
9495 * doesn't work for all systems, close the file first. */
9496 fclose(fd);
9497 fd = mch_fopen((char *)fname, "r+");
9498 if (fd == NULL)
9499 break;
9500 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009501 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009502 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009503 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009504 {
9505 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009506 smsg((char_u *)_("Word '%.*s' removed from %s"),
9507 len, word, NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009508 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009509 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009510 fseek(fd, fpos_next, SEEK_SET);
9511 }
9512 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009513 if (fd != NULL)
9514 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009515 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009516 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009517
9518 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009519 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009520 fd = mch_fopen((char *)fname, "a");
9521 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009522 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009523 char_u *p;
9524
Bram Moolenaard0131a82006-03-04 21:46:13 +00009525 /* We just initialized the 'spellfile' option and can't open the
9526 * file. We may need to create the "spell" directory first. We
9527 * already checked the runtime directory is writable in
9528 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009529 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009530 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009531 int c = *p;
9532
Bram Moolenaard0131a82006-03-04 21:46:13 +00009533 /* The directory doesn't exist. Try creating it and opening
9534 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009535 *p = NUL;
9536 vim_mkdir(fname, 0755);
9537 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009538 fd = mch_fopen((char *)fname, "a");
9539 }
9540 }
9541
9542 if (fd == NULL)
9543 EMSG2(_(e_notopen), fname);
9544 else
9545 {
9546 if (bad)
9547 fprintf(fd, "%.*s/!\n", len, word);
9548 else
9549 fprintf(fd, "%.*s\n", len, word);
9550 fclose(fd);
9551
9552 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009553 smsg((char_u *)_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009554 }
9555 }
9556
Bram Moolenaard0131a82006-03-04 21:46:13 +00009557 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009558 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009559 /* Update the .add.spl file. */
9560 mkspell(1, &fname, FALSE, TRUE, TRUE);
9561
9562 /* If the .add file is edited somewhere, reload it. */
9563 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009564 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009565
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009566 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009567 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009568 vim_free(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009569}
9570
9571/*
9572 * Initialize 'spellfile' for the current buffer.
9573 */
9574 static void
9575init_spellfile()
9576{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009577 char_u *buf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009578 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009579 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009580 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009581 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009582 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009583 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009584
Bram Moolenaar860cae12010-06-05 23:22:07 +02009585 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009586 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009587 buf = alloc(MAXPATHL);
9588 if (buf == NULL)
9589 return;
9590
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009591 /* Find the end of the language name. Exclude the region. If there
9592 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009593 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009594 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009595 if (vim_ispathsep(*lend))
9596 {
9597 aspath = TRUE;
9598 lstart = lend + 1;
9599 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009600
9601 /* Loop over all entries in 'runtimepath'. Use the first one where we
9602 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009603 rtp = p_rtp;
9604 while (*rtp != NUL)
9605 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009606 if (aspath)
9607 /* Use directory of an entry with path, e.g., for
9608 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009609 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9610 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009611 else
9612 /* Copy the path from 'runtimepath' to buf[]. */
9613 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009614 if (filewritable(buf) == 2)
9615 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009616 /* Use the first language name from 'spelllang' and the
9617 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009618 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009619 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9620 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009621 else
9622 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009623 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009624 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009625 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009626 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009627 vim_mkdir(buf, 0755);
9628
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009629 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009630 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009631 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009632 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009633 l = (int)STRLEN(buf);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009634 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9635 ->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009636 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9637 fname != NULL
9638 && strstr((char *)gettail(fname), ".ascii.") != NULL
9639 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009640 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9641 break;
9642 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009643 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009644 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009645
9646 vim_free(buf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009647 }
9648}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009649
Bram Moolenaar51485f02005-06-04 21:55:20 +00009650
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009651/*
9652 * Init the chartab used for spelling for ASCII.
9653 * EBCDIC is not supported!
9654 */
9655 static void
9656clear_spell_chartab(sp)
9657 spelltab_T *sp;
9658{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009659 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660
9661 /* Init everything to FALSE. */
9662 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9663 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9664 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009665 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009666 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009667 sp->st_upper[i] = i;
9668 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009669
9670 /* We include digits. A word shouldn't start with a digit, but handling
9671 * that is done separately. */
9672 for (i = '0'; i <= '9'; ++i)
9673 sp->st_isw[i] = TRUE;
9674 for (i = 'A'; i <= 'Z'; ++i)
9675 {
9676 sp->st_isw[i] = TRUE;
9677 sp->st_isu[i] = TRUE;
9678 sp->st_fold[i] = i + 0x20;
9679 }
9680 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009681 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009682 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009683 sp->st_upper[i] = i - 0x20;
9684 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009685}
9686
9687/*
9688 * Init the chartab used for spelling. Only depends on 'encoding'.
9689 * Called once while starting up and when 'encoding' changes.
9690 * The default is to use isalpha(), but the spell file should define the word
9691 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009692 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009693 */
9694 void
9695init_spell_chartab()
9696{
9697 int i;
9698
9699 did_set_spelltab = FALSE;
9700 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009701#ifdef FEAT_MBYTE
9702 if (enc_dbcs)
9703 {
9704 /* DBCS: assume double-wide characters are word characters. */
9705 for (i = 128; i <= 255; ++i)
9706 if (MB_BYTE2LEN(i) == 2)
9707 spelltab.st_isw[i] = TRUE;
9708 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009709 else if (enc_utf8)
9710 {
9711 for (i = 128; i < 256; ++i)
9712 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009713 int f = utf_fold(i);
9714 int u = utf_toupper(i);
9715
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009716 spelltab.st_isu[i] = utf_isupper(i);
9717 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009718 /* The folded/upper-cased value is different between latin1 and
9719 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9720 * value for utf-8 to avoid this. */
9721 spelltab.st_fold[i] = (f < 256) ? f : i;
9722 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009723 }
9724 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009725 else
9726#endif
9727 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009728 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009729 for (i = 128; i < 256; ++i)
9730 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009731 if (MB_ISUPPER(i))
9732 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009733 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009734 spelltab.st_isu[i] = TRUE;
9735 spelltab.st_fold[i] = MB_TOLOWER(i);
9736 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009737 else if (MB_ISLOWER(i))
9738 {
9739 spelltab.st_isw[i] = TRUE;
9740 spelltab.st_upper[i] = MB_TOUPPER(i);
9741 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009742 }
9743 }
9744}
9745
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009746/*
9747 * Set the spell character tables from strings in the affix file.
9748 */
9749 static int
9750set_spell_chartab(fol, low, upp)
9751 char_u *fol;
9752 char_u *low;
9753 char_u *upp;
9754{
9755 /* We build the new tables here first, so that we can compare with the
9756 * previous one. */
9757 spelltab_T new_st;
9758 char_u *pf = fol, *pl = low, *pu = upp;
9759 int f, l, u;
9760
9761 clear_spell_chartab(&new_st);
9762
9763 while (*pf != NUL)
9764 {
9765 if (*pl == NUL || *pu == NUL)
9766 {
9767 EMSG(_(e_affform));
9768 return FAIL;
9769 }
9770#ifdef FEAT_MBYTE
9771 f = mb_ptr2char_adv(&pf);
9772 l = mb_ptr2char_adv(&pl);
9773 u = mb_ptr2char_adv(&pu);
9774#else
9775 f = *pf++;
9776 l = *pl++;
9777 u = *pu++;
9778#endif
9779 /* Every character that appears is a word character. */
9780 if (f < 256)
9781 new_st.st_isw[f] = TRUE;
9782 if (l < 256)
9783 new_st.st_isw[l] = TRUE;
9784 if (u < 256)
9785 new_st.st_isw[u] = TRUE;
9786
9787 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9788 * case-folding */
9789 if (l < 256 && l != f)
9790 {
9791 if (f >= 256)
9792 {
9793 EMSG(_(e_affrange));
9794 return FAIL;
9795 }
9796 new_st.st_fold[l] = f;
9797 }
9798
9799 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009800 * case-folding, it's upper case and the "UPP" is the upper case of
9801 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009802 if (u < 256 && u != f)
9803 {
9804 if (f >= 256)
9805 {
9806 EMSG(_(e_affrange));
9807 return FAIL;
9808 }
9809 new_st.st_fold[u] = f;
9810 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009811 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009812 }
9813 }
9814
9815 if (*pl != NUL || *pu != NUL)
9816 {
9817 EMSG(_(e_affform));
9818 return FAIL;
9819 }
9820
9821 return set_spell_finish(&new_st);
9822}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009823
9824/*
9825 * Set the spell character tables from strings in the .spl file.
9826 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009827 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009828set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009829 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009830 int cnt; /* length of "flags" */
9831 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009832{
9833 /* We build the new tables here first, so that we can compare with the
9834 * previous one. */
9835 spelltab_T new_st;
9836 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009837 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009838 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009839
9840 clear_spell_chartab(&new_st);
9841
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009842 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009843 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009844 if (i < cnt)
9845 {
9846 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9847 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9848 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009849
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009850 if (*p != NUL)
9851 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009852#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009853 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009854#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009855 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009856#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009857 new_st.st_fold[i + 128] = c;
9858 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9859 new_st.st_upper[c] = i + 128;
9860 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009861 }
9862
Bram Moolenaar5195e452005-08-19 20:32:47 +00009863 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009864}
9865
9866 static int
9867set_spell_finish(new_st)
9868 spelltab_T *new_st;
9869{
9870 int i;
9871
9872 if (did_set_spelltab)
9873 {
9874 /* check that it's the same table */
9875 for (i = 0; i < 256; ++i)
9876 {
9877 if (spelltab.st_isw[i] != new_st->st_isw[i]
9878 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009879 || spelltab.st_fold[i] != new_st->st_fold[i]
9880 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009881 {
9882 EMSG(_("E763: Word characters differ between spell files"));
9883 return FAIL;
9884 }
9885 }
9886 }
9887 else
9888 {
9889 /* copy the new spelltab into the one being used */
9890 spelltab = *new_st;
9891 did_set_spelltab = TRUE;
9892 }
9893
9894 return OK;
9895}
9896
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009897/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009898 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009899 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009900 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009901 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009902 */
9903 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009904spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009905 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009906 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009907{
Bram Moolenaarea408852005-06-25 22:49:46 +00009908#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009909 char_u *s;
9910 int l;
9911 int c;
9912
9913 if (has_mbyte)
9914 {
9915 l = MB_BYTE2LEN(*p);
9916 s = p;
9917 if (l == 1)
9918 {
9919 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009920 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009921 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009922 }
9923 else
9924 {
9925 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009926 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9927 : (wp->w_s->b_spell_ismw_mb != NULL
9928 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009929 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009930 }
9931
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009932 c = mb_ptr2char(s);
9933 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009934 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009935 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009936 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009937#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009938
Bram Moolenaar860cae12010-06-05 23:22:07 +02009939 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009940}
9941
9942/*
9943 * Return TRUE if "p" points to a word character.
9944 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9945 */
9946 static int
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009947spell_iswordp_nmw(p, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009948 char_u *p;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009949 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009950{
9951#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009952 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009953
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009954 if (has_mbyte)
9955 {
9956 c = mb_ptr2char(p);
9957 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009958 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009959 return spelltab.st_isw[c];
9960 }
9961#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009962 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009963}
9964
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009965#ifdef FEAT_MBYTE
9966/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009967 * Return TRUE if word class indicates a word character.
9968 * Only for characters above 255.
9969 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009970 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009971 */
9972 static int
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009973spell_mb_isword_class(cl, wp)
9974 int cl;
9975 win_T *wp;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009976{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009977 if (wp->w_s->b_cjk)
9978 /* East Asian characters are not considered word characters. */
9979 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009980 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9981}
9982
9983/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009984 * Return TRUE if "p" points to a word character.
9985 * Wide version of spell_iswordp().
9986 */
9987 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009988spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009989 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009990 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009991{
9992 int *s;
9993
Bram Moolenaar860cae12010-06-05 23:22:07 +02009994 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9995 : (wp->w_s->b_spell_ismw_mb != NULL
9996 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009997 s = p + 1;
9998 else
9999 s = p;
10000
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010001 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010002 {
10003 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010004 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010005 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010006 return spell_mb_isword_class(
10007 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010008 return 0;
10009 }
10010 return spelltab.st_isw[*s];
10011}
10012#endif
10013
Bram Moolenaarea408852005-06-25 22:49:46 +000010014/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010015 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010016 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010017 */
Bram Moolenaar5195e452005-08-19 20:32:47 +000010018 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010019write_spell_prefcond(fd, gap)
10020 FILE *fd;
10021 garray_T *gap;
10022{
10023 int i;
10024 char_u *p;
10025 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010026 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +000010027 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010028
Bram Moolenaar5195e452005-08-19 20:32:47 +000010029 if (fd != NULL)
10030 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
10031
10032 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010033
10034 for (i = 0; i < gap->ga_len; ++i)
10035 {
10036 /* <prefcond> : <condlen> <condstr> */
10037 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +000010038 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010039 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010040 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010041 if (fd != NULL)
10042 {
10043 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +000010044 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010045 }
10046 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010047 }
Bram Moolenaar5195e452005-08-19 20:32:47 +000010048 else if (fd != NULL)
10049 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010050 }
10051
Bram Moolenaar5195e452005-08-19 20:32:47 +000010052 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010053}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010054
10055/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010056 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
10057 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010058 * When using a multi-byte 'encoding' the length may change!
10059 * Returns FAIL when something wrong.
10060 */
10061 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010062spell_casefold(str, len, buf, buflen)
10063 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010064 int len;
10065 char_u *buf;
10066 int buflen;
10067{
10068 int i;
10069
10070 if (len >= buflen)
10071 {
10072 buf[0] = NUL;
10073 return FAIL; /* result will not fit */
10074 }
10075
10076#ifdef FEAT_MBYTE
10077 if (has_mbyte)
10078 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010079 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010080 char_u *p;
10081 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010082
10083 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010084 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010085 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010086 if (outi + MB_MAXBYTES > buflen)
10087 {
10088 buf[outi] = NUL;
10089 return FAIL;
10090 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010091 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010092 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010093 }
10094 buf[outi] = NUL;
10095 }
10096 else
10097#endif
10098 {
10099 /* Be quick for non-multibyte encodings. */
10100 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010101 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010102 buf[i] = NUL;
10103 }
10104
10105 return OK;
10106}
10107
Bram Moolenaar4770d092006-01-12 23:22:24 +000010108/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010109#define SPS_BEST 1
10110#define SPS_FAST 2
10111#define SPS_DOUBLE 4
10112
Bram Moolenaar4770d092006-01-12 23:22:24 +000010113static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10114static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010115
10116/*
10117 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010118 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010119 */
10120 int
10121spell_check_sps()
10122{
10123 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010124 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010125 char_u buf[MAXPATHL];
10126 int f;
10127
10128 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010129 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010130
10131 for (p = p_sps; *p != NUL; )
10132 {
10133 copy_option_part(&p, buf, MAXPATHL, ",");
10134
10135 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010136 if (VIM_ISDIGIT(*buf))
10137 {
10138 s = buf;
10139 sps_limit = getdigits(&s);
10140 if (*s != NUL && !VIM_ISDIGIT(*s))
10141 f = -1;
10142 }
10143 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010144 f = SPS_BEST;
10145 else if (STRCMP(buf, "fast") == 0)
10146 f = SPS_FAST;
10147 else if (STRCMP(buf, "double") == 0)
10148 f = SPS_DOUBLE;
10149 else if (STRNCMP(buf, "expr:", 5) != 0
10150 && STRNCMP(buf, "file:", 5) != 0)
10151 f = -1;
10152
10153 if (f == -1 || (sps_flags != 0 && f != 0))
10154 {
10155 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010156 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010157 return FAIL;
10158 }
10159 if (f != 0)
10160 sps_flags = f;
10161 }
10162
10163 if (sps_flags == 0)
10164 sps_flags = SPS_BEST;
10165
10166 return OK;
10167}
10168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169/*
Bram Moolenaar134bf072013-09-25 18:54:24 +020010170 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010171 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010172 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010173 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010174 */
10175 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010176spell_suggest(count)
10177 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178{
10179 char_u *line;
10180 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181 char_u wcopy[MAXWLEN + 2];
10182 char_u *p;
10183 int i;
10184 int c;
10185 suginfo_T sug;
10186 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010187 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010188 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010189 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010190 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010191 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010192 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010194 if (no_spell_checking(curwin))
10195 return;
10196
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010197 if (VIsual_active)
10198 {
10199 /* Use the Visually selected text as the bad word. But reject
10200 * a multi-line selection. */
10201 if (curwin->w_cursor.lnum != VIsual.lnum)
10202 {
10203 vim_beep();
10204 return;
10205 }
10206 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10207 if (badlen < 0)
10208 badlen = -badlen;
10209 else
10210 curwin->w_cursor.col = VIsual.col;
10211 ++badlen;
10212 end_visual_mode();
10213 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010214 /* Find the start of the badly spelled word. */
10215 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010216 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010218 /* No bad word or it starts after the cursor: use the word under the
10219 * cursor. */
10220 curwin->w_cursor = prev_cursor;
10221 line = ml_get_curline();
10222 p = line + curwin->w_cursor.col;
10223 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010224 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010225 mb_ptr_back(line, p);
10226 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010227 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010228 mb_ptr_adv(p);
10229
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010230 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010231 {
10232 beep_flush();
10233 return;
10234 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010235 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 }
10237
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010238 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010239
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010240 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010241 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010242
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010243 /* Make a copy of current line since autocommands may free the line. */
10244 line = vim_strsave(ml_get_curline());
10245 if (line == NULL)
10246 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010247
Bram Moolenaar5195e452005-08-19 20:32:47 +000010248 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10249 * 'spellsuggest', whatever is smaller. */
10250 if (sps_limit > (int)Rows - 2)
10251 limit = (int)Rows - 2;
10252 else
10253 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010254 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010255 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256
10257 if (sug.su_ga.ga_len == 0)
10258 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010259 else if (count > 0)
10260 {
10261 if (count > sug.su_ga.ga_len)
10262 smsg((char_u *)_("Sorry, only %ld suggestions"),
10263 (long)sug.su_ga.ga_len);
10264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 else
10266 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010267 vim_free(repl_from);
10268 repl_from = NULL;
10269 vim_free(repl_to);
10270 repl_to = NULL;
10271
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010272#ifdef FEAT_RIGHTLEFT
10273 /* When 'rightleft' is set the list is drawn right-left. */
10274 cmdmsg_rl = curwin->w_p_rl;
10275 if (cmdmsg_rl)
10276 msg_col = Columns - 1;
10277#endif
10278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 /* List the suggestions. */
10280 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010281 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010282 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10284 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010285#ifdef FEAT_RIGHTLEFT
10286 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10287 {
10288 /* And now the rabbit from the high hat: Avoid showing the
10289 * untranslated message rightleft. */
10290 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10291 sug.su_badlen, sug.su_badptr);
10292 }
10293#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 msg_puts(IObuff);
10295 msg_clr_eos();
10296 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 msg_scroll = TRUE;
10299 for (i = 0; i < sug.su_ga.ga_len; ++i)
10300 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010301 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010302
10303 /* The suggested word may replace only part of the bad word, add
10304 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020010305 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010307 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 sug.su_badptr + stp->st_orglen,
10309 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010310 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10311#ifdef FEAT_RIGHTLEFT
10312 if (cmdmsg_rl)
10313 rl_mirror(IObuff);
10314#endif
10315 msg_puts(IObuff);
10316
10317 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010318 msg_puts(IObuff);
10319
10320 /* The word may replace more than "su_badlen". */
10321 if (sug.su_badlen < stp->st_orglen)
10322 {
10323 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10324 stp->st_orglen, sug.su_badptr);
10325 msg_puts(IObuff);
10326 }
10327
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010328 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010329 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010330 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010331 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010332 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010333 stp->st_salscore ? "s " : "",
10334 stp->st_score, stp->st_altscore);
10335 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010336 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010337 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010338#ifdef FEAT_RIGHTLEFT
10339 if (cmdmsg_rl)
10340 /* Mirror the numbers, but keep the leading space. */
10341 rl_mirror(IObuff + 1);
10342#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010343 msg_advance(30);
10344 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010346 msg_putchar('\n');
10347 }
10348
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010349#ifdef FEAT_RIGHTLEFT
10350 cmdmsg_rl = FALSE;
10351 msg_col = 0;
10352#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010353 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010354 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010355 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010356 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010357 lines_left = Rows; /* avoid more prompt */
10358 /* don't delay for 'smd' in normal_cmd() */
10359 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010360 }
10361
Bram Moolenaard12a1322005-08-21 22:08:24 +000010362 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10363 {
10364 /* Save the from and to text for :spellrepall. */
10365 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010366 if (sug.su_badlen > stp->st_orglen)
10367 {
10368 /* Replacing less than "su_badlen", append the remainder to
10369 * repl_to. */
10370 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10371 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10372 sug.su_badlen - stp->st_orglen,
10373 sug.su_badptr + stp->st_orglen);
10374 repl_to = vim_strsave(IObuff);
10375 }
10376 else
10377 {
10378 /* Replacing su_badlen or more, use the whole word. */
10379 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10380 repl_to = vim_strsave(stp->st_word);
10381 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010382
10383 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010384 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10385 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010386 if (p != NULL)
10387 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010388 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010389 mch_memmove(p, line, c);
10390 STRCPY(p + c, stp->st_word);
10391 STRCAT(p, sug.su_badptr + stp->st_orglen);
10392 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10393 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010394
10395 /* For redo we use a change-word command. */
10396 ResetRedobuff();
10397 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010398 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010399 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010400 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010401
10402 /* After this "p" may be invalid. */
10403 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010404 }
10405 }
10406 else
10407 curwin->w_cursor = prev_cursor;
10408
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010409 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010410skip:
10411 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010412}
10413
10414/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010415 * Check if the word at line "lnum" column "col" is required to start with a
10416 * capital. This uses 'spellcapcheck' of the current buffer.
10417 */
10418 static int
10419check_need_cap(lnum, col)
10420 linenr_T lnum;
10421 colnr_T col;
10422{
10423 int need_cap = FALSE;
10424 char_u *line;
10425 char_u *line_copy = NULL;
10426 char_u *p;
10427 colnr_T endcol;
10428 regmatch_T regmatch;
10429
Bram Moolenaar860cae12010-06-05 23:22:07 +020010430 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010431 return FALSE;
10432
10433 line = ml_get_curline();
10434 endcol = 0;
10435 if ((int)(skipwhite(line) - line) >= (int)col)
10436 {
10437 /* At start of line, check if previous line is empty or sentence
10438 * ends there. */
10439 if (lnum == 1)
10440 need_cap = TRUE;
10441 else
10442 {
10443 line = ml_get(lnum - 1);
10444 if (*skipwhite(line) == NUL)
10445 need_cap = TRUE;
10446 else
10447 {
10448 /* Append a space in place of the line break. */
10449 line_copy = concat_str(line, (char_u *)" ");
10450 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010451 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010452 }
10453 }
10454 }
10455 else
10456 endcol = col;
10457
10458 if (endcol > 0)
10459 {
10460 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010461 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010462 regmatch.rm_ic = FALSE;
10463 p = line + endcol;
10464 for (;;)
10465 {
10466 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010467 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010468 break;
10469 if (vim_regexec(&regmatch, p, 0)
10470 && regmatch.endp[0] == line + endcol)
10471 {
10472 need_cap = TRUE;
10473 break;
10474 }
10475 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +010010476 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010477 }
10478
10479 vim_free(line_copy);
10480
10481 return need_cap;
10482}
10483
10484
10485/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010486 * ":spellrepall"
10487 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010488 void
10489ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010490 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010491{
10492 pos_T pos = curwin->w_cursor;
10493 char_u *frompat;
10494 int addlen;
10495 char_u *line;
10496 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010497 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010498 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010499
10500 if (repl_from == NULL || repl_to == NULL)
10501 {
10502 EMSG(_("E752: No previous spell replacement"));
10503 return;
10504 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010505 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010506
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010507 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010508 if (frompat == NULL)
10509 return;
10510 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10511 p_ws = FALSE;
10512
Bram Moolenaar5195e452005-08-19 20:32:47 +000010513 sub_nsubs = 0;
10514 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010515 curwin->w_cursor.lnum = 0;
10516 while (!got_int)
10517 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010518 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010519 || u_save_cursor() == FAIL)
10520 break;
10521
10522 /* Only replace when the right word isn't there yet. This happens
10523 * when changing "etc" to "etc.". */
10524 line = ml_get_curline();
10525 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10526 repl_to, STRLEN(repl_to)) != 0)
10527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010528 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010529 if (p == NULL)
10530 break;
10531 mch_memmove(p, line, curwin->w_cursor.col);
10532 STRCPY(p + curwin->w_cursor.col, repl_to);
10533 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10534 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10535 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010536
10537 if (curwin->w_cursor.lnum != prev_lnum)
10538 {
10539 ++sub_nlines;
10540 prev_lnum = curwin->w_cursor.lnum;
10541 }
10542 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010543 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010544 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010545 }
10546
10547 p_ws = save_ws;
10548 curwin->w_cursor = pos;
10549 vim_free(frompat);
10550
Bram Moolenaar5195e452005-08-19 20:32:47 +000010551 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010552 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010553 else
10554 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010555}
10556
10557/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010558 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10559 * a list of allocated strings.
10560 */
10561 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010562spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010563 garray_T *gap;
10564 char_u *word;
10565 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010566 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010567 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010568{
10569 suginfo_T sug;
10570 int i;
10571 suggest_T *stp;
10572 char_u *wcopy;
10573
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010574 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010575
10576 /* Make room in "gap". */
10577 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010578 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010579 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010580 for (i = 0; i < sug.su_ga.ga_len; ++i)
10581 {
10582 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010583
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010584 /* The suggested word may replace only part of "word", add the not
10585 * replaced part. */
10586 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010587 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010588 if (wcopy == NULL)
10589 break;
10590 STRCPY(wcopy, stp->st_word);
10591 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10592 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10593 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010594 }
10595
10596 spell_find_cleanup(&sug);
10597}
10598
10599/*
10600 * Find spell suggestions for the word at the start of "badptr".
10601 * Return the suggestions in "su->su_ga".
10602 * The maximum number of suggestions is "maxcount".
10603 * Note: does use info for the current window.
10604 * This is based on the mechanisms of Aspell, but completely reimplemented.
10605 */
10606 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010607spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010608 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010609 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010610 suginfo_T *su;
10611 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010612 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010613 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010614 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010615{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010616 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010617 char_u buf[MAXPATHL];
10618 char_u *p;
10619 int do_combine = FALSE;
10620 char_u *sps_copy;
10621#ifdef FEAT_EVAL
10622 static int expr_busy = FALSE;
10623#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010624 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010625 int i;
10626 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010627
10628 /*
10629 * Set the info in "*su".
10630 */
10631 vim_memset(su, 0, sizeof(suginfo_T));
10632 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10633 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010634 if (*badptr == NUL)
10635 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010636 hash_init(&su->su_banned);
10637
10638 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010639 if (badlen != 0)
10640 su->su_badlen = badlen;
10641 else
10642 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010643 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010644 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010645
10646 if (su->su_badlen >= MAXWLEN)
10647 su->su_badlen = MAXWLEN - 1; /* just in case */
10648 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10649 (void)spell_casefold(su->su_badptr, su->su_badlen,
10650 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010651 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010652 su->su_badflags = badword_captype(su->su_badptr,
10653 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010654 if (need_cap)
10655 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010656
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010657 /* Find the default language for sound folding. We simply use the first
10658 * one in 'spelllang' that supports sound folding. That's good for when
10659 * using multiple files for one language, it's not that bad when mixing
10660 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010661 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010662 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010663 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010664 if (lp->lp_sallang != NULL)
10665 {
10666 su->su_sallang = lp->lp_sallang;
10667 break;
10668 }
10669 }
10670
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010671 /* Soundfold the bad word with the default sound folding, so that we don't
10672 * have to do this many times. */
10673 if (su->su_sallang != NULL)
10674 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10675 su->su_sal_badword);
10676
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010677 /* If the word is not capitalised and spell_check() doesn't consider the
10678 * word to be bad then it might need to be capitalised. Add a suggestion
10679 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010680 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010681 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010682 {
10683 make_case_word(su->su_badword, buf, WF_ONECAP);
10684 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010685 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010686 }
10687
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010688 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010689 if (banbadword)
10690 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010691
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010692 /* Make a copy of 'spellsuggest', because the expression may change it. */
10693 sps_copy = vim_strsave(p_sps);
10694 if (sps_copy == NULL)
10695 return;
10696
10697 /* Loop over the items in 'spellsuggest'. */
10698 for (p = sps_copy; *p != NUL; )
10699 {
10700 copy_option_part(&p, buf, MAXPATHL, ",");
10701
10702 if (STRNCMP(buf, "expr:", 5) == 0)
10703 {
10704#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010705 /* Evaluate an expression. Skip this when called recursively,
10706 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010707 if (!expr_busy)
10708 {
10709 expr_busy = TRUE;
10710 spell_suggest_expr(su, buf + 5);
10711 expr_busy = FALSE;
10712 }
10713#endif
10714 }
10715 else if (STRNCMP(buf, "file:", 5) == 0)
10716 /* Use list of suggestions in a file. */
10717 spell_suggest_file(su, buf + 5);
10718 else
10719 {
10720 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010721 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010722 if (sps_flags & SPS_DOUBLE)
10723 do_combine = TRUE;
10724 }
10725 }
10726
10727 vim_free(sps_copy);
10728
10729 if (do_combine)
10730 /* Combine the two list of suggestions. This must be done last,
10731 * because sorting changes the order again. */
10732 score_combine(su);
10733}
10734
10735#ifdef FEAT_EVAL
10736/*
10737 * Find suggestions by evaluating expression "expr".
10738 */
10739 static void
10740spell_suggest_expr(su, expr)
10741 suginfo_T *su;
10742 char_u *expr;
10743{
10744 list_T *list;
10745 listitem_T *li;
10746 int score;
10747 char_u *p;
10748
10749 /* The work is split up in a few parts to avoid having to export
10750 * suginfo_T.
10751 * First evaluate the expression and get the resulting list. */
10752 list = eval_spell_expr(su->su_badword, expr);
10753 if (list != NULL)
10754 {
10755 /* Loop over the items in the list. */
10756 for (li = list->lv_first; li != NULL; li = li->li_next)
10757 if (li->li_tv.v_type == VAR_LIST)
10758 {
10759 /* Get the word and the score from the items. */
10760 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010761 if (score >= 0 && score <= su->su_maxscore)
10762 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10763 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010764 }
10765 list_unref(list);
10766 }
10767
Bram Moolenaar4770d092006-01-12 23:22:24 +000010768 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10769 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010770 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10771}
10772#endif
10773
10774/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010775 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010776 */
10777 static void
10778spell_suggest_file(su, fname)
10779 suginfo_T *su;
10780 char_u *fname;
10781{
10782 FILE *fd;
10783 char_u line[MAXWLEN * 2];
10784 char_u *p;
10785 int len;
10786 char_u cword[MAXWLEN];
10787
10788 /* Open the file. */
10789 fd = mch_fopen((char *)fname, "r");
10790 if (fd == NULL)
10791 {
10792 EMSG2(_(e_notopen), fname);
10793 return;
10794 }
10795
10796 /* Read it line by line. */
10797 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10798 {
10799 line_breakcheck();
10800
10801 p = vim_strchr(line, '/');
10802 if (p == NULL)
10803 continue; /* No Tab found, just skip the line. */
10804 *p++ = NUL;
10805 if (STRICMP(su->su_badword, line) == 0)
10806 {
10807 /* Match! Isolate the good word, until CR or NL. */
10808 for (len = 0; p[len] >= ' '; ++len)
10809 ;
10810 p[len] = NUL;
10811
10812 /* If the suggestion doesn't have specific case duplicate the case
10813 * of the bad word. */
10814 if (captype(p, NULL) == 0)
10815 {
10816 make_case_word(p, cword, su->su_badflags);
10817 p = cword;
10818 }
10819
10820 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010821 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010822 }
10823 }
10824
10825 fclose(fd);
10826
Bram Moolenaar4770d092006-01-12 23:22:24 +000010827 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10828 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010829 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10830}
10831
10832/*
10833 * Find suggestions for the internal method indicated by "sps_flags".
10834 */
10835 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010836spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010837 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010838 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010839{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010840 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010841 * Load the .sug file(s) that are available and not done yet.
10842 */
10843 suggest_load_files();
10844
10845 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010846 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010847 *
10848 * Set a maximum score to limit the combination of operations that is
10849 * tried.
10850 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010851 suggest_try_special(su);
10852
10853 /*
10854 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10855 * from the .aff file and inserting a space (split the word).
10856 */
10857 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010858
10859 /* For the resulting top-scorers compute the sound-a-like score. */
10860 if (sps_flags & SPS_DOUBLE)
10861 score_comp_sal(su);
10862
10863 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010864 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010865 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010866 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010867 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010868 if (sps_flags & SPS_BEST)
10869 /* Adjust the word score for the suggestions found so far for how
10870 * they sounds like. */
10871 rescore_suggestions(su);
10872
10873 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010874 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010875 * for the soundfold word, limits the changes that are being tried,
10876 * and "su_sfmaxscore" the rescored score, which is set by
10877 * cleanup_suggestions().
10878 * First find words with a small edit distance, because this is much
10879 * faster and often already finds the top-N suggestions. If we didn't
10880 * find many suggestions try again with a higher edit distance.
10881 * "sl_sounddone" is used to avoid doing the same word twice.
10882 */
10883 suggest_try_soundalike_prep();
10884 su->su_maxscore = SCORE_SFMAX1;
10885 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010886 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010887 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10888 {
10889 /* We didn't find enough matches, try again, allowing more
10890 * changes to the soundfold word. */
10891 su->su_maxscore = SCORE_SFMAX2;
10892 suggest_try_soundalike(su);
10893 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10894 {
10895 /* Still didn't find enough matches, try again, allowing even
10896 * more changes to the soundfold word. */
10897 su->su_maxscore = SCORE_SFMAX3;
10898 suggest_try_soundalike(su);
10899 }
10900 }
10901 su->su_maxscore = su->su_sfmaxscore;
10902 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010903 }
10904
Bram Moolenaar4770d092006-01-12 23:22:24 +000010905 /* When CTRL-C was hit while searching do show the results. Only clear
10906 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010907 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010908 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010909 {
10910 (void)vgetc();
10911 got_int = FALSE;
10912 }
10913
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010914 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010915 {
10916 if (sps_flags & SPS_BEST)
10917 /* Adjust the word score for how it sounds like. */
10918 rescore_suggestions(su);
10919
Bram Moolenaar4770d092006-01-12 23:22:24 +000010920 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10921 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010922 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010923 }
10924}
10925
10926/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010927 * Load the .sug files for languages that have one and weren't loaded yet.
10928 */
10929 static void
10930suggest_load_files()
10931{
10932 langp_T *lp;
10933 int lpi;
10934 slang_T *slang;
10935 char_u *dotp;
10936 FILE *fd;
10937 char_u buf[MAXWLEN];
10938 int i;
10939 time_t timestamp;
10940 int wcount;
10941 int wordnr;
10942 garray_T ga;
10943 int c;
10944
10945 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010946 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010947 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010948 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010949 slang = lp->lp_slang;
10950 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10951 {
10952 /* Change ".spl" to ".sug" and open the file. When the file isn't
10953 * found silently skip it. Do set "sl_sugloaded" so that we
10954 * don't try again and again. */
10955 slang->sl_sugloaded = TRUE;
10956
10957 dotp = vim_strrchr(slang->sl_fname, '.');
10958 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10959 continue;
10960 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010961 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010962 if (fd == NULL)
10963 goto nextone;
10964
10965 /*
10966 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10967 */
10968 for (i = 0; i < VIMSUGMAGICL; ++i)
10969 buf[i] = getc(fd); /* <fileID> */
10970 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10971 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010972 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010973 slang->sl_fname);
10974 goto nextone;
10975 }
10976 c = getc(fd); /* <versionnr> */
10977 if (c < VIMSUGVERSION)
10978 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010979 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010980 slang->sl_fname);
10981 goto nextone;
10982 }
10983 else if (c > VIMSUGVERSION)
10984 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010985 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010986 slang->sl_fname);
10987 goto nextone;
10988 }
10989
10990 /* Check the timestamp, it must be exactly the same as the one in
10991 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010992 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010993 if (timestamp != slang->sl_sugtime)
10994 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010995 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010996 slang->sl_fname);
10997 goto nextone;
10998 }
10999
11000 /*
11001 * <SUGWORDTREE>: <wordtree>
11002 * Read the trie with the soundfolded words.
11003 */
11004 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
11005 FALSE, 0) != 0)
11006 {
11007someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000011008 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011009 slang->sl_fname);
11010 slang_clear_sug(slang);
11011 goto nextone;
11012 }
11013
11014 /*
11015 * <SUGTABLE>: <sugwcount> <sugline> ...
11016 *
11017 * Read the table with word numbers. We use a file buffer for
11018 * this, because it's so much like a file with lines. Makes it
11019 * possible to swap the info and save on memory use.
11020 */
11021 slang->sl_sugbuf = open_spellbuf();
11022 if (slang->sl_sugbuf == NULL)
11023 goto someerror;
11024 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000011025 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011026 if (wcount < 0)
11027 goto someerror;
11028
11029 /* Read all the wordnr lists into the buffer, one NUL terminated
11030 * list per line. */
11031 ga_init2(&ga, 1, 100);
11032 for (wordnr = 0; wordnr < wcount; ++wordnr)
11033 {
11034 ga.ga_len = 0;
11035 for (;;)
11036 {
11037 c = getc(fd); /* <sugline> */
11038 if (c < 0 || ga_grow(&ga, 1) == FAIL)
11039 goto someerror;
11040 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
11041 if (c == NUL)
11042 break;
11043 }
11044 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
11045 ga.ga_data, ga.ga_len, TRUE) == FAIL)
11046 goto someerror;
11047 }
11048 ga_clear(&ga);
11049
11050 /*
11051 * Need to put word counts in the word tries, so that we can find
11052 * a word by its number.
11053 */
11054 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
11055 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
11056
11057nextone:
11058 if (fd != NULL)
11059 fclose(fd);
11060 STRCPY(dotp, ".spl");
11061 }
11062 }
11063}
11064
11065
11066/*
11067 * Fill in the wordcount fields for a trie.
11068 * Returns the total number of words.
11069 */
11070 static void
11071tree_count_words(byts, idxs)
11072 char_u *byts;
11073 idx_T *idxs;
11074{
11075 int depth;
11076 idx_T arridx[MAXWLEN];
11077 int curi[MAXWLEN];
11078 int c;
11079 idx_T n;
11080 int wordcount[MAXWLEN];
11081
11082 arridx[0] = 0;
11083 curi[0] = 1;
11084 wordcount[0] = 0;
11085 depth = 0;
11086 while (depth >= 0 && !got_int)
11087 {
11088 if (curi[depth] > byts[arridx[depth]])
11089 {
11090 /* Done all bytes at this node, go up one level. */
11091 idxs[arridx[depth]] = wordcount[depth];
11092 if (depth > 0)
11093 wordcount[depth - 1] += wordcount[depth];
11094
11095 --depth;
11096 fast_breakcheck();
11097 }
11098 else
11099 {
11100 /* Do one more byte at this node. */
11101 n = arridx[depth] + curi[depth];
11102 ++curi[depth];
11103
11104 c = byts[n];
11105 if (c == 0)
11106 {
11107 /* End of word, count it. */
11108 ++wordcount[depth];
11109
11110 /* Skip over any other NUL bytes (same word with different
11111 * flags). */
11112 while (byts[n + 1] == 0)
11113 {
11114 ++n;
11115 ++curi[depth];
11116 }
11117 }
11118 else
11119 {
11120 /* Normal char, go one level deeper to count the words. */
11121 ++depth;
11122 arridx[depth] = idxs[n];
11123 curi[depth] = 1;
11124 wordcount[depth] = 0;
11125 }
11126 }
11127 }
11128}
11129
11130/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011131 * Free the info put in "*su" by spell_find_suggest().
11132 */
11133 static void
11134spell_find_cleanup(su)
11135 suginfo_T *su;
11136{
11137 int i;
11138
11139 /* Free the suggestions. */
11140 for (i = 0; i < su->su_ga.ga_len; ++i)
11141 vim_free(SUG(su->su_ga, i).st_word);
11142 ga_clear(&su->su_ga);
11143 for (i = 0; i < su->su_sga.ga_len; ++i)
11144 vim_free(SUG(su->su_sga, i).st_word);
11145 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146
11147 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011148 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149}
11150
11151/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011152 * Make a copy of "word", with the first letter upper or lower cased, to
11153 * "wcopy[MAXWLEN]". "word" must not be empty.
11154 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 */
11156 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011157onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011159 char_u *wcopy;
11160 int upper; /* TRUE: first letter made upper case */
11161{
11162 char_u *p;
11163 int c;
11164 int l;
11165
11166 p = word;
11167#ifdef FEAT_MBYTE
11168 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011169 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011170 else
11171#endif
11172 c = *p++;
11173 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011174 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011176 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177#ifdef FEAT_MBYTE
11178 if (has_mbyte)
11179 l = mb_char2bytes(c, wcopy);
11180 else
11181#endif
11182 {
11183 l = 1;
11184 wcopy[0] = c;
11185 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011186 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187}
11188
11189/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011190 * Make a copy of "word" with all the letters upper cased into
11191 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 */
11193 static void
11194allcap_copy(word, wcopy)
11195 char_u *word;
11196 char_u *wcopy;
11197{
11198 char_u *s;
11199 char_u *d;
11200 int c;
11201
11202 d = wcopy;
11203 for (s = word; *s != NUL; )
11204 {
11205#ifdef FEAT_MBYTE
11206 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011207 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 else
11209#endif
11210 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011211
11212#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +020011213 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +000011214 * would cause weird errors in other 8-bit encodings. */
11215 if (enc_latin1like && c == 0xdf)
11216 {
11217 c = 'S';
11218 if (d - wcopy >= MAXWLEN - 1)
11219 break;
11220 *d++ = c;
11221 }
11222 else
11223#endif
11224 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225
11226#ifdef FEAT_MBYTE
11227 if (has_mbyte)
11228 {
11229 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11230 break;
11231 d += mb_char2bytes(c, d);
11232 }
11233 else
11234#endif
11235 {
11236 if (d - wcopy >= MAXWLEN - 1)
11237 break;
11238 *d++ = c;
11239 }
11240 }
11241 *d = NUL;
11242}
11243
11244/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011245 * Try finding suggestions by recognizing specific situations.
11246 */
11247 static void
11248suggest_try_special(su)
11249 suginfo_T *su;
11250{
11251 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011252 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011253 int c;
11254 char_u word[MAXWLEN];
11255
11256 /*
11257 * Recognize a word that is repeated: "the the".
11258 */
11259 p = skiptowhite(su->su_fbadword);
11260 len = p - su->su_fbadword;
11261 p = skipwhite(p);
11262 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11263 {
11264 /* Include badflags: if the badword is onecap or allcap
11265 * use that for the goodword too: "The the" -> "The". */
11266 c = su->su_fbadword[len];
11267 su->su_fbadword[len] = NUL;
11268 make_case_word(su->su_fbadword, word, su->su_badflags);
11269 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011270
11271 /* Give a soundalike score of 0, compute the score as if deleting one
11272 * character. */
11273 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011274 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011275 }
11276}
11277
11278/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 * Try finding suggestions by adding/removing/swapping letters.
11280 */
11281 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011282suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283 suginfo_T *su;
11284{
11285 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011286 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011288 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011289 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290
11291 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011292 * to find matches (esp. REP items). Append some more text, changing
11293 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011294 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011295 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011296 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011297 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298
Bram Moolenaar860cae12010-06-05 23:22:07 +020011299 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011301 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011302
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011303 /* If reloading a spell file fails it's still in the list but
11304 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011305 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011306 continue;
11307
Bram Moolenaar4770d092006-01-12 23:22:24 +000011308 /* Try it for this language. Will add possible suggestions. */
11309 suggest_trie_walk(su, lp, fword, FALSE);
11310 }
11311}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313/* Check the maximum score, if we go over it we won't try this change. */
11314#define TRY_DEEPER(su, stack, depth, add) \
11315 (stack[depth].ts_score + (add) < su->su_maxscore)
11316
11317/*
11318 * Try finding suggestions by adding/removing/swapping letters.
11319 *
11320 * This uses a state machine. At each node in the tree we try various
11321 * operations. When trying if an operation works "depth" is increased and the
11322 * stack[] is used to store info. This allows combinations, thus insert one
11323 * character, replace one and delete another. The number of changes is
11324 * limited by su->su_maxscore.
11325 *
11326 * After implementing this I noticed an article by Kemal Oflazer that
11327 * describes something similar: "Error-tolerant Finite State Recognition with
11328 * Applications to Morphological Analysis and Spelling Correction" (1996).
11329 * The implementation in the article is simplified and requires a stack of
11330 * unknown depth. The implementation here only needs a stack depth equal to
11331 * the length of the word.
11332 *
11333 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11334 * The mechanism is the same, but we find a match with a sound-folded word
11335 * that comes from one or more original words. Each of these words may be
11336 * added, this is done by add_sound_suggest().
11337 * Don't use:
11338 * the prefix tree or the keep-case tree
11339 * "su->su_badlen"
11340 * anything to do with upper and lower case
11341 * anything to do with word or non-word characters ("spell_iswordp()")
11342 * banned words
11343 * word flags (rare, region, compounding)
11344 * word splitting for now
11345 * "similar_chars()"
11346 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11347 */
11348 static void
11349suggest_trie_walk(su, lp, fword, soundfold)
11350 suginfo_T *su;
11351 langp_T *lp;
11352 char_u *fword;
11353 int soundfold;
11354{
11355 char_u tword[MAXWLEN]; /* good word collected so far */
11356 trystate_T stack[MAXWLEN];
11357 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011358 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011359 * words and split word. NUL terminated
11360 * when going deeper but not when coming
11361 * back. */
11362 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11363 trystate_T *sp;
11364 int newscore;
11365 int score;
11366 char_u *byts, *fbyts, *pbyts;
11367 idx_T *idxs, *fidxs, *pidxs;
11368 int depth;
11369 int c, c2, c3;
11370 int n = 0;
11371 int flags;
11372 garray_T *gap;
11373 idx_T arridx;
11374 int len;
11375 char_u *p;
11376 fromto_T *ftp;
11377 int fl = 0, tl;
11378 int repextra = 0; /* extra bytes in fword[] from REP item */
11379 slang_T *slang = lp->lp_slang;
11380 int fword_ends;
11381 int goodword_ends;
11382#ifdef DEBUG_TRIEWALK
11383 /* Stores the name of the change made at each level. */
11384 char_u changename[MAXWLEN][80];
11385#endif
11386 int breakcheckcount = 1000;
11387 int compound_ok;
11388
11389 /*
11390 * Go through the whole case-fold tree, try changes at each node.
11391 * "tword[]" contains the word collected from nodes in the tree.
11392 * "fword[]" the word we are trying to match with (initially the bad
11393 * word).
11394 */
11395 depth = 0;
11396 sp = &stack[0];
11397 vim_memset(sp, 0, sizeof(trystate_T));
11398 sp->ts_curi = 1;
11399
11400 if (soundfold)
11401 {
11402 /* Going through the soundfold tree. */
11403 byts = fbyts = slang->sl_sbyts;
11404 idxs = fidxs = slang->sl_sidxs;
11405 pbyts = NULL;
11406 pidxs = NULL;
11407 sp->ts_prefixdepth = PFD_NOPREFIX;
11408 sp->ts_state = STATE_START;
11409 }
11410 else
11411 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011412 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011413 * When there are postponed prefixes we need to use these first. At
11414 * the end of the prefix we continue in the case-fold tree.
11415 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011416 fbyts = slang->sl_fbyts;
11417 fidxs = slang->sl_fidxs;
11418 pbyts = slang->sl_pbyts;
11419 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011420 if (pbyts != NULL)
11421 {
11422 byts = pbyts;
11423 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011424 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011425 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11426 }
11427 else
11428 {
11429 byts = fbyts;
11430 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011431 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011432 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011433 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011434 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011435
Bram Moolenaar4770d092006-01-12 23:22:24 +000011436 /*
11437 * Loop to find all suggestions. At each round we either:
11438 * - For the current state try one operation, advance "ts_curi",
11439 * increase "depth".
11440 * - When a state is done go to the next, set "ts_state".
11441 * - When all states are tried decrease "depth".
11442 */
11443 while (depth >= 0 && !got_int)
11444 {
11445 sp = &stack[depth];
11446 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011448 case STATE_START:
11449 case STATE_NOPREFIX:
11450 /*
11451 * Start of node: Deal with NUL bytes, which means
11452 * tword[] may end here.
11453 */
11454 arridx = sp->ts_arridx; /* current node in the tree */
11455 len = byts[arridx]; /* bytes in this node */
11456 arridx += sp->ts_curi; /* index of current byte */
11457
11458 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011460 /* Skip over the NUL bytes, we use them later. */
11461 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11462 ;
11463 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 /* Always past NUL bytes now. */
11466 n = (int)sp->ts_state;
11467 sp->ts_state = STATE_ENDNUL;
11468 sp->ts_save_badflags = su->su_badflags;
11469
11470 /* At end of a prefix or at start of prefixtree: check for
11471 * following word. */
11472 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011473 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011474 /* Set su->su_badflags to the caps type at this position.
11475 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011476#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011477 if (has_mbyte)
11478 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11479 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011480#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011481 n = sp->ts_fidx;
11482 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11483 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011484 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011485#ifdef DEBUG_TRIEWALK
11486 sprintf(changename[depth], "prefix");
11487#endif
11488 go_deeper(stack, depth, 0);
11489 ++depth;
11490 sp = &stack[depth];
11491 sp->ts_prefixdepth = depth - 1;
11492 byts = fbyts;
11493 idxs = fidxs;
11494 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011495
Bram Moolenaar4770d092006-01-12 23:22:24 +000011496 /* Move the prefix to preword[] with the right case
11497 * and make find_keepcap_word() works. */
11498 tword[sp->ts_twordlen] = NUL;
11499 make_case_word(tword + sp->ts_splitoff,
11500 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011501 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011502 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011503 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 break;
11505 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011506
Bram Moolenaar4770d092006-01-12 23:22:24 +000011507 if (sp->ts_curi > len || byts[arridx] != 0)
11508 {
11509 /* Past bytes in node and/or past NUL bytes. */
11510 sp->ts_state = STATE_ENDNUL;
11511 sp->ts_save_badflags = su->su_badflags;
11512 break;
11513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514
Bram Moolenaar4770d092006-01-12 23:22:24 +000011515 /*
11516 * End of word in tree.
11517 */
11518 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519
Bram Moolenaar4770d092006-01-12 23:22:24 +000011520 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011521
11522 /* Skip words with the NOSUGGEST flag. */
11523 if (flags & WF_NOSUGGEST)
11524 break;
11525
Bram Moolenaar4770d092006-01-12 23:22:24 +000011526 fword_ends = (fword[sp->ts_fidx] == NUL
11527 || (soundfold
11528 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011529 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011530 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531
Bram Moolenaar4770d092006-01-12 23:22:24 +000011532 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011533 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011534 {
11535 /* There was a prefix before the word. Check that the prefix
11536 * can be used with this word. */
11537 /* Count the length of the NULs in the prefix. If there are
11538 * none this must be the first try without a prefix. */
11539 n = stack[sp->ts_prefixdepth].ts_arridx;
11540 len = pbyts[n++];
11541 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11542 ;
11543 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011544 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011545 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011546 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011547 if (c == 0)
11548 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011549
Bram Moolenaar4770d092006-01-12 23:22:24 +000011550 /* Use the WF_RARE flag for a rare prefix. */
11551 if (c & WF_RAREPFX)
11552 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011553
Bram Moolenaar4770d092006-01-12 23:22:24 +000011554 /* Tricky: when checking for both prefix and compounding
11555 * we run into the prefix flag first.
11556 * Remember that it's OK, so that we accept the prefix
11557 * when arriving at a compound flag. */
11558 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011559 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011560 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011561
Bram Moolenaar4770d092006-01-12 23:22:24 +000011562 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11563 * appending another compound word below. */
11564 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011565 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011566 goodword_ends = FALSE;
11567 else
11568 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011569
Bram Moolenaar4770d092006-01-12 23:22:24 +000011570 p = NULL;
11571 compound_ok = TRUE;
11572 if (sp->ts_complen > sp->ts_compsplit)
11573 {
11574 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011575 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011576 /* There was a word before this word. When there was no
11577 * change in this word (it was correct) add the first word
11578 * as a suggestion. If this word was corrected too, we
11579 * need to check if a correct word follows. */
11580 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011581 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011582 && STRNCMP(fword + sp->ts_splitfidx,
11583 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011584 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011585 {
11586 preword[sp->ts_prewordlen] = NUL;
11587 newscore = score_wordcount_adj(slang, sp->ts_score,
11588 preword + sp->ts_prewordlen,
11589 sp->ts_prewordlen > 0);
11590 /* Add the suggestion if the score isn't too bad. */
11591 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011592 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011593 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011594 newscore, 0, FALSE,
11595 lp->lp_sallang, FALSE);
11596 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011597 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011598 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011599 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011600 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011601 /* There was a compound word before this word. If this
11602 * word does not support compounding then give up
11603 * (splitting is tried for the word without compound
11604 * flag). */
11605 if (((unsigned)flags >> 24) == 0
11606 || sp->ts_twordlen - sp->ts_splitoff
11607 < slang->sl_compminlen)
11608 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011609#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011610 /* For multi-byte chars check character length against
11611 * COMPOUNDMIN. */
11612 if (has_mbyte
11613 && slang->sl_compminlen > 0
11614 && mb_charlen(tword + sp->ts_splitoff)
11615 < slang->sl_compminlen)
11616 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011617#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011618
Bram Moolenaar4770d092006-01-12 23:22:24 +000011619 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11620 compflags[sp->ts_complen + 1] = NUL;
11621 vim_strncpy(preword + sp->ts_prewordlen,
11622 tword + sp->ts_splitoff,
11623 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011624
11625 /* Verify CHECKCOMPOUNDPATTERN rules. */
11626 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11627 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011628 compound_ok = FALSE;
11629
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011630 if (compound_ok)
11631 {
11632 p = preword;
11633 while (*skiptowhite(p) != NUL)
11634 p = skipwhite(skiptowhite(p));
11635 if (fword_ends && !can_compound(slang, p,
11636 compflags + sp->ts_compsplit))
11637 /* Compound is not allowed. But it may still be
11638 * possible if we add another (short) word. */
11639 compound_ok = FALSE;
11640 }
11641
Bram Moolenaar4770d092006-01-12 23:22:24 +000011642 /* Get pointer to last char of previous word. */
11643 p = preword + sp->ts_prewordlen;
11644 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011645 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011646 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647
Bram Moolenaar4770d092006-01-12 23:22:24 +000011648 /*
11649 * Form the word with proper case in preword.
11650 * If there is a word from a previous split, append.
11651 * For the soundfold tree don't change the case, simply append.
11652 */
11653 if (soundfold)
11654 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11655 else if (flags & WF_KEEPCAP)
11656 /* Must find the word in the keep-case tree. */
11657 find_keepcap_word(slang, tword + sp->ts_splitoff,
11658 preword + sp->ts_prewordlen);
11659 else
11660 {
11661 /* Include badflags: If the badword is onecap or allcap
11662 * use that for the goodword too. But if the badword is
11663 * allcap and it's only one char long use onecap. */
11664 c = su->su_badflags;
11665 if ((c & WF_ALLCAP)
11666#ifdef FEAT_MBYTE
11667 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11668#else
11669 && su->su_badlen == 1
11670#endif
11671 )
11672 c = WF_ONECAP;
11673 c |= flags;
11674
11675 /* When appending a compound word after a word character don't
11676 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011677 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011678 c &= ~WF_ONECAP;
11679 make_case_word(tword + sp->ts_splitoff,
11680 preword + sp->ts_prewordlen, c);
11681 }
11682
11683 if (!soundfold)
11684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011685 /* Don't use a banned word. It may appear again as a good
11686 * word, thus remember it. */
11687 if (flags & WF_BANNED)
11688 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011689 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 break;
11691 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011692 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011693 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11694 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011695 {
11696 if (slang->sl_compprog == NULL)
11697 break;
11698 /* the word so far was banned but we may try compounding */
11699 goodword_ends = FALSE;
11700 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011701 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702
Bram Moolenaar4770d092006-01-12 23:22:24 +000011703 newscore = 0;
11704 if (!soundfold) /* soundfold words don't have flags */
11705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011707 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011708 newscore += SCORE_REGION;
11709 if (flags & WF_RARE)
11710 newscore += SCORE_RARE;
11711
Bram Moolenaar0c405862005-06-22 22:26:26 +000011712 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011713 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011714 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716
Bram Moolenaar4770d092006-01-12 23:22:24 +000011717 /* TODO: how about splitting in the soundfold tree? */
11718 if (fword_ends
11719 && goodword_ends
11720 && sp->ts_fidx >= sp->ts_fidxtry
11721 && compound_ok)
11722 {
11723 /* The badword also ends: add suggestions. */
11724#ifdef DEBUG_TRIEWALK
11725 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011726 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011727 int j;
11728
11729 /* print the stack of changes that brought us here */
11730 smsg("------ %s -------", fword);
11731 for (j = 0; j < depth; ++j)
11732 smsg("%s", changename[j]);
11733 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011734#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011735 if (soundfold)
11736 {
11737 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011738 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011739 add_sound_suggest(su, preword, sp->ts_score, lp);
11740 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011741 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011742 {
11743 /* Give a penalty when changing non-word char to word
11744 * char, e.g., "thes," -> "these". */
11745 p = fword + sp->ts_fidx;
11746 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011747 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011748 {
11749 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011750 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011751 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011752 newscore += SCORE_NONWORD;
11753 }
11754
Bram Moolenaar4770d092006-01-12 23:22:24 +000011755 /* Give a bonus to words seen before. */
11756 score = score_wordcount_adj(slang,
11757 sp->ts_score + newscore,
11758 preword + sp->ts_prewordlen,
11759 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011760
Bram Moolenaar4770d092006-01-12 23:22:24 +000011761 /* Add the suggestion if the score isn't too bad. */
11762 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011763 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011764 add_suggestion(su, &su->su_ga, preword,
11765 sp->ts_fidx - repextra,
11766 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011767
11768 if (su->su_badflags & WF_MIXCAP)
11769 {
11770 /* We really don't know if the word should be
11771 * upper or lower case, add both. */
11772 c = captype(preword, NULL);
11773 if (c == 0 || c == WF_ALLCAP)
11774 {
11775 make_case_word(tword + sp->ts_splitoff,
11776 preword + sp->ts_prewordlen,
11777 c == 0 ? WF_ALLCAP : 0);
11778
11779 add_suggestion(su, &su->su_ga, preword,
11780 sp->ts_fidx - repextra,
11781 score + SCORE_ICASE, 0, FALSE,
11782 lp->lp_sallang, FALSE);
11783 }
11784 }
11785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011787 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011788
Bram Moolenaar4770d092006-01-12 23:22:24 +000011789 /*
11790 * Try word split and/or compounding.
11791 */
11792 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011793#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011794 /* Don't split halfway a character. */
11795 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011796#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011797 )
11798 {
11799 int try_compound;
11800 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011801
Bram Moolenaar4770d092006-01-12 23:22:24 +000011802 /* If past the end of the bad word don't try a split.
11803 * Otherwise try changing the next word. E.g., find
11804 * suggestions for "the the" where the second "the" is
11805 * different. It's done like a split.
11806 * TODO: word split for soundfold words */
11807 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11808 && !soundfold;
11809
11810 /* Get here in several situations:
11811 * 1. The word in the tree ends:
11812 * If the word allows compounding try that. Otherwise try
11813 * a split by inserting a space. For both check that a
11814 * valid words starts at fword[sp->ts_fidx].
11815 * For NOBREAK do like compounding to be able to check if
11816 * the next word is valid.
11817 * 2. The badword does end, but it was due to a change (e.g.,
11818 * a swap). No need to split, but do check that the
11819 * following word is valid.
11820 * 3. The badword and the word in the tree end. It may still
11821 * be possible to compound another (short) word.
11822 */
11823 try_compound = FALSE;
11824 if (!soundfold
11825 && slang->sl_compprog != NULL
11826 && ((unsigned)flags >> 24) != 0
11827 && sp->ts_twordlen - sp->ts_splitoff
11828 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011829#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011830 && (!has_mbyte
11831 || slang->sl_compminlen == 0
11832 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011833 >= slang->sl_compminlen)
11834#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011835 && (slang->sl_compsylmax < MAXWLEN
11836 || sp->ts_complen + 1 - sp->ts_compsplit
11837 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011838 && (can_be_compound(sp, slang,
11839 compflags, ((unsigned)flags >> 24))))
11840
Bram Moolenaar4770d092006-01-12 23:22:24 +000011841 {
11842 try_compound = TRUE;
11843 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11844 compflags[sp->ts_complen + 1] = NUL;
11845 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011846
Bram Moolenaar4770d092006-01-12 23:22:24 +000011847 /* For NOBREAK we never try splitting, it won't make any word
11848 * valid. */
11849 if (slang->sl_nobreak)
11850 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011851
Bram Moolenaar4770d092006-01-12 23:22:24 +000011852 /* If we could add a compound word, and it's also possible to
11853 * split at this point, do the split first and set
11854 * TSF_DIDSPLIT to avoid doing it again. */
11855 else if (!fword_ends
11856 && try_compound
11857 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11858 {
11859 try_compound = FALSE;
11860 sp->ts_flags |= TSF_DIDSPLIT;
11861 --sp->ts_curi; /* do the same NUL again */
11862 compflags[sp->ts_complen] = NUL;
11863 }
11864 else
11865 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011866
Bram Moolenaar4770d092006-01-12 23:22:24 +000011867 if (try_split || try_compound)
11868 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011869 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011870 {
11871 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011872 * words so far are valid for compounding. If there
11873 * is only one word it must not have the NEEDCOMPOUND
11874 * flag. */
11875 if (sp->ts_complen == sp->ts_compsplit
11876 && (flags & WF_NEEDCOMP))
11877 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011878 p = preword;
11879 while (*skiptowhite(p) != NUL)
11880 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011881 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011882 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011883 compflags + sp->ts_compsplit))
11884 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011885
11886 if (slang->sl_nosplitsugs)
11887 newscore += SCORE_SPLIT_NO;
11888 else
11889 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011890
11891 /* Give a bonus to words seen before. */
11892 newscore = score_wordcount_adj(slang, newscore,
11893 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011894 }
11895
Bram Moolenaar4770d092006-01-12 23:22:24 +000011896 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011897 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011898 go_deeper(stack, depth, newscore);
11899#ifdef DEBUG_TRIEWALK
11900 if (!try_compound && !fword_ends)
11901 sprintf(changename[depth], "%.*s-%s: split",
11902 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11903 else
11904 sprintf(changename[depth], "%.*s-%s: compound",
11905 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11906#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011908 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011909 sp->ts_state = STATE_SPLITUNDO;
11910
11911 ++depth;
11912 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011914 /* Append a space to preword when splitting. */
11915 if (!try_compound && !fword_ends)
11916 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011917 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011918 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011919 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011920
11921 /* If the badword has a non-word character at this
11922 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011923 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011924 * character when the word ends. But only when the
11925 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011926 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011927 + sp->ts_fidx,
11928 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011929 || fword_ends)
11930 && fword[sp->ts_fidx] != NUL
11931 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011932 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011933 int l;
11934
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011935#ifdef FEAT_MBYTE
11936 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011937 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011938 else
11939#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011940 l = 1;
11941 if (fword_ends)
11942 {
11943 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011944 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011945 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011946 sp->ts_prewordlen += l;
11947 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011948 }
11949 else
11950 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11951 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011952 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011953
Bram Moolenaard12a1322005-08-21 22:08:24 +000011954 /* When compounding include compound flag in
11955 * compflags[] (already set above). When splitting we
11956 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011957 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011958 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011959 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011960 sp->ts_compsplit = sp->ts_complen;
11961 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011962
Bram Moolenaar53805d12005-08-01 07:08:33 +000011963 /* set su->su_badflags to the caps type at this
11964 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011965#ifdef FEAT_MBYTE
11966 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011967 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011968 else
11969#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011970 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011971 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011972 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011975 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011976
11977 /* If there are postponed prefixes, try these too. */
11978 if (pbyts != NULL)
11979 {
11980 byts = pbyts;
11981 idxs = pidxs;
11982 sp->ts_prefixdepth = PFD_PREFIXTREE;
11983 sp->ts_state = STATE_NOPREFIX;
11984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 }
11986 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011987 }
11988 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011989
Bram Moolenaar4770d092006-01-12 23:22:24 +000011990 case STATE_SPLITUNDO:
11991 /* Undo the changes done for word split or compound word. */
11992 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993
Bram Moolenaar4770d092006-01-12 23:22:24 +000011994 /* Continue looking for NUL bytes. */
11995 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011996
Bram Moolenaar4770d092006-01-12 23:22:24 +000011997 /* In case we went into the prefix tree. */
11998 byts = fbyts;
11999 idxs = fidxs;
12000 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012001
Bram Moolenaar4770d092006-01-12 23:22:24 +000012002 case STATE_ENDNUL:
12003 /* Past the NUL bytes in the node. */
12004 su->su_badflags = sp->ts_save_badflags;
12005 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000012006#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012007 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000012008#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012009 )
12010 {
12011 /* The badword ends, can't use STATE_PLAIN. */
12012 sp->ts_state = STATE_DEL;
12013 break;
12014 }
12015 sp->ts_state = STATE_PLAIN;
12016 /*FALLTHROUGH*/
12017
12018 case STATE_PLAIN:
12019 /*
12020 * Go over all possible bytes at this node, add each to tword[]
12021 * and use child node. "ts_curi" is the index.
12022 */
12023 arridx = sp->ts_arridx;
12024 if (sp->ts_curi > byts[arridx])
12025 {
12026 /* Done all bytes at this node, do next state. When still at
12027 * already changed bytes skip the other tricks. */
12028 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012031 sp->ts_state = STATE_FINAL;
12032 }
12033 else
12034 {
12035 arridx += sp->ts_curi++;
12036 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037
Bram Moolenaar4770d092006-01-12 23:22:24 +000012038 /* Normal byte, go one level deeper. If it's not equal to the
12039 * byte in the bad word adjust the score. But don't even try
12040 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +010012041 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +000012042 * delete + substitute. */
12043 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000012044#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012045 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012046#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012047 )
12048 newscore = 0;
12049 else
12050 newscore = SCORE_SUBST;
12051 if ((newscore == 0
12052 || (sp->ts_fidx >= sp->ts_fidxtry
12053 && ((sp->ts_flags & TSF_DIDDEL) == 0
12054 || c != fword[sp->ts_delidx])))
12055 && TRY_DEEPER(su, stack, depth, newscore))
12056 {
12057 go_deeper(stack, depth, newscore);
12058#ifdef DEBUG_TRIEWALK
12059 if (newscore > 0)
12060 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12061 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12062 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012063 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012064 sprintf(changename[depth], "%.*s-%s: accept %c",
12065 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12066 fword[sp->ts_fidx]);
12067#endif
12068 ++depth;
12069 sp = &stack[depth];
12070 ++sp->ts_fidx;
12071 tword[sp->ts_twordlen++] = c;
12072 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012073#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012074 if (newscore == SCORE_SUBST)
12075 sp->ts_isdiff = DIFF_YES;
12076 if (has_mbyte)
12077 {
12078 /* Multi-byte characters are a bit complicated to
12079 * handle: They differ when any of the bytes differ
12080 * and then their length may also differ. */
12081 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012082 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012083 /* First byte. */
12084 sp->ts_tcharidx = 0;
12085 sp->ts_tcharlen = MB_BYTE2LEN(c);
12086 sp->ts_fcharstart = sp->ts_fidx - 1;
12087 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012088 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012089 }
12090 else if (sp->ts_isdiff == DIFF_INSERT)
12091 /* When inserting trail bytes don't advance in the
12092 * bad word. */
12093 --sp->ts_fidx;
12094 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12095 {
12096 /* Last byte of character. */
12097 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012098 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012099 /* Correct ts_fidx for the byte length of the
12100 * character (we didn't check that before). */
12101 sp->ts_fidx = sp->ts_fcharstart
12102 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012103 fword[sp->ts_fcharstart]);
12104
Bram Moolenaar4770d092006-01-12 23:22:24 +000012105 /* For changing a composing character adjust
12106 * the score from SCORE_SUBST to
12107 * SCORE_SUBCOMP. */
12108 if (enc_utf8
12109 && utf_iscomposing(
12110 mb_ptr2char(tword
12111 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012112 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012113 && utf_iscomposing(
12114 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012115 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012116 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012117 SCORE_SUBST - SCORE_SUBCOMP;
12118
Bram Moolenaar4770d092006-01-12 23:22:24 +000012119 /* For a similar character adjust score from
12120 * SCORE_SUBST to SCORE_SIMILAR. */
12121 else if (!soundfold
12122 && slang->sl_has_map
12123 && similar_chars(slang,
12124 mb_ptr2char(tword
12125 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012126 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012127 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012128 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012129 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012130 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012131 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012132 else if (sp->ts_isdiff == DIFF_INSERT
12133 && sp->ts_twordlen > sp->ts_tcharlen)
12134 {
12135 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12136 c = mb_ptr2char(p);
12137 if (enc_utf8 && utf_iscomposing(c))
12138 {
12139 /* Inserting a composing char doesn't
12140 * count that much. */
12141 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12142 }
12143 else
12144 {
12145 /* If the previous character was the same,
12146 * thus doubling a character, give a bonus
12147 * to the score. Also for the soundfold
12148 * tree (might seem illogical but does
12149 * give better scores). */
12150 mb_ptr_back(tword, p);
12151 if (c == mb_ptr2char(p))
12152 sp->ts_score -= SCORE_INS
12153 - SCORE_INSDUP;
12154 }
12155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012156
Bram Moolenaar4770d092006-01-12 23:22:24 +000012157 /* Starting a new char, reset the length. */
12158 sp->ts_tcharlen = 0;
12159 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012160 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012161 else
12162#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012163 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012164 /* If we found a similar char adjust the score.
12165 * We do this after calling go_deeper() because
12166 * it's slow. */
12167 if (newscore != 0
12168 && !soundfold
12169 && slang->sl_has_map
12170 && similar_chars(slang,
12171 c, fword[sp->ts_fidx - 1]))
12172 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012175 }
12176 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177
Bram Moolenaar4770d092006-01-12 23:22:24 +000012178 case STATE_DEL:
12179#ifdef FEAT_MBYTE
12180 /* When past the first byte of a multi-byte char don't try
12181 * delete/insert/swap a character. */
12182 if (has_mbyte && sp->ts_tcharlen > 0)
12183 {
12184 sp->ts_state = STATE_FINAL;
12185 break;
12186 }
12187#endif
12188 /*
12189 * Try skipping one character in the bad word (delete it).
12190 */
12191 sp->ts_state = STATE_INS_PREP;
12192 sp->ts_curi = 1;
12193 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12194 /* Deleting a vowel at the start of a word counts less, see
12195 * soundalike_score(). */
12196 newscore = 2 * SCORE_DEL / 3;
12197 else
12198 newscore = SCORE_DEL;
12199 if (fword[sp->ts_fidx] != NUL
12200 && TRY_DEEPER(su, stack, depth, newscore))
12201 {
12202 go_deeper(stack, depth, newscore);
12203#ifdef DEBUG_TRIEWALK
12204 sprintf(changename[depth], "%.*s-%s: delete %c",
12205 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12206 fword[sp->ts_fidx]);
12207#endif
12208 ++depth;
12209
12210 /* Remember what character we deleted, so that we can avoid
12211 * inserting it again. */
12212 stack[depth].ts_flags |= TSF_DIDDEL;
12213 stack[depth].ts_delidx = sp->ts_fidx;
12214
12215 /* Advance over the character in fword[]. Give a bonus to the
12216 * score if the same character is following "nn" -> "n". It's
12217 * a bit illogical for soundfold tree but it does give better
12218 * results. */
12219#ifdef FEAT_MBYTE
12220 if (has_mbyte)
12221 {
12222 c = mb_ptr2char(fword + sp->ts_fidx);
12223 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12224 if (enc_utf8 && utf_iscomposing(c))
12225 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12226 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12227 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12228 }
12229 else
12230#endif
12231 {
12232 ++stack[depth].ts_fidx;
12233 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12234 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12235 }
12236 break;
12237 }
12238 /*FALLTHROUGH*/
12239
12240 case STATE_INS_PREP:
12241 if (sp->ts_flags & TSF_DIDDEL)
12242 {
12243 /* If we just deleted a byte then inserting won't make sense,
12244 * a substitute is always cheaper. */
12245 sp->ts_state = STATE_SWAP;
12246 break;
12247 }
12248
12249 /* skip over NUL bytes */
12250 n = sp->ts_arridx;
12251 for (;;)
12252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 if (sp->ts_curi > byts[n])
12254 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012255 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012256 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012257 break;
12258 }
12259 if (byts[n + sp->ts_curi] != NUL)
12260 {
12261 /* Found a byte to insert. */
12262 sp->ts_state = STATE_INS;
12263 break;
12264 }
12265 ++sp->ts_curi;
12266 }
12267 break;
12268
12269 /*FALLTHROUGH*/
12270
12271 case STATE_INS:
12272 /* Insert one byte. Repeat this for each possible byte at this
12273 * node. */
12274 n = sp->ts_arridx;
12275 if (sp->ts_curi > byts[n])
12276 {
12277 /* Done all bytes at this node, go to next state. */
12278 sp->ts_state = STATE_SWAP;
12279 break;
12280 }
12281
12282 /* Do one more byte at this node, but:
12283 * - Skip NUL bytes.
12284 * - Skip the byte if it's equal to the byte in the word,
12285 * accepting that byte is always better.
12286 */
12287 n += sp->ts_curi++;
12288 c = byts[n];
12289 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12290 /* Inserting a vowel at the start of a word counts less,
12291 * see soundalike_score(). */
12292 newscore = 2 * SCORE_INS / 3;
12293 else
12294 newscore = SCORE_INS;
12295 if (c != fword[sp->ts_fidx]
12296 && TRY_DEEPER(su, stack, depth, newscore))
12297 {
12298 go_deeper(stack, depth, newscore);
12299#ifdef DEBUG_TRIEWALK
12300 sprintf(changename[depth], "%.*s-%s: insert %c",
12301 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12302 c);
12303#endif
12304 ++depth;
12305 sp = &stack[depth];
12306 tword[sp->ts_twordlen++] = c;
12307 sp->ts_arridx = idxs[n];
12308#ifdef FEAT_MBYTE
12309 if (has_mbyte)
12310 {
12311 fl = MB_BYTE2LEN(c);
12312 if (fl > 1)
12313 {
12314 /* There are following bytes for the same character.
12315 * We must find all bytes before trying
12316 * delete/insert/swap/etc. */
12317 sp->ts_tcharlen = fl;
12318 sp->ts_tcharidx = 1;
12319 sp->ts_isdiff = DIFF_INSERT;
12320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012321 }
12322 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012323 fl = 1;
12324 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012325#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012326 {
12327 /* If the previous character was the same, thus doubling a
12328 * character, give a bonus to the score. Also for
12329 * soundfold words (illogical but does give a better
12330 * score). */
12331 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012332 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012333 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012334 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012335 }
12336 break;
12337
12338 case STATE_SWAP:
12339 /*
12340 * Swap two bytes in the bad word: "12" -> "21".
12341 * We change "fword" here, it's changed back afterwards at
12342 * STATE_UNSWAP.
12343 */
12344 p = fword + sp->ts_fidx;
12345 c = *p;
12346 if (c == NUL)
12347 {
12348 /* End of word, can't swap or replace. */
12349 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352
Bram Moolenaar4770d092006-01-12 23:22:24 +000012353 /* Don't swap if the first character is not a word character.
12354 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012355 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012356 {
12357 sp->ts_state = STATE_REP_INI;
12358 break;
12359 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012360
Bram Moolenaar4770d092006-01-12 23:22:24 +000012361#ifdef FEAT_MBYTE
12362 if (has_mbyte)
12363 {
12364 n = mb_cptr2len(p);
12365 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012366 if (p[n] == NUL)
12367 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012368 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012369 c2 = c; /* don't swap non-word char */
12370 else
12371 c2 = mb_ptr2char(p + n);
12372 }
12373 else
12374#endif
12375 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012376 if (p[1] == NUL)
12377 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012378 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012379 c2 = c; /* don't swap non-word char */
12380 else
12381 c2 = p[1];
12382 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012383
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012384 /* When the second character is NUL we can't swap. */
12385 if (c2 == NUL)
12386 {
12387 sp->ts_state = STATE_REP_INI;
12388 break;
12389 }
12390
Bram Moolenaar4770d092006-01-12 23:22:24 +000012391 /* When characters are identical, swap won't do anything.
12392 * Also get here if the second char is not a word character. */
12393 if (c == c2)
12394 {
12395 sp->ts_state = STATE_SWAP3;
12396 break;
12397 }
12398 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12399 {
12400 go_deeper(stack, depth, SCORE_SWAP);
12401#ifdef DEBUG_TRIEWALK
12402 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12403 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12404 c, c2);
12405#endif
12406 sp->ts_state = STATE_UNSWAP;
12407 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012408#ifdef FEAT_MBYTE
12409 if (has_mbyte)
12410 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012411 fl = mb_char2len(c2);
12412 mch_memmove(p, p + n, fl);
12413 mb_char2bytes(c, p + fl);
12414 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012415 }
12416 else
12417#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012418 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012419 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012420 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012421 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012422 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012423 }
12424 else
12425 /* If this swap doesn't work then SWAP3 won't either. */
12426 sp->ts_state = STATE_REP_INI;
12427 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012428
Bram Moolenaar4770d092006-01-12 23:22:24 +000012429 case STATE_UNSWAP:
12430 /* Undo the STATE_SWAP swap: "21" -> "12". */
12431 p = fword + sp->ts_fidx;
12432#ifdef FEAT_MBYTE
12433 if (has_mbyte)
12434 {
12435 n = MB_BYTE2LEN(*p);
12436 c = mb_ptr2char(p + n);
12437 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12438 mb_char2bytes(c, p);
12439 }
12440 else
12441#endif
12442 {
12443 c = *p;
12444 *p = p[1];
12445 p[1] = c;
12446 }
12447 /*FALLTHROUGH*/
12448
12449 case STATE_SWAP3:
12450 /* Swap two bytes, skipping one: "123" -> "321". We change
12451 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12452 p = fword + sp->ts_fidx;
12453#ifdef FEAT_MBYTE
12454 if (has_mbyte)
12455 {
12456 n = mb_cptr2len(p);
12457 c = mb_ptr2char(p);
12458 fl = mb_cptr2len(p + n);
12459 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012460 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012461 c3 = c; /* don't swap non-word char */
12462 else
12463 c3 = mb_ptr2char(p + n + fl);
12464 }
12465 else
12466#endif
12467 {
12468 c = *p;
12469 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012470 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012471 c3 = c; /* don't swap non-word char */
12472 else
12473 c3 = p[2];
12474 }
12475
12476 /* When characters are identical: "121" then SWAP3 result is
12477 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12478 * same as SWAP on next char: "112". Thus skip all swapping.
12479 * Also skip when c3 is NUL.
12480 * Also get here when the third character is not a word character.
12481 * Second character may any char: "a.b" -> "b.a" */
12482 if (c == c3 || c3 == NUL)
12483 {
12484 sp->ts_state = STATE_REP_INI;
12485 break;
12486 }
12487 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12488 {
12489 go_deeper(stack, depth, SCORE_SWAP3);
12490#ifdef DEBUG_TRIEWALK
12491 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12492 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12493 c, c3);
12494#endif
12495 sp->ts_state = STATE_UNSWAP3;
12496 ++depth;
12497#ifdef FEAT_MBYTE
12498 if (has_mbyte)
12499 {
12500 tl = mb_char2len(c3);
12501 mch_memmove(p, p + n + fl, tl);
12502 mb_char2bytes(c2, p + tl);
12503 mb_char2bytes(c, p + fl + tl);
12504 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12505 }
12506 else
12507#endif
12508 {
12509 p[0] = p[2];
12510 p[2] = c;
12511 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12512 }
12513 }
12514 else
12515 sp->ts_state = STATE_REP_INI;
12516 break;
12517
12518 case STATE_UNSWAP3:
12519 /* Undo STATE_SWAP3: "321" -> "123" */
12520 p = fword + sp->ts_fidx;
12521#ifdef FEAT_MBYTE
12522 if (has_mbyte)
12523 {
12524 n = MB_BYTE2LEN(*p);
12525 c2 = mb_ptr2char(p + n);
12526 fl = MB_BYTE2LEN(p[n]);
12527 c = mb_ptr2char(p + n + fl);
12528 tl = MB_BYTE2LEN(p[n + fl]);
12529 mch_memmove(p + fl + tl, p, n);
12530 mb_char2bytes(c, p);
12531 mb_char2bytes(c2, p + tl);
12532 p = p + tl;
12533 }
12534 else
12535#endif
12536 {
12537 c = *p;
12538 *p = p[2];
12539 p[2] = c;
12540 ++p;
12541 }
12542
Bram Moolenaar860cae12010-06-05 23:22:07 +020012543 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012544 {
12545 /* Middle char is not a word char, skip the rotate. First and
12546 * third char were already checked at swap and swap3. */
12547 sp->ts_state = STATE_REP_INI;
12548 break;
12549 }
12550
12551 /* Rotate three characters left: "123" -> "231". We change
12552 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12553 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12554 {
12555 go_deeper(stack, depth, SCORE_SWAP3);
12556#ifdef DEBUG_TRIEWALK
12557 p = fword + sp->ts_fidx;
12558 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12559 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12560 p[0], p[1], p[2]);
12561#endif
12562 sp->ts_state = STATE_UNROT3L;
12563 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012564 p = fword + sp->ts_fidx;
12565#ifdef FEAT_MBYTE
12566 if (has_mbyte)
12567 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012568 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012569 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012570 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012571 fl += mb_cptr2len(p + n + fl);
12572 mch_memmove(p, p + n, fl);
12573 mb_char2bytes(c, p + fl);
12574 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012575 }
12576 else
12577#endif
12578 {
12579 c = *p;
12580 *p = p[1];
12581 p[1] = p[2];
12582 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012583 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012584 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012585 }
12586 else
12587 sp->ts_state = STATE_REP_INI;
12588 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012589
Bram Moolenaar4770d092006-01-12 23:22:24 +000012590 case STATE_UNROT3L:
12591 /* Undo ROT3L: "231" -> "123" */
12592 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012593#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012594 if (has_mbyte)
12595 {
12596 n = MB_BYTE2LEN(*p);
12597 n += MB_BYTE2LEN(p[n]);
12598 c = mb_ptr2char(p + n);
12599 tl = MB_BYTE2LEN(p[n]);
12600 mch_memmove(p + tl, p, n);
12601 mb_char2bytes(c, p);
12602 }
12603 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012604#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012605 {
12606 c = p[2];
12607 p[2] = p[1];
12608 p[1] = *p;
12609 *p = c;
12610 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611
Bram Moolenaar4770d092006-01-12 23:22:24 +000012612 /* Rotate three bytes right: "123" -> "312". We change "fword"
12613 * here, it's changed back afterwards at STATE_UNROT3R. */
12614 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12615 {
12616 go_deeper(stack, depth, SCORE_SWAP3);
12617#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012619 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12620 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12621 p[0], p[1], p[2]);
12622#endif
12623 sp->ts_state = STATE_UNROT3R;
12624 ++depth;
12625 p = fword + sp->ts_fidx;
12626#ifdef FEAT_MBYTE
12627 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012628 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012629 n = mb_cptr2len(p);
12630 n += mb_cptr2len(p + n);
12631 c = mb_ptr2char(p + n);
12632 tl = mb_cptr2len(p + n);
12633 mch_memmove(p + tl, p, n);
12634 mb_char2bytes(c, p);
12635 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012636 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012637 else
12638#endif
12639 {
12640 c = p[2];
12641 p[2] = p[1];
12642 p[1] = *p;
12643 *p = c;
12644 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12645 }
12646 }
12647 else
12648 sp->ts_state = STATE_REP_INI;
12649 break;
12650
12651 case STATE_UNROT3R:
12652 /* Undo ROT3R: "312" -> "123" */
12653 p = fword + sp->ts_fidx;
12654#ifdef FEAT_MBYTE
12655 if (has_mbyte)
12656 {
12657 c = mb_ptr2char(p);
12658 tl = MB_BYTE2LEN(*p);
12659 n = MB_BYTE2LEN(p[tl]);
12660 n += MB_BYTE2LEN(p[tl + n]);
12661 mch_memmove(p, p + tl, n);
12662 mb_char2bytes(c, p + n);
12663 }
12664 else
12665#endif
12666 {
12667 c = *p;
12668 *p = p[1];
12669 p[1] = p[2];
12670 p[2] = c;
12671 }
12672 /*FALLTHROUGH*/
12673
12674 case STATE_REP_INI:
12675 /* Check if matching with REP items from the .aff file would work.
12676 * Quickly skip if:
12677 * - there are no REP items and we are not in the soundfold trie
12678 * - the score is going to be too high anyway
12679 * - already applied a REP item or swapped here */
12680 if ((lp->lp_replang == NULL && !soundfold)
12681 || sp->ts_score + SCORE_REP >= su->su_maxscore
12682 || sp->ts_fidx < sp->ts_fidxtry)
12683 {
12684 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687
Bram Moolenaar4770d092006-01-12 23:22:24 +000012688 /* Use the first byte to quickly find the first entry that may
12689 * match. If the index is -1 there is none. */
12690 if (soundfold)
12691 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12692 else
12693 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694
Bram Moolenaar4770d092006-01-12 23:22:24 +000012695 if (sp->ts_curi < 0)
12696 {
12697 sp->ts_state = STATE_FINAL;
12698 break;
12699 }
12700
12701 sp->ts_state = STATE_REP;
12702 /*FALLTHROUGH*/
12703
12704 case STATE_REP:
12705 /* Try matching with REP items from the .aff file. For each match
12706 * replace the characters and check if the resulting word is
12707 * valid. */
12708 p = fword + sp->ts_fidx;
12709
12710 if (soundfold)
12711 gap = &slang->sl_repsal;
12712 else
12713 gap = &lp->lp_replang->sl_rep;
12714 while (sp->ts_curi < gap->ga_len)
12715 {
12716 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12717 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012718 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012719 /* past possible matching entries */
12720 sp->ts_curi = gap->ga_len;
12721 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012722 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012723 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12724 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12725 {
12726 go_deeper(stack, depth, SCORE_REP);
12727#ifdef DEBUG_TRIEWALK
12728 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12729 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12730 ftp->ft_from, ftp->ft_to);
12731#endif
12732 /* Need to undo this afterwards. */
12733 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012734
Bram Moolenaar4770d092006-01-12 23:22:24 +000012735 /* Change the "from" to the "to" string. */
12736 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012737 fl = (int)STRLEN(ftp->ft_from);
12738 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012739 if (fl != tl)
12740 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012741 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012742 repextra += tl - fl;
12743 }
12744 mch_memmove(p, ftp->ft_to, tl);
12745 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12746#ifdef FEAT_MBYTE
12747 stack[depth].ts_tcharlen = 0;
12748#endif
12749 break;
12750 }
12751 }
12752
12753 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12754 /* No (more) matches. */
12755 sp->ts_state = STATE_FINAL;
12756
12757 break;
12758
12759 case STATE_REP_UNDO:
12760 /* Undo a REP replacement and continue with the next one. */
12761 if (soundfold)
12762 gap = &slang->sl_repsal;
12763 else
12764 gap = &lp->lp_replang->sl_rep;
12765 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012766 fl = (int)STRLEN(ftp->ft_from);
12767 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012768 p = fword + sp->ts_fidx;
12769 if (fl != tl)
12770 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012771 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012772 repextra -= tl - fl;
12773 }
12774 mch_memmove(p, ftp->ft_from, fl);
12775 sp->ts_state = STATE_REP;
12776 break;
12777
12778 default:
12779 /* Did all possible states at this level, go up one level. */
12780 --depth;
12781
12782 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12783 {
12784 /* Continue in or go back to the prefix tree. */
12785 byts = pbyts;
12786 idxs = pidxs;
12787 }
12788
12789 /* Don't check for CTRL-C too often, it takes time. */
12790 if (--breakcheckcount == 0)
12791 {
12792 ui_breakcheck();
12793 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 }
12796 }
12797}
12798
Bram Moolenaar4770d092006-01-12 23:22:24 +000012799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012801 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012802 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012803 static void
12804go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012805 trystate_T *stack;
12806 int depth;
12807 int score_add;
12808{
Bram Moolenaarea424162005-06-16 21:51:00 +000012809 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012810 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012811 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012812 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012813 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012814}
12815
Bram Moolenaar53805d12005-08-01 07:08:33 +000012816#ifdef FEAT_MBYTE
12817/*
12818 * Case-folding may change the number of bytes: Count nr of chars in
12819 * fword[flen] and return the byte length of that many chars in "word".
12820 */
12821 static int
12822nofold_len(fword, flen, word)
12823 char_u *fword;
12824 int flen;
12825 char_u *word;
12826{
12827 char_u *p;
12828 int i = 0;
12829
12830 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12831 ++i;
12832 for (p = word; i > 0; mb_ptr_adv(p))
12833 --i;
12834 return (int)(p - word);
12835}
12836#endif
12837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012838/*
12839 * "fword" is a good word with case folded. Find the matching keep-case
12840 * words and put it in "kword".
12841 * Theoretically there could be several keep-case words that result in the
12842 * same case-folded word, but we only find one...
12843 */
12844 static void
12845find_keepcap_word(slang, fword, kword)
12846 slang_T *slang;
12847 char_u *fword;
12848 char_u *kword;
12849{
12850 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12851 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012852 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012853
12854 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012855 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 int round[MAXWLEN];
12857 int fwordidx[MAXWLEN];
12858 int uwordidx[MAXWLEN];
12859 int kwordlen[MAXWLEN];
12860
12861 int flen, ulen;
12862 int l;
12863 int len;
12864 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012865 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 char_u *p;
12867 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012868 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869
12870 if (byts == NULL)
12871 {
12872 /* array is empty: "cannot happen" */
12873 *kword = NUL;
12874 return;
12875 }
12876
12877 /* Make an all-cap version of "fword". */
12878 allcap_copy(fword, uword);
12879
12880 /*
12881 * Each character needs to be tried both case-folded and upper-case.
12882 * All this gets very complicated if we keep in mind that changing case
12883 * may change the byte length of a multi-byte character...
12884 */
12885 depth = 0;
12886 arridx[0] = 0;
12887 round[0] = 0;
12888 fwordidx[0] = 0;
12889 uwordidx[0] = 0;
12890 kwordlen[0] = 0;
12891 while (depth >= 0)
12892 {
12893 if (fword[fwordidx[depth]] == NUL)
12894 {
12895 /* We are at the end of "fword". If the tree allows a word to end
12896 * here we have found a match. */
12897 if (byts[arridx[depth] + 1] == 0)
12898 {
12899 kword[kwordlen[depth]] = NUL;
12900 return;
12901 }
12902
12903 /* kword is getting too long, continue one level up */
12904 --depth;
12905 }
12906 else if (++round[depth] > 2)
12907 {
12908 /* tried both fold-case and upper-case character, continue one
12909 * level up */
12910 --depth;
12911 }
12912 else
12913 {
12914 /*
12915 * round[depth] == 1: Try using the folded-case character.
12916 * round[depth] == 2: Try using the upper-case character.
12917 */
12918#ifdef FEAT_MBYTE
12919 if (has_mbyte)
12920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012921 flen = mb_cptr2len(fword + fwordidx[depth]);
12922 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 }
12924 else
12925#endif
12926 ulen = flen = 1;
12927 if (round[depth] == 1)
12928 {
12929 p = fword + fwordidx[depth];
12930 l = flen;
12931 }
12932 else
12933 {
12934 p = uword + uwordidx[depth];
12935 l = ulen;
12936 }
12937
12938 for (tryidx = arridx[depth]; l > 0; --l)
12939 {
12940 /* Perform a binary search in the list of accepted bytes. */
12941 len = byts[tryidx++];
12942 c = *p++;
12943 lo = tryidx;
12944 hi = tryidx + len - 1;
12945 while (lo < hi)
12946 {
12947 m = (lo + hi) / 2;
12948 if (byts[m] > c)
12949 hi = m - 1;
12950 else if (byts[m] < c)
12951 lo = m + 1;
12952 else
12953 {
12954 lo = hi = m;
12955 break;
12956 }
12957 }
12958
12959 /* Stop if there is no matching byte. */
12960 if (hi < lo || byts[lo] != c)
12961 break;
12962
12963 /* Continue at the child (if there is one). */
12964 tryidx = idxs[lo];
12965 }
12966
12967 if (l == 0)
12968 {
12969 /*
12970 * Found the matching char. Copy it to "kword" and go a
12971 * level deeper.
12972 */
12973 if (round[depth] == 1)
12974 {
12975 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12976 flen);
12977 kwordlen[depth + 1] = kwordlen[depth] + flen;
12978 }
12979 else
12980 {
12981 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12982 ulen);
12983 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12984 }
12985 fwordidx[depth + 1] = fwordidx[depth] + flen;
12986 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12987
12988 ++depth;
12989 arridx[depth] = tryidx;
12990 round[depth] = 0;
12991 }
12992 }
12993 }
12994
12995 /* Didn't find it: "cannot happen". */
12996 *kword = NUL;
12997}
12998
12999/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013000 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
13001 * su->su_sga.
13002 */
13003 static void
13004score_comp_sal(su)
13005 suginfo_T *su;
13006{
13007 langp_T *lp;
13008 char_u badsound[MAXWLEN];
13009 int i;
13010 suggest_T *stp;
13011 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013012 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013013 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013014
13015 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
13016 return;
13017
13018 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013019 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013020 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013021 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013022 if (lp->lp_slang->sl_sal.ga_len > 0)
13023 {
13024 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013025 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013026
13027 for (i = 0; i < su->su_ga.ga_len; ++i)
13028 {
13029 stp = &SUG(su->su_ga, i);
13030
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013031 /* Case-fold the suggested word, sound-fold it and compute the
13032 * sound-a-like score. */
13033 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013034 if (score < SCORE_MAXMAX)
13035 {
13036 /* Add the suggestion. */
13037 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13038 sstp->st_word = vim_strsave(stp->st_word);
13039 if (sstp->st_word != NULL)
13040 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013041 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013042 sstp->st_score = score;
13043 sstp->st_altscore = 0;
13044 sstp->st_orglen = stp->st_orglen;
13045 ++su->su_sga.ga_len;
13046 }
13047 }
13048 }
13049 break;
13050 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013051 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013052}
13053
13054/*
13055 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013056 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013057 */
13058 static void
13059score_combine(su)
13060 suginfo_T *su;
13061{
13062 int i;
13063 int j;
13064 garray_T ga;
13065 garray_T *gap;
13066 langp_T *lp;
13067 suggest_T *stp;
13068 char_u *p;
13069 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013070 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013071 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013072 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013073
13074 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013075 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013076 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013077 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013078 if (lp->lp_slang->sl_sal.ga_len > 0)
13079 {
13080 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013081 slang = lp->lp_slang;
13082 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013083
13084 for (i = 0; i < su->su_ga.ga_len; ++i)
13085 {
13086 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013087 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013088 if (stp->st_altscore == SCORE_MAXMAX)
13089 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13090 else
13091 stp->st_score = (stp->st_score * 3
13092 + stp->st_altscore) / 4;
13093 stp->st_salscore = FALSE;
13094 }
13095 break;
13096 }
13097 }
13098
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013099 if (slang == NULL) /* Using "double" without sound folding. */
13100 {
13101 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13102 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013103 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013104 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013105
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013106 /* Add the alternate score to su_sga. */
13107 for (i = 0; i < su->su_sga.ga_len; ++i)
13108 {
13109 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013110 stp->st_altscore = spell_edit_score(slang,
13111 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013112 if (stp->st_score == SCORE_MAXMAX)
13113 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13114 else
13115 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13116 stp->st_salscore = TRUE;
13117 }
13118
Bram Moolenaar4770d092006-01-12 23:22:24 +000013119 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13120 * for both lists. */
13121 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013122 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013123 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013124 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13125
13126 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13127 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13128 return;
13129
13130 stp = &SUG(ga, 0);
13131 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13132 {
13133 /* round 1: get a suggestion from su_ga
13134 * round 2: get a suggestion from su_sga */
13135 for (round = 1; round <= 2; ++round)
13136 {
13137 gap = round == 1 ? &su->su_ga : &su->su_sga;
13138 if (i < gap->ga_len)
13139 {
13140 /* Don't add a word if it's already there. */
13141 p = SUG(*gap, i).st_word;
13142 for (j = 0; j < ga.ga_len; ++j)
13143 if (STRCMP(stp[j].st_word, p) == 0)
13144 break;
13145 if (j == ga.ga_len)
13146 stp[ga.ga_len++] = SUG(*gap, i);
13147 else
13148 vim_free(p);
13149 }
13150 }
13151 }
13152
13153 ga_clear(&su->su_ga);
13154 ga_clear(&su->su_sga);
13155
13156 /* Truncate the list to the number of suggestions that will be displayed. */
13157 if (ga.ga_len > su->su_maxcount)
13158 {
13159 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13160 vim_free(stp[i].st_word);
13161 ga.ga_len = su->su_maxcount;
13162 }
13163
13164 su->su_ga = ga;
13165}
13166
13167/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013168 * For the goodword in "stp" compute the soundalike score compared to the
13169 * badword.
13170 */
13171 static int
13172stp_sal_score(stp, su, slang, badsound)
13173 suggest_T *stp;
13174 suginfo_T *su;
13175 slang_T *slang;
13176 char_u *badsound; /* sound-folded badword */
13177{
13178 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013179 char_u *pbad;
13180 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013181 char_u badsound2[MAXWLEN];
13182 char_u fword[MAXWLEN];
13183 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013184 char_u goodword[MAXWLEN];
13185 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013186
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013187 lendiff = (int)(su->su_badlen - stp->st_orglen);
13188 if (lendiff >= 0)
13189 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013190 else
13191 {
13192 /* soundfold the bad word with more characters following */
13193 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13194
13195 /* When joining two words the sound often changes a lot. E.g., "t he"
13196 * sounds like "t h" while "the" sounds like "@". Avoid that by
13197 * removing the space. Don't do it when the good word also contains a
13198 * space. */
13199 if (vim_iswhite(su->su_badptr[su->su_badlen])
13200 && *skiptowhite(stp->st_word) == NUL)
13201 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013202 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013203
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013204 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013205 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013206 }
13207
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013208 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013209 {
13210 /* Add part of the bad word to the good word, so that we soundfold
13211 * what replaces the bad word. */
13212 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013213 vim_strncpy(goodword + stp->st_wordlen,
13214 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013215 pgood = goodword;
13216 }
13217 else
13218 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013219
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013220 /* Sound-fold the word and compute the score for the difference. */
13221 spell_soundfold(slang, pgood, FALSE, goodsound);
13222
13223 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013224}
13225
Bram Moolenaar4770d092006-01-12 23:22:24 +000013226/* structure used to store soundfolded words that add_sound_suggest() has
13227 * handled already. */
13228typedef struct
13229{
13230 short sft_score; /* lowest score used */
13231 char_u sft_word[1]; /* soundfolded word, actually longer */
13232} sftword_T;
13233
13234static sftword_T dumsft;
13235#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13236#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13237
13238/*
13239 * Prepare for calling suggest_try_soundalike().
13240 */
13241 static void
13242suggest_try_soundalike_prep()
13243{
13244 langp_T *lp;
13245 int lpi;
13246 slang_T *slang;
13247
13248 /* Do this for all languages that support sound folding and for which a
13249 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013250 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013251 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013252 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013253 slang = lp->lp_slang;
13254 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13255 /* prepare the hashtable used by add_sound_suggest() */
13256 hash_init(&slang->sl_sounddone);
13257 }
13258}
13259
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013260/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013262 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 */
13264 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013265suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 suginfo_T *su;
13267{
13268 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013269 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013270 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013271 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272
Bram Moolenaar4770d092006-01-12 23:22:24 +000013273 /* Do this for all languages that support sound folding and for which a
13274 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013275 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013277 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013278 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013279 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 {
13281 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013282 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283
Bram Moolenaar4770d092006-01-12 23:22:24 +000013284 /* try all kinds of inserts/deletes/swaps/etc. */
13285 /* TODO: also soundfold the next words, so that we can try joining
13286 * and splitting */
13287 suggest_trie_walk(su, lp, salword, TRUE);
13288 }
13289 }
13290}
13291
13292/*
13293 * Finish up after calling suggest_try_soundalike().
13294 */
13295 static void
13296suggest_try_soundalike_finish()
13297{
13298 langp_T *lp;
13299 int lpi;
13300 slang_T *slang;
13301 int todo;
13302 hashitem_T *hi;
13303
13304 /* Do this for all languages that support sound folding and for which a
13305 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013306 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013307 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013308 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013309 slang = lp->lp_slang;
13310 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13311 {
13312 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013313 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013314 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13315 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013316 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013317 vim_free(HI2SFT(hi));
13318 --todo;
13319 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013320
13321 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013322 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013323 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013324 }
13325 }
13326}
13327
13328/*
13329 * A match with a soundfolded word is found. Add the good word(s) that
13330 * produce this soundfolded word.
13331 */
13332 static void
13333add_sound_suggest(su, goodword, score, lp)
13334 suginfo_T *su;
13335 char_u *goodword;
13336 int score; /* soundfold score */
13337 langp_T *lp;
13338{
13339 slang_T *slang = lp->lp_slang; /* language for sound folding */
13340 int sfwordnr;
13341 char_u *nrline;
13342 int orgnr;
13343 char_u theword[MAXWLEN];
13344 int i;
13345 int wlen;
13346 char_u *byts;
13347 idx_T *idxs;
13348 int n;
13349 int wordcount;
13350 int wc;
13351 int goodscore;
13352 hash_T hash;
13353 hashitem_T *hi;
13354 sftword_T *sft;
13355 int bc, gc;
13356 int limit;
13357
13358 /*
13359 * It's very well possible that the same soundfold word is found several
13360 * times with different scores. Since the following is quite slow only do
13361 * the words that have a better score than before. Use a hashtable to
13362 * remember the words that have been done.
13363 */
13364 hash = hash_hash(goodword);
13365 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13366 if (HASHITEM_EMPTY(hi))
13367 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013368 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13369 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013370 if (sft != NULL)
13371 {
13372 sft->sft_score = score;
13373 STRCPY(sft->sft_word, goodword);
13374 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13375 }
13376 }
13377 else
13378 {
13379 sft = HI2SFT(hi);
13380 if (score >= sft->sft_score)
13381 return;
13382 sft->sft_score = score;
13383 }
13384
13385 /*
13386 * Find the word nr in the soundfold tree.
13387 */
13388 sfwordnr = soundfold_find(slang, goodword);
13389 if (sfwordnr < 0)
13390 {
13391 EMSG2(_(e_intern2), "add_sound_suggest()");
13392 return;
13393 }
13394
13395 /*
13396 * go over the list of good words that produce this soundfold word
13397 */
13398 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13399 orgnr = 0;
13400 while (*nrline != NUL)
13401 {
13402 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13403 * previous wordnr. */
13404 orgnr += bytes2offset(&nrline);
13405
13406 byts = slang->sl_fbyts;
13407 idxs = slang->sl_fidxs;
13408
13409 /* Lookup the word "orgnr" one of the two tries. */
13410 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013411 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013412 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013413 {
13414 i = 1;
13415 if (wordcount == orgnr && byts[n + 1] == NUL)
13416 break; /* found end of word */
13417
13418 if (byts[n + 1] == NUL)
13419 ++wordcount;
13420
13421 /* skip over the NUL bytes */
13422 for ( ; byts[n + i] == NUL; ++i)
13423 if (i > byts[n]) /* safety check */
13424 {
13425 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013426 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013427 goto badword;
13428 }
13429
13430 /* One of the siblings must have the word. */
13431 for ( ; i < byts[n]; ++i)
13432 {
13433 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13434 if (wordcount + wc > orgnr)
13435 break;
13436 wordcount += wc;
13437 }
13438
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013439 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +000013440 n = idxs[n + i];
13441 }
13442badword:
13443 theword[wlen] = NUL;
13444
13445 /* Go over the possible flags and regions. */
13446 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13447 {
13448 char_u cword[MAXWLEN];
13449 char_u *p;
13450 int flags = (int)idxs[n + i];
13451
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013452 /* Skip words with the NOSUGGEST flag */
13453 if (flags & WF_NOSUGGEST)
13454 continue;
13455
Bram Moolenaar4770d092006-01-12 23:22:24 +000013456 if (flags & WF_KEEPCAP)
13457 {
13458 /* Must find the word in the keep-case tree. */
13459 find_keepcap_word(slang, theword, cword);
13460 p = cword;
13461 }
13462 else
13463 {
13464 flags |= su->su_badflags;
13465 if ((flags & WF_CAPMASK) != 0)
13466 {
13467 /* Need to fix case according to "flags". */
13468 make_case_word(theword, cword, flags);
13469 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013470 }
13471 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013472 p = theword;
13473 }
13474
13475 /* Add the suggestion. */
13476 if (sps_flags & SPS_DOUBLE)
13477 {
13478 /* Add the suggestion if the score isn't too bad. */
13479 if (score <= su->su_maxscore)
13480 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13481 score, 0, FALSE, slang, FALSE);
13482 }
13483 else
13484 {
13485 /* Add a penalty for words in another region. */
13486 if ((flags & WF_REGION)
13487 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13488 goodscore = SCORE_REGION;
13489 else
13490 goodscore = 0;
13491
13492 /* Add a small penalty for changing the first letter from
13493 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013494 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +000013495 * letter is the same, that has already been counted. */
13496 gc = PTR2CHAR(p);
13497 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013498 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013499 bc = PTR2CHAR(su->su_badword);
13500 if (!SPELL_ISUPPER(bc)
13501 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13502 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013503 }
13504
Bram Moolenaar4770d092006-01-12 23:22:24 +000013505 /* Compute the score for the good word. This only does letter
13506 * insert/delete/swap/replace. REP items are not considered,
13507 * which may make the score a bit higher.
13508 * Use a limit for the score to make it work faster. Use
13509 * MAXSCORE(), because RESCORE() will change the score.
13510 * If the limit is very high then the iterative method is
13511 * inefficient, using an array is quicker. */
13512 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13513 if (limit > SCORE_LIMITMAX)
13514 goodscore += spell_edit_score(slang, su->su_badword, p);
13515 else
13516 goodscore += spell_edit_score_limit(slang, su->su_badword,
13517 p, limit);
13518
13519 /* When going over the limit don't bother to do the rest. */
13520 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013522 /* Give a bonus to words seen before. */
13523 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524
Bram Moolenaar4770d092006-01-12 23:22:24 +000013525 /* Add the suggestion if the score isn't too bad. */
13526 goodscore = RESCORE(goodscore, score);
13527 if (goodscore <= su->su_sfmaxscore)
13528 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13529 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 }
13532 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013533 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 }
13535}
13536
13537/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013538 * Find word "word" in fold-case tree for "slang" and return the word number.
13539 */
13540 static int
13541soundfold_find(slang, word)
13542 slang_T *slang;
13543 char_u *word;
13544{
13545 idx_T arridx = 0;
13546 int len;
13547 int wlen = 0;
13548 int c;
13549 char_u *ptr = word;
13550 char_u *byts;
13551 idx_T *idxs;
13552 int wordnr = 0;
13553
13554 byts = slang->sl_sbyts;
13555 idxs = slang->sl_sidxs;
13556
13557 for (;;)
13558 {
13559 /* First byte is the number of possible bytes. */
13560 len = byts[arridx++];
13561
13562 /* If the first possible byte is a zero the word could end here.
13563 * If the word ends we found the word. If not skip the NUL bytes. */
13564 c = ptr[wlen];
13565 if (byts[arridx] == NUL)
13566 {
13567 if (c == NUL)
13568 break;
13569
13570 /* Skip over the zeros, there can be several. */
13571 while (len > 0 && byts[arridx] == NUL)
13572 {
13573 ++arridx;
13574 --len;
13575 }
13576 if (len == 0)
13577 return -1; /* no children, word should have ended here */
13578 ++wordnr;
13579 }
13580
13581 /* If the word ends we didn't find it. */
13582 if (c == NUL)
13583 return -1;
13584
13585 /* Perform a binary search in the list of accepted bytes. */
13586 if (c == TAB) /* <Tab> is handled like <Space> */
13587 c = ' ';
13588 while (byts[arridx] < c)
13589 {
13590 /* The word count is in the first idxs[] entry of the child. */
13591 wordnr += idxs[idxs[arridx]];
13592 ++arridx;
13593 if (--len == 0) /* end of the bytes, didn't find it */
13594 return -1;
13595 }
13596 if (byts[arridx] != c) /* didn't find the byte */
13597 return -1;
13598
13599 /* Continue at the child (if there is one). */
13600 arridx = idxs[arridx];
13601 ++wlen;
13602
13603 /* One space in the good word may stand for several spaces in the
13604 * checked word. */
13605 if (c == ' ')
13606 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13607 ++wlen;
13608 }
13609
13610 return wordnr;
13611}
13612
13613/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013614 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615 */
13616 static void
13617make_case_word(fword, cword, flags)
13618 char_u *fword;
13619 char_u *cword;
13620 int flags;
13621{
13622 if (flags & WF_ALLCAP)
13623 /* Make it all upper-case */
13624 allcap_copy(fword, cword);
13625 else if (flags & WF_ONECAP)
13626 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013627 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013628 else
13629 /* Use goodword as-is. */
13630 STRCPY(cword, fword);
13631}
13632
Bram Moolenaarea424162005-06-16 21:51:00 +000013633/*
13634 * Use map string "map" for languages "lp".
13635 */
13636 static void
13637set_map_str(lp, map)
13638 slang_T *lp;
13639 char_u *map;
13640{
13641 char_u *p;
13642 int headc = 0;
13643 int c;
13644 int i;
13645
13646 if (*map == NUL)
13647 {
13648 lp->sl_has_map = FALSE;
13649 return;
13650 }
13651 lp->sl_has_map = TRUE;
13652
Bram Moolenaar4770d092006-01-12 23:22:24 +000013653 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013654 for (i = 0; i < 256; ++i)
13655 lp->sl_map_array[i] = 0;
13656#ifdef FEAT_MBYTE
13657 hash_init(&lp->sl_map_hash);
13658#endif
13659
13660 /*
13661 * The similar characters are stored separated with slashes:
13662 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13663 * before the same slash. For characters above 255 sl_map_hash is used.
13664 */
13665 for (p = map; *p != NUL; )
13666 {
13667#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013668 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013669#else
13670 c = *p++;
13671#endif
13672 if (c == '/')
13673 headc = 0;
13674 else
13675 {
13676 if (headc == 0)
13677 headc = c;
13678
13679#ifdef FEAT_MBYTE
13680 /* Characters above 255 don't fit in sl_map_array[], put them in
13681 * the hash table. Each entry is the char, a NUL the headchar and
13682 * a NUL. */
13683 if (c >= 256)
13684 {
13685 int cl = mb_char2len(c);
13686 int headcl = mb_char2len(headc);
13687 char_u *b;
13688 hash_T hash;
13689 hashitem_T *hi;
13690
13691 b = alloc((unsigned)(cl + headcl + 2));
13692 if (b == NULL)
13693 return;
13694 mb_char2bytes(c, b);
13695 b[cl] = NUL;
13696 mb_char2bytes(headc, b + cl + 1);
13697 b[cl + 1 + headcl] = NUL;
13698 hash = hash_hash(b);
13699 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13700 if (HASHITEM_EMPTY(hi))
13701 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13702 else
13703 {
13704 /* This should have been checked when generating the .spl
13705 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013706 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013707 vim_free(b);
13708 }
13709 }
13710 else
13711#endif
13712 lp->sl_map_array[c] = headc;
13713 }
13714 }
13715}
13716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717/*
13718 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13719 * lines in the .aff file.
13720 */
13721 static int
13722similar_chars(slang, c1, c2)
13723 slang_T *slang;
13724 int c1;
13725 int c2;
13726{
Bram Moolenaarea424162005-06-16 21:51:00 +000013727 int m1, m2;
13728#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +020013729 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +000013730 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731
Bram Moolenaarea424162005-06-16 21:51:00 +000013732 if (c1 >= 256)
13733 {
13734 buf[mb_char2bytes(c1, buf)] = 0;
13735 hi = hash_find(&slang->sl_map_hash, buf);
13736 if (HASHITEM_EMPTY(hi))
13737 m1 = 0;
13738 else
13739 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13740 }
13741 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013742#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013743 m1 = slang->sl_map_array[c1];
13744 if (m1 == 0)
13745 return FALSE;
13746
13747
13748#ifdef FEAT_MBYTE
13749 if (c2 >= 256)
13750 {
13751 buf[mb_char2bytes(c2, buf)] = 0;
13752 hi = hash_find(&slang->sl_map_hash, buf);
13753 if (HASHITEM_EMPTY(hi))
13754 m2 = 0;
13755 else
13756 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13757 }
13758 else
13759#endif
13760 m2 = slang->sl_map_array[c2];
13761
13762 return m1 == m2;
13763}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764
13765/*
13766 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013767 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 */
13769 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013770add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13771 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013773 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013775 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013776 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013777 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013778 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013779 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013780 int maxsf; /* su_maxscore applies to soundfold score,
13781 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013783 int goodlen; /* len of goodword changed */
13784 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013786 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013787 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013788 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013790 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13791 * "thee the" is added next to changing the first "the" the "thee". */
13792 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013793 pbad = su->su_badptr + badlenarg;
13794 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013795 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013796 goodlen = (int)(pgood - goodword);
13797 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013798 if (goodlen <= 0 || badlen <= 0)
13799 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013800 mb_ptr_back(goodword, pgood);
13801 mb_ptr_back(su->su_badptr, pbad);
13802#ifdef FEAT_MBYTE
13803 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013804 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013805 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13806 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013807 }
13808 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013809#endif
13810 if (*pgood != *pbad)
13811 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013812 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013813
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013814 if (badlen == 0 && goodlen == 0)
13815 /* goodword doesn't change anything; may happen for "the the" changing
13816 * the first "the" to itself. */
13817 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013818
Bram Moolenaar89d40322006-08-29 15:30:07 +000013819 if (gap->ga_len == 0)
13820 i = -1;
13821 else
13822 {
13823 /* Check if the word is already there. Also check the length that is
13824 * being replaced "thes," -> "these" is a different suggestion from
13825 * "thes" -> "these". */
13826 stp = &SUG(*gap, 0);
13827 for (i = gap->ga_len; --i >= 0; ++stp)
13828 if (stp->st_wordlen == goodlen
13829 && stp->st_orglen == badlen
13830 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013832 /*
13833 * Found it. Remember the word with the lowest score.
13834 */
13835 if (stp->st_slang == NULL)
13836 stp->st_slang = slang;
13837
13838 new_sug.st_score = score;
13839 new_sug.st_altscore = altscore;
13840 new_sug.st_had_bonus = had_bonus;
13841
13842 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013843 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013844 /* Only one of the two had the soundalike score computed.
13845 * Need to do that for the other one now, otherwise the
13846 * scores can't be compared. This happens because
13847 * suggest_try_change() doesn't compute the soundalike
13848 * word to keep it fast, while some special methods set
13849 * the soundalike score to zero. */
13850 if (had_bonus)
13851 rescore_one(su, stp);
13852 else
13853 {
13854 new_sug.st_word = stp->st_word;
13855 new_sug.st_wordlen = stp->st_wordlen;
13856 new_sug.st_slang = stp->st_slang;
13857 new_sug.st_orglen = badlen;
13858 rescore_one(su, &new_sug);
13859 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013860 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861
Bram Moolenaar89d40322006-08-29 15:30:07 +000013862 if (stp->st_score > new_sug.st_score)
13863 {
13864 stp->st_score = new_sug.st_score;
13865 stp->st_altscore = new_sug.st_altscore;
13866 stp->st_had_bonus = new_sug.st_had_bonus;
13867 }
13868 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013869 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013870 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871
Bram Moolenaar4770d092006-01-12 23:22:24 +000013872 if (i < 0 && ga_grow(gap, 1) == OK)
13873 {
13874 /* Add a suggestion. */
13875 stp = &SUG(*gap, gap->ga_len);
13876 stp->st_word = vim_strnsave(goodword, goodlen);
13877 if (stp->st_word != NULL)
13878 {
13879 stp->st_wordlen = goodlen;
13880 stp->st_score = score;
13881 stp->st_altscore = altscore;
13882 stp->st_had_bonus = had_bonus;
13883 stp->st_orglen = badlen;
13884 stp->st_slang = slang;
13885 ++gap->ga_len;
13886
13887 /* If we have too many suggestions now, sort the list and keep
13888 * the best suggestions. */
13889 if (gap->ga_len > SUG_MAX_COUNT(su))
13890 {
13891 if (maxsf)
13892 su->su_sfmaxscore = cleanup_suggestions(gap,
13893 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13894 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013895 su->su_maxscore = cleanup_suggestions(gap,
13896 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 }
13898 }
13899 }
13900}
13901
13902/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013903 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13904 * for split words, such as "the the". Remove these from the list here.
13905 */
13906 static void
13907check_suggestions(su, gap)
13908 suginfo_T *su;
13909 garray_T *gap; /* either su_ga or su_sga */
13910{
13911 suggest_T *stp;
13912 int i;
13913 char_u longword[MAXWLEN + 1];
13914 int len;
13915 hlf_T attr;
13916
13917 stp = &SUG(*gap, 0);
13918 for (i = gap->ga_len - 1; i >= 0; --i)
13919 {
13920 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013921 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013922 len = stp[i].st_wordlen;
13923 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13924 MAXWLEN - len);
13925 attr = HLF_COUNT;
13926 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13927 if (attr != HLF_COUNT)
13928 {
13929 /* Remove this entry. */
13930 vim_free(stp[i].st_word);
13931 --gap->ga_len;
13932 if (i < gap->ga_len)
13933 mch_memmove(stp + i, stp + i + 1,
13934 sizeof(suggest_T) * (gap->ga_len - i));
13935 }
13936 }
13937}
13938
13939
13940/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 * Add a word to be banned.
13942 */
13943 static void
13944add_banned(su, word)
13945 suginfo_T *su;
13946 char_u *word;
13947{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013948 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 hash_T hash;
13950 hashitem_T *hi;
13951
Bram Moolenaar4770d092006-01-12 23:22:24 +000013952 hash = hash_hash(word);
13953 hi = hash_lookup(&su->su_banned, word, hash);
13954 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013956 s = vim_strsave(word);
13957 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 hash_add_item(&su->su_banned, hi, s, hash);
13959 }
13960}
13961
13962/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013963 * Recompute the score for all suggestions if sound-folding is possible. This
13964 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013965 */
13966 static void
13967rescore_suggestions(su)
13968 suginfo_T *su;
13969{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013970 int i;
13971
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013972 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013973 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013974 rescore_one(su, &SUG(su->su_ga, i));
13975}
13976
13977/*
13978 * Recompute the score for one suggestion if sound-folding is possible.
13979 */
13980 static void
13981rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013982 suginfo_T *su;
13983 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013984{
13985 slang_T *slang = stp->st_slang;
13986 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013987 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013988
13989 /* Only rescore suggestions that have no sal score yet and do have a
13990 * language. */
13991 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13992 {
13993 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013994 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013995 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013996 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013997 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013998 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013999 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000014000
14001 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014002 if (stp->st_altscore == SCORE_MAXMAX)
14003 stp->st_altscore = SCORE_BIG;
14004 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
14005 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014006 }
14007}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009static int
14010#ifdef __BORLANDC__
14011_RTLENTRYF
14012#endif
14013sug_compare __ARGS((const void *s1, const void *s2));
14014
14015/*
14016 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014017 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 */
14019 static int
14020#ifdef __BORLANDC__
14021_RTLENTRYF
14022#endif
14023sug_compare(s1, s2)
14024 const void *s1;
14025 const void *s2;
14026{
14027 suggest_T *p1 = (suggest_T *)s1;
14028 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014029 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014031 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014032 {
14033 n = p1->st_altscore - p2->st_altscore;
14034 if (n == 0)
14035 n = STRICMP(p1->st_word, p2->st_word);
14036 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014037 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014038}
14039
14040/*
14041 * Cleanup the suggestions:
14042 * - Sort on score.
14043 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014044 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014046 static int
14047cleanup_suggestions(gap, maxscore, keep)
14048 garray_T *gap;
14049 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014050 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014052 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014053 int i;
14054
14055 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014056 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057
14058 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014059 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014061 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014063 gap->ga_len = keep;
14064 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014065 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014066 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014067}
14068
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014069#if defined(FEAT_EVAL) || defined(PROTO)
14070/*
14071 * Soundfold a string, for soundfold().
14072 * Result is in allocated memory, NULL for an error.
14073 */
14074 char_u *
14075eval_soundfold(word)
14076 char_u *word;
14077{
14078 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014079 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014080 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014081
Bram Moolenaar860cae12010-06-05 23:22:07 +020014082 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014083 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014084 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014085 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014086 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014087 if (lp->lp_slang->sl_sal.ga_len > 0)
14088 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014089 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014090 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014091 return vim_strsave(sound);
14092 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014093 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014094
14095 /* No language with sound folding, return word as-is. */
14096 return vim_strsave(word);
14097}
14098#endif
14099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100/*
14101 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014102 *
14103 * There are many ways to turn a word into a sound-a-like representation. The
14104 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14105 * swedish name matching - survey and test of different algorithms" by Klas
14106 * Erikson.
14107 *
14108 * We support two methods:
14109 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14110 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 */
14112 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014113spell_soundfold(slang, inword, folded, res)
14114 slang_T *slang;
14115 char_u *inword;
14116 int folded; /* "inword" is already case-folded */
14117 char_u *res;
14118{
14119 char_u fword[MAXWLEN];
14120 char_u *word;
14121
14122 if (slang->sl_sofo)
14123 /* SOFOFROM and SOFOTO used */
14124 spell_soundfold_sofo(slang, inword, res);
14125 else
14126 {
14127 /* SAL items used. Requires the word to be case-folded. */
14128 if (folded)
14129 word = inword;
14130 else
14131 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014132 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014133 word = fword;
14134 }
14135
14136#ifdef FEAT_MBYTE
14137 if (has_mbyte)
14138 spell_soundfold_wsal(slang, word, res);
14139 else
14140#endif
14141 spell_soundfold_sal(slang, word, res);
14142 }
14143}
14144
14145/*
14146 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14147 * SOFOTO lines.
14148 */
14149 static void
14150spell_soundfold_sofo(slang, inword, res)
14151 slang_T *slang;
14152 char_u *inword;
14153 char_u *res;
14154{
14155 char_u *s;
14156 int ri = 0;
14157 int c;
14158
14159#ifdef FEAT_MBYTE
14160 if (has_mbyte)
14161 {
14162 int prevc = 0;
14163 int *ip;
14164
14165 /* The sl_sal_first[] table contains the translation for chars up to
14166 * 255, sl_sal the rest. */
14167 for (s = inword; *s != NUL; )
14168 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014169 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014170 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14171 c = ' ';
14172 else if (c < 256)
14173 c = slang->sl_sal_first[c];
14174 else
14175 {
14176 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14177 if (ip == NULL) /* empty list, can't match */
14178 c = NUL;
14179 else
14180 for (;;) /* find "c" in the list */
14181 {
14182 if (*ip == 0) /* not found */
14183 {
14184 c = NUL;
14185 break;
14186 }
14187 if (*ip == c) /* match! */
14188 {
14189 c = ip[1];
14190 break;
14191 }
14192 ip += 2;
14193 }
14194 }
14195
14196 if (c != NUL && c != prevc)
14197 {
14198 ri += mb_char2bytes(c, res + ri);
14199 if (ri + MB_MAXBYTES > MAXWLEN)
14200 break;
14201 prevc = c;
14202 }
14203 }
14204 }
14205 else
14206#endif
14207 {
14208 /* The sl_sal_first[] table contains the translation. */
14209 for (s = inword; (c = *s) != NUL; ++s)
14210 {
14211 if (vim_iswhite(c))
14212 c = ' ';
14213 else
14214 c = slang->sl_sal_first[c];
14215 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14216 res[ri++] = c;
14217 }
14218 }
14219
14220 res[ri] = NUL;
14221}
14222
14223 static void
14224spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 slang_T *slang;
14226 char_u *inword;
14227 char_u *res;
14228{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014229 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014231 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014233 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014235 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 int n, k = 0;
14237 int z0;
14238 int k0;
14239 int n0;
14240 int c;
14241 int pri;
14242 int p0 = -333;
14243 int c0;
14244
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014245 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014246 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 if (slang->sl_rem_accents)
14248 {
14249 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014250 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014252 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014253 {
14254 *t++ = ' ';
14255 s = skipwhite(s);
14256 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014257 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014259 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 *t++ = *s;
14261 ++s;
14262 }
14263 }
14264 *t = NUL;
14265 }
14266 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014267 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014269 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270
14271 /*
14272 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014273 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014275 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276 while ((c = word[i]) != NUL)
14277 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014278 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 n = slang->sl_sal_first[c];
14280 z0 = 0;
14281
14282 if (n >= 0)
14283 {
14284 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014285 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014287 /* Quickly skip entries that don't match the word. Most
14288 * entries are less then three chars, optimize for that. */
14289 k = smp[n].sm_leadlen;
14290 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014292 if (word[i + 1] != s[1])
14293 continue;
14294 if (k > 2)
14295 {
14296 for (j = 2; j < k; ++j)
14297 if (word[i + j] != s[j])
14298 break;
14299 if (j < k)
14300 continue;
14301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 }
14303
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014304 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014306 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307 while (*pf != NUL && *pf != word[i + k])
14308 ++pf;
14309 if (*pf == NUL)
14310 continue;
14311 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014313 s = smp[n].sm_rules;
14314 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315
14316 p0 = *s;
14317 k0 = k;
14318 while (*s == '-' && k > 1)
14319 {
14320 k--;
14321 s++;
14322 }
14323 if (*s == '<')
14324 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014325 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 {
14327 /* determine priority */
14328 pri = *s - '0';
14329 s++;
14330 }
14331 if (*s == '^' && *(s + 1) == '^')
14332 s++;
14333
14334 if (*s == NUL
14335 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014336 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014337 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014339 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014341 && spell_iswordp(word + i - 1, curwin)
14342 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 {
14344 /* search for followup rules, if: */
14345 /* followup and k > 1 and NO '-' in searchstring */
14346 c0 = word[i + k - 1];
14347 n0 = slang->sl_sal_first[c0];
14348
14349 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014350 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 {
14352 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014353 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014355 /* Quickly skip entries that don't match the word.
14356 * */
14357 k0 = smp[n0].sm_leadlen;
14358 if (k0 > 1)
14359 {
14360 if (word[i + k] != s[1])
14361 continue;
14362 if (k0 > 2)
14363 {
14364 pf = word + i + k + 1;
14365 for (j = 2; j < k0; ++j)
14366 if (*pf++ != s[j])
14367 break;
14368 if (j < k0)
14369 continue;
14370 }
14371 }
14372 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014374 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014375 {
14376 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014377 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014378 while (*pf != NUL && *pf != word[i + k0])
14379 ++pf;
14380 if (*pf == NUL)
14381 continue;
14382 ++k0;
14383 }
14384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014386 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 while (*s == '-')
14388 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389 /* "k0" gets NOT reduced because
14390 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 s++;
14392 }
14393 if (*s == '<')
14394 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014395 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 {
14397 p0 = *s - '0';
14398 s++;
14399 }
14400
14401 if (*s == NUL
14402 /* *s == '^' cuts */
14403 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014404 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014405 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 {
14407 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410
14411 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014412 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 /* rule fits; stop search */
14415 break;
14416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 }
14418
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014419 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 }
14422
14423 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014424 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014425 if (s == NULL)
14426 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014427 pf = smp[n].sm_rules;
14428 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 if (p0 == 1 && z == 0)
14430 {
14431 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014432 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14433 || res[reslen - 1] == *s))
14434 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 z0 = 1;
14436 z = 1;
14437 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014438 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 {
14440 word[i + k0] = *s;
14441 k0++;
14442 s++;
14443 }
14444 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014445 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446
14447 /* new "actual letter" */
14448 c = word[i];
14449 }
14450 else
14451 {
14452 /* no '<' rule used */
14453 i += k - 1;
14454 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014455 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014457 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014458 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 s++;
14460 }
14461 /* new "actual letter" */
14462 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014463 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 {
14465 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014466 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014467 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 i = 0;
14469 z0 = 1;
14470 }
14471 }
14472 break;
14473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 }
14475 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014476 else if (vim_iswhite(c))
14477 {
14478 c = ' ';
14479 k = 1;
14480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481
14482 if (z0 == 0)
14483 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014484 if (k && !p0 && reslen < MAXWLEN && c != NUL
14485 && (!slang->sl_collapse || reslen == 0
14486 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014487 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014488 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014489
14490 i++;
14491 z = 0;
14492 k = 0;
14493 }
14494 }
14495
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014496 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497}
14498
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014499#ifdef FEAT_MBYTE
14500/*
14501 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14502 * Multi-byte version of spell_soundfold().
14503 */
14504 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014505spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014506 slang_T *slang;
14507 char_u *inword;
14508 char_u *res;
14509{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014510 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014511 int word[MAXWLEN];
14512 int wres[MAXWLEN];
14513 int l;
14514 char_u *s;
14515 int *ws;
14516 char_u *t;
14517 int *pf;
14518 int i, j, z;
14519 int reslen;
14520 int n, k = 0;
14521 int z0;
14522 int k0;
14523 int n0;
14524 int c;
14525 int pri;
14526 int p0 = -333;
14527 int c0;
14528 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014529 int wordlen;
14530
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014531
14532 /*
14533 * Convert the multi-byte string to a wide-character string.
14534 * Remove accents, if wanted. We actually remove all non-word characters.
14535 * But keep white space.
14536 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014537 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014538 for (s = inword; *s != NUL; )
14539 {
14540 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014541 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014542 if (slang->sl_rem_accents)
14543 {
14544 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14545 {
14546 if (did_white)
14547 continue;
14548 c = ' ';
14549 did_white = TRUE;
14550 }
14551 else
14552 {
14553 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014554 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014555 continue;
14556 }
14557 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014558 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014559 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014560 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014561
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014562 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014563 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014564 * Converted from C++ to C. Added support for multi-byte chars.
14565 * Changed to keep spaces.
14566 */
14567 i = reslen = z = 0;
14568 while ((c = word[i]) != NUL)
14569 {
14570 /* Start with the first rule that has the character in the word. */
14571 n = slang->sl_sal_first[c & 0xff];
14572 z0 = 0;
14573
14574 if (n >= 0)
14575 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014576 /* Check all rules for the same index byte.
14577 * If c is 0x300 need extra check for the end of the array, as
14578 * (c & 0xff) is NUL. */
14579 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14580 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014581 {
14582 /* Quickly skip entries that don't match the word. Most
14583 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014584 if (c != ws[0])
14585 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014586 k = smp[n].sm_leadlen;
14587 if (k > 1)
14588 {
14589 if (word[i + 1] != ws[1])
14590 continue;
14591 if (k > 2)
14592 {
14593 for (j = 2; j < k; ++j)
14594 if (word[i + j] != ws[j])
14595 break;
14596 if (j < k)
14597 continue;
14598 }
14599 }
14600
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014601 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014602 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014603 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014604 while (*pf != NUL && *pf != word[i + k])
14605 ++pf;
14606 if (*pf == NUL)
14607 continue;
14608 ++k;
14609 }
14610 s = smp[n].sm_rules;
14611 pri = 5; /* default priority */
14612
14613 p0 = *s;
14614 k0 = k;
14615 while (*s == '-' && k > 1)
14616 {
14617 k--;
14618 s++;
14619 }
14620 if (*s == '<')
14621 s++;
14622 if (VIM_ISDIGIT(*s))
14623 {
14624 /* determine priority */
14625 pri = *s - '0';
14626 s++;
14627 }
14628 if (*s == '^' && *(s + 1) == '^')
14629 s++;
14630
14631 if (*s == NUL
14632 || (*s == '^'
14633 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014634 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014635 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014636 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014637 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014638 && spell_iswordp_w(word + i - 1, curwin)
14639 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014640 {
14641 /* search for followup rules, if: */
14642 /* followup and k > 1 and NO '-' in searchstring */
14643 c0 = word[i + k - 1];
14644 n0 = slang->sl_sal_first[c0 & 0xff];
14645
14646 if (slang->sl_followup && k > 1 && n0 >= 0
14647 && p0 != '-' && word[i + k] != NUL)
14648 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014649 /* Test follow-up rule for "word[i + k]"; loop over
14650 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014651 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14652 == (c0 & 0xff); ++n0)
14653 {
14654 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014655 */
14656 if (c0 != ws[0])
14657 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014658 k0 = smp[n0].sm_leadlen;
14659 if (k0 > 1)
14660 {
14661 if (word[i + k] != ws[1])
14662 continue;
14663 if (k0 > 2)
14664 {
14665 pf = word + i + k + 1;
14666 for (j = 2; j < k0; ++j)
14667 if (*pf++ != ws[j])
14668 break;
14669 if (j < k0)
14670 continue;
14671 }
14672 }
14673 k0 += k - 1;
14674
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014675 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014676 {
14677 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014678 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014679 while (*pf != NUL && *pf != word[i + k0])
14680 ++pf;
14681 if (*pf == NUL)
14682 continue;
14683 ++k0;
14684 }
14685
14686 p0 = 5;
14687 s = smp[n0].sm_rules;
14688 while (*s == '-')
14689 {
14690 /* "k0" gets NOT reduced because
14691 * "if (k0 == k)" */
14692 s++;
14693 }
14694 if (*s == '<')
14695 s++;
14696 if (VIM_ISDIGIT(*s))
14697 {
14698 p0 = *s - '0';
14699 s++;
14700 }
14701
14702 if (*s == NUL
14703 /* *s == '^' cuts */
14704 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014705 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014706 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014707 {
14708 if (k0 == k)
14709 /* this is just a piece of the string */
14710 continue;
14711
14712 if (p0 < pri)
14713 /* priority too low */
14714 continue;
14715 /* rule fits; stop search */
14716 break;
14717 }
14718 }
14719
14720 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14721 == (c0 & 0xff))
14722 continue;
14723 }
14724
14725 /* replace string */
14726 ws = smp[n].sm_to_w;
14727 s = smp[n].sm_rules;
14728 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14729 if (p0 == 1 && z == 0)
14730 {
14731 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014732 if (reslen > 0 && ws != NULL && *ws != NUL
14733 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014734 || wres[reslen - 1] == *ws))
14735 reslen--;
14736 z0 = 1;
14737 z = 1;
14738 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014739 if (ws != NULL)
14740 while (*ws != NUL && word[i + k0] != NUL)
14741 {
14742 word[i + k0] = *ws;
14743 k0++;
14744 ws++;
14745 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014746 if (k > k0)
14747 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014748 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014749
14750 /* new "actual letter" */
14751 c = word[i];
14752 }
14753 else
14754 {
14755 /* no '<' rule used */
14756 i += k - 1;
14757 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014758 if (ws != NULL)
14759 while (*ws != NUL && ws[1] != NUL
14760 && reslen < MAXWLEN)
14761 {
14762 if (reslen == 0 || wres[reslen - 1] != *ws)
14763 wres[reslen++] = *ws;
14764 ws++;
14765 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014766 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014767 if (ws == NULL)
14768 c = NUL;
14769 else
14770 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014771 if (strstr((char *)s, "^^") != NULL)
14772 {
14773 if (c != NUL)
14774 wres[reslen++] = c;
14775 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014776 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014777 i = 0;
14778 z0 = 1;
14779 }
14780 }
14781 break;
14782 }
14783 }
14784 }
14785 else if (vim_iswhite(c))
14786 {
14787 c = ' ';
14788 k = 1;
14789 }
14790
14791 if (z0 == 0)
14792 {
14793 if (k && !p0 && reslen < MAXWLEN && c != NUL
14794 && (!slang->sl_collapse || reslen == 0
14795 || wres[reslen - 1] != c))
14796 /* condense only double letters */
14797 wres[reslen++] = c;
14798
14799 i++;
14800 z = 0;
14801 k = 0;
14802 }
14803 }
14804
14805 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14806 l = 0;
14807 for (n = 0; n < reslen; ++n)
14808 {
14809 l += mb_char2bytes(wres[n], res + l);
14810 if (l + MB_MAXBYTES > MAXWLEN)
14811 break;
14812 }
14813 res[l] = NUL;
14814}
14815#endif
14816
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014817/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014818 * Compute a score for two sound-a-like words.
14819 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14820 * Instead of a generic loop we write out the code. That keeps it fast by
14821 * avoiding checks that will not be possible.
14822 */
14823 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014824soundalike_score(goodstart, badstart)
14825 char_u *goodstart; /* sound-folded good word */
14826 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014827{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014828 char_u *goodsound = goodstart;
14829 char_u *badsound = badstart;
14830 int goodlen;
14831 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014832 int n;
14833 char_u *pl, *ps;
14834 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014835 int score = 0;
14836
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014837 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014838 * counted so much, vowels halfway the word aren't counted at all. */
14839 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14840 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014841 if ((badsound[0] == NUL && goodsound[1] == NUL)
14842 || (goodsound[0] == NUL && badsound[1] == NUL))
14843 /* changing word with vowel to word without a sound */
14844 return SCORE_DEL;
14845 if (badsound[0] == NUL || goodsound[0] == NUL)
14846 /* more than two changes */
14847 return SCORE_MAXMAX;
14848
Bram Moolenaar4770d092006-01-12 23:22:24 +000014849 if (badsound[1] == goodsound[1]
14850 || (badsound[1] != NUL
14851 && goodsound[1] != NUL
14852 && badsound[2] == goodsound[2]))
14853 {
14854 /* handle like a substitute */
14855 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014856 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014857 {
14858 score = 2 * SCORE_DEL / 3;
14859 if (*badsound == '*')
14860 ++badsound;
14861 else
14862 ++goodsound;
14863 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014864 }
14865
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014866 goodlen = (int)STRLEN(goodsound);
14867 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014868
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014869 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014870 * changes. */
14871 n = goodlen - badlen;
14872 if (n < -2 || n > 2)
14873 return SCORE_MAXMAX;
14874
14875 if (n > 0)
14876 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014877 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014878 ps = badsound;
14879 }
14880 else
14881 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014882 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014883 ps = goodsound;
14884 }
14885
14886 /* Skip over the identical part. */
14887 while (*pl == *ps && *pl != NUL)
14888 {
14889 ++pl;
14890 ++ps;
14891 }
14892
14893 switch (n)
14894 {
14895 case -2:
14896 case 2:
14897 /*
14898 * Must delete two characters from "pl".
14899 */
14900 ++pl; /* first delete */
14901 while (*pl == *ps)
14902 {
14903 ++pl;
14904 ++ps;
14905 }
14906 /* strings must be equal after second delete */
14907 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014908 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014909
14910 /* Failed to compare. */
14911 break;
14912
14913 case -1:
14914 case 1:
14915 /*
14916 * Minimal one delete from "pl" required.
14917 */
14918
14919 /* 1: delete */
14920 pl2 = pl + 1;
14921 ps2 = ps;
14922 while (*pl2 == *ps2)
14923 {
14924 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014925 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014926 ++pl2;
14927 ++ps2;
14928 }
14929
14930 /* 2: delete then swap, then rest must be equal */
14931 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14932 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014933 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014934
14935 /* 3: delete then substitute, then the rest must be equal */
14936 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014937 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014938
14939 /* 4: first swap then delete */
14940 if (pl[0] == ps[1] && pl[1] == ps[0])
14941 {
14942 pl2 = pl + 2; /* swap, skip two chars */
14943 ps2 = ps + 2;
14944 while (*pl2 == *ps2)
14945 {
14946 ++pl2;
14947 ++ps2;
14948 }
14949 /* delete a char and then strings must be equal */
14950 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014951 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014952 }
14953
14954 /* 5: first substitute then delete */
14955 pl2 = pl + 1; /* substitute, skip one char */
14956 ps2 = ps + 1;
14957 while (*pl2 == *ps2)
14958 {
14959 ++pl2;
14960 ++ps2;
14961 }
14962 /* delete a char and then strings must be equal */
14963 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014964 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014965
14966 /* Failed to compare. */
14967 break;
14968
14969 case 0:
14970 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014971 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014972 * insert is only possible in combination with a delete.
14973 * 1: check if for identical strings
14974 */
14975 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014976 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014977
14978 /* 2: swap */
14979 if (pl[0] == ps[1] && pl[1] == ps[0])
14980 {
14981 pl2 = pl + 2; /* swap, skip two chars */
14982 ps2 = ps + 2;
14983 while (*pl2 == *ps2)
14984 {
14985 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014986 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014987 ++pl2;
14988 ++ps2;
14989 }
14990 /* 3: swap and swap again */
14991 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14992 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014993 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014994
14995 /* 4: swap and substitute */
14996 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014997 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014998 }
14999
15000 /* 5: substitute */
15001 pl2 = pl + 1;
15002 ps2 = ps + 1;
15003 while (*pl2 == *ps2)
15004 {
15005 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015006 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015007 ++pl2;
15008 ++ps2;
15009 }
15010
15011 /* 6: substitute and swap */
15012 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
15013 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015014 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015015
15016 /* 7: substitute and substitute */
15017 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015018 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015019
15020 /* 8: insert then delete */
15021 pl2 = pl;
15022 ps2 = ps + 1;
15023 while (*pl2 == *ps2)
15024 {
15025 ++pl2;
15026 ++ps2;
15027 }
15028 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015029 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015030
15031 /* 9: delete then insert */
15032 pl2 = pl + 1;
15033 ps2 = ps;
15034 while (*pl2 == *ps2)
15035 {
15036 ++pl2;
15037 ++ps2;
15038 }
15039 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015040 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015041
15042 /* Failed to compare. */
15043 break;
15044 }
15045
15046 return SCORE_MAXMAX;
15047}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049/*
15050 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015051 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015052 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015053 * The algorithm is described by Du and Chang, 1992.
15054 * The implementation of the algorithm comes from Aspell editdist.cpp,
15055 * edit_distance(). It has been converted from C++ to C and modified to
15056 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015057 */
15058 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000015059spell_edit_score(slang, badword, goodword)
15060 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 char_u *badword;
15062 char_u *goodword;
15063{
15064 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015065 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015066 int j, i;
15067 int t;
15068 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015069 int pbc, pgc;
15070#ifdef FEAT_MBYTE
15071 char_u *p;
15072 int wbadword[MAXWLEN];
15073 int wgoodword[MAXWLEN];
15074
15075 if (has_mbyte)
15076 {
15077 /* Get the characters from the multi-byte strings and put them in an
15078 * int array for easy access. */
15079 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015080 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015081 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015082 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015083 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015084 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015085 }
15086 else
15087#endif
15088 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015089 badlen = (int)STRLEN(badword) + 1;
15090 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092
15093 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15094#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15096 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015097 if (cnt == NULL)
15098 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099
15100 CNT(0, 0) = 0;
15101 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015102 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103
15104 for (i = 1; i <= badlen; ++i)
15105 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015106 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015107 for (j = 1; j <= goodlen; ++j)
15108 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015109#ifdef FEAT_MBYTE
15110 if (has_mbyte)
15111 {
15112 bc = wbadword[i - 1];
15113 gc = wgoodword[j - 1];
15114 }
15115 else
15116#endif
15117 {
15118 bc = badword[i - 1];
15119 gc = goodword[j - 1];
15120 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015121 if (bc == gc)
15122 CNT(i, j) = CNT(i - 1, j - 1);
15123 else
15124 {
15125 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015126 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015127 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15128 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015129 {
15130 /* For a similar character use SCORE_SIMILAR. */
15131 if (slang != NULL
15132 && slang->sl_has_map
15133 && similar_chars(slang, gc, bc))
15134 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15135 else
15136 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015138
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015139 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015141#ifdef FEAT_MBYTE
15142 if (has_mbyte)
15143 {
15144 pbc = wbadword[i - 2];
15145 pgc = wgoodword[j - 2];
15146 }
15147 else
15148#endif
15149 {
15150 pbc = badword[i - 2];
15151 pgc = goodword[j - 2];
15152 }
15153 if (bc == pgc && pbc == gc)
15154 {
15155 t = SCORE_SWAP + CNT(i - 2, j - 2);
15156 if (t < CNT(i, j))
15157 CNT(i, j) = t;
15158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015159 }
15160 t = SCORE_DEL + CNT(i - 1, j);
15161 if (t < CNT(i, j))
15162 CNT(i, j) = t;
15163 t = SCORE_INS + CNT(i, j - 1);
15164 if (t < CNT(i, j))
15165 CNT(i, j) = t;
15166 }
15167 }
15168 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015169
15170 i = CNT(badlen - 1, goodlen - 1);
15171 vim_free(cnt);
15172 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015174
Bram Moolenaar4770d092006-01-12 23:22:24 +000015175typedef struct
15176{
15177 int badi;
15178 int goodi;
15179 int score;
15180} limitscore_T;
15181
15182/*
15183 * Like spell_edit_score(), but with a limit on the score to make it faster.
15184 * May return SCORE_MAXMAX when the score is higher than "limit".
15185 *
15186 * This uses a stack for the edits still to be tried.
15187 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15188 * for multi-byte characters.
15189 */
15190 static int
15191spell_edit_score_limit(slang, badword, goodword, limit)
15192 slang_T *slang;
15193 char_u *badword;
15194 char_u *goodword;
15195 int limit;
15196{
15197 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15198 int stackidx;
15199 int bi, gi;
15200 int bi2, gi2;
15201 int bc, gc;
15202 int score;
15203 int score_off;
15204 int minscore;
15205 int round;
15206
15207#ifdef FEAT_MBYTE
15208 /* Multi-byte characters require a bit more work, use a different function
15209 * to avoid testing "has_mbyte" quite often. */
15210 if (has_mbyte)
15211 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15212#endif
15213
15214 /*
15215 * The idea is to go from start to end over the words. So long as
15216 * characters are equal just continue, this always gives the lowest score.
15217 * When there is a difference try several alternatives. Each alternative
15218 * increases "score" for the edit distance. Some of the alternatives are
15219 * pushed unto a stack and tried later, some are tried right away. At the
15220 * end of the word the score for one alternative is known. The lowest
15221 * possible score is stored in "minscore".
15222 */
15223 stackidx = 0;
15224 bi = 0;
15225 gi = 0;
15226 score = 0;
15227 minscore = limit + 1;
15228
15229 for (;;)
15230 {
15231 /* Skip over an equal part, score remains the same. */
15232 for (;;)
15233 {
15234 bc = badword[bi];
15235 gc = goodword[gi];
15236 if (bc != gc) /* stop at a char that's different */
15237 break;
15238 if (bc == NUL) /* both words end */
15239 {
15240 if (score < minscore)
15241 minscore = score;
15242 goto pop; /* do next alternative */
15243 }
15244 ++bi;
15245 ++gi;
15246 }
15247
15248 if (gc == NUL) /* goodword ends, delete badword chars */
15249 {
15250 do
15251 {
15252 if ((score += SCORE_DEL) >= minscore)
15253 goto pop; /* do next alternative */
15254 } while (badword[++bi] != NUL);
15255 minscore = score;
15256 }
15257 else if (bc == NUL) /* badword ends, insert badword chars */
15258 {
15259 do
15260 {
15261 if ((score += SCORE_INS) >= minscore)
15262 goto pop; /* do next alternative */
15263 } while (goodword[++gi] != NUL);
15264 minscore = score;
15265 }
15266 else /* both words continue */
15267 {
15268 /* If not close to the limit, perform a change. Only try changes
15269 * that may lead to a lower score than "minscore".
15270 * round 0: try deleting a char from badword
15271 * round 1: try inserting a char in badword */
15272 for (round = 0; round <= 1; ++round)
15273 {
15274 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15275 if (score_off < minscore)
15276 {
15277 if (score_off + SCORE_EDIT_MIN >= minscore)
15278 {
15279 /* Near the limit, rest of the words must match. We
15280 * can check that right now, no need to push an item
15281 * onto the stack. */
15282 bi2 = bi + 1 - round;
15283 gi2 = gi + round;
15284 while (goodword[gi2] == badword[bi2])
15285 {
15286 if (goodword[gi2] == NUL)
15287 {
15288 minscore = score_off;
15289 break;
15290 }
15291 ++bi2;
15292 ++gi2;
15293 }
15294 }
15295 else
15296 {
15297 /* try deleting/inserting a character later */
15298 stack[stackidx].badi = bi + 1 - round;
15299 stack[stackidx].goodi = gi + round;
15300 stack[stackidx].score = score_off;
15301 ++stackidx;
15302 }
15303 }
15304 }
15305
15306 if (score + SCORE_SWAP < minscore)
15307 {
15308 /* If swapping two characters makes a match then the
15309 * substitution is more expensive, thus there is no need to
15310 * try both. */
15311 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15312 {
15313 /* Swap two characters, that is: skip them. */
15314 gi += 2;
15315 bi += 2;
15316 score += SCORE_SWAP;
15317 continue;
15318 }
15319 }
15320
15321 /* Substitute one character for another which is the same
15322 * thing as deleting a character from both goodword and badword.
15323 * Use a better score when there is only a case difference. */
15324 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15325 score += SCORE_ICASE;
15326 else
15327 {
15328 /* For a similar character use SCORE_SIMILAR. */
15329 if (slang != NULL
15330 && slang->sl_has_map
15331 && similar_chars(slang, gc, bc))
15332 score += SCORE_SIMILAR;
15333 else
15334 score += SCORE_SUBST;
15335 }
15336
15337 if (score < minscore)
15338 {
15339 /* Do the substitution. */
15340 ++gi;
15341 ++bi;
15342 continue;
15343 }
15344 }
15345pop:
15346 /*
15347 * Get here to try the next alternative, pop it from the stack.
15348 */
15349 if (stackidx == 0) /* stack is empty, finished */
15350 break;
15351
15352 /* pop an item from the stack */
15353 --stackidx;
15354 gi = stack[stackidx].goodi;
15355 bi = stack[stackidx].badi;
15356 score = stack[stackidx].score;
15357 }
15358
15359 /* When the score goes over "limit" it may actually be much higher.
15360 * Return a very large number to avoid going below the limit when giving a
15361 * bonus. */
15362 if (minscore > limit)
15363 return SCORE_MAXMAX;
15364 return minscore;
15365}
15366
15367#ifdef FEAT_MBYTE
15368/*
15369 * Multi-byte version of spell_edit_score_limit().
15370 * Keep it in sync with the above!
15371 */
15372 static int
15373spell_edit_score_limit_w(slang, badword, goodword, limit)
15374 slang_T *slang;
15375 char_u *badword;
15376 char_u *goodword;
15377 int limit;
15378{
15379 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15380 int stackidx;
15381 int bi, gi;
15382 int bi2, gi2;
15383 int bc, gc;
15384 int score;
15385 int score_off;
15386 int minscore;
15387 int round;
15388 char_u *p;
15389 int wbadword[MAXWLEN];
15390 int wgoodword[MAXWLEN];
15391
15392 /* Get the characters from the multi-byte strings and put them in an
15393 * int array for easy access. */
15394 bi = 0;
15395 for (p = badword; *p != NUL; )
15396 wbadword[bi++] = mb_cptr2char_adv(&p);
15397 wbadword[bi++] = 0;
15398 gi = 0;
15399 for (p = goodword; *p != NUL; )
15400 wgoodword[gi++] = mb_cptr2char_adv(&p);
15401 wgoodword[gi++] = 0;
15402
15403 /*
15404 * The idea is to go from start to end over the words. So long as
15405 * characters are equal just continue, this always gives the lowest score.
15406 * When there is a difference try several alternatives. Each alternative
15407 * increases "score" for the edit distance. Some of the alternatives are
15408 * pushed unto a stack and tried later, some are tried right away. At the
15409 * end of the word the score for one alternative is known. The lowest
15410 * possible score is stored in "minscore".
15411 */
15412 stackidx = 0;
15413 bi = 0;
15414 gi = 0;
15415 score = 0;
15416 minscore = limit + 1;
15417
15418 for (;;)
15419 {
15420 /* Skip over an equal part, score remains the same. */
15421 for (;;)
15422 {
15423 bc = wbadword[bi];
15424 gc = wgoodword[gi];
15425
15426 if (bc != gc) /* stop at a char that's different */
15427 break;
15428 if (bc == NUL) /* both words end */
15429 {
15430 if (score < minscore)
15431 minscore = score;
15432 goto pop; /* do next alternative */
15433 }
15434 ++bi;
15435 ++gi;
15436 }
15437
15438 if (gc == NUL) /* goodword ends, delete badword chars */
15439 {
15440 do
15441 {
15442 if ((score += SCORE_DEL) >= minscore)
15443 goto pop; /* do next alternative */
15444 } while (wbadword[++bi] != NUL);
15445 minscore = score;
15446 }
15447 else if (bc == NUL) /* badword ends, insert badword chars */
15448 {
15449 do
15450 {
15451 if ((score += SCORE_INS) >= minscore)
15452 goto pop; /* do next alternative */
15453 } while (wgoodword[++gi] != NUL);
15454 minscore = score;
15455 }
15456 else /* both words continue */
15457 {
15458 /* If not close to the limit, perform a change. Only try changes
15459 * that may lead to a lower score than "minscore".
15460 * round 0: try deleting a char from badword
15461 * round 1: try inserting a char in badword */
15462 for (round = 0; round <= 1; ++round)
15463 {
15464 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15465 if (score_off < minscore)
15466 {
15467 if (score_off + SCORE_EDIT_MIN >= minscore)
15468 {
15469 /* Near the limit, rest of the words must match. We
15470 * can check that right now, no need to push an item
15471 * onto the stack. */
15472 bi2 = bi + 1 - round;
15473 gi2 = gi + round;
15474 while (wgoodword[gi2] == wbadword[bi2])
15475 {
15476 if (wgoodword[gi2] == NUL)
15477 {
15478 minscore = score_off;
15479 break;
15480 }
15481 ++bi2;
15482 ++gi2;
15483 }
15484 }
15485 else
15486 {
15487 /* try deleting a character from badword later */
15488 stack[stackidx].badi = bi + 1 - round;
15489 stack[stackidx].goodi = gi + round;
15490 stack[stackidx].score = score_off;
15491 ++stackidx;
15492 }
15493 }
15494 }
15495
15496 if (score + SCORE_SWAP < minscore)
15497 {
15498 /* If swapping two characters makes a match then the
15499 * substitution is more expensive, thus there is no need to
15500 * try both. */
15501 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15502 {
15503 /* Swap two characters, that is: skip them. */
15504 gi += 2;
15505 bi += 2;
15506 score += SCORE_SWAP;
15507 continue;
15508 }
15509 }
15510
15511 /* Substitute one character for another which is the same
15512 * thing as deleting a character from both goodword and badword.
15513 * Use a better score when there is only a case difference. */
15514 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15515 score += SCORE_ICASE;
15516 else
15517 {
15518 /* For a similar character use SCORE_SIMILAR. */
15519 if (slang != NULL
15520 && slang->sl_has_map
15521 && similar_chars(slang, gc, bc))
15522 score += SCORE_SIMILAR;
15523 else
15524 score += SCORE_SUBST;
15525 }
15526
15527 if (score < minscore)
15528 {
15529 /* Do the substitution. */
15530 ++gi;
15531 ++bi;
15532 continue;
15533 }
15534 }
15535pop:
15536 /*
15537 * Get here to try the next alternative, pop it from the stack.
15538 */
15539 if (stackidx == 0) /* stack is empty, finished */
15540 break;
15541
15542 /* pop an item from the stack */
15543 --stackidx;
15544 gi = stack[stackidx].goodi;
15545 bi = stack[stackidx].badi;
15546 score = stack[stackidx].score;
15547 }
15548
15549 /* When the score goes over "limit" it may actually be much higher.
15550 * Return a very large number to avoid going below the limit when giving a
15551 * bonus. */
15552 if (minscore > limit)
15553 return SCORE_MAXMAX;
15554 return minscore;
15555}
15556#endif
15557
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015558/*
15559 * ":spellinfo"
15560 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015561 void
15562ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015563 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015564{
15565 int lpi;
15566 langp_T *lp;
15567 char_u *p;
15568
15569 if (no_spell_checking(curwin))
15570 return;
15571
15572 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015573 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015574 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015575 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015576 msg_puts((char_u *)"file: ");
15577 msg_puts(lp->lp_slang->sl_fname);
15578 msg_putchar('\n');
15579 p = lp->lp_slang->sl_info;
15580 if (p != NULL)
15581 {
15582 msg_puts(p);
15583 msg_putchar('\n');
15584 }
15585 }
15586 msg_end();
15587}
15588
Bram Moolenaar4770d092006-01-12 23:22:24 +000015589#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15590#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015591#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015592#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15593#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015594
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015595/*
15596 * ":spelldump"
15597 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015598 void
15599ex_spelldump(eap)
15600 exarg_T *eap;
15601{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015602 char_u *spl;
15603 long dummy;
15604
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015605 if (no_spell_checking(curwin))
15606 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015607 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015608
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015609 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015610 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015611
15612 /* enable spelling locally in the new window */
15613 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
15614 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
15615 vim_free(spl);
15616
Bram Moolenaar860cae12010-06-05 23:22:07 +020015617 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015618 return;
15619
Bram Moolenaar860cae12010-06-05 23:22:07 +020015620 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015621
15622 /* Delete the empty line that we started with. */
15623 if (curbuf->b_ml.ml_line_count > 1)
15624 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15625
15626 redraw_later(NOT_VALID);
15627}
15628
15629/*
15630 * Go through all possible words and:
15631 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15632 * "ic" and "dir" are not used.
15633 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15634 */
15635 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015636spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015637 char_u *pat; /* leading part of the word */
15638 int ic; /* ignore case */
15639 int *dir; /* direction for adding matches */
15640 int dumpflags_arg; /* DUMPFLAG_* */
15641{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015642 langp_T *lp;
15643 slang_T *slang;
15644 idx_T arridx[MAXWLEN];
15645 int curi[MAXWLEN];
15646 char_u word[MAXWLEN];
15647 int c;
15648 char_u *byts;
15649 idx_T *idxs;
15650 linenr_T lnum = 0;
15651 int round;
15652 int depth;
15653 int n;
15654 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015655 char_u *region_names = NULL; /* region names being used */
15656 int do_region = TRUE; /* dump region names and numbers */
15657 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015658 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015659 int dumpflags = dumpflags_arg;
15660 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015661
Bram Moolenaard0131a82006-03-04 21:46:13 +000015662 /* When ignoring case or when the pattern starts with capital pass this on
15663 * to dump_word(). */
15664 if (pat != NULL)
15665 {
15666 if (ic)
15667 dumpflags |= DUMPFLAG_ICASE;
15668 else
15669 {
15670 n = captype(pat, NULL);
15671 if (n == WF_ONECAP)
15672 dumpflags |= DUMPFLAG_ONECAP;
15673 else if (n == WF_ALLCAP
15674#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015675 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015676#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015677 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015678#endif
15679 )
15680 dumpflags |= DUMPFLAG_ALLCAP;
15681 }
15682 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015683
Bram Moolenaar7887d882005-07-01 22:33:52 +000015684 /* Find out if we can support regions: All languages must support the same
15685 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015686 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015687 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015688 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015689 p = lp->lp_slang->sl_regions;
15690 if (p[0] != 0)
15691 {
15692 if (region_names == NULL) /* first language with regions */
15693 region_names = p;
15694 else if (STRCMP(region_names, p) != 0)
15695 {
15696 do_region = FALSE; /* region names are different */
15697 break;
15698 }
15699 }
15700 }
15701
15702 if (do_region && region_names != NULL)
15703 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015704 if (pat == NULL)
15705 {
15706 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15707 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15708 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015709 }
15710 else
15711 do_region = FALSE;
15712
15713 /*
15714 * Loop over all files loaded for the entries in 'spelllang'.
15715 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015716 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015717 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015718 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015719 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015720 if (slang->sl_fbyts == NULL) /* reloading failed */
15721 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015722
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015723 if (pat == NULL)
15724 {
15725 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15726 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15727 }
15728
15729 /* When matching with a pattern and there are no prefixes only use
15730 * parts of the tree that match "pat". */
15731 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015732 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015733 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015734 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015735
15736 /* round 1: case-folded tree
15737 * round 2: keep-case tree */
15738 for (round = 1; round <= 2; ++round)
15739 {
15740 if (round == 1)
15741 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015742 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015743 byts = slang->sl_fbyts;
15744 idxs = slang->sl_fidxs;
15745 }
15746 else
15747 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015748 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015749 byts = slang->sl_kbyts;
15750 idxs = slang->sl_kidxs;
15751 }
15752 if (byts == NULL)
15753 continue; /* array is empty */
15754
15755 depth = 0;
15756 arridx[0] = 0;
15757 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015758 while (depth >= 0 && !got_int
15759 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015760 {
15761 if (curi[depth] > byts[arridx[depth]])
15762 {
15763 /* Done all bytes at this node, go up one level. */
15764 --depth;
15765 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015766 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015767 }
15768 else
15769 {
15770 /* Do one more byte at this node. */
15771 n = arridx[depth] + curi[depth];
15772 ++curi[depth];
15773 c = byts[n];
15774 if (c == 0)
15775 {
15776 /* End of word, deal with the word.
15777 * Don't use keep-case words in the fold-case tree,
15778 * they will appear in the keep-case tree.
15779 * Only use the word when the region matches. */
15780 flags = (int)idxs[n];
15781 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015782 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015783 && (do_region
15784 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015785 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015786 & lp->lp_region) != 0))
15787 {
15788 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015789 if (!do_region)
15790 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015791
15792 /* Dump the basic word if there is no prefix or
15793 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015794 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015795 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015796 {
15797 dump_word(slang, word, pat, dir,
15798 dumpflags, flags, lnum);
15799 if (pat == NULL)
15800 ++lnum;
15801 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015802
15803 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015804 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015805 lnum = dump_prefixes(slang, word, pat, dir,
15806 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015807 }
15808 }
15809 else
15810 {
15811 /* Normal char, go one level deeper. */
15812 word[depth++] = c;
15813 arridx[depth] = idxs[n];
15814 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015815
15816 /* Check if this characters matches with the pattern.
15817 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015818 * Always ignore case here, dump_word() will check
15819 * proper case later. This isn't exactly right when
15820 * length changes for multi-byte characters with
15821 * ignore case... */
15822 if (depth <= patlen
15823 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015824 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015825 }
15826 }
15827 }
15828 }
15829 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015830}
15831
15832/*
15833 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015834 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015835 */
15836 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015837dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015838 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015839 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015840 char_u *pat;
15841 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015842 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015843 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015844 linenr_T lnum;
15845{
15846 int keepcap = FALSE;
15847 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015848 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015849 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015850 char_u badword[MAXWLEN + 10];
15851 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015852 int flags = wordflags;
15853
15854 if (dumpflags & DUMPFLAG_ONECAP)
15855 flags |= WF_ONECAP;
15856 if (dumpflags & DUMPFLAG_ALLCAP)
15857 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015858
Bram Moolenaar4770d092006-01-12 23:22:24 +000015859 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015860 {
15861 /* Need to fix case according to "flags". */
15862 make_case_word(word, cword, flags);
15863 p = cword;
15864 }
15865 else
15866 {
15867 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015868 if ((dumpflags & DUMPFLAG_KEEPCASE)
15869 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015870 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015871 keepcap = TRUE;
15872 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015873 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015874
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015875 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015876 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015877 /* Add flags and regions after a slash. */
15878 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015879 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015880 STRCPY(badword, p);
15881 STRCAT(badword, "/");
15882 if (keepcap)
15883 STRCAT(badword, "=");
15884 if (flags & WF_BANNED)
15885 STRCAT(badword, "!");
15886 else if (flags & WF_RARE)
15887 STRCAT(badword, "?");
15888 if (flags & WF_REGION)
15889 for (i = 0; i < 7; ++i)
15890 if (flags & (0x10000 << i))
15891 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15892 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015893 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015894
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015895 if (dumpflags & DUMPFLAG_COUNT)
15896 {
15897 hashitem_T *hi;
15898
15899 /* Include the word count for ":spelldump!". */
15900 hi = hash_find(&slang->sl_wordcount, tw);
15901 if (!HASHITEM_EMPTY(hi))
15902 {
15903 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15904 tw, HI2WC(hi)->wc_count);
15905 p = IObuff;
15906 }
15907 }
15908
15909 ml_append(lnum, p, (colnr_T)0, FALSE);
15910 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015911 else if (((dumpflags & DUMPFLAG_ICASE)
15912 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15913 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015914 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015915 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015916 /* if dir was BACKWARD then honor it just once */
15917 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015918}
15919
15920/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015921 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15922 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015923 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015924 * Return the updated line number.
15925 */
15926 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015927dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015928 slang_T *slang;
15929 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015930 char_u *pat;
15931 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015932 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015933 int flags; /* flags with prefix ID */
15934 linenr_T startlnum;
15935{
15936 idx_T arridx[MAXWLEN];
15937 int curi[MAXWLEN];
15938 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015939 char_u word_up[MAXWLEN];
15940 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015941 int c;
15942 char_u *byts;
15943 idx_T *idxs;
15944 linenr_T lnum = startlnum;
15945 int depth;
15946 int n;
15947 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015948 int i;
15949
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015950 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015951 * upper-case letter in word_up[]. */
15952 c = PTR2CHAR(word);
15953 if (SPELL_TOUPPER(c) != c)
15954 {
15955 onecap_copy(word, word_up, TRUE);
15956 has_word_up = TRUE;
15957 }
15958
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015959 byts = slang->sl_pbyts;
15960 idxs = slang->sl_pidxs;
15961 if (byts != NULL) /* array not is empty */
15962 {
15963 /*
15964 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015965 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015966 */
15967 depth = 0;
15968 arridx[0] = 0;
15969 curi[0] = 1;
15970 while (depth >= 0 && !got_int)
15971 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015972 n = arridx[depth];
15973 len = byts[n];
15974 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015975 {
15976 /* Done all bytes at this node, go up one level. */
15977 --depth;
15978 line_breakcheck();
15979 }
15980 else
15981 {
15982 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015983 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015984 ++curi[depth];
15985 c = byts[n];
15986 if (c == 0)
15987 {
15988 /* End of prefix, find out how many IDs there are. */
15989 for (i = 1; i < len; ++i)
15990 if (byts[n + i] != 0)
15991 break;
15992 curi[depth] += i - 1;
15993
Bram Moolenaar53805d12005-08-01 07:08:33 +000015994 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15995 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015996 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015997 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015998 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015999 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016000 : flags, lnum);
16001 if (lnum != 0)
16002 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016003 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000016004
16005 /* Check for prefix that matches the word when the
16006 * first letter is upper-case, but only if the prefix has
16007 * a condition. */
16008 if (has_word_up)
16009 {
16010 c = valid_word_prefix(i, n, flags, word_up, slang,
16011 TRUE);
16012 if (c != 0)
16013 {
16014 vim_strncpy(prefix + depth, word_up,
16015 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016016 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000016017 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016018 : flags, lnum);
16019 if (lnum != 0)
16020 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000016021 }
16022 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016023 }
16024 else
16025 {
16026 /* Normal char, go one level deeper. */
16027 prefix[depth++] = c;
16028 arridx[depth] = idxs[n];
16029 curi[depth] = 1;
16030 }
16031 }
16032 }
16033 }
16034
16035 return lnum;
16036}
16037
Bram Moolenaar95529562005-08-25 21:21:38 +000016038/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016039 * Move "p" to the end of word "start".
16040 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000016041 */
16042 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020016043spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000016044 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020016045 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000016046{
16047 char_u *p = start;
16048
Bram Moolenaar860cae12010-06-05 23:22:07 +020016049 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000016050 mb_ptr_adv(p);
16051 return p;
16052}
16053
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016054#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016055/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016056 * For Insert mode completion CTRL-X s:
16057 * Find start of the word in front of column "startcol".
16058 * We don't check if it is badly spelled, with completion we can only change
16059 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016060 * Returns the column number of the word.
16061 */
16062 int
16063spell_word_start(startcol)
16064 int startcol;
16065{
16066 char_u *line;
16067 char_u *p;
16068 int col = 0;
16069
Bram Moolenaar95529562005-08-25 21:21:38 +000016070 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016071 return startcol;
16072
16073 /* Find a word character before "startcol". */
16074 line = ml_get_curline();
16075 for (p = line + startcol; p > line; )
16076 {
16077 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010016078 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016079 break;
16080 }
16081
16082 /* Go back to start of the word. */
16083 while (p > line)
16084 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016085 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016086 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016087 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016088 break;
16089 col = 0;
16090 }
16091
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016092 return col;
16093}
16094
16095/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016096 * Need to check for 'spellcapcheck' now, the word is removed before
16097 * expand_spelling() is called. Therefore the ugly global variable.
16098 */
16099static int spell_expand_need_cap;
16100
16101 void
16102spell_expand_check_cap(col)
16103 colnr_T col;
16104{
16105 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16106}
16107
16108/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016109 * Get list of spelling suggestions.
16110 * Used for Insert mode completion CTRL-X ?.
16111 * Returns the number of matches. The matches are in "matchp[]", array of
16112 * allocated strings.
16113 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016114 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016115expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016116 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016117 char_u *pat;
16118 char_u ***matchp;
16119{
16120 garray_T ga;
16121
Bram Moolenaar4770d092006-01-12 23:22:24 +000016122 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016123 *matchp = ga.ga_data;
16124 return ga.ga_len;
16125}
16126#endif
16127
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016128#endif /* FEAT_SPELL */