blob: 2972fe9154594e5858e9ab6fdafb534fc3dfcaab [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 Moolenaare52325c2005-08-22 22:54:29 +0000320#if 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 Moolenaar9c96f592005-06-30 21:52:39 +0000757static int spell_iswordp_nmw __ARGS((char_u *p));
758#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000759static int spell_mb_isword_class __ARGS((int cl));
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 Moolenaardfb9ac02005-07-05 21:36:03 +00001152 else if (!spell_iswordp_nmw(ptr))
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;
1157
1158 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001159 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001160 regmatch.rm_ic = FALSE;
1161 if (vim_regexec(&regmatch, ptr, 0))
1162 *capcol = (int)(regmatch.endp[0] - ptr);
1163 }
1164
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001165#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001166 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001167 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001168#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001169 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001170 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001171 else if (mi.mi_end == ptr)
1172 /* Always include at least one character. Required for when there
1173 * is a mixup in "midword". */
1174 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001175 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001176 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001177 {
1178 char_u *p, *fp;
1179 int save_result = mi.mi_result;
1180
1181 /* First language in 'spelllang' is NOBREAK. Find first position
1182 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001183 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001184 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001186 p = mi.mi_word;
1187 fp = mi.mi_fword;
1188 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001189 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001190 mb_ptr_adv(p);
1191 mb_ptr_adv(fp);
1192 if (p >= mi.mi_end)
1193 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001194 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001195 find_word(&mi, FIND_COMPOUND);
1196 if (mi.mi_result != SP_BAD)
1197 {
1198 mi.mi_end = p;
1199 break;
1200 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001201 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001202 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001203 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001204 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001205
1206 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001207 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001208 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001209 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001210 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001211 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001212 }
1213
Bram Moolenaar5195e452005-08-19 20:32:47 +00001214 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1215 {
1216 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001217 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001218 return wrongcaplen;
1219 }
1220
Bram Moolenaar51485f02005-06-04 21:55:20 +00001221 return (int)(mi.mi_end - ptr);
1222}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001223
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224/*
1225 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001226 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1227 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1228 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1229 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230 *
1231 * For a match mip->mi_result is updated.
1232 */
1233 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001234find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001236 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001238 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001240 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001241 int endidxcnt = 0;
1242 int len;
1243 int wlen = 0;
1244 int flen;
1245 int c;
1246 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001247 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001248#ifdef FEAT_MBYTE
1249 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001250#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001251 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001252 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001253 slang_T *slang = mip->mi_lp->lp_slang;
1254 unsigned flags;
1255 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001256 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001258 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001259 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001260
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001261 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001262 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001263 /* Check for word with matching case in keep-case tree. */
1264 ptr = mip->mi_word;
1265 flen = 9999; /* no case folding, always enough bytes */
1266 byts = slang->sl_kbyts;
1267 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001268
1269 if (mode == FIND_KEEPCOMPOUND)
1270 /* Skip over the previously found word(s). */
1271 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001272 }
1273 else
1274 {
1275 /* Check for case-folded in case-folded tree. */
1276 ptr = mip->mi_fword;
1277 flen = mip->mi_fwordlen; /* available case-folded bytes */
1278 byts = slang->sl_fbyts;
1279 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001280
1281 if (mode == FIND_PREFIX)
1282 {
1283 /* Skip over the prefix. */
1284 wlen = mip->mi_prefixlen;
1285 flen -= mip->mi_prefixlen;
1286 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001287 else if (mode == FIND_COMPOUND)
1288 {
1289 /* Skip over the previously found word(s). */
1290 wlen = mip->mi_compoff;
1291 flen -= mip->mi_compoff;
1292 }
1293
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294 }
1295
Bram Moolenaar51485f02005-06-04 21:55:20 +00001296 if (byts == NULL)
1297 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001298
Bram Moolenaar51485f02005-06-04 21:55:20 +00001299 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001300 * Repeat advancing in the tree until:
1301 * - there is a byte that doesn't match,
1302 * - we reach the end of the tree,
1303 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001304 */
1305 for (;;)
1306 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001307 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001308 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001309
1310 len = byts[arridx++];
1311
1312 /* If the first possible byte is a zero the word could end here.
1313 * Remember this index, we first check for the longest word. */
1314 if (byts[arridx] == 0)
1315 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001316 if (endidxcnt == MAXWLEN)
1317 {
1318 /* Must be a corrupted spell file. */
1319 EMSG(_(e_format));
1320 return;
1321 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001322 endlen[endidxcnt] = wlen;
1323 endidx[endidxcnt++] = arridx++;
1324 --len;
1325
1326 /* Skip over the zeros, there can be several flag/region
1327 * combinations. */
1328 while (len > 0 && byts[arridx] == 0)
1329 {
1330 ++arridx;
1331 --len;
1332 }
1333 if (len == 0)
1334 break; /* no children, word must end here */
1335 }
1336
1337 /* Stop looking at end of the line. */
1338 if (ptr[wlen] == NUL)
1339 break;
1340
1341 /* Perform a binary search in the list of accepted bytes. */
1342 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001343 if (c == TAB) /* <Tab> is handled like <Space> */
1344 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001345 lo = arridx;
1346 hi = arridx + len - 1;
1347 while (lo < hi)
1348 {
1349 m = (lo + hi) / 2;
1350 if (byts[m] > c)
1351 hi = m - 1;
1352 else if (byts[m] < c)
1353 lo = m + 1;
1354 else
1355 {
1356 lo = hi = m;
1357 break;
1358 }
1359 }
1360
1361 /* Stop if there is no matching byte. */
1362 if (hi < lo || byts[lo] != c)
1363 break;
1364
1365 /* Continue at the child (if there is one). */
1366 arridx = idxs[lo];
1367 ++wlen;
1368 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001369
1370 /* One space in the good word may stand for several spaces in the
1371 * checked word. */
1372 if (c == ' ')
1373 {
1374 for (;;)
1375 {
1376 if (flen <= 0 && *mip->mi_fend != NUL)
1377 flen = fold_more(mip);
1378 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1379 break;
1380 ++wlen;
1381 --flen;
1382 }
1383 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001384 }
1385
1386 /*
1387 * Verify that one of the possible endings is valid. Try the longest
1388 * first.
1389 */
1390 while (endidxcnt > 0)
1391 {
1392 --endidxcnt;
1393 arridx = endidx[endidxcnt];
1394 wlen = endlen[endidxcnt];
1395
1396#ifdef FEAT_MBYTE
1397 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1398 continue; /* not at first byte of character */
1399#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001400 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001401 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001402 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001403 continue; /* next char is a word character */
1404 word_ends = FALSE;
1405 }
1406 else
1407 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001408 /* The prefix flag is before compound flags. Once a valid prefix flag
1409 * has been found we try compound flags. */
1410 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411
1412#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 {
1415 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001416 * when folding case. This can be slow, take a shortcut when the
1417 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001419 if (STRNCMP(ptr, p, wlen) != 0)
1420 {
1421 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1422 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001423 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425 }
1426#endif
1427
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001428 /* Check flags and region. For FIND_PREFIX check the condition and
1429 * prefix ID.
1430 * Repeat this if there are more flags/region alternatives until there
1431 * is a match. */
1432 res = SP_BAD;
1433 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1434 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001435 {
1436 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001437
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001438 /* For the fold-case tree check that the case of the checked word
1439 * matches with what the word in the tree requires.
1440 * For keep-case tree the case is always right. For prefixes we
1441 * don't bother to check. */
1442 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 if (mip->mi_cend != mip->mi_word + wlen)
1445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001446 /* mi_capflags was set for a different word length, need
1447 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001450 }
1451
Bram Moolenaar0c405862005-06-22 22:26:26 +00001452 if (mip->mi_capflags == WF_KEEPCAP
1453 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001454 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001455 }
1456
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001457 /* When mode is FIND_PREFIX the word must support the prefix:
1458 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001459 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001460 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001461 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001463 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001464 mip->mi_word + mip->mi_cprefixlen, slang,
1465 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001466 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001467 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001468
1469 /* Use the WF_RARE flag for a rare prefix. */
1470 if (c & WF_RAREPFX)
1471 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001472 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473 }
1474
Bram Moolenaar78622822005-08-23 21:00:13 +00001475 if (slang->sl_nobreak)
1476 {
1477 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1478 && (flags & WF_BANNED) == 0)
1479 {
1480 /* NOBREAK: found a valid following word. That's all we
1481 * need to know, so return. */
1482 mip->mi_result = SP_OK;
1483 break;
1484 }
1485 }
1486
1487 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1488 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001490 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001491 * COMPOUNDMIN reject it quickly.
1492 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 * that's too short... Myspell compatibility requires this
1494 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001495 if (((unsigned)flags >> 24) == 0
1496 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001497 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001498#ifdef FEAT_MBYTE
1499 /* For multi-byte chars check character length against
1500 * COMPOUNDMIN. */
1501 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001502 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001503 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1504 wlen - mip->mi_compoff) < slang->sl_compminlen)
1505 continue;
1506#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001507
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001508 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001509 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001510 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1511 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001512 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001513 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001514
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001515 /* Don't allow compounding on a side where an affix was added,
1516 * unless COMPOUNDPERMITFLAG was used. */
1517 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1518 continue;
1519 if (!word_ends && (flags & WF_NOCOMPAFT))
1520 continue;
1521
Bram Moolenaard12a1322005-08-21 22:08:24 +00001522 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001523 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001524 ? slang->sl_compstartflags
1525 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001526 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001527 continue;
1528
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001529 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1530 * discard the compound word. */
1531 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1532 continue;
1533
Bram Moolenaare52325c2005-08-22 22:54:29 +00001534 if (mode == FIND_COMPOUND)
1535 {
1536 int capflags;
1537
1538 /* Need to check the caps type of the appended compound
1539 * word. */
1540#ifdef FEAT_MBYTE
1541 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1542 mip->mi_compoff) != 0)
1543 {
1544 /* case folding may have changed the length */
1545 p = mip->mi_word;
1546 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1547 mb_ptr_adv(p);
1548 }
1549 else
1550#endif
1551 p = mip->mi_word + mip->mi_compoff;
1552 capflags = captype(p, mip->mi_word + wlen);
1553 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1554 && (flags & WF_FIXCAP) != 0))
1555 continue;
1556
1557 if (capflags != WF_ALLCAP)
1558 {
1559 /* When the character before the word is a word
1560 * character we do not accept a Onecap word. We do
1561 * accept a no-caps word, even when the dictionary
1562 * word specifies ONECAP. */
1563 mb_ptr_back(mip->mi_word, p);
1564 if (spell_iswordp_nmw(p)
1565 ? capflags == WF_ONECAP
1566 : (flags & WF_ONECAP) != 0
1567 && capflags != WF_ONECAP)
1568 continue;
1569 }
1570 }
1571
Bram Moolenaar5195e452005-08-19 20:32:47 +00001572 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001573 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001574 * the number of syllables must not be too large. */
1575 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1576 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1577 if (word_ends)
1578 {
1579 char_u fword[MAXWLEN];
1580
1581 if (slang->sl_compsylmax < MAXWLEN)
1582 {
1583 /* "fword" is only needed for checking syllables. */
1584 if (ptr == mip->mi_word)
1585 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1586 else
1587 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1588 }
1589 if (!can_compound(slang, fword, mip->mi_compflags))
1590 continue;
1591 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001592 else if (slang->sl_comprules != NULL
1593 && !match_compoundrule(slang, mip->mi_compflags))
1594 /* The compound flags collected so far do not match any
1595 * COMPOUNDRULE, discard the compounded word. */
1596 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001597 }
1598
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001599 /* Check NEEDCOMPOUND: can't use word without compounding. */
1600 else if (flags & WF_NEEDCOMP)
1601 continue;
1602
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 nobreak_result = SP_OK;
1604
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001605 if (!word_ends)
1606 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001607 int save_result = mip->mi_result;
1608 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001609 langp_T *save_lp = mip->mi_lp;
1610 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001611
1612 /* Check that a valid word follows. If there is one and we
1613 * are compounding, it will set "mi_result", thus we are
1614 * always finished here. For NOBREAK we only check that a
1615 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001616 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001617 if (slang->sl_nobreak)
1618 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001619
1620 /* Find following word in case-folded tree. */
1621 mip->mi_compoff = endlen[endidxcnt];
1622#ifdef FEAT_MBYTE
1623 if (has_mbyte && mode == FIND_KEEPWORD)
1624 {
1625 /* Compute byte length in case-folded word from "wlen":
1626 * byte length in keep-case word. Length may change when
1627 * folding case. This can be slow, take a shortcut when
1628 * the case-folded word is equal to the keep-case word. */
1629 p = mip->mi_fword;
1630 if (STRNCMP(ptr, p, wlen) != 0)
1631 {
1632 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1633 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001634 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001635 }
1636 }
1637#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001638 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001639 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001640 if (flags & WF_COMPROOT)
1641 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001642
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001643 /* For NOBREAK we need to try all NOBREAK languages, at least
1644 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001645 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001646 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001647 if (slang->sl_nobreak)
1648 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001649 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001650 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1651 || !mip->mi_lp->lp_slang->sl_nobreak)
1652 continue;
1653 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001654
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655 find_word(mip, FIND_COMPOUND);
1656
1657 /* When NOBREAK any word that matches is OK. Otherwise we
1658 * need to find the longest match, thus try with keep-case
1659 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001660 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1661 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001662 /* Find following word in keep-case tree. */
1663 mip->mi_compoff = wlen;
1664 find_word(mip, FIND_KEEPCOMPOUND);
1665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001666#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1667 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1668 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001669 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1670 {
1671 /* Check for following word with prefix. */
1672 mip->mi_compoff = c;
1673 find_prefix(mip, FIND_COMPOUND);
1674 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001675#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001676 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001677
1678 if (!slang->sl_nobreak)
1679 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001680 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001681 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001682 if (flags & WF_COMPROOT)
1683 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001684 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001685
Bram Moolenaar78622822005-08-23 21:00:13 +00001686 if (slang->sl_nobreak)
1687 {
1688 nobreak_result = mip->mi_result;
1689 mip->mi_result = save_result;
1690 mip->mi_end = save_end;
1691 }
1692 else
1693 {
1694 if (mip->mi_result == SP_OK)
1695 break;
1696 continue;
1697 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001698 }
1699
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001700 if (flags & WF_BANNED)
1701 res = SP_BANNED;
1702 else if (flags & WF_REGION)
1703 {
1704 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001705 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001706 res = SP_OK;
1707 else
1708 res = SP_LOCAL;
1709 }
1710 else if (flags & WF_RARE)
1711 res = SP_RARE;
1712 else
1713 res = SP_OK;
1714
Bram Moolenaar78622822005-08-23 21:00:13 +00001715 /* Always use the longest match and the best result. For NOBREAK
1716 * we separately keep the longest match without a following good
1717 * word as a fall-back. */
1718 if (nobreak_result == SP_BAD)
1719 {
1720 if (mip->mi_result2 > res)
1721 {
1722 mip->mi_result2 = res;
1723 mip->mi_end2 = mip->mi_word + wlen;
1724 }
1725 else if (mip->mi_result2 == res
1726 && mip->mi_end2 < mip->mi_word + wlen)
1727 mip->mi_end2 = mip->mi_word + wlen;
1728 }
1729 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001730 {
1731 mip->mi_result = res;
1732 mip->mi_end = mip->mi_word + wlen;
1733 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001734 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001735 mip->mi_end = mip->mi_word + wlen;
1736
Bram Moolenaar78622822005-08-23 21:00:13 +00001737 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001738 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001739 }
1740
Bram Moolenaar78622822005-08-23 21:00:13 +00001741 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001742 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001743 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001744}
1745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001747 * Return TRUE if there is a match between the word ptr[wlen] and
1748 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1749 * word.
1750 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1751 * end of ptr[wlen] and the second part matches after it.
1752 */
1753 static int
1754match_checkcompoundpattern(ptr, wlen, gap)
1755 char_u *ptr;
1756 int wlen;
1757 garray_T *gap; /* &sl_comppat */
1758{
1759 int i;
1760 char_u *p;
1761 int len;
1762
1763 for (i = 0; i + 1 < gap->ga_len; i += 2)
1764 {
1765 p = ((char_u **)gap->ga_data)[i + 1];
1766 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1767 {
1768 /* Second part matches at start of following compound word, now
1769 * check if first part matches at end of previous word. */
1770 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001771 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001772 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1773 return TRUE;
1774 }
1775 }
1776 return FALSE;
1777}
1778
1779/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001780 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1781 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001782 */
1783 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001784can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001785 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001786 char_u *word;
1787 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001788{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001789 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001790#ifdef FEAT_MBYTE
1791 char_u uflags[MAXWLEN * 2];
1792 int i;
1793#endif
1794 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001795
1796 if (slang->sl_compprog == NULL)
1797 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001798#ifdef FEAT_MBYTE
1799 if (enc_utf8)
1800 {
1801 /* Need to convert the single byte flags to utf8 characters. */
1802 p = uflags;
1803 for (i = 0; flags[i] != NUL; ++i)
1804 p += mb_char2bytes(flags[i], p);
1805 *p = NUL;
1806 p = uflags;
1807 }
1808 else
1809#endif
1810 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001811 regmatch.regprog = slang->sl_compprog;
1812 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001813 if (!vim_regexec(&regmatch, 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;
1933 regprog_T *rp;
1934 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001935 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001936
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001937 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001938 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1939 {
1940 pidx = slang->sl_pidxs[arridx + prefcnt];
1941
1942 /* Check the prefix ID. */
1943 if (prefid != (pidx & 0xff))
1944 continue;
1945
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001946 /* Check if the prefix doesn't combine and the word already has a
1947 * suffix. */
1948 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1949 continue;
1950
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001951 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001952 * stored in the two bytes above the prefix ID byte. */
1953 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001954 if (rp != NULL)
1955 {
1956 regmatch.regprog = rp;
1957 regmatch.rm_ic = FALSE;
1958 if (!vim_regexec(&regmatch, word, 0))
1959 continue;
1960 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001961 else if (cond_req)
1962 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963
Bram Moolenaar53805d12005-08-01 07:08:33 +00001964 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001965 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001966 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001967 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001968}
1969
1970/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971 * Check if the word at "mip->mi_word" has a matching prefix.
1972 * If it does, then check the following word.
1973 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001974 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1975 * prefix in a compound word.
1976 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 * For a match mip->mi_result is updated.
1978 */
1979 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001980find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001981 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001982 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001983{
1984 idx_T arridx = 0;
1985 int len;
1986 int wlen = 0;
1987 int flen;
1988 int c;
1989 char_u *ptr;
1990 idx_T lo, hi, m;
1991 slang_T *slang = mip->mi_lp->lp_slang;
1992 char_u *byts;
1993 idx_T *idxs;
1994
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001995 byts = slang->sl_pbyts;
1996 if (byts == NULL)
1997 return; /* array is empty */
1998
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001999 /* We use the case-folded word here, since prefixes are always
2000 * case-folded. */
2001 ptr = mip->mi_fword;
2002 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002003 if (mode == FIND_COMPOUND)
2004 {
2005 /* Skip over the previously found word(s). */
2006 ptr += mip->mi_compoff;
2007 flen -= mip->mi_compoff;
2008 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002009 idxs = slang->sl_pidxs;
2010
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002011 /*
2012 * Repeat advancing in the tree until:
2013 * - there is a byte that doesn't match,
2014 * - we reach the end of the tree,
2015 * - or we reach the end of the line.
2016 */
2017 for (;;)
2018 {
2019 if (flen == 0 && *mip->mi_fend != NUL)
2020 flen = fold_more(mip);
2021
2022 len = byts[arridx++];
2023
2024 /* If the first possible byte is a zero the prefix could end here.
2025 * Check if the following word matches and supports the prefix. */
2026 if (byts[arridx] == 0)
2027 {
2028 /* There can be several prefixes with different conditions. We
2029 * try them all, since we don't know which one will give the
2030 * longest match. The word is the same each time, pass the list
2031 * of possible prefixes to find_word(). */
2032 mip->mi_prefarridx = arridx;
2033 mip->mi_prefcnt = len;
2034 while (len > 0 && byts[arridx] == 0)
2035 {
2036 ++arridx;
2037 --len;
2038 }
2039 mip->mi_prefcnt -= len;
2040
2041 /* Find the word that comes after the prefix. */
2042 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002043 if (mode == FIND_COMPOUND)
2044 /* Skip over the previously found word(s). */
2045 mip->mi_prefixlen += mip->mi_compoff;
2046
Bram Moolenaar53805d12005-08-01 07:08:33 +00002047#ifdef FEAT_MBYTE
2048 if (has_mbyte)
2049 {
2050 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002051 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2052 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002053 }
2054 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002055 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002056#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002057 find_word(mip, FIND_PREFIX);
2058
2059
2060 if (len == 0)
2061 break; /* no children, word must end here */
2062 }
2063
2064 /* Stop looking at end of the line. */
2065 if (ptr[wlen] == NUL)
2066 break;
2067
2068 /* Perform a binary search in the list of accepted bytes. */
2069 c = ptr[wlen];
2070 lo = arridx;
2071 hi = arridx + len - 1;
2072 while (lo < hi)
2073 {
2074 m = (lo + hi) / 2;
2075 if (byts[m] > c)
2076 hi = m - 1;
2077 else if (byts[m] < c)
2078 lo = m + 1;
2079 else
2080 {
2081 lo = hi = m;
2082 break;
2083 }
2084 }
2085
2086 /* Stop if there is no matching byte. */
2087 if (hi < lo || byts[lo] != c)
2088 break;
2089
2090 /* Continue at the child (if there is one). */
2091 arridx = idxs[lo];
2092 ++wlen;
2093 --flen;
2094 }
2095}
2096
2097/*
2098 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002099 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002100 * Return the length of the folded chars in bytes.
2101 */
2102 static int
2103fold_more(mip)
2104 matchinf_T *mip;
2105{
2106 int flen;
2107 char_u *p;
2108
2109 p = mip->mi_fend;
2110 do
2111 {
2112 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002113 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002114
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002115 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002116 if (*mip->mi_fend != NUL)
2117 mb_ptr_adv(mip->mi_fend);
2118
2119 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2120 mip->mi_fword + mip->mi_fwordlen,
2121 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002122 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002123 mip->mi_fwordlen += flen;
2124 return flen;
2125}
2126
2127/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 * Check case flags for a word. Return TRUE if the word has the requested
2129 * case.
2130 */
2131 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002132spell_valid_case(wordflags, treeflags)
2133 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 int treeflags; /* flags for the word in the spell tree */
2135{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002136 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002138 && ((treeflags & WF_ONECAP) == 0
2139 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140}
2141
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002142/*
2143 * Return TRUE if spell checking is not enabled.
2144 */
2145 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002146no_spell_checking(wp)
2147 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002148{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002149 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2150 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002151 {
2152 EMSG(_("E756: Spell checking is not enabled"));
2153 return TRUE;
2154 }
2155 return FALSE;
2156}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002158/*
2159 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002160 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2161 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002162 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2163 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002164 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 */
2166 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002167spell_move_to(wp, dir, allwords, curline, attrp)
2168 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002170 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002172 hlf_T *attrp; /* return: attributes of bad word or NULL
2173 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002174{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002175 linenr_T lnum;
2176 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002177 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002178 char_u *line;
2179 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002180 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002181 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002183# ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002184 int has_syntax = syntax_present(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002185# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002186 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002187 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002188 char_u *buf = NULL;
2189 int buflen = 0;
2190 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002191 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002192 int found_one = FALSE;
2193 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaar95529562005-08-25 21:21:38 +00002195 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002197
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002198 /*
2199 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002200 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002201 *
2202 * When searching backwards, we continue in the line to find the last
2203 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002204 *
2205 * We concatenate the start of the next line, so that wrapped words work
2206 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2207 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002208 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002209 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002210 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002211
2212 while (!got_int)
2213 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002214 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002216 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002217 if (buflen < len + MAXWLEN + 2)
2218 {
2219 vim_free(buf);
2220 buflen = len + MAXWLEN + 2;
2221 buf = alloc(buflen);
2222 if (buf == NULL)
2223 break;
2224 }
2225
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002226 /* In first line check first word for Capital. */
2227 if (lnum == 1)
2228 capcol = 0;
2229
2230 /* For checking first word with a capital skip white space. */
2231 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002232 capcol = (int)(skipwhite(line) - line);
2233 else if (curline && wp == curwin)
2234 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002235 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002236 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002237 if (check_need_cap(lnum, col))
2238 capcol = col;
2239
2240 /* Need to get the line again, may have looked at the previous
2241 * one. */
2242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2243 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002244
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 /* Copy the line into "buf" and append the start of the next line if
2246 * possible. */
2247 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002248 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002249 spell_cat_line(buf + STRLEN(buf),
2250 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251
2252 p = buf + skip;
2253 endp = buf + len;
2254 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002255 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002256 /* When searching backward don't search after the cursor. Unless
2257 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002258 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002259 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002260 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002261 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002262 break;
2263
2264 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002266 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002268 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002270 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002271 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002273 /* When searching forward only accept a bad word after
2274 * the cursor. */
2275 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002276 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002277 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002278 && (wrapped
2279 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002280 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002281 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002283# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002284 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002285 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002286 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002287 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002288 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002289 if (!can_spell)
2290 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002291 }
2292 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002293#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002294 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002295
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 if (can_spell)
2297 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002298 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002299 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002300 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002301#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002302 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002303#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002304 if (dir == FORWARD)
2305 {
2306 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002307 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002308 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002309 if (attrp != NULL)
2310 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002311 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002312 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002313 else if (curline)
2314 /* Insert mode completion: put cursor after
2315 * the bad word. */
2316 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002317 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002318 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002320 else
2321 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 }
2324
Bram Moolenaar51485f02005-06-04 21:55:20 +00002325 /* advance to character after the word */
2326 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002327 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 }
2329
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 if (dir == BACKWARD && found_pos.lnum != 0)
2331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002332 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002333 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002335 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002336 }
2337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002339 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002341 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002342 if (dir == BACKWARD)
2343 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002344 /* If we are back at the starting line and searched it again there
2345 * is no match, give up. */
2346 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002347 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002348
2349 if (lnum > 1)
2350 --lnum;
2351 else if (!p_ws)
2352 break; /* at first line and 'nowrapscan' */
2353 else
2354 {
2355 /* Wrap around to the end of the buffer. May search the
2356 * starting line again and accept the last match. */
2357 lnum = wp->w_buffer->b_ml.ml_line_count;
2358 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002359 if (!shortmess(SHM_SEARCH))
2360 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002361 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002362 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002363 }
2364 else
2365 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002366 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2367 ++lnum;
2368 else if (!p_ws)
2369 break; /* at first line and 'nowrapscan' */
2370 else
2371 {
2372 /* Wrap around to the start of the buffer. May search the
2373 * starting line again and accept the first match. */
2374 lnum = 1;
2375 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376 if (!shortmess(SHM_SEARCH))
2377 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002378 }
2379
2380 /* If we are back at the starting line and there is no match then
2381 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002382 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002383 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002384
2385 /* Skip the characters at the start of the next line that were
2386 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002387 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002389 else
2390 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002392 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002393 --capcol;
2394
2395 /* But after empty line check first word in next line */
2396 if (*skipwhite(line) == NUL)
2397 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399
2400 line_breakcheck();
2401 }
2402
Bram Moolenaar0c405862005-06-22 22:26:26 +00002403 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002404 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002405}
2406
2407/*
2408 * For spell checking: concatenate the start of the following line "line" into
2409 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002410 * Keep the blanks at the start of the next line, this is used in win_line()
2411 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002412 */
2413 void
2414spell_cat_line(buf, line, maxlen)
2415 char_u *buf;
2416 char_u *line;
2417 int maxlen;
2418{
2419 char_u *p;
2420 int n;
2421
2422 p = skipwhite(line);
2423 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2424 p = skipwhite(p + 1);
2425
2426 if (*p != NUL)
2427 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002428 /* Only worth concatenating if there is something else than spaces to
2429 * concatenate. */
2430 n = (int)(p - line) + 1;
2431 if (n < maxlen - 1)
2432 {
2433 vim_memset(buf, ' ', n);
2434 vim_strncpy(buf + n, p, maxlen - 1 - n);
2435 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002437}
2438
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002439/*
2440 * Structure used for the cookie argument of do_in_runtimepath().
2441 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002442typedef struct spelload_S
2443{
2444 char_u sl_lang[MAXWLEN + 1]; /* language name */
2445 slang_T *sl_slang; /* resulting slang_T struct */
2446 int sl_nobreak; /* NOBREAK language found */
2447} spelload_T;
2448
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002450 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002453 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454spell_load_lang(lang)
2455 char_u *lang;
2456{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002457 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002459 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002460#ifdef FEAT_AUTOCMD
2461 int round;
2462#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463
Bram Moolenaarb765d632005-06-07 21:00:02 +00002464 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002465 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002466 STRCPY(sl.sl_lang, lang);
2467 sl.sl_slang = NULL;
2468 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002470#ifdef FEAT_AUTOCMD
2471 /* We may retry when no spell file is found for the language, an
2472 * autocommand may load it then. */
2473 for (round = 1; round <= 2; ++round)
2474#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002475 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002476 /*
2477 * Find the first spell file for "lang" in 'runtimepath' and load it.
2478 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002479 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002480#ifdef VMS
2481 "spell/%s_%s.spl",
2482#else
2483 "spell/%s.%s.spl",
2484#endif
2485 lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002486 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002487
2488 if (r == FAIL && *sl.sl_lang != NUL)
2489 {
2490 /* Try loading the ASCII version. */
2491 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002492#ifdef VMS
2493 "spell/%s_ascii.spl",
2494#else
2495 "spell/%s.ascii.spl",
2496#endif
2497 lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002498 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2499
2500#ifdef FEAT_AUTOCMD
2501 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2502 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2503 curbuf->b_fname, FALSE, curbuf))
2504 continue;
2505 break;
2506#endif
2507 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002508#ifdef FEAT_AUTOCMD
2509 break;
2510#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 }
2512
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002513 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002514 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01002515 smsg((char_u *)
2516#ifdef VMS
2517 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2518#else
2519 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2520#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002522 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002523 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002524 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002525 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002526 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002527 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002528 }
2529}
2530
2531/*
2532 * Return the encoding used for spell checking: Use 'encoding', except that we
2533 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2534 */
2535 static char_u *
2536spell_enc()
2537{
2538
2539#ifdef FEAT_MBYTE
2540 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2541 return p_enc;
2542#endif
2543 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544}
2545
2546/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002547 * Get the name of the .spl file for the internal wordlist into
2548 * "fname[MAXPATHL]".
2549 */
2550 static void
2551int_wordlist_spl(fname)
2552 char_u *fname;
2553{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002554 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002555 int_wordlist, spell_enc());
2556}
2557
2558/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560 * Caller must fill "sl_next".
2561 */
2562 static slang_T *
2563slang_alloc(lang)
2564 char_u *lang;
2565{
2566 slang_T *lp;
2567
Bram Moolenaar51485f02005-06-04 21:55:20 +00002568 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 if (lp != NULL)
2570 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002571 if (lang != NULL)
2572 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002575 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002576 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002577 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 return lp;
2581}
2582
2583/*
2584 * Free the contents of an slang_T and the structure itself.
2585 */
2586 static void
2587slang_free(lp)
2588 slang_T *lp;
2589{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 vim_free(lp->sl_fname);
2592 slang_clear(lp);
2593 vim_free(lp);
2594}
2595
2596/*
2597 * Clear an slang_T so that the file can be reloaded.
2598 */
2599 static void
2600slang_clear(lp)
2601 slang_T *lp;
2602{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002603 garray_T *gap;
2604 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002605 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002606 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002607 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608
Bram Moolenaar51485f02005-06-04 21:55:20 +00002609 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002610 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002613 vim_free(lp->sl_pbyts);
2614 lp->sl_pbyts = NULL;
2615
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002617 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002620 vim_free(lp->sl_pidxs);
2621 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002624 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002625 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2626 while (gap->ga_len > 0)
2627 {
2628 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2629 vim_free(ftp->ft_from);
2630 vim_free(ftp->ft_to);
2631 }
2632 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002634
2635 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002636 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002637 {
2638 /* "ga_len" is set to 1 without adding an item for latin1 */
2639 if (gap->ga_data != NULL)
2640 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2641 for (i = 0; i < gap->ga_len; ++i)
2642 vim_free(((int **)gap->ga_data)[i]);
2643 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002644 else
2645 /* SAL items: free salitem_T items */
2646 while (gap->ga_len > 0)
2647 {
2648 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2649 vim_free(smp->sm_lead);
2650 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2651 vim_free(smp->sm_to);
2652#ifdef FEAT_MBYTE
2653 vim_free(smp->sm_lead_w);
2654 vim_free(smp->sm_oneof_w);
2655 vim_free(smp->sm_to_w);
2656#endif
2657 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002658 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002660 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002661 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002662 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002663 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002664 lp->sl_prefprog = NULL;
2665
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002666 vim_free(lp->sl_info);
2667 lp->sl_info = NULL;
2668
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002669 vim_free(lp->sl_midword);
2670 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002671
Bram Moolenaar473de612013-06-08 18:19:48 +02002672 vim_regfree(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002673 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002674 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002675 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002677 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002679 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002680
2681 vim_free(lp->sl_syllable);
2682 lp->sl_syllable = NULL;
2683 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002684
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002685 ga_clear_strings(&lp->sl_comppat);
2686
Bram Moolenaar4770d092006-01-12 23:22:24 +00002687 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2688 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002689
Bram Moolenaar4770d092006-01-12 23:22:24 +00002690#ifdef FEAT_MBYTE
2691 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002692#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693
Bram Moolenaar4770d092006-01-12 23:22:24 +00002694 /* Clear info from .sug file. */
2695 slang_clear_sug(lp);
2696
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002698 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002699 lp->sl_compsylmax = MAXWLEN;
2700 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002701}
2702
2703/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002704 * Clear the info from the .sug file in "lp".
2705 */
2706 static void
2707slang_clear_sug(lp)
2708 slang_T *lp;
2709{
2710 vim_free(lp->sl_sbyts);
2711 lp->sl_sbyts = NULL;
2712 vim_free(lp->sl_sidxs);
2713 lp->sl_sidxs = NULL;
2714 close_spellbuf(lp->sl_sugbuf);
2715 lp->sl_sugbuf = NULL;
2716 lp->sl_sugloaded = FALSE;
2717 lp->sl_sugtime = 0;
2718}
2719
2720/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002721 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002722 * Invoked through do_in_runtimepath().
2723 */
2724 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002725spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002727 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002728{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002729 spelload_T *slp = (spelload_T *)cookie;
2730 slang_T *slang;
2731
2732 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2733 if (slang != NULL)
2734 {
2735 /* When a previously loaded file has NOBREAK also use it for the
2736 * ".add" files. */
2737 if (slp->sl_nobreak && slang->sl_add)
2738 slang->sl_nobreak = TRUE;
2739 else if (slang->sl_nobreak)
2740 slp->sl_nobreak = TRUE;
2741
2742 slp->sl_slang = slang;
2743 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002744}
2745
2746/*
2747 * Load one spell file and store the info into a slang_T.
2748 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002749 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002750 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2751 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2752 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2753 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002754 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002755 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2756 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002758 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002759 static slang_T *
2760spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002761 char_u *fname;
2762 char_u *lang;
2763 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002765{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002767 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002768 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002770 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002771 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002772 char_u *save_sourcing_name = sourcing_name;
2773 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002774 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002775 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002776 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002777
Bram Moolenaarb765d632005-06-07 21:00:02 +00002778 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002779 if (fd == NULL)
2780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 if (!silent)
2782 EMSG2(_(e_notopen), fname);
2783 else if (p_verbose > 2)
2784 {
2785 verbose_enter();
2786 smsg((char_u *)e_notopen, fname);
2787 verbose_leave();
2788 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002789 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002790 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002791 if (p_verbose > 2)
2792 {
2793 verbose_enter();
2794 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2795 verbose_leave();
2796 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797
Bram Moolenaarb765d632005-06-07 21:00:02 +00002798 if (old_lp == NULL)
2799 {
2800 lp = slang_alloc(lang);
2801 if (lp == NULL)
2802 goto endFAIL;
2803
2804 /* Remember the file name, used to reload the file when it's updated. */
2805 lp->sl_fname = vim_strsave(fname);
2806 if (lp->sl_fname == NULL)
2807 goto endFAIL;
2808
Bram Moolenaar56f78042010-12-08 17:09:32 +01002809 /* Check for .add.spl (_add.spl for VMS). */
2810 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002811 }
2812 else
2813 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002814
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002815 /* Set sourcing_name, so that error messages mention the file name. */
2816 sourcing_name = fname;
2817 sourcing_lnum = 0;
2818
Bram Moolenaar4770d092006-01-12 23:22:24 +00002819 /*
2820 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002821 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002822 for (i = 0; i < VIMSPELLMAGICL; ++i)
2823 buf[i] = getc(fd); /* <fileID> */
2824 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2825 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002826 EMSG(_("E757: This does not look like a spell file"));
2827 goto endFAIL;
2828 }
2829 c = getc(fd); /* <versionnr> */
2830 if (c < VIMSPELLVERSION)
2831 {
2832 EMSG(_("E771: Old spell file, needs to be updated"));
2833 goto endFAIL;
2834 }
2835 else if (c > VIMSPELLVERSION)
2836 {
2837 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002838 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 }
2840
Bram Moolenaar5195e452005-08-19 20:32:47 +00002841
2842 /*
2843 * <SECTIONS>: <section> ... <sectionend>
2844 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2845 */
2846 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002847 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002848 n = getc(fd); /* <sectionID> or <sectionend> */
2849 if (n == SN_END)
2850 break;
2851 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002852 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002853 if (len < 0)
2854 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002855
Bram Moolenaar5195e452005-08-19 20:32:47 +00002856 res = 0;
2857 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002858 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002859 case SN_INFO:
2860 lp->sl_info = read_string(fd, len); /* <infotext> */
2861 if (lp->sl_info == NULL)
2862 goto endFAIL;
2863 break;
2864
Bram Moolenaar5195e452005-08-19 20:32:47 +00002865 case SN_REGION:
2866 res = read_region_section(fd, lp, len);
2867 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002868
Bram Moolenaar5195e452005-08-19 20:32:47 +00002869 case SN_CHARFLAGS:
2870 res = read_charflags_section(fd);
2871 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002872
Bram Moolenaar5195e452005-08-19 20:32:47 +00002873 case SN_MIDWORD:
2874 lp->sl_midword = read_string(fd, len); /* <midword> */
2875 if (lp->sl_midword == NULL)
2876 goto endFAIL;
2877 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002878
Bram Moolenaar5195e452005-08-19 20:32:47 +00002879 case SN_PREFCOND:
2880 res = read_prefcond_section(fd, lp);
2881 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002882
Bram Moolenaar5195e452005-08-19 20:32:47 +00002883 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002884 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2885 break;
2886
2887 case SN_REPSAL:
2888 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002889 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002890
Bram Moolenaar5195e452005-08-19 20:32:47 +00002891 case SN_SAL:
2892 res = read_sal_section(fd, lp);
2893 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002894
Bram Moolenaar5195e452005-08-19 20:32:47 +00002895 case SN_SOFO:
2896 res = read_sofo_section(fd, lp);
2897 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002898
Bram Moolenaar5195e452005-08-19 20:32:47 +00002899 case SN_MAP:
2900 p = read_string(fd, len); /* <mapstr> */
2901 if (p == NULL)
2902 goto endFAIL;
2903 set_map_str(lp, p);
2904 vim_free(p);
2905 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002906
Bram Moolenaar4770d092006-01-12 23:22:24 +00002907 case SN_WORDS:
2908 res = read_words_section(fd, lp, len);
2909 break;
2910
2911 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002912 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002913 break;
2914
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002915 case SN_NOSPLITSUGS:
2916 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2917 break;
2918
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 case SN_COMPOUND:
2920 res = read_compound(fd, lp, len);
2921 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002922
Bram Moolenaar78622822005-08-23 21:00:13 +00002923 case SN_NOBREAK:
2924 lp->sl_nobreak = TRUE;
2925 break;
2926
Bram Moolenaar5195e452005-08-19 20:32:47 +00002927 case SN_SYLLABLE:
2928 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2929 if (lp->sl_syllable == NULL)
2930 goto endFAIL;
2931 if (init_syl_tab(lp) == FAIL)
2932 goto endFAIL;
2933 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002934
Bram Moolenaar5195e452005-08-19 20:32:47 +00002935 default:
2936 /* Unsupported section. When it's required give an error
2937 * message. When it's not required skip the contents. */
2938 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002939 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002941 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002942 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 while (--len >= 0)
2944 if (getc(fd) < 0)
2945 goto truncerr;
2946 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002947 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002948someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002949 if (res == SP_FORMERROR)
2950 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002951 EMSG(_(e_format));
2952 goto endFAIL;
2953 }
2954 if (res == SP_TRUNCERROR)
2955 {
2956truncerr:
2957 EMSG(_(e_spell_trunc));
2958 goto endFAIL;
2959 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002960 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002961 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002962 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002963
Bram Moolenaar4770d092006-01-12 23:22:24 +00002964 /* <LWORDTREE> */
2965 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2966 if (res != 0)
2967 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002968
Bram Moolenaar4770d092006-01-12 23:22:24 +00002969 /* <KWORDTREE> */
2970 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2971 if (res != 0)
2972 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973
Bram Moolenaar4770d092006-01-12 23:22:24 +00002974 /* <PREFIXTREE> */
2975 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2976 lp->sl_prefixcnt);
2977 if (res != 0)
2978 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002979
Bram Moolenaarb765d632005-06-07 21:00:02 +00002980 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002981 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002982 {
2983 lp->sl_next = first_lang;
2984 first_lang = lp;
2985 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002986
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002987 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002988
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002989endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002990 if (lang != NULL)
2991 /* truncating the name signals the error to spell_load_lang() */
2992 *lang = NUL;
2993 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002994 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002995 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002996
2997endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002998 if (fd != NULL)
2999 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003000 sourcing_name = save_sourcing_name;
3001 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002
3003 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004}
3005
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003006/*
3007 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003008 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003009 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3011 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003012 */
3013 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003014read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003015 FILE *fd;
3016 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003017 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003018{
3019 int cnt = 0;
3020 int i;
3021 char_u *str;
3022
3023 /* read the length bytes, MSB first */
3024 for (i = 0; i < cnt_bytes; ++i)
3025 cnt = (cnt << 8) + getc(fd);
3026 if (cnt < 0)
3027 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003028 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003029 return NULL;
3030 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003031 *cntp = cnt;
3032 if (cnt == 0)
3033 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003034
Bram Moolenaar5195e452005-08-19 20:32:47 +00003035 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003036 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003037 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003038 return str;
3039}
3040
Bram Moolenaar7887d882005-07-01 22:33:52 +00003041/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003042 * Read SN_REGION: <regionname> ...
3043 * Return SP_*ERROR flags.
3044 */
3045 static int
3046read_region_section(fd, lp, len)
3047 FILE *fd;
3048 slang_T *lp;
3049 int len;
3050{
3051 int i;
3052
3053 if (len > 16)
3054 return SP_FORMERROR;
3055 for (i = 0; i < len; ++i)
3056 lp->sl_regions[i] = getc(fd); /* <regionname> */
3057 lp->sl_regions[len] = NUL;
3058 return 0;
3059}
3060
3061/*
3062 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3063 * <folcharslen> <folchars>
3064 * Return SP_*ERROR flags.
3065 */
3066 static int
3067read_charflags_section(fd)
3068 FILE *fd;
3069{
3070 char_u *flags;
3071 char_u *fol;
3072 int flagslen, follen;
3073
3074 /* <charflagslen> <charflags> */
3075 flags = read_cnt_string(fd, 1, &flagslen);
3076 if (flagslen < 0)
3077 return flagslen;
3078
3079 /* <folcharslen> <folchars> */
3080 fol = read_cnt_string(fd, 2, &follen);
3081 if (follen < 0)
3082 {
3083 vim_free(flags);
3084 return follen;
3085 }
3086
3087 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3088 if (flags != NULL && fol != NULL)
3089 set_spell_charflags(flags, flagslen, fol);
3090
3091 vim_free(flags);
3092 vim_free(fol);
3093
3094 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3095 if ((flags == NULL) != (fol == NULL))
3096 return SP_FORMERROR;
3097 return 0;
3098}
3099
3100/*
3101 * Read SN_PREFCOND section.
3102 * Return SP_*ERROR flags.
3103 */
3104 static int
3105read_prefcond_section(fd, lp)
3106 FILE *fd;
3107 slang_T *lp;
3108{
3109 int cnt;
3110 int i;
3111 int n;
3112 char_u *p;
3113 char_u buf[MAXWLEN + 1];
3114
3115 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003116 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003117 if (cnt <= 0)
3118 return SP_FORMERROR;
3119
3120 lp->sl_prefprog = (regprog_T **)alloc_clear(
3121 (unsigned)sizeof(regprog_T *) * cnt);
3122 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003123 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003124 lp->sl_prefixcnt = cnt;
3125
3126 for (i = 0; i < cnt; ++i)
3127 {
3128 /* <prefcond> : <condlen> <condstr> */
3129 n = getc(fd); /* <condlen> */
3130 if (n < 0 || n >= MAXWLEN)
3131 return SP_FORMERROR;
3132
3133 /* When <condlen> is zero we have an empty condition. Otherwise
3134 * compile the regexp program used to check for the condition. */
3135 if (n > 0)
3136 {
3137 buf[0] = '^'; /* always match at one position only */
3138 p = buf + 1;
3139 while (n-- > 0)
3140 *p++ = getc(fd); /* <condstr> */
3141 *p = NUL;
3142 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3143 }
3144 }
3145 return 0;
3146}
3147
3148/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003149 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150 * Return SP_*ERROR flags.
3151 */
3152 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003153read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003155 garray_T *gap;
3156 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003157{
3158 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003160 int i;
3161
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003162 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003163 if (cnt < 0)
3164 return SP_TRUNCERROR;
3165
Bram Moolenaar5195e452005-08-19 20:32:47 +00003166 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003167 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003168
3169 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3170 for (; gap->ga_len < cnt; ++gap->ga_len)
3171 {
3172 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3173 ftp->ft_from = read_cnt_string(fd, 1, &i);
3174 if (i < 0)
3175 return i;
3176 if (i == 0)
3177 return SP_FORMERROR;
3178 ftp->ft_to = read_cnt_string(fd, 1, &i);
3179 if (i <= 0)
3180 {
3181 vim_free(ftp->ft_from);
3182 if (i < 0)
3183 return i;
3184 return SP_FORMERROR;
3185 }
3186 }
3187
3188 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003189 for (i = 0; i < 256; ++i)
3190 first[i] = -1;
3191 for (i = 0; i < gap->ga_len; ++i)
3192 {
3193 ftp = &((fromto_T *)gap->ga_data)[i];
3194 if (first[*ftp->ft_from] == -1)
3195 first[*ftp->ft_from] = i;
3196 }
3197 return 0;
3198}
3199
3200/*
3201 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3202 * Return SP_*ERROR flags.
3203 */
3204 static int
3205read_sal_section(fd, slang)
3206 FILE *fd;
3207 slang_T *slang;
3208{
3209 int i;
3210 int cnt;
3211 garray_T *gap;
3212 salitem_T *smp;
3213 int ccnt;
3214 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003215 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003216
3217 slang->sl_sofo = FALSE;
3218
3219 i = getc(fd); /* <salflags> */
3220 if (i & SAL_F0LLOWUP)
3221 slang->sl_followup = TRUE;
3222 if (i & SAL_COLLAPSE)
3223 slang->sl_collapse = TRUE;
3224 if (i & SAL_REM_ACCENTS)
3225 slang->sl_rem_accents = TRUE;
3226
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003227 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003228 if (cnt < 0)
3229 return SP_TRUNCERROR;
3230
3231 gap = &slang->sl_sal;
3232 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003233 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003234 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003235
3236 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3237 for (; gap->ga_len < cnt; ++gap->ga_len)
3238 {
3239 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3240 ccnt = getc(fd); /* <salfromlen> */
3241 if (ccnt < 0)
3242 return SP_TRUNCERROR;
3243 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003244 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003245 smp->sm_lead = p;
3246
3247 /* Read up to the first special char into sm_lead. */
3248 for (i = 0; i < ccnt; ++i)
3249 {
3250 c = getc(fd); /* <salfrom> */
3251 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3252 break;
3253 *p++ = c;
3254 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003255 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003256 *p++ = NUL;
3257
3258 /* Put (abc) chars in sm_oneof, if any. */
3259 if (c == '(')
3260 {
3261 smp->sm_oneof = p;
3262 for (++i; i < ccnt; ++i)
3263 {
3264 c = getc(fd); /* <salfrom> */
3265 if (c == ')')
3266 break;
3267 *p++ = c;
3268 }
3269 *p++ = NUL;
3270 if (++i < ccnt)
3271 c = getc(fd);
3272 }
3273 else
3274 smp->sm_oneof = NULL;
3275
3276 /* Any following chars go in sm_rules. */
3277 smp->sm_rules = p;
3278 if (i < ccnt)
3279 /* store the char we got while checking for end of sm_lead */
3280 *p++ = c;
3281 for (++i; i < ccnt; ++i)
3282 *p++ = getc(fd); /* <salfrom> */
3283 *p++ = NUL;
3284
3285 /* <saltolen> <salto> */
3286 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3287 if (ccnt < 0)
3288 {
3289 vim_free(smp->sm_lead);
3290 return ccnt;
3291 }
3292
3293#ifdef FEAT_MBYTE
3294 if (has_mbyte)
3295 {
3296 /* convert the multi-byte strings to wide char strings */
3297 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3298 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3299 if (smp->sm_oneof == NULL)
3300 smp->sm_oneof_w = NULL;
3301 else
3302 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3303 if (smp->sm_to == NULL)
3304 smp->sm_to_w = NULL;
3305 else
3306 smp->sm_to_w = mb_str2wide(smp->sm_to);
3307 if (smp->sm_lead_w == NULL
3308 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3309 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3310 {
3311 vim_free(smp->sm_lead);
3312 vim_free(smp->sm_to);
3313 vim_free(smp->sm_lead_w);
3314 vim_free(smp->sm_oneof_w);
3315 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003316 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003317 }
3318 }
3319#endif
3320 }
3321
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003322 if (gap->ga_len > 0)
3323 {
3324 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3325 * that we need to check the index every time. */
3326 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3327 if ((p = alloc(1)) == NULL)
3328 return SP_OTHERERROR;
3329 p[0] = NUL;
3330 smp->sm_lead = p;
3331 smp->sm_leadlen = 0;
3332 smp->sm_oneof = NULL;
3333 smp->sm_rules = p;
3334 smp->sm_to = NULL;
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 {
3338 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3339 smp->sm_leadlen = 0;
3340 smp->sm_oneof_w = NULL;
3341 smp->sm_to_w = NULL;
3342 }
3343#endif
3344 ++gap->ga_len;
3345 }
3346
Bram Moolenaar5195e452005-08-19 20:32:47 +00003347 /* Fill the first-index table. */
3348 set_sal_first(slang);
3349
3350 return 0;
3351}
3352
3353/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003354 * Read SN_WORDS: <word> ...
3355 * Return SP_*ERROR flags.
3356 */
3357 static int
3358read_words_section(fd, lp, len)
3359 FILE *fd;
3360 slang_T *lp;
3361 int len;
3362{
3363 int done = 0;
3364 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003365 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003366 char_u word[MAXWLEN];
3367
3368 while (done < len)
3369 {
3370 /* Read one word at a time. */
3371 for (i = 0; ; ++i)
3372 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003373 c = getc(fd);
3374 if (c == EOF)
3375 return SP_TRUNCERROR;
3376 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003377 if (word[i] == NUL)
3378 break;
3379 if (i == MAXWLEN - 1)
3380 return SP_FORMERROR;
3381 }
3382
3383 /* Init the count to 10. */
3384 count_common_word(lp, word, -1, 10);
3385 done += i + 1;
3386 }
3387 return 0;
3388}
3389
3390/*
3391 * Add a word to the hashtable of common words.
3392 * If it's already there then the counter is increased.
3393 */
3394 static void
3395count_common_word(lp, word, len, count)
3396 slang_T *lp;
3397 char_u *word;
3398 int len; /* word length, -1 for upto NUL */
3399 int count; /* 1 to count once, 10 to init */
3400{
3401 hash_T hash;
3402 hashitem_T *hi;
3403 wordcount_T *wc;
3404 char_u buf[MAXWLEN];
3405 char_u *p;
3406
3407 if (len == -1)
3408 p = word;
3409 else
3410 {
3411 vim_strncpy(buf, word, len);
3412 p = buf;
3413 }
3414
3415 hash = hash_hash(p);
3416 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3417 if (HASHITEM_EMPTY(hi))
3418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003420 if (wc == NULL)
3421 return;
3422 STRCPY(wc->wc_word, p);
3423 wc->wc_count = count;
3424 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3425 }
3426 else
3427 {
3428 wc = HI2WC(hi);
3429 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3430 wc->wc_count = MAXWORDCOUNT;
3431 }
3432}
3433
3434/*
3435 * Adjust the score of common words.
3436 */
3437 static int
3438score_wordcount_adj(slang, score, word, split)
3439 slang_T *slang;
3440 int score;
3441 char_u *word;
3442 int split; /* word was split, less bonus */
3443{
3444 hashitem_T *hi;
3445 wordcount_T *wc;
3446 int bonus;
3447 int newscore;
3448
3449 hi = hash_find(&slang->sl_wordcount, word);
3450 if (!HASHITEM_EMPTY(hi))
3451 {
3452 wc = HI2WC(hi);
3453 if (wc->wc_count < SCORE_THRES2)
3454 bonus = SCORE_COMMON1;
3455 else if (wc->wc_count < SCORE_THRES3)
3456 bonus = SCORE_COMMON2;
3457 else
3458 bonus = SCORE_COMMON3;
3459 if (split)
3460 newscore = score - bonus / 2;
3461 else
3462 newscore = score - bonus;
3463 if (newscore < 0)
3464 return 0;
3465 return newscore;
3466 }
3467 return score;
3468}
3469
3470/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003471 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3472 * Return SP_*ERROR flags.
3473 */
3474 static int
3475read_sofo_section(fd, slang)
3476 FILE *fd;
3477 slang_T *slang;
3478{
3479 int cnt;
3480 char_u *from, *to;
3481 int res;
3482
3483 slang->sl_sofo = TRUE;
3484
3485 /* <sofofromlen> <sofofrom> */
3486 from = read_cnt_string(fd, 2, &cnt);
3487 if (cnt < 0)
3488 return cnt;
3489
3490 /* <sofotolen> <sofoto> */
3491 to = read_cnt_string(fd, 2, &cnt);
3492 if (cnt < 0)
3493 {
3494 vim_free(from);
3495 return cnt;
3496 }
3497
3498 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3499 if (from != NULL && to != NULL)
3500 res = set_sofo(slang, from, to);
3501 else if (from != NULL || to != NULL)
3502 res = SP_FORMERROR; /* only one of two strings is an error */
3503 else
3504 res = 0;
3505
3506 vim_free(from);
3507 vim_free(to);
3508 return res;
3509}
3510
3511/*
3512 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003513 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003514 * Returns SP_*ERROR flags.
3515 */
3516 static int
3517read_compound(fd, slang, len)
3518 FILE *fd;
3519 slang_T *slang;
3520 int len;
3521{
3522 int todo = len;
3523 int c;
3524 int atstart;
3525 char_u *pat;
3526 char_u *pp;
3527 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003528 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003529 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003530 int cnt;
3531 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003532
3533 if (todo < 2)
3534 return SP_FORMERROR; /* need at least two bytes */
3535
3536 --todo;
3537 c = getc(fd); /* <compmax> */
3538 if (c < 2)
3539 c = MAXWLEN;
3540 slang->sl_compmax = c;
3541
3542 --todo;
3543 c = getc(fd); /* <compminlen> */
3544 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003545 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003546 slang->sl_compminlen = c;
3547
3548 --todo;
3549 c = getc(fd); /* <compsylmax> */
3550 if (c < 1)
3551 c = MAXWLEN;
3552 slang->sl_compsylmax = c;
3553
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003554 c = getc(fd); /* <compoptions> */
3555 if (c != 0)
3556 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3557 else
3558 {
3559 --todo;
3560 c = getc(fd); /* only use the lower byte for now */
3561 --todo;
3562 slang->sl_compoptions = c;
3563
3564 gap = &slang->sl_comppat;
3565 c = get2c(fd); /* <comppatcount> */
3566 todo -= 2;
3567 ga_init2(gap, sizeof(char_u *), c);
3568 if (ga_grow(gap, c) == OK)
3569 while (--c >= 0)
3570 {
3571 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3572 read_cnt_string(fd, 1, &cnt);
3573 /* <comppatlen> <comppattext> */
3574 if (cnt < 0)
3575 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003576 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003577 }
3578 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003579 if (todo < 0)
3580 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003581
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003582 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003583 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003584 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3585 * Conversion to utf-8 may double the size. */
3586 c = todo * 2 + 7;
3587#ifdef FEAT_MBYTE
3588 if (enc_utf8)
3589 c += todo * 2;
3590#endif
3591 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003592 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003593 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003594
Bram Moolenaard12a1322005-08-21 22:08:24 +00003595 /* We also need a list of all flags that can appear at the start and one
3596 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003597 cp = alloc(todo + 1);
3598 if (cp == NULL)
3599 {
3600 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003601 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003602 }
3603 slang->sl_compstartflags = cp;
3604 *cp = NUL;
3605
Bram Moolenaard12a1322005-08-21 22:08:24 +00003606 ap = alloc(todo + 1);
3607 if (ap == NULL)
3608 {
3609 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003610 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003611 }
3612 slang->sl_compallflags = ap;
3613 *ap = NUL;
3614
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003615 /* And a list of all patterns in their original form, for checking whether
3616 * compounding may work in match_compoundrule(). This is freed when we
3617 * encounter a wildcard, the check doesn't work then. */
3618 crp = alloc(todo + 1);
3619 slang->sl_comprules = crp;
3620
Bram Moolenaar5195e452005-08-19 20:32:47 +00003621 pp = pat;
3622 *pp++ = '^';
3623 *pp++ = '\\';
3624 *pp++ = '(';
3625
3626 atstart = 1;
3627 while (todo-- > 0)
3628 {
3629 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003630 if (c == EOF)
3631 {
3632 vim_free(pat);
3633 return SP_TRUNCERROR;
3634 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003635
3636 /* Add all flags to "sl_compallflags". */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003637 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003638 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003639 {
3640 *ap++ = c;
3641 *ap = NUL;
3642 }
3643
Bram Moolenaar5195e452005-08-19 20:32:47 +00003644 if (atstart != 0)
3645 {
3646 /* At start of item: copy flags to "sl_compstartflags". For a
3647 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3648 if (c == '[')
3649 atstart = 2;
3650 else if (c == ']')
3651 atstart = 0;
3652 else
3653 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003654 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003655 {
3656 *cp++ = c;
3657 *cp = NUL;
3658 }
3659 if (atstart == 1)
3660 atstart = 0;
3661 }
3662 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003663
3664 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3665 if (crp != NULL)
3666 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003667 if (c == '?' || c == '+' || c == '*')
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003668 {
3669 vim_free(slang->sl_comprules);
3670 slang->sl_comprules = NULL;
3671 crp = NULL;
3672 }
3673 else
3674 *crp++ = c;
3675 }
3676
Bram Moolenaar5195e452005-08-19 20:32:47 +00003677 if (c == '/') /* slash separates two items */
3678 {
3679 *pp++ = '\\';
3680 *pp++ = '|';
3681 atstart = 1;
3682 }
3683 else /* normal char, "[abc]" and '*' are copied as-is */
3684 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003685 if (c == '?' || c == '+' || c == '~')
3686 *pp++ = '\\'; /* "a?" becomes "a\?", "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003687#ifdef FEAT_MBYTE
3688 if (enc_utf8)
3689 pp += mb_char2bytes(c, pp);
3690 else
3691#endif
3692 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003693 }
3694 }
3695
3696 *pp++ = '\\';
3697 *pp++ = ')';
3698 *pp++ = '$';
3699 *pp = NUL;
3700
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003701 if (crp != NULL)
3702 *crp = NUL;
3703
Bram Moolenaar5195e452005-08-19 20:32:47 +00003704 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3705 vim_free(pat);
3706 if (slang->sl_compprog == NULL)
3707 return SP_FORMERROR;
3708
3709 return 0;
3710}
3711
Bram Moolenaar6de68532005-08-24 22:08:48 +00003712/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003713 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003714 * Like strchr() but independent of locale.
3715 */
3716 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003717byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003718 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003719 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003720{
3721 char_u *p;
3722
3723 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003724 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003725 return TRUE;
3726 return FALSE;
3727}
3728
Bram Moolenaar5195e452005-08-19 20:32:47 +00003729#define SY_MAXLEN 30
3730typedef struct syl_item_S
3731{
3732 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3733 int sy_len;
3734} syl_item_T;
3735
3736/*
3737 * Truncate "slang->sl_syllable" at the first slash and put the following items
3738 * in "slang->sl_syl_items".
3739 */
3740 static int
3741init_syl_tab(slang)
3742 slang_T *slang;
3743{
3744 char_u *p;
3745 char_u *s;
3746 int l;
3747 syl_item_T *syl;
3748
3749 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3750 p = vim_strchr(slang->sl_syllable, '/');
3751 while (p != NULL)
3752 {
3753 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003754 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003755 break;
3756 s = p;
3757 p = vim_strchr(p, '/');
3758 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003759 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003760 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003761 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003762 if (l >= SY_MAXLEN)
3763 return SP_FORMERROR;
3764 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003765 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003766 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3767 + slang->sl_syl_items.ga_len++;
3768 vim_strncpy(syl->sy_chars, s, l);
3769 syl->sy_len = l;
3770 }
3771 return OK;
3772}
3773
3774/*
3775 * Count the number of syllables in "word".
3776 * When "word" contains spaces the syllables after the last space are counted.
3777 * Returns zero if syllables are not defines.
3778 */
3779 static int
3780count_syllables(slang, word)
3781 slang_T *slang;
3782 char_u *word;
3783{
3784 int cnt = 0;
3785 int skip = FALSE;
3786 char_u *p;
3787 int len;
3788 int i;
3789 syl_item_T *syl;
3790 int c;
3791
3792 if (slang->sl_syllable == NULL)
3793 return 0;
3794
3795 for (p = word; *p != NUL; p += len)
3796 {
3797 /* When running into a space reset counter. */
3798 if (*p == ' ')
3799 {
3800 len = 1;
3801 cnt = 0;
3802 continue;
3803 }
3804
3805 /* Find longest match of syllable items. */
3806 len = 0;
3807 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3808 {
3809 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3810 if (syl->sy_len > len
3811 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3812 len = syl->sy_len;
3813 }
3814 if (len != 0) /* found a match, count syllable */
3815 {
3816 ++cnt;
3817 skip = FALSE;
3818 }
3819 else
3820 {
3821 /* No recognized syllable item, at least a syllable char then? */
3822#ifdef FEAT_MBYTE
3823 c = mb_ptr2char(p);
3824 len = (*mb_ptr2len)(p);
3825#else
3826 c = *p;
3827 len = 1;
3828#endif
3829 if (vim_strchr(slang->sl_syllable, c) == NULL)
3830 skip = FALSE; /* No, search for next syllable */
3831 else if (!skip)
3832 {
3833 ++cnt; /* Yes, count it */
3834 skip = TRUE; /* don't count following syllable chars */
3835 }
3836 }
3837 }
3838 return cnt;
3839}
3840
3841/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003842 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003843 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003844 */
3845 static int
3846set_sofo(lp, from, to)
3847 slang_T *lp;
3848 char_u *from;
3849 char_u *to;
3850{
3851 int i;
3852
3853#ifdef FEAT_MBYTE
3854 garray_T *gap;
3855 char_u *s;
3856 char_u *p;
3857 int c;
3858 int *inp;
3859
3860 if (has_mbyte)
3861 {
3862 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3863 * characters. The index is the low byte of the character.
3864 * The list contains from-to pairs with a terminating NUL.
3865 * sl_sal_first[] is used for latin1 "from" characters. */
3866 gap = &lp->sl_sal;
3867 ga_init2(gap, sizeof(int *), 1);
3868 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003869 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003870 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3871 gap->ga_len = 256;
3872
3873 /* First count the number of items for each list. Temporarily use
3874 * sl_sal_first[] for this. */
3875 for (p = from, s = to; *p != NUL && *s != NUL; )
3876 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003877 c = mb_cptr2char_adv(&p);
3878 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003879 if (c >= 256)
3880 ++lp->sl_sal_first[c & 0xff];
3881 }
3882 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003883 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003884
3885 /* Allocate the lists. */
3886 for (i = 0; i < 256; ++i)
3887 if (lp->sl_sal_first[i] > 0)
3888 {
3889 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3890 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003891 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003892 ((int **)gap->ga_data)[i] = (int *)p;
3893 *(int *)p = 0;
3894 }
3895
3896 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3897 * list. */
3898 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3899 for (p = from, s = to; *p != NUL && *s != NUL; )
3900 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003901 c = mb_cptr2char_adv(&p);
3902 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003903 if (c >= 256)
3904 {
3905 /* Append the from-to chars at the end of the list with
3906 * the low byte. */
3907 inp = ((int **)gap->ga_data)[c & 0xff];
3908 while (*inp != 0)
3909 ++inp;
3910 *inp++ = c; /* from char */
3911 *inp++ = i; /* to char */
3912 *inp++ = NUL; /* NUL at the end */
3913 }
3914 else
3915 /* mapping byte to char is done in sl_sal_first[] */
3916 lp->sl_sal_first[c] = i;
3917 }
3918 }
3919 else
3920#endif
3921 {
3922 /* mapping bytes to bytes is done in sl_sal_first[] */
3923 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003924 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003925
3926 for (i = 0; to[i] != NUL; ++i)
3927 lp->sl_sal_first[from[i]] = to[i];
3928 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3929 }
3930
Bram Moolenaar5195e452005-08-19 20:32:47 +00003931 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003932}
3933
3934/*
3935 * Fill the first-index table for "lp".
3936 */
3937 static void
3938set_sal_first(lp)
3939 slang_T *lp;
3940{
3941 salfirst_T *sfirst;
3942 int i;
3943 salitem_T *smp;
3944 int c;
3945 garray_T *gap = &lp->sl_sal;
3946
3947 sfirst = lp->sl_sal_first;
3948 for (i = 0; i < 256; ++i)
3949 sfirst[i] = -1;
3950 smp = (salitem_T *)gap->ga_data;
3951 for (i = 0; i < gap->ga_len; ++i)
3952 {
3953#ifdef FEAT_MBYTE
3954 if (has_mbyte)
3955 /* Use the lowest byte of the first character. For latin1 it's
3956 * the character, for other encodings it should differ for most
3957 * characters. */
3958 c = *smp[i].sm_lead_w & 0xff;
3959 else
3960#endif
3961 c = *smp[i].sm_lead;
3962 if (sfirst[c] == -1)
3963 {
3964 sfirst[c] = i;
3965#ifdef FEAT_MBYTE
3966 if (has_mbyte)
3967 {
3968 int n;
3969
3970 /* Make sure all entries with this byte are following each
3971 * other. Move the ones that are in the wrong position. Do
3972 * keep the same ordering! */
3973 while (i + 1 < gap->ga_len
3974 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3975 /* Skip over entry with same index byte. */
3976 ++i;
3977
3978 for (n = 1; i + n < gap->ga_len; ++n)
3979 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3980 {
3981 salitem_T tsal;
3982
3983 /* Move entry with same index byte after the entries
3984 * we already found. */
3985 ++i;
3986 --n;
3987 tsal = smp[i + n];
3988 mch_memmove(smp + i + 1, smp + i,
3989 sizeof(salitem_T) * n);
3990 smp[i] = tsal;
3991 }
3992 }
3993#endif
3994 }
3995 }
3996}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003997
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003998#ifdef FEAT_MBYTE
3999/*
4000 * Turn a multi-byte string into a wide character string.
4001 * Return it in allocated memory (NULL for out-of-memory)
4002 */
4003 static int *
4004mb_str2wide(s)
4005 char_u *s;
4006{
4007 int *res;
4008 char_u *p;
4009 int i = 0;
4010
4011 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
4012 if (res != NULL)
4013 {
4014 for (p = s; *p != NUL; )
4015 res[i++] = mb_ptr2char_adv(&p);
4016 res[i] = NUL;
4017 }
4018 return res;
4019}
4020#endif
4021
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004022/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004023 * Read a tree from the .spl or .sug file.
4024 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4025 * This is skipped when the tree has zero length.
4026 * Returns zero when OK, SP_ value for an error.
4027 */
4028 static int
4029spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4030 FILE *fd;
4031 char_u **bytsp;
4032 idx_T **idxsp;
4033 int prefixtree; /* TRUE for the prefix tree */
4034 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4035{
4036 int len;
4037 int idx;
4038 char_u *bp;
4039 idx_T *ip;
4040
4041 /* The tree size was computed when writing the file, so that we can
4042 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004043 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004044 if (len < 0)
4045 return SP_TRUNCERROR;
4046 if (len > 0)
4047 {
4048 /* Allocate the byte array. */
4049 bp = lalloc((long_u)len, TRUE);
4050 if (bp == NULL)
4051 return SP_OTHERERROR;
4052 *bytsp = bp;
4053
4054 /* Allocate the index array. */
4055 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4056 if (ip == NULL)
4057 return SP_OTHERERROR;
4058 *idxsp = ip;
4059
4060 /* Recursively read the tree and store it in the array. */
4061 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4062 if (idx < 0)
4063 return idx;
4064 }
4065 return 0;
4066}
4067
4068/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 * Read one row of siblings from the spell file and store it in the byte array
4070 * "byts" and index array "idxs". Recursively read the children.
4071 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004072 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004074 * Returns the index (>= 0) following the siblings.
4075 * Returns SP_TRUNCERROR if the file is shorter than expected.
4076 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004078 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004079read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 FILE *fd;
4081 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004082 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004084 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004085 int prefixtree; /* TRUE for reading PREFIXTREE */
4086 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004088 int len;
4089 int i;
4090 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004091 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004093 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004094#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095
Bram Moolenaar51485f02005-06-04 21:55:20 +00004096 len = getc(fd); /* <siblingcount> */
4097 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004098 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004099
4100 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004101 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004102 byts[idx++] = len;
4103
4104 /* Read the byte values, flag/region bytes and shared indexes. */
4105 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004106 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004107 c = getc(fd); /* <byte> */
4108 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004109 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004110 if (c <= BY_SPECIAL)
4111 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004112 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004113 {
4114 /* No flags, all regions. */
4115 idxs[idx] = 0;
4116 c = 0;
4117 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004118 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004119 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004120 if (prefixtree)
4121 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004122 /* Read the optional pflags byte, the prefix ID and the
4123 * condition nr. In idxs[] store the prefix ID in the low
4124 * byte, the condition index shifted up 8 bits, the flags
4125 * shifted up 24 bits. */
4126 if (c == BY_FLAGS)
4127 c = getc(fd) << 24; /* <pflags> */
4128 else
4129 c = 0;
4130
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004131 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004132
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004133 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004134 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004135 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004136 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004137 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004138 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004139 {
4140 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004141 * idxs[] the flags go in the low two bytes, region above
4142 * that and prefix ID above the region. */
4143 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004144 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004145 if (c2 == BY_FLAGS2)
4146 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004147 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004148 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004149 if (c & WF_AFX)
4150 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004151 }
4152
Bram Moolenaar51485f02005-06-04 21:55:20 +00004153 idxs[idx] = c;
4154 c = 0;
4155 }
4156 else /* c == BY_INDEX */
4157 {
4158 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004159 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004160 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004161 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004162 idxs[idx] = n + SHARED_MASK;
4163 c = getc(fd); /* <xbyte> */
4164 }
4165 }
4166 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 }
4168
Bram Moolenaar51485f02005-06-04 21:55:20 +00004169 /* Recursively read the children for non-shared siblings.
4170 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4171 * remove SHARED_MASK) */
4172 for (i = 1; i <= len; ++i)
4173 if (byts[startidx + i] != 0)
4174 {
4175 if (idxs[startidx + i] & SHARED_MASK)
4176 idxs[startidx + i] &= ~SHARED_MASK;
4177 else
4178 {
4179 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004180 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004181 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004182 if (idx < 0)
4183 break;
4184 }
4185 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004186
Bram Moolenaar51485f02005-06-04 21:55:20 +00004187 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004188}
4189
4190/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004191 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004192 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193 */
4194 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004195did_set_spelllang(wp)
4196 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197{
4198 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004199 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004201 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004202 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004204 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004205 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004208 int len;
4209 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004210 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004211 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004212 char_u *use_region = NULL;
4213 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004214 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004215 int i, j;
4216 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004217 static int recursive = FALSE;
4218 char_u *ret_msg = NULL;
4219 char_u *spl_copy;
4220
4221 /* We don't want to do this recursively. May happen when a language is
4222 * not available and the SpellFileMissing autocommand opens a new buffer
4223 * in which 'spell' is set. */
4224 if (recursive)
4225 return NULL;
4226 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227
4228 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004229 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004230
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004231 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004232 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004233 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004234 if (spl_copy == NULL)
4235 goto theend;
4236
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004237 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004238 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004239 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004240 /* Get one language name. */
4241 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004242 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004243 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004244
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004245 /* If the name ends in ".spl" use it as the name of the spell file.
4246 * If there is a region name let "region" point to it and remove it
4247 * from the name. */
4248 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4249 {
4250 filename = TRUE;
4251
Bram Moolenaarb6356332005-07-18 21:40:44 +00004252 /* Locate a region and remove it from the file name. */
4253 p = vim_strchr(gettail(lang), '_');
4254 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4255 && !ASCII_ISALPHA(p[3]))
4256 {
4257 vim_strncpy(region_cp, p + 1, 2);
4258 mch_memmove(p, p + 3, len - (p - lang) - 2);
4259 len -= 3;
4260 region = region_cp;
4261 }
4262 else
4263 dont_use_region = TRUE;
4264
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004265 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004266 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4267 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004268 break;
4269 }
4270 else
4271 {
4272 filename = FALSE;
4273 if (len > 3 && lang[len - 3] == '_')
4274 {
4275 region = lang + len - 2;
4276 len -= 3;
4277 lang[len] = NUL;
4278 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004279 else
4280 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004281
4282 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004283 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4284 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004285 break;
4286 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004287
Bram Moolenaarb6356332005-07-18 21:40:44 +00004288 if (region != NULL)
4289 {
4290 /* If the region differs from what was used before then don't
4291 * use it for 'spellfile'. */
4292 if (use_region != NULL && STRCMP(region, use_region) != 0)
4293 dont_use_region = TRUE;
4294 use_region = region;
4295 }
4296
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004297 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004299 {
4300 if (filename)
4301 (void)spell_load_file(lang, lang, NULL, FALSE);
4302 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004303 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004304 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004305#ifdef FEAT_AUTOCMD
4306 /* SpellFileMissing autocommands may do anything, including
4307 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004308 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004309 {
4310 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4311 goto theend;
4312 }
4313#endif
4314 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004315 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004316
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004317 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004318 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004319 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004320 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4321 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4322 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004323 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004324 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004325 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004326 {
4327 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004328 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004329 if (c == REGION_ALL)
4330 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004331 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004332 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004333 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004334 /* This addition file is for other regions. */
4335 region_mask = 0;
4336 }
4337 else
4338 /* This is probably an error. Give a warning and
4339 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004340 smsg((char_u *)
4341 _("Warning: region %s not supported"),
4342 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004343 }
4344 else
4345 region_mask = 1 << c;
4346 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004347
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004348 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004349 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004350 if (ga_grow(&ga, 1) == FAIL)
4351 {
4352 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004353 ret_msg = e_outofmem;
4354 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004355 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004357 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4358 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004359 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004360 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004361 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004362 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364 }
4365
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004366 /* round 0: load int_wordlist, if possible.
4367 * round 1: load first name in 'spellfile'.
4368 * round 2: load second name in 'spellfile.
4369 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004370 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004371 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004373 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004374 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004375 /* Internal wordlist, if there is one. */
4376 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004377 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004378 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004379 }
4380 else
4381 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004382 /* One entry in 'spellfile'. */
4383 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4384 STRCAT(spf_name, ".spl");
4385
4386 /* If it was already found above then skip it. */
4387 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004388 {
4389 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4390 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004391 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004392 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004393 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004394 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004395 }
4396
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004397 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004398 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4399 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004401 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004402 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004403 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004404 * region name, the region is ignored otherwise. for int_wordlist
4405 * use an arbitrary name. */
4406 if (round == 0)
4407 STRCPY(lang, "internal wordlist");
4408 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004409 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004410 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004411 p = vim_strchr(lang, '.');
4412 if (p != NULL)
4413 *p = NUL; /* truncate at ".encoding.add" */
4414 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004415 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004416
4417 /* If one of the languages has NOBREAK we assume the addition
4418 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004419 if (slang != NULL && nobreak)
4420 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004422 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004424 region_mask = REGION_ALL;
4425 if (use_region != NULL && !dont_use_region)
4426 {
4427 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004428 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004429 if (c != REGION_ALL)
4430 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004432 /* This spell file is for other regions. */
4433 region_mask = 0;
4434 }
4435
4436 if (region_mask != 0)
4437 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004438 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4439 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4440 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004441 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4442 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004443 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 }
4446 }
4447
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004448 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004449 ga_clear(&wp->w_s->b_langp);
4450 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004451
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004452 /* For each language figure out what language to use for sound folding and
4453 * REP items. If the language doesn't support it itself use another one
4454 * with the same name. E.g. for "en-math" use "en". */
4455 for (i = 0; i < ga.ga_len; ++i)
4456 {
4457 lp = LANGP_ENTRY(ga, i);
4458
4459 /* sound folding */
4460 if (lp->lp_slang->sl_sal.ga_len > 0)
4461 /* language does sound folding itself */
4462 lp->lp_sallang = lp->lp_slang;
4463 else
4464 /* find first similar language that does sound folding */
4465 for (j = 0; j < ga.ga_len; ++j)
4466 {
4467 lp2 = LANGP_ENTRY(ga, j);
4468 if (lp2->lp_slang->sl_sal.ga_len > 0
4469 && STRNCMP(lp->lp_slang->sl_name,
4470 lp2->lp_slang->sl_name, 2) == 0)
4471 {
4472 lp->lp_sallang = lp2->lp_slang;
4473 break;
4474 }
4475 }
4476
4477 /* REP items */
4478 if (lp->lp_slang->sl_rep.ga_len > 0)
4479 /* language has REP items itself */
4480 lp->lp_replang = lp->lp_slang;
4481 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004482 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004483 for (j = 0; j < ga.ga_len; ++j)
4484 {
4485 lp2 = LANGP_ENTRY(ga, j);
4486 if (lp2->lp_slang->sl_rep.ga_len > 0
4487 && STRNCMP(lp->lp_slang->sl_name,
4488 lp2->lp_slang->sl_name, 2) == 0)
4489 {
4490 lp->lp_replang = lp2->lp_slang;
4491 break;
4492 }
4493 }
4494 }
4495
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004496theend:
4497 vim_free(spl_copy);
4498 recursive = FALSE;
4499 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004500}
4501
4502/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004503 * Clear the midword characters for buffer "buf".
4504 */
4505 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004506clear_midword(wp)
4507 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004508{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004509 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004510#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004511 vim_free(wp->w_s->b_spell_ismw_mb);
4512 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004513#endif
4514}
4515
4516/*
4517 * Use the "sl_midword" field of language "lp" for buffer "buf".
4518 * They add up to any currently used midword characters.
4519 */
4520 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004521use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004522 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004523 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004524{
4525 char_u *p;
4526
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004527 if (lp->sl_midword == NULL) /* there aren't any */
4528 return;
4529
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004530 for (p = lp->sl_midword; *p != NUL; )
4531#ifdef FEAT_MBYTE
4532 if (has_mbyte)
4533 {
4534 int c, l, n;
4535 char_u *bp;
4536
4537 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004538 l = (*mb_ptr2len)(p);
4539 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004540 wp->w_s->b_spell_ismw[c] = TRUE;
4541 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004542 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004543 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004544 else
4545 {
4546 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004547 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4548 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004549 if (bp != NULL)
4550 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004551 vim_free(wp->w_s->b_spell_ismw_mb);
4552 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004553 vim_strncpy(bp + n, p, l);
4554 }
4555 }
4556 p += l;
4557 }
4558 else
4559#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004560 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004561}
4562
4563/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564 * Find the region "region[2]" in "rp" (points to "sl_regions").
4565 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004566 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004567 */
4568 static int
4569find_region(rp, region)
4570 char_u *rp;
4571 char_u *region;
4572{
4573 int i;
4574
4575 for (i = 0; ; i += 2)
4576 {
4577 if (rp[i] == NUL)
4578 return REGION_ALL;
4579 if (rp[i] == region[0] && rp[i + 1] == region[1])
4580 break;
4581 }
4582 return i / 2;
4583}
4584
4585/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004587 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004588 * Word WF_ONECAP
4589 * W WORD WF_ALLCAP
4590 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 */
4592 static int
4593captype(word, end)
4594 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004596{
4597 char_u *p;
4598 int c;
4599 int firstcap;
4600 int allcap;
4601 int past_second = FALSE; /* past second word char */
4602
4603 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004604 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 return 0; /* only non-word characters, illegal word */
4607#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004608 if (has_mbyte)
4609 c = mb_ptr2char_adv(&p);
4610 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004612 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004613 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004614
4615 /*
4616 * Need to check all letters to find a word with mixed upper/lower.
4617 * But a word with an upper char only at start is a ONECAP.
4618 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004620 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004621 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004622 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004623 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624 {
4625 /* UUl -> KEEPCAP */
4626 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004627 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004628 allcap = FALSE;
4629 }
4630 else if (!allcap)
4631 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004632 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004633 past_second = TRUE;
4634 }
4635
4636 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004637 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004638 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004639 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004640 return 0;
4641}
4642
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004643/*
4644 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4645 * capital. So that make_case_word() can turn WOrd into Word.
4646 * Add ALLCAP for "WOrD".
4647 */
4648 static int
4649badword_captype(word, end)
4650 char_u *word;
4651 char_u *end;
4652{
4653 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004654 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004655 int l, u;
4656 int first;
4657 char_u *p;
4658
4659 if (flags & WF_KEEPCAP)
4660 {
4661 /* Count the number of UPPER and lower case letters. */
4662 l = u = 0;
4663 first = FALSE;
4664 for (p = word; p < end; mb_ptr_adv(p))
4665 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004666 c = PTR2CHAR(p);
4667 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004668 {
4669 ++u;
4670 if (p == word)
4671 first = TRUE;
4672 }
4673 else
4674 ++l;
4675 }
4676
4677 /* If there are more UPPER than lower case letters suggest an
4678 * ALLCAP word. Otherwise, if the first letter is UPPER then
4679 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4680 * require three upper case letters. */
4681 if (u > l && u > 2)
4682 flags |= WF_ALLCAP;
4683 else if (first)
4684 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004685
4686 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4687 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004688 }
4689 return flags;
4690}
4691
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004692# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4693/*
4694 * Free all languages.
4695 */
4696 void
4697spell_free_all()
4698{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004699 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004700 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004701 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004702
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004703 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004704 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004705 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004706
4707 while (first_lang != NULL)
4708 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004709 slang = first_lang;
4710 first_lang = slang->sl_next;
4711 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004712 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004713
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004714 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004715 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004716 /* Delete the internal wordlist and its .spl file */
4717 mch_remove(int_wordlist);
4718 int_wordlist_spl(fname);
4719 mch_remove(fname);
4720 vim_free(int_wordlist);
4721 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004722 }
4723
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004724 vim_free(repl_to);
4725 repl_to = NULL;
4726 vim_free(repl_from);
4727 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004728}
4729# endif
4730
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004731# if defined(FEAT_MBYTE) || defined(PROTO)
4732/*
4733 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004734 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004735 */
4736 void
4737spell_reload()
4738{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004739 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004740
Bram Moolenaarea408852005-06-25 22:49:46 +00004741 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004742 init_spell_chartab();
4743
4744 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004745 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004746
4747 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004748 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004749 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004750 /* Only load the wordlists when 'spelllang' is set and there is a
4751 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004752 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004753 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004754 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004755 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004756 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004757# ifdef FEAT_WINDOWS
4758 break;
4759# endif
4760 }
4761 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004762 }
4763}
4764# endif
4765
Bram Moolenaarb765d632005-06-07 21:00:02 +00004766/*
4767 * Reload the spell file "fname" if it's loaded.
4768 */
4769 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004771 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004773{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004774 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004776
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004777 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004778 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004779 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004780 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004781 slang_clear(slang);
4782 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004783 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004784 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004785 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004787 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004788 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789
4790 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004791 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004793 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004794}
4795
4796
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004797/*
4798 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799 */
4800
Bram Moolenaar51485f02005-06-04 21:55:20 +00004801#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802 and .dic file. */
4803/*
4804 * Main structure to store the contents of a ".aff" file.
4805 */
4806typedef struct afffile_S
4807{
4808 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004809 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004810 unsigned af_rare; /* RARE ID for rare word */
4811 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004812 unsigned af_bad; /* BAD ID for banned word */
4813 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004814 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004815 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004816 unsigned af_comproot; /* COMPOUNDROOT ID */
4817 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4818 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004819 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004820 int af_pfxpostpone; /* postpone prefixes without chop string and
4821 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004822 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4823 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004824 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004825} afffile_T;
4826
Bram Moolenaar6de68532005-08-24 22:08:48 +00004827#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004828#define AFT_LONG 1 /* flags are two characters */
4829#define AFT_CAPLONG 2 /* flags are one or two characters */
4830#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004831
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004832typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004833/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4834struct affentry_S
4835{
4836 affentry_T *ae_next; /* next affix with same name/number */
4837 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4838 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004839 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004840 char_u *ae_cond; /* condition (NULL for ".") */
4841 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004842 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4843 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004844};
4845
Bram Moolenaar6de68532005-08-24 22:08:48 +00004846#ifdef FEAT_MBYTE
4847# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4848#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004849# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004850#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004851
Bram Moolenaar51485f02005-06-04 21:55:20 +00004852/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4853typedef struct affheader_S
4854{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004855 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4856 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4857 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004858 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004859 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004860 affentry_T *ah_first; /* first affix entry */
4861} affheader_T;
4862
4863#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4864
Bram Moolenaar6de68532005-08-24 22:08:48 +00004865/* Flag used in compound items. */
4866typedef struct compitem_S
4867{
4868 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4869 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4870 int ci_newID; /* affix ID after renumbering. */
4871} compitem_T;
4872
4873#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4874
Bram Moolenaar51485f02005-06-04 21:55:20 +00004875/*
4876 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004877 * the need to keep track of each allocated thing, everything is freed all at
4878 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004879 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4880 * "sb_data" is correct for systems where pointers must be aligned on
4881 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004882 */
4883#define SBLOCKSIZE 16000 /* size of sb_data */
4884typedef struct sblock_S sblock_T;
4885struct sblock_S
4886{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004887 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004888 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004889 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004890};
4891
4892/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004893 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004894 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004895typedef struct wordnode_S wordnode_T;
4896struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004898 union /* shared to save space */
4899 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004900 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004901 int index; /* index in written nodes (valid after first
4902 round) */
4903 } wn_u1;
4904 union /* shared to save space */
4905 {
4906 wordnode_T *next; /* next node with same hash key */
4907 wordnode_T *wnode; /* parent node that will write this node */
4908 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004909 wordnode_T *wn_child; /* child (next byte in word) */
4910 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4911 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004912 int wn_refs; /* Nr. of references to this node. Only
4913 relevant for first node in a list of
4914 siblings, in following siblings it is
4915 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004917
4918 /* Info for when "wn_byte" is NUL.
4919 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4920 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4921 * "wn_region" the LSW of the wordnr. */
4922 char_u wn_affixID; /* supported/required prefix ID or 0 */
4923 short_u wn_flags; /* WF_ flags */
4924 short wn_region; /* region mask */
4925
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004926#ifdef SPELL_PRINTTREE
4927 int wn_nr; /* sequence nr for printing */
4928#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004929};
4930
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004931#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4932
Bram Moolenaar51485f02005-06-04 21:55:20 +00004933#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004934
Bram Moolenaar51485f02005-06-04 21:55:20 +00004935/*
4936 * Info used while reading the spell files.
4937 */
4938typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004939{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004940 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004941 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004942
Bram Moolenaar51485f02005-06-04 21:55:20 +00004943 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004944 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004945
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004946 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004947
Bram Moolenaar4770d092006-01-12 23:22:24 +00004948 long si_sugtree; /* creating the soundfolding trie */
4949
Bram Moolenaar51485f02005-06-04 21:55:20 +00004950 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004951 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004952 int si_did_emsg; /* TRUE when ran out of memory */
4953
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004954 long si_compress_cnt; /* words to add before lowering
4955 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004956 wordnode_T *si_first_free; /* List of nodes that have been freed during
4957 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004958 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004959#ifdef SPELL_PRINTTREE
4960 int si_wordnode_nr; /* sequence nr for nodes */
4961#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004962 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004963
Bram Moolenaar51485f02005-06-04 21:55:20 +00004964 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004965 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004966 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004967 int si_region; /* region mask */
4968 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004969 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004970 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004971 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004972 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004973 int si_region_count; /* number of regions supported (1 when there
4974 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004975 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004976 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004977
4978 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004979 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004980 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004981 char_u *si_sofofr; /* SOFOFROM text */
4982 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004984 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004985 int si_followup; /* soundsalike: ? */
4986 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004987 hashtab_T si_commonwords; /* hashtable for common words */
4988 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004989 int si_rem_accents; /* soundsalike: remove accents */
4990 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004991 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004992 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004993 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004994 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004995 int si_compoptions; /* COMP_ flags */
4996 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4997 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004998 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004999 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005000 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005001 garray_T si_prefcond; /* table with conditions for postponed
5002 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005003 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005004 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005005} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005007static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005008static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005009static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005010static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005011static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5012static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5013static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005014static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005015static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5016static void aff_check_number __ARGS((int spinval, int affval, char *name));
5017static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005018static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005019static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5020static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005021static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005022static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005023static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005024static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005025static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005026static 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 +00005027static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5028static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5029static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005030static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005031static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005032static 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 +00005033static 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 +00005034static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005035static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005036static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5037static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5038static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005039static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005040static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005041static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005042static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005043static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5044static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5045static int sug_maketable __ARGS((spellinfo_T *spin));
5046static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5047static int offset2bytes __ARGS((int nr, char_u *buf));
5048static int bytes2offset __ARGS((char_u **pp));
5049static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005050static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int over_write, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005051static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005052static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053
Bram Moolenaar53805d12005-08-01 07:08:33 +00005054/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5055 * but it must be negative to indicate the prefix tree to tree_add_word().
5056 * Use a negative number with the lower 8 bits zero. */
5057#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005058
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005059/* flags for "condit" argument of store_aff_word() */
5060#define CONDIT_COMB 1 /* affix must combine */
5061#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5062#define CONDIT_SUF 4 /* add a suffix for matching flags */
5063#define CONDIT_AFF 8 /* word already has an affix */
5064
Bram Moolenaar5195e452005-08-19 20:32:47 +00005065/*
5066 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5067 */
5068static long compress_start = 30000; /* memory / SBLOCKSIZE */
5069static long compress_inc = 100; /* memory / SBLOCKSIZE */
5070static long compress_added = 500000; /* word count */
5071
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005072#ifdef SPELL_PRINTTREE
5073/*
5074 * For debugging the tree code: print the current tree in a (more or less)
5075 * readable format, so that we can see what happens when adding a word and/or
5076 * compressing the tree.
5077 * Based on code from Olaf Seibert.
5078 */
5079#define PRINTLINESIZE 1000
5080#define PRINTWIDTH 6
5081
5082#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5083 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5084
5085static char line1[PRINTLINESIZE];
5086static char line2[PRINTLINESIZE];
5087static char line3[PRINTLINESIZE];
5088
5089 static void
5090spell_clear_flags(wordnode_T *node)
5091{
5092 wordnode_T *np;
5093
5094 for (np = node; np != NULL; np = np->wn_sibling)
5095 {
5096 np->wn_u1.index = FALSE;
5097 spell_clear_flags(np->wn_child);
5098 }
5099}
5100
5101 static void
5102spell_print_node(wordnode_T *node, int depth)
5103{
5104 if (node->wn_u1.index)
5105 {
5106 /* Done this node before, print the reference. */
5107 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5108 PRINTSOME(line2, depth, " ", 0, 0);
5109 PRINTSOME(line3, depth, " ", 0, 0);
5110 msg(line1);
5111 msg(line2);
5112 msg(line3);
5113 }
5114 else
5115 {
5116 node->wn_u1.index = TRUE;
5117
5118 if (node->wn_byte != NUL)
5119 {
5120 if (node->wn_child != NULL)
5121 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5122 else
5123 /* Cannot happen? */
5124 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5125 }
5126 else
5127 PRINTSOME(line1, depth, " $ ", 0, 0);
5128
5129 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5130
5131 if (node->wn_sibling != NULL)
5132 PRINTSOME(line3, depth, " | ", 0, 0);
5133 else
5134 PRINTSOME(line3, depth, " ", 0, 0);
5135
5136 if (node->wn_byte == NUL)
5137 {
5138 msg(line1);
5139 msg(line2);
5140 msg(line3);
5141 }
5142
5143 /* do the children */
5144 if (node->wn_byte != NUL && node->wn_child != NULL)
5145 spell_print_node(node->wn_child, depth + 1);
5146
5147 /* do the siblings */
5148 if (node->wn_sibling != NULL)
5149 {
5150 /* get rid of all parent details except | */
5151 STRCPY(line1, line3);
5152 STRCPY(line2, line3);
5153 spell_print_node(node->wn_sibling, depth);
5154 }
5155 }
5156}
5157
5158 static void
5159spell_print_tree(wordnode_T *root)
5160{
5161 if (root != NULL)
5162 {
5163 /* Clear the "wn_u1.index" fields, used to remember what has been
5164 * done. */
5165 spell_clear_flags(root);
5166
5167 /* Recursively print the tree. */
5168 spell_print_node(root, 0);
5169 }
5170}
5171#endif /* SPELL_PRINTTREE */
5172
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005173/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005174 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005175 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005176 */
5177 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005178spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005179 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005180 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005181{
5182 FILE *fd;
5183 afffile_T *aff;
5184 char_u rline[MAXLINELEN];
5185 char_u *line;
5186 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005187#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005188 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005189 int itemcnt;
5190 char_u *p;
5191 int lnum = 0;
5192 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005193 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005194 int aff_todo = 0;
5195 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005196 char_u *low = NULL;
5197 char_u *fol = NULL;
5198 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005199 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005200 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005202 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005204 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005205 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005206 int compminlen = 0; /* COMPOUNDMIN value */
5207 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005208 int compoptions = 0; /* COMP_ flags */
5209 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005210 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005211 concatenated */
5212 char_u *midword = NULL; /* MIDWORD value */
5213 char_u *syllable = NULL; /* SYLLABLE value */
5214 char_u *sofofrom = NULL; /* SOFOFROM value */
5215 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005216
Bram Moolenaar51485f02005-06-04 21:55:20 +00005217 /*
5218 * Open the file.
5219 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005220 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005221 if (fd == NULL)
5222 {
5223 EMSG2(_(e_notopen), fname);
5224 return NULL;
5225 }
5226
Bram Moolenaar4770d092006-01-12 23:22:24 +00005227 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5228 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 /* Only do REP lines when not done in another .aff file already. */
5231 do_rep = spin->si_rep.ga_len == 0;
5232
Bram Moolenaar4770d092006-01-12 23:22:24 +00005233 /* Only do REPSAL lines when not done in another .aff file already. */
5234 do_repsal = spin->si_repsal.ga_len == 0;
5235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005236 /* Only do SAL lines when not done in another .aff file already. */
5237 do_sal = spin->si_sal.ga_len == 0;
5238
5239 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005240 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005241
Bram Moolenaar51485f02005-06-04 21:55:20 +00005242 /*
5243 * Allocate and init the afffile_T structure.
5244 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005245 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005246 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005247 {
5248 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005249 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005250 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251 hash_init(&aff->af_pref);
5252 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005253 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254
5255 /*
5256 * Read all the lines in the file one by one.
5257 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005258 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005260 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005261 ++lnum;
5262
5263 /* Skip comment lines. */
5264 if (*rline == '#')
5265 continue;
5266
5267 /* Convert from "SET" to 'encoding' when needed. */
5268 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005269#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005270 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005271 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005272 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005273 if (pc == NULL)
5274 {
5275 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5276 fname, lnum, rline);
5277 continue;
5278 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005279 line = pc;
5280 }
5281 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005282#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005283 {
5284 pc = NULL;
5285 line = rline;
5286 }
5287
5288 /* Split the line up in white separated items. Put a NUL after each
5289 * item. */
5290 itemcnt = 0;
5291 for (p = line; ; )
5292 {
5293 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5294 ++p;
5295 if (*p == NUL)
5296 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005297 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005298 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005299 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005300 /* A few items have arbitrary text argument, don't split them. */
5301 if (itemcnt == 2 && spell_info_item(items[0]))
5302 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5303 ++p;
5304 else
5305 while (*p > ' ') /* skip until white space or CR/NL */
5306 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005307 if (*p == NUL)
5308 break;
5309 *p++ = NUL;
5310 }
5311
5312 /* Handle non-empty lines. */
5313 if (itemcnt > 0)
5314 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005315 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005316 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005317#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005318 /* Setup for conversion from "ENC" to 'encoding'. */
5319 aff->af_enc = enc_canonize(items[1]);
5320 if (aff->af_enc != NULL && !spin->si_ascii
5321 && convert_setup(&spin->si_conv, aff->af_enc,
5322 p_enc) == FAIL)
5323 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5324 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005325 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005326#else
5327 smsg((char_u *)_("Conversion in %s not supported"), fname);
5328#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005329 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005330 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005331 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005332 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005333 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005334 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005335 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005336 aff->af_flagtype = AFT_NUM;
5337 else if (STRCMP(items[1], "caplong") == 0)
5338 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005339 else
5340 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5341 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005342 if (aff->af_rare != 0
5343 || aff->af_keepcase != 0
5344 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005345 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005346 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005347 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005348 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005349 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005350 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005351 || aff->af_suff.ht_used > 0
5352 || aff->af_pref.ht_used > 0)
5353 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5354 fname, lnum, items[1]);
5355 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005356 else if (spell_info_item(items[0]))
5357 {
5358 p = (char_u *)getroom(spin,
5359 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5360 + STRLEN(items[0])
5361 + STRLEN(items[1]) + 3, FALSE);
5362 if (p != NULL)
5363 {
5364 if (spin->si_info != NULL)
5365 {
5366 STRCPY(p, spin->si_info);
5367 STRCAT(p, "\n");
5368 }
5369 STRCAT(p, items[0]);
5370 STRCAT(p, " ");
5371 STRCAT(p, items[1]);
5372 spin->si_info = p;
5373 }
5374 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005375 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005376 && midword == NULL)
5377 {
5378 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005379 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005380 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005382 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005383 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005384 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005385 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5386 || is_aff_rule(items, itemcnt, "RARE", 2))
5387 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005388 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005389 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005390 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005391 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005392 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005393 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5394 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005395 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005396 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005397 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005398 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005399 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005400 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5401 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5402 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005403 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005404 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5405 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005406 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005407 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005408 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005409 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005410 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5411 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005412 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005413 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005414 && aff->af_circumfix == 0)
5415 {
5416 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5417 fname, lnum);
5418 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005419 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005420 && aff->af_nosuggest == 0)
5421 {
5422 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5423 fname, lnum);
5424 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005425 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5426 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005427 && aff->af_needcomp == 0)
5428 {
5429 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5430 fname, lnum);
5431 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005432 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005433 && aff->af_comproot == 0)
5434 {
5435 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5436 fname, lnum);
5437 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005438 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5439 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005440 {
5441 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5442 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005443 if (aff->af_pref.ht_used > 0)
5444 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5445 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005446 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005447 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5448 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005449 {
5450 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5451 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005452 if (aff->af_pref.ht_used > 0)
5453 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5454 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005455 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005456 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005457 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005458 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005459 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005460 * "Na" into "Na+", "1234" into "1234+". */
5461 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005462 if (p != NULL)
5463 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005464 STRCPY(p, items[1]);
5465 STRCAT(p, "+");
5466 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005467 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005468 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005469 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5470 {
5471 /* We don't use the count, but do check that it's a number and
5472 * not COMPOUNDRULE mistyped. */
5473 if (atoi((char *)items[1]) == 0)
5474 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5475 fname, lnum, items[1]);
5476 }
5477 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005478 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005479 /* Don't use the first rule if it is a number. */
5480 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005481 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005482 /* Concatenate this string to previously defined ones,
5483 * using a slash to separate them. */
5484 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005485 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005486 l += (int)STRLEN(compflags) + 1;
5487 p = getroom(spin, l, FALSE);
5488 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005489 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005490 if (compflags != NULL)
5491 {
5492 STRCPY(p, compflags);
5493 STRCAT(p, "/");
5494 }
5495 STRCAT(p, items[1]);
5496 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005497 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005498 }
5499 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005500 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005501 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005502 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005503 compmax = atoi((char *)items[1]);
5504 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005505 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005506 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005507 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005508 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005509 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005510 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005511 compminlen = atoi((char *)items[1]);
5512 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005513 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5514 fname, lnum, items[1]);
5515 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005516 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005517 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005518 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005519 compsylmax = atoi((char *)items[1]);
5520 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005521 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5522 fname, lnum, items[1]);
5523 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005524 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005525 {
5526 compoptions |= COMP_CHECKDUP;
5527 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005528 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005529 {
5530 compoptions |= COMP_CHECKREP;
5531 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005532 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005533 {
5534 compoptions |= COMP_CHECKCASE;
5535 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005536 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005537 {
5538 compoptions |= COMP_CHECKTRIPLE;
5539 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005540 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005541 {
5542 if (atoi((char *)items[1]) == 0)
5543 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5544 fname, lnum, items[1]);
5545 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005546 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005547 {
5548 garray_T *gap = &spin->si_comppat;
5549 int i;
5550
5551 /* Only add the couple if it isn't already there. */
5552 for (i = 0; i < gap->ga_len - 1; i += 2)
5553 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5554 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5555 items[2]) == 0)
5556 break;
5557 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5558 {
5559 ((char_u **)(gap->ga_data))[gap->ga_len++]
5560 = getroom_save(spin, items[1]);
5561 ((char_u **)(gap->ga_data))[gap->ga_len++]
5562 = getroom_save(spin, items[2]);
5563 }
5564 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005565 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005566 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005567 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005568 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005569 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005570 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005571 {
5572 spin->si_nobreak = TRUE;
5573 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005574 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005575 {
5576 spin->si_nosplitsugs = TRUE;
5577 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005578 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005579 {
5580 spin->si_nosugfile = TRUE;
5581 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005582 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005583 {
5584 aff->af_pfxpostpone = TRUE;
5585 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005586 else if ((STRCMP(items[0], "PFX") == 0
5587 || STRCMP(items[0], "SFX") == 0)
5588 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005589 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005590 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005591 int lasti = 4;
5592 char_u key[AH_KEY_LEN];
5593
5594 if (*items[0] == 'P')
5595 tp = &aff->af_pref;
5596 else
5597 tp = &aff->af_suff;
5598
5599 /* Myspell allows the same affix name to be used multiple
5600 * times. The affix files that do this have an undocumented
5601 * "S" flag on all but the last block, thus we check for that
5602 * and store it in ah_follows. */
5603 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5604 hi = hash_find(tp, key);
5605 if (!HASHITEM_EMPTY(hi))
5606 {
5607 cur_aff = HI2AH(hi);
5608 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5609 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5610 fname, lnum, items[1]);
5611 if (!cur_aff->ah_follows)
5612 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5613 fname, lnum, items[1]);
5614 }
5615 else
5616 {
5617 /* New affix letter. */
5618 cur_aff = (affheader_T *)getroom(spin,
5619 sizeof(affheader_T), TRUE);
5620 if (cur_aff == NULL)
5621 break;
5622 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5623 fname, lnum);
5624 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5625 break;
5626 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005627 || cur_aff->ah_flag == aff->af_rare
5628 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005629 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005630 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005631 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005632 || cur_aff->ah_flag == aff->af_needcomp
5633 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005634 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 +00005635 fname, lnum, items[1]);
5636 STRCPY(cur_aff->ah_key, items[1]);
5637 hash_add(tp, cur_aff->ah_key);
5638
5639 cur_aff->ah_combine = (*items[2] == 'Y');
5640 }
5641
5642 /* Check for the "S" flag, which apparently means that another
5643 * block with the same affix name is following. */
5644 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5645 {
5646 ++lasti;
5647 cur_aff->ah_follows = TRUE;
5648 }
5649 else
5650 cur_aff->ah_follows = FALSE;
5651
Bram Moolenaar8db73182005-06-17 21:51:16 +00005652 /* Myspell allows extra text after the item, but that might
5653 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005654 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005655 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005656
Bram Moolenaar95529562005-08-25 21:21:38 +00005657 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005658 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5659 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005660
Bram Moolenaar95529562005-08-25 21:21:38 +00005661 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005662 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005663 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005664 {
5665 /* Use a new number in the .spl file later, to be able
5666 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005667 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005668 cur_aff->ah_newID = ++spin->si_newprefID;
5669
5670 /* We only really use ah_newID if the prefix is
5671 * postponed. We know that only after handling all
5672 * the items. */
5673 did_postpone_prefix = FALSE;
5674 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005675 else
5676 /* Did use the ID in a previous block. */
5677 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005678 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005679
Bram Moolenaar51485f02005-06-04 21:55:20 +00005680 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005681 }
5682 else if ((STRCMP(items[0], "PFX") == 0
5683 || STRCMP(items[0], "SFX") == 0)
5684 && aff_todo > 0
5685 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005686 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005687 {
5688 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005689 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005690 int lasti = 5;
5691
Bram Moolenaar8db73182005-06-17 21:51:16 +00005692 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005693 * mean mistakes go unnoticed. Require a comment-starter.
5694 * Hunspell uses a "-" item. */
5695 if (itemcnt > lasti && *items[lasti] != '#'
5696 && (STRCMP(items[lasti], "-") != 0
5697 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005698 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005699
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005700 /* New item for an affix letter. */
5701 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005702 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005703 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005704 if (aff_entry == NULL)
5705 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005706
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005707 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005708 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005709 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005710 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005711 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005712
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005713 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005714 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5715 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005716 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005717 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005718 aff_process_flags(aff, aff_entry);
5719 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005720 }
5721
Bram Moolenaar51485f02005-06-04 21:55:20 +00005722 /* Don't use an affix entry with non-ASCII characters when
5723 * "spin->si_ascii" is TRUE. */
5724 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005725 || has_non_ascii(aff_entry->ae_add)))
5726 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005727 aff_entry->ae_next = cur_aff->ah_first;
5728 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005729
5730 if (STRCMP(items[4], ".") != 0)
5731 {
5732 char_u buf[MAXLINELEN];
5733
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005734 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 if (*items[0] == 'P')
5736 sprintf((char *)buf, "^%s", items[4]);
5737 else
5738 sprintf((char *)buf, "%s$", items[4]);
5739 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005740 RE_MAGIC + RE_STRING + RE_STRICT);
5741 if (aff_entry->ae_prog == NULL)
5742 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5743 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005744 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005745
5746 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005747 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005748 * Can't be done for an affix with flags, ignoring
5749 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005750 if (*items[0] == 'P' && aff->af_pfxpostpone
5751 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005752 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005753 /* When the chop string is one lower-case letter and
5754 * the add string ends in the upper-case letter we set
5755 * the "upper" flag, clear "ae_chop" and remove the
5756 * letters from "ae_add". The condition must either
5757 * be empty or start with the same letter. */
5758 if (aff_entry->ae_chop != NULL
5759 && aff_entry->ae_add != NULL
5760#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005761 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005762 aff_entry->ae_chop)] == NUL
5763#else
5764 && aff_entry->ae_chop[1] == NUL
5765#endif
5766 )
5767 {
5768 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005769
Bram Moolenaar53805d12005-08-01 07:08:33 +00005770 c = PTR2CHAR(aff_entry->ae_chop);
5771 c_up = SPELL_TOUPPER(c);
5772 if (c_up != c
5773 && (aff_entry->ae_cond == NULL
5774 || PTR2CHAR(aff_entry->ae_cond) == c))
5775 {
5776 p = aff_entry->ae_add
5777 + STRLEN(aff_entry->ae_add);
5778 mb_ptr_back(aff_entry->ae_add, p);
5779 if (PTR2CHAR(p) == c_up)
5780 {
5781 upper = TRUE;
5782 aff_entry->ae_chop = NULL;
5783 *p = NUL;
5784
5785 /* The condition is matched with the
5786 * actual word, thus must check for the
5787 * upper-case letter. */
5788 if (aff_entry->ae_cond != NULL)
5789 {
5790 char_u buf[MAXLINELEN];
5791#ifdef FEAT_MBYTE
5792 if (has_mbyte)
5793 {
5794 onecap_copy(items[4], buf, TRUE);
5795 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005796 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005797 }
5798 else
5799#endif
5800 *aff_entry->ae_cond = c_up;
5801 if (aff_entry->ae_cond != NULL)
5802 {
5803 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005804 aff_entry->ae_cond);
Bram Moolenaar473de612013-06-08 18:19:48 +02005805 vim_regfree(aff_entry->ae_prog);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005806 aff_entry->ae_prog = vim_regcomp(
5807 buf, RE_MAGIC + RE_STRING);
5808 }
5809 }
5810 }
5811 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005812 }
5813
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005814 if (aff_entry->ae_chop == NULL
5815 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005816 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005817 int idx;
5818 char_u **pp;
5819 int n;
5820
Bram Moolenaar6de68532005-08-24 22:08:48 +00005821 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005822 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5823 --idx)
5824 {
5825 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5826 if (str_equal(p, aff_entry->ae_cond))
5827 break;
5828 }
5829 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5830 {
5831 /* Not found, add a new condition. */
5832 idx = spin->si_prefcond.ga_len++;
5833 pp = ((char_u **)spin->si_prefcond.ga_data)
5834 + idx;
5835 if (aff_entry->ae_cond == NULL)
5836 *pp = NULL;
5837 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005838 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005839 aff_entry->ae_cond);
5840 }
5841
5842 /* Add the prefix to the prefix tree. */
5843 if (aff_entry->ae_add == NULL)
5844 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005845 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005846 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005847
Bram Moolenaar53805d12005-08-01 07:08:33 +00005848 /* PFX_FLAGS is a negative number, so that
5849 * tree_add_word() knows this is the prefix tree. */
5850 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005851 if (!cur_aff->ah_combine)
5852 n |= WFP_NC;
5853 if (upper)
5854 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005855 if (aff_entry->ae_comppermit)
5856 n |= WFP_COMPPERMIT;
5857 if (aff_entry->ae_compforbid)
5858 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005859 tree_add_word(spin, p, spin->si_prefroot, n,
5860 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005861 did_postpone_prefix = TRUE;
5862 }
5863
5864 /* Didn't actually use ah_newID, backup si_newprefID. */
5865 if (aff_todo == 0 && !did_postpone_prefix)
5866 {
5867 --spin->si_newprefID;
5868 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005869 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005870 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005871 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005872 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005873 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005874 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005875 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005876 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005877 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005878 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005879 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005880 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005881 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005882 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005883 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005884 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005885 else if (is_aff_rule(items, itemcnt, "REP", 2)
5886 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005887 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005888 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005889 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005890 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005891 fname, lnum);
5892 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005893 else if ((STRCMP(items[0], "REP") == 0
5894 || STRCMP(items[0], "REPSAL") == 0)
5895 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005896 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005897 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005898 /* Myspell ignores extra arguments, we require it starts with
5899 * # to detect mistakes. */
5900 if (itemcnt > 3 && items[3][0] != '#')
5901 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005902 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005903 {
5904 /* Replace underscore with space (can't include a space
5905 * directly). */
5906 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5907 if (*p == '_')
5908 *p = ' ';
5909 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5910 if (*p == '_')
5911 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005912 add_fromto(spin, items[0][3] == 'S'
5913 ? &spin->si_repsal
5914 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005915 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005916 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005917 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005918 {
5919 /* MAP item or count */
5920 if (!found_map)
5921 {
5922 /* First line contains the count. */
5923 found_map = TRUE;
5924 if (!isdigit(*items[1]))
5925 smsg((char_u *)_("Expected MAP count in %s line %d"),
5926 fname, lnum);
5927 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005928 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005929 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005930 int c;
5931
5932 /* Check that every character appears only once. */
5933 for (p = items[1]; *p != NUL; )
5934 {
5935#ifdef FEAT_MBYTE
5936 c = mb_ptr2char_adv(&p);
5937#else
5938 c = *p++;
5939#endif
5940 if ((spin->si_map.ga_len > 0
5941 && vim_strchr(spin->si_map.ga_data, c)
5942 != NULL)
5943 || vim_strchr(p, c) != NULL)
5944 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5945 fname, lnum);
5946 }
5947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005948 /* We simply concatenate all the MAP strings, separated by
5949 * slashes. */
5950 ga_concat(&spin->si_map, items[1]);
5951 ga_append(&spin->si_map, '/');
5952 }
5953 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005954 /* Accept "SAL from to" and "SAL from to #comment". */
5955 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005956 {
5957 if (do_sal)
5958 {
5959 /* SAL item (sounds-a-like)
5960 * Either one of the known keys or a from-to pair. */
5961 if (STRCMP(items[1], "followup") == 0)
5962 spin->si_followup = sal_to_bool(items[2]);
5963 else if (STRCMP(items[1], "collapse_result") == 0)
5964 spin->si_collapse = sal_to_bool(items[2]);
5965 else if (STRCMP(items[1], "remove_accents") == 0)
5966 spin->si_rem_accents = sal_to_bool(items[2]);
5967 else
5968 /* when "to" is "_" it means empty */
5969 add_fromto(spin, &spin->si_sal, items[1],
5970 STRCMP(items[2], "_") == 0 ? (char_u *)""
5971 : items[2]);
5972 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005973 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005974 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005975 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005976 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005977 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005978 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005979 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005980 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005981 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005982 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005983 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005984 else if (STRCMP(items[0], "COMMON") == 0)
5985 {
5986 int i;
5987
5988 for (i = 1; i < itemcnt; ++i)
5989 {
5990 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5991 items[i])))
5992 {
5993 p = vim_strsave(items[i]);
5994 if (p == NULL)
5995 break;
5996 hash_add(&spin->si_commonwords, p);
5997 }
5998 }
5999 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006000 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006001 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006002 fname, lnum, items[0]);
6003 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006004 }
6005
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006006 if (fol != NULL || low != NULL || upp != NULL)
6007 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006008 if (spin->si_clear_chartab)
6009 {
6010 /* Clear the char type tables, don't want to use any of the
6011 * currently used spell properties. */
6012 init_spell_chartab();
6013 spin->si_clear_chartab = FALSE;
6014 }
6015
Bram Moolenaar3982c542005-06-08 21:56:31 +00006016 /*
6017 * Don't write a word table for an ASCII file, so that we don't check
6018 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006019 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006020 * mb_get_class(), the list of chars in the file will be incomplete.
6021 */
6022 if (!spin->si_ascii
6023#ifdef FEAT_MBYTE
6024 && !enc_utf8
6025#endif
6026 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006027 {
6028 if (fol == NULL || low == NULL || upp == NULL)
6029 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6030 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006031 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006032 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006033
6034 vim_free(fol);
6035 vim_free(low);
6036 vim_free(upp);
6037 }
6038
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006039 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006040 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006041 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006042 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006043 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006044 }
6045
Bram Moolenaar6de68532005-08-24 22:08:48 +00006046 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006047 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006048 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6049 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006050 }
6051
Bram Moolenaar6de68532005-08-24 22:08:48 +00006052 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006053 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006054 if (syllable == NULL)
6055 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6056 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6057 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006058 }
6059
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006060 if (compoptions != 0)
6061 {
6062 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6063 spin->si_compoptions |= compoptions;
6064 }
6065
Bram Moolenaar6de68532005-08-24 22:08:48 +00006066 if (compflags != NULL)
6067 process_compflags(spin, aff, compflags);
6068
6069 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006070 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006071 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006072 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006073 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006074 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006075 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006076 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006077 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006078 }
6079
Bram Moolenaar6de68532005-08-24 22:08:48 +00006080 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006081 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006082 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6083 spin->si_syllable = syllable;
6084 }
6085
6086 if (sofofrom != NULL || sofoto != NULL)
6087 {
6088 if (sofofrom == NULL || sofoto == NULL)
6089 smsg((char_u *)_("Missing SOFO%s line in %s"),
6090 sofofrom == NULL ? "FROM" : "TO", fname);
6091 else if (spin->si_sal.ga_len > 0)
6092 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006093 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006094 {
6095 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6096 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6097 spin->si_sofofr = sofofrom;
6098 spin->si_sofoto = sofoto;
6099 }
6100 }
6101
6102 if (midword != NULL)
6103 {
6104 aff_check_string(spin->si_midword, midword, "MIDWORD");
6105 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006106 }
6107
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006108 vim_free(pc);
6109 fclose(fd);
6110 return aff;
6111}
6112
6113/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006114 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6115 * a comment is following after item "mincount".
6116 */
6117 static int
6118is_aff_rule(items, itemcnt, rulename, mincount)
6119 char_u **items;
6120 int itemcnt;
6121 char *rulename;
6122 int mincount;
6123{
6124 return (STRCMP(items[0], rulename) == 0
6125 && (itemcnt == mincount
6126 || (itemcnt > mincount && items[mincount][0] == '#')));
6127}
6128
6129/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006130 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6131 * ae_flags to ae_comppermit and ae_compforbid.
6132 */
6133 static void
6134aff_process_flags(affile, entry)
6135 afffile_T *affile;
6136 affentry_T *entry;
6137{
6138 char_u *p;
6139 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006140 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006141
6142 if (entry->ae_flags != NULL
6143 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6144 {
6145 for (p = entry->ae_flags; *p != NUL; )
6146 {
6147 prevp = p;
6148 flag = get_affitem(affile->af_flagtype, &p);
6149 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6150 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006151 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006152 p = prevp;
6153 if (flag == affile->af_comppermit)
6154 entry->ae_comppermit = TRUE;
6155 else
6156 entry->ae_compforbid = TRUE;
6157 }
6158 if (affile->af_flagtype == AFT_NUM && *p == ',')
6159 ++p;
6160 }
6161 if (*entry->ae_flags == NUL)
6162 entry->ae_flags = NULL; /* nothing left */
6163 }
6164}
6165
6166/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006167 * Return TRUE if "s" is the name of an info item in the affix file.
6168 */
6169 static int
6170spell_info_item(s)
6171 char_u *s;
6172{
6173 return STRCMP(s, "NAME") == 0
6174 || STRCMP(s, "HOME") == 0
6175 || STRCMP(s, "VERSION") == 0
6176 || STRCMP(s, "AUTHOR") == 0
6177 || STRCMP(s, "EMAIL") == 0
6178 || STRCMP(s, "COPYRIGHT") == 0;
6179}
6180
6181/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006182 * Turn an affix flag name into a number, according to the FLAG type.
6183 * returns zero for failure.
6184 */
6185 static unsigned
6186affitem2flag(flagtype, item, fname, lnum)
6187 int flagtype;
6188 char_u *item;
6189 char_u *fname;
6190 int lnum;
6191{
6192 unsigned res;
6193 char_u *p = item;
6194
6195 res = get_affitem(flagtype, &p);
6196 if (res == 0)
6197 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006198 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006199 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6200 fname, lnum, item);
6201 else
6202 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6203 fname, lnum, item);
6204 }
6205 if (*p != NUL)
6206 {
6207 smsg((char_u *)_(e_affname), fname, lnum, item);
6208 return 0;
6209 }
6210
6211 return res;
6212}
6213
6214/*
6215 * Get one affix name from "*pp" and advance the pointer.
6216 * Returns zero for an error, still advances the pointer then.
6217 */
6218 static unsigned
6219get_affitem(flagtype, pp)
6220 int flagtype;
6221 char_u **pp;
6222{
6223 int res;
6224
Bram Moolenaar95529562005-08-25 21:21:38 +00006225 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006226 {
6227 if (!VIM_ISDIGIT(**pp))
6228 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006229 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006230 return 0;
6231 }
6232 res = getdigits(pp);
6233 }
6234 else
6235 {
6236#ifdef FEAT_MBYTE
6237 res = mb_ptr2char_adv(pp);
6238#else
6239 res = *(*pp)++;
6240#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006241 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006242 && res >= 'A' && res <= 'Z'))
6243 {
6244 if (**pp == NUL)
6245 return 0;
6246#ifdef FEAT_MBYTE
6247 res = mb_ptr2char_adv(pp) + (res << 16);
6248#else
6249 res = *(*pp)++ + (res << 16);
6250#endif
6251 }
6252 }
6253 return res;
6254}
6255
6256/*
6257 * Process the "compflags" string used in an affix file and append it to
6258 * spin->si_compflags.
6259 * The processing involves changing the affix names to ID numbers, so that
6260 * they fit in one byte.
6261 */
6262 static void
6263process_compflags(spin, aff, compflags)
6264 spellinfo_T *spin;
6265 afffile_T *aff;
6266 char_u *compflags;
6267{
6268 char_u *p;
6269 char_u *prevp;
6270 unsigned flag;
6271 compitem_T *ci;
6272 int id;
6273 int len;
6274 char_u *tp;
6275 char_u key[AH_KEY_LEN];
6276 hashitem_T *hi;
6277
6278 /* Make room for the old and the new compflags, concatenated with a / in
6279 * between. Processing it makes it shorter, but we don't know by how
6280 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006281 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006282 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006283 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006284 p = getroom(spin, len, FALSE);
6285 if (p == NULL)
6286 return;
6287 if (spin->si_compflags != NULL)
6288 {
6289 STRCPY(p, spin->si_compflags);
6290 STRCAT(p, "/");
6291 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006292 spin->si_compflags = p;
6293 tp = p + STRLEN(p);
6294
6295 for (p = compflags; *p != NUL; )
6296 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006297 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006298 /* Copy non-flag characters directly. */
6299 *tp++ = *p++;
6300 else
6301 {
6302 /* First get the flag number, also checks validity. */
6303 prevp = p;
6304 flag = get_affitem(aff->af_flagtype, &p);
6305 if (flag != 0)
6306 {
6307 /* Find the flag in the hashtable. If it was used before, use
6308 * the existing ID. Otherwise add a new entry. */
6309 vim_strncpy(key, prevp, p - prevp);
6310 hi = hash_find(&aff->af_comp, key);
6311 if (!HASHITEM_EMPTY(hi))
6312 id = HI2CI(hi)->ci_newID;
6313 else
6314 {
6315 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6316 if (ci == NULL)
6317 break;
6318 STRCPY(ci->ci_key, key);
6319 ci->ci_flag = flag;
6320 /* Avoid using a flag ID that has a special meaning in a
6321 * regexp (also inside []). */
6322 do
6323 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006324 check_renumber(spin);
6325 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006326 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006327 ci->ci_newID = id;
6328 hash_add(&aff->af_comp, ci->ci_key);
6329 }
6330 *tp++ = id;
6331 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006332 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006333 ++p;
6334 }
6335 }
6336
6337 *tp = NUL;
6338}
6339
6340/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006341 * Check that the new IDs for postponed affixes and compounding don't overrun
6342 * each other. We have almost 255 available, but start at 0-127 to avoid
6343 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6344 * When that is used up an error message is given.
6345 */
6346 static void
6347check_renumber(spin)
6348 spellinfo_T *spin;
6349{
6350 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6351 {
6352 spin->si_newprefID = 127;
6353 spin->si_newcompID = 255;
6354 }
6355}
6356
6357/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006358 * Return TRUE if flag "flag" appears in affix list "afflist".
6359 */
6360 static int
6361flag_in_afflist(flagtype, afflist, flag)
6362 int flagtype;
6363 char_u *afflist;
6364 unsigned flag;
6365{
6366 char_u *p;
6367 unsigned n;
6368
6369 switch (flagtype)
6370 {
6371 case AFT_CHAR:
6372 return vim_strchr(afflist, flag) != NULL;
6373
Bram Moolenaar95529562005-08-25 21:21:38 +00006374 case AFT_CAPLONG:
6375 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006376 for (p = afflist; *p != NUL; )
6377 {
6378#ifdef FEAT_MBYTE
6379 n = mb_ptr2char_adv(&p);
6380#else
6381 n = *p++;
6382#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006383 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006384 && *p != NUL)
6385#ifdef FEAT_MBYTE
6386 n = mb_ptr2char_adv(&p) + (n << 16);
6387#else
6388 n = *p++ + (n << 16);
6389#endif
6390 if (n == flag)
6391 return TRUE;
6392 }
6393 break;
6394
Bram Moolenaar95529562005-08-25 21:21:38 +00006395 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006396 for (p = afflist; *p != NUL; )
6397 {
6398 n = getdigits(&p);
6399 if (n == flag)
6400 return TRUE;
6401 if (*p != NUL) /* skip over comma */
6402 ++p;
6403 }
6404 break;
6405 }
6406 return FALSE;
6407}
6408
6409/*
6410 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6411 */
6412 static void
6413aff_check_number(spinval, affval, name)
6414 int spinval;
6415 int affval;
6416 char *name;
6417{
6418 if (spinval != 0 && spinval != affval)
6419 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6420}
6421
6422/*
6423 * Give a warning when "spinval" and "affval" strings are set and not the same.
6424 */
6425 static void
6426aff_check_string(spinval, affval, name)
6427 char_u *spinval;
6428 char_u *affval;
6429 char *name;
6430{
6431 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6432 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6433}
6434
6435/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006436 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6437 * NULL as equal.
6438 */
6439 static int
6440str_equal(s1, s2)
6441 char_u *s1;
6442 char_u *s2;
6443{
6444 if (s1 == NULL || s2 == NULL)
6445 return s1 == s2;
6446 return STRCMP(s1, s2) == 0;
6447}
6448
6449/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006450 * Add a from-to item to "gap". Used for REP and SAL items.
6451 * They are stored case-folded.
6452 */
6453 static void
6454add_fromto(spin, gap, from, to)
6455 spellinfo_T *spin;
6456 garray_T *gap;
6457 char_u *from;
6458 char_u *to;
6459{
6460 fromto_T *ftp;
6461 char_u word[MAXWLEN];
6462
6463 if (ga_grow(gap, 1) == OK)
6464 {
6465 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006466 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006467 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006468 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006469 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006470 ++gap->ga_len;
6471 }
6472}
6473
6474/*
6475 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6476 */
6477 static int
6478sal_to_bool(s)
6479 char_u *s;
6480{
6481 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6482}
6483
6484/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006485 * Free the structure filled by spell_read_aff().
6486 */
6487 static void
6488spell_free_aff(aff)
6489 afffile_T *aff;
6490{
6491 hashtab_T *ht;
6492 hashitem_T *hi;
6493 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006494 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006495 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006496
6497 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006498
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006499 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006500 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6501 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006502 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006503 for (hi = ht->ht_array; todo > 0; ++hi)
6504 {
6505 if (!HASHITEM_EMPTY(hi))
6506 {
6507 --todo;
6508 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006509 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar473de612013-06-08 18:19:48 +02006510 vim_regfree(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006511 }
6512 }
6513 if (ht == &aff->af_suff)
6514 break;
6515 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 hash_clear(&aff->af_pref);
6518 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006519 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520}
6521
6522/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006523 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006524 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006525 */
6526 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006527spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006528 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006529 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006530 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006531{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006532 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006533 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006534 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006535 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006536 char_u store_afflist[MAXWLEN];
6537 int pfxlen;
6538 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006539 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006540 char_u *pc;
6541 char_u *w;
6542 int l;
6543 hash_T hash;
6544 hashitem_T *hi;
6545 FILE *fd;
6546 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006547 int non_ascii = 0;
6548 int retval = OK;
6549 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006550 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006551 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006552
Bram Moolenaar51485f02005-06-04 21:55:20 +00006553 /*
6554 * Open the file.
6555 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006556 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006557 if (fd == NULL)
6558 {
6559 EMSG2(_(e_notopen), fname);
6560 return FAIL;
6561 }
6562
Bram Moolenaar51485f02005-06-04 21:55:20 +00006563 /* The hashtable is only used to detect duplicated words. */
6564 hash_init(&ht);
6565
Bram Moolenaar4770d092006-01-12 23:22:24 +00006566 vim_snprintf((char *)IObuff, IOSIZE,
6567 _("Reading dictionary file %s ..."), fname);
6568 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006569
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006570 /* start with a message for the first line */
6571 spin->si_msg_count = 999999;
6572
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006573 /* Read and ignore the first line: word count. */
6574 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006575 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006576 EMSG2(_("E760: No word count in %s"), fname);
6577
6578 /*
6579 * Read all the lines in the file one by one.
6580 * The words are converted to 'encoding' here, before being added to
6581 * the hashtable.
6582 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006583 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006584 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006585 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006586 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006587 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006588 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006589
Bram Moolenaar51485f02005-06-04 21:55:20 +00006590 /* Remove CR, LF and white space from the end. White space halfway
6591 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006592 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006593 while (l > 0 && line[l - 1] <= ' ')
6594 --l;
6595 if (l == 0)
6596 continue; /* empty line */
6597 line[l] = NUL;
6598
Bram Moolenaarb765d632005-06-07 21:00:02 +00006599#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006600 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006601 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006602 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006603 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006604 if (pc == NULL)
6605 {
6606 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6607 fname, lnum, line);
6608 continue;
6609 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006610 w = pc;
6611 }
6612 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006613#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006614 {
6615 pc = NULL;
6616 w = line;
6617 }
6618
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006619 /* Truncate the word at the "/", set "afflist" to what follows.
6620 * Replace "\/" by "/" and "\\" by "\". */
6621 afflist = NULL;
6622 for (p = w; *p != NUL; mb_ptr_adv(p))
6623 {
6624 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006625 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006626 else if (*p == '/')
6627 {
6628 *p = NUL;
6629 afflist = p + 1;
6630 break;
6631 }
6632 }
6633
6634 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6635 if (spin->si_ascii && has_non_ascii(w))
6636 {
6637 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006638 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006639 continue;
6640 }
6641
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006642 /* This takes time, print a message every 10000 words. */
6643 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006644 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006645 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006646 vim_snprintf((char *)message, sizeof(message),
6647 _("line %6d, word %6d - %s"),
6648 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6649 msg_start();
6650 msg_puts_long_attr(message, 0);
6651 msg_clr_eos();
6652 msg_didout = FALSE;
6653 msg_col = 0;
6654 out_flush();
6655 }
6656
Bram Moolenaar51485f02005-06-04 21:55:20 +00006657 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006658 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006659 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006660 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006661 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006662 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006663 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006664 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006665
Bram Moolenaar51485f02005-06-04 21:55:20 +00006666 hash = hash_hash(dw);
6667 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006668 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006669 {
6670 if (p_verbose > 0)
6671 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006672 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006673 else if (duplicate == 0)
6674 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6675 fname, lnum, dw);
6676 ++duplicate;
6677 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006678 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006679 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006680
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006681 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006682 store_afflist[0] = NUL;
6683 pfxlen = 0;
6684 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006685 if (afflist != NULL)
6686 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006687 /* Extract flags from the affix list. */
6688 flags |= get_affix_flags(affile, afflist);
6689
Bram Moolenaar6de68532005-08-24 22:08:48 +00006690 if (affile->af_needaffix != 0 && flag_in_afflist(
6691 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006692 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006693
6694 if (affile->af_pfxpostpone)
6695 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006696 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006697
Bram Moolenaar5195e452005-08-19 20:32:47 +00006698 if (spin->si_compflags != NULL)
6699 /* Need to store the list of compound flags with the word.
6700 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006701 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006702 }
6703
Bram Moolenaar51485f02005-06-04 21:55:20 +00006704 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006705 if (store_word(spin, dw, flags, spin->si_region,
6706 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006707 retval = FAIL;
6708
6709 if (afflist != NULL)
6710 {
6711 /* Find all matching suffixes and add the resulting words.
6712 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006713 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006714 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006715 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006716 retval = FAIL;
6717
6718 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006719 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006720 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006721 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006722 retval = FAIL;
6723 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006724
6725 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006726 }
6727
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006728 if (duplicate > 0)
6729 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006730 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006731 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6732 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006733 hash_clear(&ht);
6734
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006735 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006736 return retval;
6737}
6738
6739/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006740 * Check for affix flags in "afflist" that are turned into word flags.
6741 * Return WF_ flags.
6742 */
6743 static int
6744get_affix_flags(affile, afflist)
6745 afffile_T *affile;
6746 char_u *afflist;
6747{
6748 int flags = 0;
6749
6750 if (affile->af_keepcase != 0 && flag_in_afflist(
6751 affile->af_flagtype, afflist, affile->af_keepcase))
6752 flags |= WF_KEEPCAP | WF_FIXCAP;
6753 if (affile->af_rare != 0 && flag_in_afflist(
6754 affile->af_flagtype, afflist, affile->af_rare))
6755 flags |= WF_RARE;
6756 if (affile->af_bad != 0 && flag_in_afflist(
6757 affile->af_flagtype, afflist, affile->af_bad))
6758 flags |= WF_BANNED;
6759 if (affile->af_needcomp != 0 && flag_in_afflist(
6760 affile->af_flagtype, afflist, affile->af_needcomp))
6761 flags |= WF_NEEDCOMP;
6762 if (affile->af_comproot != 0 && flag_in_afflist(
6763 affile->af_flagtype, afflist, affile->af_comproot))
6764 flags |= WF_COMPROOT;
6765 if (affile->af_nosuggest != 0 && flag_in_afflist(
6766 affile->af_flagtype, afflist, affile->af_nosuggest))
6767 flags |= WF_NOSUGGEST;
6768 return flags;
6769}
6770
6771/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006772 * Get the list of prefix IDs from the affix list "afflist".
6773 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006774 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6775 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006776 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006777 static int
6778get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006779 afffile_T *affile;
6780 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006781 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006782{
6783 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006784 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006785 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006786 int id;
6787 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006788 hashitem_T *hi;
6789
Bram Moolenaar6de68532005-08-24 22:08:48 +00006790 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006791 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006792 prevp = p;
6793 if (get_affitem(affile->af_flagtype, &p) != 0)
6794 {
6795 /* A flag is a postponed prefix flag if it appears in "af_pref"
6796 * and it's ID is not zero. */
6797 vim_strncpy(key, prevp, p - prevp);
6798 hi = hash_find(&affile->af_pref, key);
6799 if (!HASHITEM_EMPTY(hi))
6800 {
6801 id = HI2AH(hi)->ah_newID;
6802 if (id != 0)
6803 store_afflist[cnt++] = id;
6804 }
6805 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006806 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006807 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006808 }
6809
Bram Moolenaar5195e452005-08-19 20:32:47 +00006810 store_afflist[cnt] = NUL;
6811 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006812}
6813
6814/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006815 * Get the list of compound IDs from the affix list "afflist" that are used
6816 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006817 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006818 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006819 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006820get_compflags(affile, afflist, store_afflist)
6821 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006822 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006823 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006824{
6825 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006826 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006827 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006828 char_u key[AH_KEY_LEN];
6829 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006830
Bram Moolenaar6de68532005-08-24 22:08:48 +00006831 for (p = afflist; *p != NUL; )
6832 {
6833 prevp = p;
6834 if (get_affitem(affile->af_flagtype, &p) != 0)
6835 {
6836 /* A flag is a compound flag if it appears in "af_comp". */
6837 vim_strncpy(key, prevp, p - prevp);
6838 hi = hash_find(&affile->af_comp, key);
6839 if (!HASHITEM_EMPTY(hi))
6840 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6841 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006842 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006843 ++p;
6844 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006845
Bram Moolenaar5195e452005-08-19 20:32:47 +00006846 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006847}
6848
6849/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006850 * Apply affixes to a word and store the resulting words.
6851 * "ht" is the hashtable with affentry_T that need to be applied, either
6852 * prefixes or suffixes.
6853 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6854 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006855 *
6856 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006857 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006858 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006859store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006860 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006861 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006862 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006863 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006864 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006865 hashtab_T *ht;
6866 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006867 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006868 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006869 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006870 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6871 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006872{
6873 int todo;
6874 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006875 affheader_T *ah;
6876 affentry_T *ae;
6877 regmatch_T regmatch;
6878 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006879 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006880 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006881 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006882 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006883 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006884 int use_pfxlen;
6885 int need_affix;
6886 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006887 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006888 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006889 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006890
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006891 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006892 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006893 {
6894 if (!HASHITEM_EMPTY(hi))
6895 {
6896 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006897 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006898
Bram Moolenaar51485f02005-06-04 21:55:20 +00006899 /* Check that the affix combines, if required, and that the word
6900 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006901 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6902 && flag_in_afflist(affile->af_flagtype, afflist,
6903 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006904 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006905 /* Loop over all affix entries with this name. */
6906 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006907 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006908 /* Check the condition. It's not logical to match case
6909 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006910 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006911 * Another requirement from Myspell is that the chop
6912 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006913 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006914 * prefixes with a chop string and/or flags.
6915 * When a previously added affix had CIRCUMFIX this one
6916 * must have it too, if it had not then this one must not
6917 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006918 regmatch.regprog = ae->ae_prog;
6919 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006920 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006921 || ae->ae_chop != NULL
6922 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006923 && (ae->ae_chop == NULL
6924 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006925 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006926 || vim_regexec(&regmatch, word, (colnr_T)0))
6927 && (((condit & CONDIT_CFIX) == 0)
6928 == ((condit & CONDIT_AFF) == 0
6929 || ae->ae_flags == NULL
6930 || !flag_in_afflist(affile->af_flagtype,
6931 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006932 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006933 /* Match. Remove the chop and add the affix. */
6934 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006935 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006936 /* prefix: chop/add at the start of the word */
6937 if (ae->ae_add == NULL)
6938 *newword = NUL;
6939 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006940 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006941 p = word;
6942 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006943 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006944 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006945#ifdef FEAT_MBYTE
6946 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006947 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006948 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006949 for ( ; i > 0; --i)
6950 mb_ptr_adv(p);
6951 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006952 else
6953#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006954 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006955 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006956 STRCAT(newword, p);
6957 }
6958 else
6959 {
6960 /* suffix: chop/add at the end of the word */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006961 vim_strncpy(newword, word, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006962 if (ae->ae_chop != NULL)
6963 {
6964 /* Remove chop string. */
6965 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006966 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006967 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 mb_ptr_back(newword, p);
6969 *p = NUL;
6970 }
6971 if (ae->ae_add != NULL)
6972 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006973 }
6974
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006975 use_flags = flags;
6976 use_pfxlist = pfxlist;
6977 use_pfxlen = pfxlen;
6978 need_affix = FALSE;
6979 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6980 if (ae->ae_flags != NULL)
6981 {
6982 /* Extract flags from the affix list. */
6983 use_flags |= get_affix_flags(affile, ae->ae_flags);
6984
6985 if (affile->af_needaffix != 0 && flag_in_afflist(
6986 affile->af_flagtype, ae->ae_flags,
6987 affile->af_needaffix))
6988 need_affix = TRUE;
6989
6990 /* When there is a CIRCUMFIX flag the other affix
6991 * must also have it and we don't add the word
6992 * with one affix. */
6993 if (affile->af_circumfix != 0 && flag_in_afflist(
6994 affile->af_flagtype, ae->ae_flags,
6995 affile->af_circumfix))
6996 {
6997 use_condit |= CONDIT_CFIX;
6998 if ((condit & CONDIT_CFIX) == 0)
6999 need_affix = TRUE;
7000 }
7001
7002 if (affile->af_pfxpostpone
7003 || spin->si_compflags != NULL)
7004 {
7005 if (affile->af_pfxpostpone)
7006 /* Get prefix IDS from the affix list. */
7007 use_pfxlen = get_pfxlist(affile,
7008 ae->ae_flags, store_afflist);
7009 else
7010 use_pfxlen = 0;
7011 use_pfxlist = store_afflist;
7012
7013 /* Combine the prefix IDs. Avoid adding the
7014 * same ID twice. */
7015 for (i = 0; i < pfxlen; ++i)
7016 {
7017 for (j = 0; j < use_pfxlen; ++j)
7018 if (pfxlist[i] == use_pfxlist[j])
7019 break;
7020 if (j == use_pfxlen)
7021 use_pfxlist[use_pfxlen++] = pfxlist[i];
7022 }
7023
7024 if (spin->si_compflags != NULL)
7025 /* Get compound IDS from the affix list. */
7026 get_compflags(affile, ae->ae_flags,
7027 use_pfxlist + use_pfxlen);
7028
7029 /* Combine the list of compound flags.
7030 * Concatenate them to the prefix IDs list.
7031 * Avoid adding the same ID twice. */
7032 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7033 {
7034 for (j = use_pfxlen;
7035 use_pfxlist[j] != NUL; ++j)
7036 if (pfxlist[i] == use_pfxlist[j])
7037 break;
7038 if (use_pfxlist[j] == NUL)
7039 {
7040 use_pfxlist[j++] = pfxlist[i];
7041 use_pfxlist[j] = NUL;
7042 }
7043 }
7044 }
7045 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007046
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007047 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007048 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007049 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007050 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007051 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007052 use_pfxlist = pfx_pfxlist;
7053 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007054
7055 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007056 if (spin->si_prefroot != NULL
7057 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007058 {
7059 /* ... add a flag to indicate an affix was used. */
7060 use_flags |= WF_HAS_AFF;
7061
7062 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007063 * affixes is not allowed. But do use the
7064 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007065 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007066 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007067 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007069 /* When compounding is supported and there is no
7070 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7071 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007072 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007073 {
7074 if (xht != NULL)
7075 use_flags |= WF_NOCOMPAFT;
7076 else
7077 use_flags |= WF_NOCOMPBEF;
7078 }
7079
Bram Moolenaar51485f02005-06-04 21:55:20 +00007080 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007081 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007082 spin->si_region, use_pfxlist,
7083 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007084 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007085
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007086 /* When added a prefix or a first suffix and the affix
7087 * has flags may add a(nother) suffix. RECURSIVE! */
7088 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7089 if (store_aff_word(spin, newword, ae->ae_flags,
7090 affile, &affile->af_suff, xht,
7091 use_condit & (xht == NULL
7092 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007093 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007094 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007095
7096 /* When added a suffix and combining is allowed also
7097 * try adding a prefix additionally. Both for the
7098 * word flags and for the affix flags. RECURSIVE! */
7099 if (xht != NULL && ah->ah_combine)
7100 {
7101 if (store_aff_word(spin, newword,
7102 afflist, affile,
7103 xht, NULL, use_condit,
7104 use_flags, use_pfxlist,
7105 pfxlen) == FAIL
7106 || (ae->ae_flags != NULL
7107 && store_aff_word(spin, newword,
7108 ae->ae_flags, affile,
7109 xht, NULL, use_condit,
7110 use_flags, use_pfxlist,
7111 pfxlen) == FAIL))
7112 retval = FAIL;
7113 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007114 }
7115 }
7116 }
7117 }
7118 }
7119
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007120 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007121}
7122
7123/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007124 * Read a file with a list of words.
7125 */
7126 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007127spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007128 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007129 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007130{
7131 FILE *fd;
7132 long lnum = 0;
7133 char_u rline[MAXLINELEN];
7134 char_u *line;
7135 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007136 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007137 int l;
7138 int retval = OK;
7139 int did_word = FALSE;
7140 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007141 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007142 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007143
7144 /*
7145 * Open the file.
7146 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007147 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007148 if (fd == NULL)
7149 {
7150 EMSG2(_(e_notopen), fname);
7151 return FAIL;
7152 }
7153
Bram Moolenaar4770d092006-01-12 23:22:24 +00007154 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7155 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007156
7157 /*
7158 * Read all the lines in the file one by one.
7159 */
7160 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7161 {
7162 line_breakcheck();
7163 ++lnum;
7164
7165 /* Skip comment lines. */
7166 if (*rline == '#')
7167 continue;
7168
7169 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007170 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007171 while (l > 0 && rline[l - 1] <= ' ')
7172 --l;
7173 if (l == 0)
7174 continue; /* empty or blank line */
7175 rline[l] = NUL;
7176
Bram Moolenaar9c102382006-05-03 21:26:49 +00007177 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007178 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007179#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007180 if (spin->si_conv.vc_type != CONV_NONE)
7181 {
7182 pc = string_convert(&spin->si_conv, rline, NULL);
7183 if (pc == NULL)
7184 {
7185 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7186 fname, lnum, rline);
7187 continue;
7188 }
7189 line = pc;
7190 }
7191 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007192#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007193 {
7194 pc = NULL;
7195 line = rline;
7196 }
7197
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007198 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007200 ++line;
7201 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202 {
7203 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007204 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7205 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007206 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007207 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7208 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007209 else
7210 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007211#ifdef FEAT_MBYTE
7212 char_u *enc;
7213
Bram Moolenaar51485f02005-06-04 21:55:20 +00007214 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007215 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007216 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007217 if (enc != NULL && !spin->si_ascii
7218 && convert_setup(&spin->si_conv, enc,
7219 p_enc) == FAIL)
7220 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007221 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007222 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007223 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007224#else
7225 smsg((char_u *)_("Conversion in %s not supported"), fname);
7226#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007227 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007228 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007229 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007230
Bram Moolenaar3982c542005-06-08 21:56:31 +00007231 if (STRNCMP(line, "regions=", 8) == 0)
7232 {
7233 if (spin->si_region_count > 1)
7234 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7235 fname, lnum, line);
7236 else
7237 {
7238 line += 8;
7239 if (STRLEN(line) > 16)
7240 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7241 fname, lnum, line);
7242 else
7243 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007244 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007245 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007246
7247 /* Adjust the mask for a word valid in all regions. */
7248 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007249 }
7250 }
7251 continue;
7252 }
7253
Bram Moolenaar7887d882005-07-01 22:33:52 +00007254 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7255 fname, lnum, line - 1);
7256 continue;
7257 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007258
Bram Moolenaar7887d882005-07-01 22:33:52 +00007259 flags = 0;
7260 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007261
Bram Moolenaar7887d882005-07-01 22:33:52 +00007262 /* Check for flags and region after a slash. */
7263 p = vim_strchr(line, '/');
7264 if (p != NULL)
7265 {
7266 *p++ = NUL;
7267 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007268 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007269 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007270 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007271 else if (*p == '!') /* Bad, bad, wicked word. */
7272 flags |= WF_BANNED;
7273 else if (*p == '?') /* Rare word. */
7274 flags |= WF_RARE;
7275 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007276 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007277 if ((flags & WF_REGION) == 0) /* first one */
7278 regionmask = 0;
7279 flags |= WF_REGION;
7280
7281 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007282 if (l > spin->si_region_count)
7283 {
7284 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007285 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007286 break;
7287 }
7288 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007289 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007290 else
7291 {
7292 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7293 fname, lnum, p);
7294 break;
7295 }
7296 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007297 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007298 }
7299
7300 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7301 if (spin->si_ascii && has_non_ascii(line))
7302 {
7303 ++non_ascii;
7304 continue;
7305 }
7306
7307 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007308 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007309 {
7310 retval = FAIL;
7311 break;
7312 }
7313 did_word = TRUE;
7314 }
7315
7316 vim_free(pc);
7317 fclose(fd);
7318
Bram Moolenaar4770d092006-01-12 23:22:24 +00007319 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007320 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007321 vim_snprintf((char *)IObuff, IOSIZE,
7322 _("Ignored %d words with non-ASCII characters"), non_ascii);
7323 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007324 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007325
Bram Moolenaar51485f02005-06-04 21:55:20 +00007326 return retval;
7327}
7328
7329/*
7330 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007331 * This avoids calling free() for every little struct we use (and keeping
7332 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007333 * The memory is cleared to all zeros.
7334 * Returns NULL when out of memory.
7335 */
7336 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007337getroom(spin, len, align)
7338 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007339 size_t len; /* length needed */
7340 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007341{
7342 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007343 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007344
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007345 if (align && bl != NULL)
7346 /* Round size up for alignment. On some systems structures need to be
7347 * aligned to the size of a pointer (e.g., SPARC). */
7348 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7349 & ~(sizeof(char *) - 1);
7350
Bram Moolenaar51485f02005-06-04 21:55:20 +00007351 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7352 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007353 if (len >= SBLOCKSIZE)
7354 bl = NULL;
7355 else
7356 /* Allocate a block of memory. It is not freed until much later. */
7357 bl = (sblock_T *)alloc_clear(
7358 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007359 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007360 {
7361 if (!spin->si_did_emsg)
7362 {
7363 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7364 spin->si_did_emsg = TRUE;
7365 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007366 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007367 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007368 bl->sb_next = spin->si_blocks;
7369 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007370 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007371 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007372 }
7373
7374 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007375 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007376
7377 return p;
7378}
7379
7380/*
7381 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007382 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007383 */
7384 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007385getroom_save(spin, s)
7386 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007387 char_u *s;
7388{
7389 char_u *sc;
7390
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007391 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007392 if (sc != NULL)
7393 STRCPY(sc, s);
7394 return sc;
7395}
7396
7397
7398/*
7399 * Free the list of allocated sblock_T.
7400 */
7401 static void
7402free_blocks(bl)
7403 sblock_T *bl;
7404{
7405 sblock_T *next;
7406
7407 while (bl != NULL)
7408 {
7409 next = bl->sb_next;
7410 vim_free(bl);
7411 bl = next;
7412 }
7413}
7414
7415/*
7416 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007417 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007418 */
7419 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007420wordtree_alloc(spin)
7421 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007422{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007423 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007424}
7425
7426/*
7427 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007428 * Always store it in the case-folded tree. For a keep-case word this is
7429 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7430 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007431 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007432 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7433 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007434 */
7435 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007436store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007437 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007438 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007439 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007440 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007441 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007442 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007443{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007444 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007445 int ct = captype(word, word + len);
7446 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007447 int res = OK;
7448 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007450 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007451 for (p = pfxlist; res == OK; ++p)
7452 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007453 if (!need_affix || (p != NULL && *p != NUL))
7454 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007455 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007456 if (p == NULL || *p == NUL)
7457 break;
7458 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007459 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007460
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007461 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007462 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007463 for (p = pfxlist; res == OK; ++p)
7464 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007465 if (!need_affix || (p != NULL && *p != NUL))
7466 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007467 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007468 if (p == NULL || *p == NUL)
7469 break;
7470 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007471 ++spin->si_keepwcount;
7472 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007473 return res;
7474}
7475
7476/*
7477 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007478 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007479 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007480 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007481 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007482 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007483tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007484 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007485 char_u *word;
7486 wordnode_T *root;
7487 int flags;
7488 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007489 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007490{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007491 wordnode_T *node = root;
7492 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007493 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007494 wordnode_T **prev = NULL;
7495 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007496
Bram Moolenaar51485f02005-06-04 21:55:20 +00007497 /* Add each byte of the word to the tree, including the NUL at the end. */
7498 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007499 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007500 /* When there is more than one reference to this node we need to make
7501 * a copy, so that we can modify it. Copy the whole list of siblings
7502 * (we don't optimize for a partly shared list of siblings). */
7503 if (node != NULL && node->wn_refs > 1)
7504 {
7505 --node->wn_refs;
7506 copyprev = prev;
7507 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7508 {
7509 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007510 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007511 if (np == NULL)
7512 return FAIL;
7513 np->wn_child = copyp->wn_child;
7514 if (np->wn_child != NULL)
7515 ++np->wn_child->wn_refs; /* child gets extra ref */
7516 np->wn_byte = copyp->wn_byte;
7517 if (np->wn_byte == NUL)
7518 {
7519 np->wn_flags = copyp->wn_flags;
7520 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007521 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007522 }
7523
7524 /* Link the new node in the list, there will be one ref. */
7525 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007526 if (copyprev != NULL)
7527 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007528 copyprev = &np->wn_sibling;
7529
7530 /* Let "node" point to the head of the copied list. */
7531 if (copyp == node)
7532 node = np;
7533 }
7534 }
7535
Bram Moolenaar51485f02005-06-04 21:55:20 +00007536 /* Look for the sibling that has the same character. They are sorted
7537 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007538 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007539 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007540 while (node != NULL
7541 && (node->wn_byte < word[i]
7542 || (node->wn_byte == NUL
7543 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007544 ? node->wn_affixID < (unsigned)affixID
7545 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007546 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007547 && (spin->si_sugtree
7548 ? (node->wn_region & 0xffff) < region
7549 : node->wn_affixID
7550 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007551 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007552 prev = &node->wn_sibling;
7553 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007554 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007555 if (node == NULL
7556 || node->wn_byte != word[i]
7557 || (word[i] == NUL
7558 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007559 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007560 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007561 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007562 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007563 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007564 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007565 if (np == NULL)
7566 return FAIL;
7567 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007568
7569 /* If "node" is NULL this is a new child or the end of the sibling
7570 * list: ref count is one. Otherwise use ref count of sibling and
7571 * make ref count of sibling one (matters when inserting in front
7572 * of the list of siblings). */
7573 if (node == NULL)
7574 np->wn_refs = 1;
7575 else
7576 {
7577 np->wn_refs = node->wn_refs;
7578 node->wn_refs = 1;
7579 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007580 if (prev != NULL)
7581 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007582 np->wn_sibling = node;
7583 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007584 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007585
Bram Moolenaar51485f02005-06-04 21:55:20 +00007586 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007587 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007588 node->wn_flags = flags;
7589 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007590 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007591 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007592 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007593 prev = &node->wn_child;
7594 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007595 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007596#ifdef SPELL_PRINTTREE
7597 smsg("Added \"%s\"", word);
7598 spell_print_tree(root->wn_sibling);
7599#endif
7600
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007601 /* count nr of words added since last message */
7602 ++spin->si_msg_count;
7603
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007604 if (spin->si_compress_cnt > 1)
7605 {
7606 if (--spin->si_compress_cnt == 1)
7607 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007608 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007609 }
7610
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007611 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007612 * When we have allocated lots of memory we need to compress the word tree
7613 * to free up some room. But compression is slow, and we might actually
7614 * need that room, thus only compress in the following situations:
7615 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007616 * "compress_start" blocks.
7617 * 2. When compressed before and used "compress_inc" blocks before
7618 * adding "compress_added" words (si_compress_cnt > 1).
7619 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007620 * (si_compress_cnt == 1) and the number of free nodes drops below the
7621 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007622 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007623#ifndef SPELL_PRINTTREE
7624 if (spin->si_compress_cnt == 1
7625 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007626 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007627#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007628 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007629 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007630 * when the freed up room has been used and another "compress_inc"
7631 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007632 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007633 spin->si_blocks_cnt -= compress_inc;
7634 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007635
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007636 if (spin->si_verbose)
7637 {
7638 msg_start();
7639 msg_puts((char_u *)_(msg_compressing));
7640 msg_clr_eos();
7641 msg_didout = FALSE;
7642 msg_col = 0;
7643 out_flush();
7644 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007645
7646 /* Compress both trees. Either they both have many nodes, which makes
7647 * compression useful, or one of them is small, which means
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007648 * compression goes fast. But when filling the soundfold word tree
Bram Moolenaar4770d092006-01-12 23:22:24 +00007649 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007650 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007651 if (affixID >= 0)
7652 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007653 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007654
7655 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007656}
7657
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007658/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007659 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7660 * Sets "sps_flags".
7661 */
7662 int
7663spell_check_msm()
7664{
7665 char_u *p = p_msm;
7666 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007667 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007668 long added = 0;
7669
7670 if (!VIM_ISDIGIT(*p))
7671 return FAIL;
7672 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7673 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7674 if (*p != ',')
7675 return FAIL;
7676 ++p;
7677 if (!VIM_ISDIGIT(*p))
7678 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007679 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007680 if (*p != ',')
7681 return FAIL;
7682 ++p;
7683 if (!VIM_ISDIGIT(*p))
7684 return FAIL;
7685 added = getdigits(&p) * 1024;
7686 if (*p != NUL)
7687 return FAIL;
7688
Bram Moolenaar89d40322006-08-29 15:30:07 +00007689 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007690 return FAIL;
7691
7692 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007693 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007694 compress_added = added;
7695 return OK;
7696}
7697
7698
7699/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007700 * Get a wordnode_T, either from the list of previously freed nodes or
7701 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007702 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007703 */
7704 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007705get_wordnode(spin)
7706 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007707{
7708 wordnode_T *n;
7709
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007710 if (spin->si_first_free == NULL)
7711 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007712 else
7713 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007714 n = spin->si_first_free;
7715 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007716 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007717 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007718 }
7719#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007720 if (n != NULL)
7721 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007722#endif
7723 return n;
7724}
7725
7726/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007727 * Decrement the reference count on a node (which is the head of a list of
7728 * siblings). If the reference count becomes zero free the node and its
7729 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007730 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007731 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007732 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007733deref_wordnode(spin, node)
7734 spellinfo_T *spin;
7735 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007736{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007737 wordnode_T *np;
7738 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007739
7740 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007741 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007742 for (np = node; np != NULL; np = np->wn_sibling)
7743 {
7744 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007745 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007746 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007747 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007748 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007749 ++cnt; /* length field */
7750 }
7751 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007752}
7753
7754/*
7755 * Free a wordnode_T for re-use later.
7756 * Only the "wn_child" field becomes invalid.
7757 */
7758 static void
7759free_wordnode(spin, n)
7760 spellinfo_T *spin;
7761 wordnode_T *n;
7762{
7763 n->wn_child = spin->si_first_free;
7764 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007765 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007766}
7767
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007768/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007769 * Compress a tree: find tails that are identical and can be shared.
7770 */
7771 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007772wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007773 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007774 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007775{
7776 hashtab_T ht;
7777 int n;
7778 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007779 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007780
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007781 /* Skip the root itself, it's not actually used. The first sibling is the
7782 * start of the tree. */
7783 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007784 {
7785 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007786 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007787
7788#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007789 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007790#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007791 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007792 if (tot > 1000000)
7793 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007794 else if (tot == 0)
7795 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007796 else
7797 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007798 vim_snprintf((char *)IObuff, IOSIZE,
7799 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7800 n, tot, tot - n, perc);
7801 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007802 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007803#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007804 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007805#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007806 hash_clear(&ht);
7807 }
7808}
7809
7810/*
7811 * Compress a node, its siblings and its children, depth first.
7812 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007813 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007814 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007815node_compress(spin, node, ht, tot)
7816 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007817 wordnode_T *node;
7818 hashtab_T *ht;
7819 int *tot; /* total count of nodes before compressing,
7820 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007821{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007822 wordnode_T *np;
7823 wordnode_T *tp;
7824 wordnode_T *child;
7825 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007826 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007827 int len = 0;
7828 unsigned nr, n;
7829 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007830
Bram Moolenaar51485f02005-06-04 21:55:20 +00007831 /*
7832 * Go through the list of siblings. Compress each child and then try
7833 * finding an identical child to replace it.
7834 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007835 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007836 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007837 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007838 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 ++len;
7840 if ((child = np->wn_child) != NULL)
7841 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007842 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007843 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007844
7845 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007846 hash = hash_hash(child->wn_u1.hashkey);
7847 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007848 if (!HASHITEM_EMPTY(hi))
7849 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007850 /* There are children we encountered before with a hash value
7851 * identical to the current child. Now check if there is one
7852 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007853 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007854 if (node_equal(child, tp))
7855 {
7856 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007857 * current one. This means the current child and all
7858 * its siblings is unlinked from the tree. */
7859 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007860 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007861 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007862 break;
7863 }
7864 if (tp == NULL)
7865 {
7866 /* No other child with this hash value equals the child of
7867 * the node, add it to the linked list after the first
7868 * item. */
7869 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007870 child->wn_u2.next = tp->wn_u2.next;
7871 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007872 }
7873 }
7874 else
7875 /* No other child has this hash value, add it to the
7876 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007877 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007878 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007879 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007880 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007881
7882 /*
7883 * Make a hash key for the node and its siblings, so that we can quickly
7884 * find a lookalike node. This must be done after compressing the sibling
7885 * list, otherwise the hash key would become invalid by the compression.
7886 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007887 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007888 nr = 0;
7889 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007890 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007891 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007892 /* end node: use wn_flags, wn_region and wn_affixID */
7893 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007894 else
7895 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007896 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007897 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007898 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007899
7900 /* Avoid NUL bytes, it terminates the hash key. */
7901 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007902 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007903 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007904 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007905 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007906 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007907 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007908 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7909 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007910
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007911 /* Check for CTRL-C pressed now and then. */
7912 fast_breakcheck();
7913
Bram Moolenaar51485f02005-06-04 21:55:20 +00007914 return compressed;
7915}
7916
7917/*
7918 * Return TRUE when two nodes have identical siblings and children.
7919 */
7920 static int
7921node_equal(n1, n2)
7922 wordnode_T *n1;
7923 wordnode_T *n2;
7924{
7925 wordnode_T *p1;
7926 wordnode_T *p2;
7927
7928 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7929 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7930 if (p1->wn_byte != p2->wn_byte
7931 || (p1->wn_byte == NUL
7932 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007933 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007934 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007935 : (p1->wn_child != p2->wn_child)))
7936 break;
7937
7938 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007939}
7940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007941static int
7942#ifdef __BORLANDC__
7943_RTLENTRYF
7944#endif
7945rep_compare __ARGS((const void *s1, const void *s2));
7946
7947/*
7948 * Function given to qsort() to sort the REP items on "from" string.
7949 */
7950 static int
7951#ifdef __BORLANDC__
7952_RTLENTRYF
7953#endif
7954rep_compare(s1, s2)
7955 const void *s1;
7956 const void *s2;
7957{
7958 fromto_T *p1 = (fromto_T *)s1;
7959 fromto_T *p2 = (fromto_T *)s2;
7960
7961 return STRCMP(p1->ft_from, p2->ft_from);
7962}
7963
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007964/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007965 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007966 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007967 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007968 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007969write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007970 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007971 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007972{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007973 FILE *fd;
7974 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007975 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007976 wordnode_T *tree;
7977 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007978 int i;
7979 int l;
7980 garray_T *gap;
7981 fromto_T *ftp;
7982 char_u *p;
7983 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007984 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007985 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007986 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007987
Bram Moolenaarb765d632005-06-07 21:00:02 +00007988 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007989 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007990 {
7991 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007992 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007993 }
7994
Bram Moolenaar5195e452005-08-19 20:32:47 +00007995 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007996 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007997 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007998 if (fwv != (size_t)1)
7999 /* Catch first write error, don't try writing more. */
8000 goto theend;
8001
Bram Moolenaar5195e452005-08-19 20:32:47 +00008002 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008003
Bram Moolenaar5195e452005-08-19 20:32:47 +00008004 /*
8005 * <SECTIONS>: <section> ... <sectionend>
8006 */
8007
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008008 /* SN_INFO: <infotext> */
8009 if (spin->si_info != NULL)
8010 {
8011 putc(SN_INFO, fd); /* <sectionID> */
8012 putc(0, fd); /* <sectionflags> */
8013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008014 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008015 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008016 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008017 }
8018
Bram Moolenaar5195e452005-08-19 20:32:47 +00008019 /* SN_REGION: <regionname> ...
8020 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008021 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008022 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008023 putc(SN_REGION, fd); /* <sectionID> */
8024 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8025 l = spin->si_region_count * 2;
8026 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008027 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008028 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008029 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008030 }
8031 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008032 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008033
Bram Moolenaar5195e452005-08-19 20:32:47 +00008034 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8035 *
8036 * The table with character flags and the table for case folding.
8037 * This makes sure the same characters are recognized as word characters
8038 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008039 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008040 * 'encoding'.
8041 * Also skip this for an .add.spl file, the main spell file must contain
8042 * the table (avoids that it conflicts). File is shorter too.
8043 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008044 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008045 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008046 char_u folchars[128 * 8];
8047 int flags;
8048
Bram Moolenaard12a1322005-08-21 22:08:24 +00008049 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008050 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8051
8052 /* Form the <folchars> string first, we need to know its length. */
8053 l = 0;
8054 for (i = 128; i < 256; ++i)
8055 {
8056#ifdef FEAT_MBYTE
8057 if (has_mbyte)
8058 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8059 else
8060#endif
8061 folchars[l++] = spelltab.st_fold[i];
8062 }
8063 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8064
8065 fputc(128, fd); /* <charflagslen> */
8066 for (i = 128; i < 256; ++i)
8067 {
8068 flags = 0;
8069 if (spelltab.st_isw[i])
8070 flags |= CF_WORD;
8071 if (spelltab.st_isu[i])
8072 flags |= CF_UPPER;
8073 fputc(flags, fd); /* <charflags> */
8074 }
8075
8076 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008077 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008078 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008079
Bram Moolenaar5195e452005-08-19 20:32:47 +00008080 /* SN_MIDWORD: <midword> */
8081 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008082 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008083 putc(SN_MIDWORD, fd); /* <sectionID> */
8084 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8085
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008086 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008087 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008088 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8089 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008090 }
8091
Bram Moolenaar5195e452005-08-19 20:32:47 +00008092 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8093 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008094 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008095 putc(SN_PREFCOND, fd); /* <sectionID> */
8096 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8097
8098 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8099 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8100
8101 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008102 }
8103
Bram Moolenaar5195e452005-08-19 20:32:47 +00008104 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008105 * SN_SAL: <salflags> <salcount> <sal> ...
8106 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008107
Bram Moolenaar5195e452005-08-19 20:32:47 +00008108 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008109 * round 2: SN_SAL section (unless SN_SOFO is used)
8110 * round 3: SN_REPSAL section */
8111 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112 {
8113 if (round == 1)
8114 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008115 else if (round == 2)
8116 {
8117 /* Don't write SN_SAL when using a SN_SOFO section */
8118 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8119 continue;
8120 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008121 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008123 gap = &spin->si_repsal;
8124
8125 /* Don't write the section if there are no items. */
8126 if (gap->ga_len == 0)
8127 continue;
8128
8129 /* Sort the REP/REPSAL items. */
8130 if (round != 2)
8131 qsort(gap->ga_data, (size_t)gap->ga_len,
8132 sizeof(fromto_T), rep_compare);
8133
8134 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8135 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008136
Bram Moolenaar5195e452005-08-19 20:32:47 +00008137 /* This is for making suggestions, section is not required. */
8138 putc(0, fd); /* <sectionflags> */
8139
8140 /* Compute the length of what follows. */
8141 l = 2; /* count <repcount> or <salcount> */
8142 for (i = 0; i < gap->ga_len; ++i)
8143 {
8144 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008145 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8146 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008147 }
8148 if (round == 2)
8149 ++l; /* count <salflags> */
8150 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8151
8152 if (round == 2)
8153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154 i = 0;
8155 if (spin->si_followup)
8156 i |= SAL_F0LLOWUP;
8157 if (spin->si_collapse)
8158 i |= SAL_COLLAPSE;
8159 if (spin->si_rem_accents)
8160 i |= SAL_REM_ACCENTS;
8161 putc(i, fd); /* <salflags> */
8162 }
8163
8164 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8165 for (i = 0; i < gap->ga_len; ++i)
8166 {
8167 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8168 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8169 ftp = &((fromto_T *)gap->ga_data)[i];
8170 for (rr = 1; rr <= 2; ++rr)
8171 {
8172 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008173 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008175 if (l > 0)
8176 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177 }
8178 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008180 }
8181
Bram Moolenaar5195e452005-08-19 20:32:47 +00008182 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8183 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008184 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8185 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008186 putc(SN_SOFO, fd); /* <sectionID> */
8187 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008188
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008189 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008190 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8191 /* <sectionlen> */
8192
8193 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008194 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008196 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008197 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008198 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008199 }
8200
Bram Moolenaar4770d092006-01-12 23:22:24 +00008201 /* SN_WORDS: <word> ...
8202 * This is for making suggestions, section is not required. */
8203 if (spin->si_commonwords.ht_used > 0)
8204 {
8205 putc(SN_WORDS, fd); /* <sectionID> */
8206 putc(0, fd); /* <sectionflags> */
8207
8208 /* round 1: count the bytes
8209 * round 2: write the bytes */
8210 for (round = 1; round <= 2; ++round)
8211 {
8212 int todo;
8213 int len = 0;
8214 hashitem_T *hi;
8215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008216 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008217 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8218 if (!HASHITEM_EMPTY(hi))
8219 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008220 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008221 len += l;
8222 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008223 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008224 --todo;
8225 }
8226 if (round == 1)
8227 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8228 }
8229 }
8230
Bram Moolenaar5195e452005-08-19 20:32:47 +00008231 /* SN_MAP: <mapstr>
8232 * This is for making suggestions, section is not required. */
8233 if (spin->si_map.ga_len > 0)
8234 {
8235 putc(SN_MAP, fd); /* <sectionID> */
8236 putc(0, fd); /* <sectionflags> */
8237 l = spin->si_map.ga_len;
8238 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008239 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008240 /* <mapstr> */
8241 }
8242
Bram Moolenaar4770d092006-01-12 23:22:24 +00008243 /* SN_SUGFILE: <timestamp>
8244 * This is used to notify that a .sug file may be available and at the
8245 * same time allows for checking that a .sug file that is found matches
8246 * with this .spl file. That's because the word numbers must be exactly
8247 * right. */
8248 if (!spin->si_nosugfile
8249 && (spin->si_sal.ga_len > 0
8250 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8251 {
8252 putc(SN_SUGFILE, fd); /* <sectionID> */
8253 putc(0, fd); /* <sectionflags> */
8254 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8255
8256 /* Set si_sugtime and write it to the file. */
8257 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008258 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008259 }
8260
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008261 /* SN_NOSPLITSUGS: nothing
8262 * This is used to notify that no suggestions with word splits are to be
8263 * made. */
8264 if (spin->si_nosplitsugs)
8265 {
8266 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8267 putc(0, fd); /* <sectionflags> */
8268 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8269 }
8270
Bram Moolenaar5195e452005-08-19 20:32:47 +00008271 /* SN_COMPOUND: compound info.
8272 * We don't mark it required, when not supported all compound words will
8273 * be bad words. */
8274 if (spin->si_compflags != NULL)
8275 {
8276 putc(SN_COMPOUND, fd); /* <sectionID> */
8277 putc(0, fd); /* <sectionflags> */
8278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008279 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008280 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008281 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008282 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8283
Bram Moolenaar5195e452005-08-19 20:32:47 +00008284 putc(spin->si_compmax, fd); /* <compmax> */
8285 putc(spin->si_compminlen, fd); /* <compminlen> */
8286 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008287 putc(0, fd); /* for Vim 7.0b compatibility */
8288 putc(spin->si_compoptions, fd); /* <compoptions> */
8289 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8290 /* <comppatcount> */
8291 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8292 {
8293 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008294 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008295 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8296 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008297 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008298 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008299 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008300 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008301 }
8302
Bram Moolenaar78622822005-08-23 21:00:13 +00008303 /* SN_NOBREAK: NOBREAK flag */
8304 if (spin->si_nobreak)
8305 {
8306 putc(SN_NOBREAK, fd); /* <sectionID> */
8307 putc(0, fd); /* <sectionflags> */
8308
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008309 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008310 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8311 }
8312
Bram Moolenaar5195e452005-08-19 20:32:47 +00008313 /* SN_SYLLABLE: syllable info.
8314 * We don't mark it required, when not supported syllables will not be
8315 * counted. */
8316 if (spin->si_syllable != NULL)
8317 {
8318 putc(SN_SYLLABLE, fd); /* <sectionID> */
8319 putc(0, fd); /* <sectionflags> */
8320
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008321 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008322 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008323 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8324 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008325 }
8326
8327 /* end of <SECTIONS> */
8328 putc(SN_END, fd); /* <sectionend> */
8329
Bram Moolenaar50cde822005-06-05 21:54:54 +00008330
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008331 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008332 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008333 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008335 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008336 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008337 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008338 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008339 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008340 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008341 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008342 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008343
Bram Moolenaar0c405862005-06-22 22:26:26 +00008344 /* Clear the index and wnode fields in the tree. */
8345 clear_node(tree);
8346
Bram Moolenaar51485f02005-06-04 21:55:20 +00008347 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008348 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008349 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008350 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008351
Bram Moolenaar51485f02005-06-04 21:55:20 +00008352 /* number of nodes in 4 bytes */
8353 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008354 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008355
Bram Moolenaar51485f02005-06-04 21:55:20 +00008356 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008357 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358 }
8359
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008360 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008361 if (putc(0, fd) == EOF)
8362 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008363theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008364 if (fclose(fd) == EOF)
8365 retval = FAIL;
8366
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008367 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008368 retval = FAIL;
8369 if (retval == FAIL)
8370 EMSG(_(e_write));
8371
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008372 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008373}
8374
8375/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008376 * Clear the index and wnode fields of "node", it siblings and its
8377 * children. This is needed because they are a union with other items to save
8378 * space.
8379 */
8380 static void
8381clear_node(node)
8382 wordnode_T *node;
8383{
8384 wordnode_T *np;
8385
8386 if (node != NULL)
8387 for (np = node; np != NULL; np = np->wn_sibling)
8388 {
8389 np->wn_u1.index = 0;
8390 np->wn_u2.wnode = NULL;
8391
8392 if (np->wn_byte != NUL)
8393 clear_node(np->wn_child);
8394 }
8395}
8396
8397
8398/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008399 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008400 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008401 * This first writes the list of possible bytes (siblings). Then for each
8402 * byte recursively write the children.
8403 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008404 * NOTE: The code here must match the code in read_tree_node(), since
8405 * assumptions are made about the indexes (so that we don't have to write them
8406 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008407 *
8408 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008409 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008410 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008411put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008412 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008413 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008414 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008415 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008416 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008417{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008418 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008419 int siblingcount = 0;
8420 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008421 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008422
Bram Moolenaar51485f02005-06-04 21:55:20 +00008423 /* If "node" is zero the tree is empty. */
8424 if (node == NULL)
8425 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008426
Bram Moolenaar51485f02005-06-04 21:55:20 +00008427 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008428 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008429
8430 /* Count the number of siblings. */
8431 for (np = node; np != NULL; np = np->wn_sibling)
8432 ++siblingcount;
8433
8434 /* Write the sibling count. */
8435 if (fd != NULL)
8436 putc(siblingcount, fd); /* <siblingcount> */
8437
8438 /* Write each sibling byte and optionally extra info. */
8439 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008440 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008441 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008442 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008443 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008444 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008445 /* For a NUL byte (end of word) write the flags etc. */
8446 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008447 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008448 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008449 * associated condition nr (stored in wn_region). The
8450 * byte value is misused to store the "rare" and "not
8451 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008452 if (np->wn_flags == (short_u)PFX_FLAGS)
8453 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008454 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008455 {
8456 putc(BY_FLAGS, fd); /* <byte> */
8457 putc(np->wn_flags, fd); /* <pflags> */
8458 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008459 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008460 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008461 }
8462 else
8463 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008464 /* For word trees we write the flag/region items. */
8465 flags = np->wn_flags;
8466 if (regionmask != 0 && np->wn_region != regionmask)
8467 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008468 if (np->wn_affixID != 0)
8469 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008470 if (flags == 0)
8471 {
8472 /* word without flags or region */
8473 putc(BY_NOFLAGS, fd); /* <byte> */
8474 }
8475 else
8476 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008477 if (np->wn_flags >= 0x100)
8478 {
8479 putc(BY_FLAGS2, fd); /* <byte> */
8480 putc(flags, fd); /* <flags> */
8481 putc((unsigned)flags >> 8, fd); /* <flags2> */
8482 }
8483 else
8484 {
8485 putc(BY_FLAGS, fd); /* <byte> */
8486 putc(flags, fd); /* <flags> */
8487 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008488 if (flags & WF_REGION)
8489 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008490 if (flags & WF_AFX)
8491 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008492 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008493 }
8494 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008495 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008496 else
8497 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008498 if (np->wn_child->wn_u1.index != 0
8499 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008500 {
8501 /* The child is written elsewhere, write the reference. */
8502 if (fd != NULL)
8503 {
8504 putc(BY_INDEX, fd); /* <byte> */
8505 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008506 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008507 }
8508 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008509 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008510 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008511 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008512
Bram Moolenaar51485f02005-06-04 21:55:20 +00008513 if (fd != NULL)
8514 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8515 {
8516 EMSG(_(e_write));
8517 return 0;
8518 }
8519 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008520 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008521
8522 /* Space used in the array when reading: one for each sibling and one for
8523 * the count. */
8524 newindex += siblingcount + 1;
8525
8526 /* Recursively dump the children of each sibling. */
8527 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008528 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8529 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008530 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008531
8532 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008533}
8534
8535
8536/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008537 * ":mkspell [-ascii] outfile infile ..."
8538 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008539 */
8540 void
8541ex_mkspell(eap)
8542 exarg_T *eap;
8543{
8544 int fcount;
8545 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008546 char_u *arg = eap->arg;
8547 int ascii = FALSE;
8548
8549 if (STRNCMP(arg, "-ascii", 6) == 0)
8550 {
8551 ascii = TRUE;
8552 arg = skipwhite(arg + 6);
8553 }
8554
8555 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02008556 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008559 FreeWild(fcount, fnames);
8560 }
8561}
8562
8563/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008564 * Create the .sug file.
8565 * Uses the soundfold info in "spin".
8566 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8567 */
8568 static void
8569spell_make_sugfile(spin, wfname)
8570 spellinfo_T *spin;
8571 char_u *wfname;
8572{
Bram Moolenaard9462e32011-04-11 21:35:11 +02008573 char_u *fname = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008574 int len;
8575 slang_T *slang;
8576 int free_slang = FALSE;
8577
8578 /*
8579 * Read back the .spl file that was written. This fills the required
8580 * info for soundfolding. This also uses less memory than the
8581 * pointer-linked version of the trie. And it avoids having two versions
8582 * of the code for the soundfolding stuff.
8583 * It might have been done already by spell_reload_one().
8584 */
8585 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8586 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8587 break;
8588 if (slang == NULL)
8589 {
8590 spell_message(spin, (char_u *)_("Reading back spell file..."));
8591 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8592 if (slang == NULL)
8593 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008594 free_slang = TRUE;
8595 }
8596
8597 /*
8598 * Clear the info in "spin" that is used.
8599 */
8600 spin->si_blocks = NULL;
8601 spin->si_blocks_cnt = 0;
8602 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8603 spin->si_free_count = 0;
8604 spin->si_first_free = NULL;
8605 spin->si_foldwcount = 0;
8606
8607 /*
8608 * Go through the trie of good words, soundfold each word and add it to
8609 * the soundfold trie.
8610 */
8611 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8612 if (sug_filltree(spin, slang) == FAIL)
8613 goto theend;
8614
8615 /*
8616 * Create the table which links each soundfold word with a list of the
8617 * good words it may come from. Creates buffer "spin->si_spellbuf".
8618 * This also removes the wordnr from the NUL byte entries to make
8619 * compression possible.
8620 */
8621 if (sug_maketable(spin) == FAIL)
8622 goto theend;
8623
8624 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8625 (long)spin->si_spellbuf->b_ml.ml_line_count);
8626
8627 /*
8628 * Compress the soundfold trie.
8629 */
8630 spell_message(spin, (char_u *)_(msg_compressing));
8631 wordtree_compress(spin, spin->si_foldroot);
8632
8633 /*
8634 * Write the .sug file.
8635 * Make the file name by changing ".spl" to ".sug".
8636 */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008637 fname = alloc(MAXPATHL);
8638 if (fname == NULL)
8639 goto theend;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008640 vim_strncpy(fname, wfname, MAXPATHL - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008641 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008642 fname[len - 2] = 'u';
8643 fname[len - 1] = 'g';
8644 sug_write(spin, fname);
8645
8646theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02008647 vim_free(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008648 if (free_slang)
8649 slang_free(slang);
8650 free_blocks(spin->si_blocks);
8651 close_spellbuf(spin->si_spellbuf);
8652}
8653
8654/*
8655 * Build the soundfold trie for language "slang".
8656 */
8657 static int
8658sug_filltree(spin, slang)
8659 spellinfo_T *spin;
8660 slang_T *slang;
8661{
8662 char_u *byts;
8663 idx_T *idxs;
8664 int depth;
8665 idx_T arridx[MAXWLEN];
8666 int curi[MAXWLEN];
8667 char_u tword[MAXWLEN];
8668 char_u tsalword[MAXWLEN];
8669 int c;
8670 idx_T n;
8671 unsigned words_done = 0;
8672 int wordcount[MAXWLEN];
8673
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008674 /* We use si_foldroot for the soundfolded trie. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008675 spin->si_foldroot = wordtree_alloc(spin);
8676 if (spin->si_foldroot == NULL)
8677 return FAIL;
8678
8679 /* let tree_add_word() know we're adding to the soundfolded tree */
8680 spin->si_sugtree = TRUE;
8681
8682 /*
8683 * Go through the whole case-folded tree, soundfold each word and put it
8684 * in the trie.
8685 */
8686 byts = slang->sl_fbyts;
8687 idxs = slang->sl_fidxs;
8688
8689 arridx[0] = 0;
8690 curi[0] = 1;
8691 wordcount[0] = 0;
8692
8693 depth = 0;
8694 while (depth >= 0 && !got_int)
8695 {
8696 if (curi[depth] > byts[arridx[depth]])
8697 {
8698 /* Done all bytes at this node, go up one level. */
8699 idxs[arridx[depth]] = wordcount[depth];
8700 if (depth > 0)
8701 wordcount[depth - 1] += wordcount[depth];
8702
8703 --depth;
8704 line_breakcheck();
8705 }
8706 else
8707 {
8708
8709 /* Do one more byte at this node. */
8710 n = arridx[depth] + curi[depth];
8711 ++curi[depth];
8712
8713 c = byts[n];
8714 if (c == 0)
8715 {
8716 /* Sound-fold the word. */
8717 tword[depth] = NUL;
8718 spell_soundfold(slang, tword, TRUE, tsalword);
8719
8720 /* We use the "flags" field for the MSB of the wordnr,
8721 * "region" for the LSB of the wordnr. */
8722 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8723 words_done >> 16, words_done & 0xffff,
8724 0) == FAIL)
8725 return FAIL;
8726
8727 ++words_done;
8728 ++wordcount[depth];
8729
8730 /* Reset the block count each time to avoid compression
8731 * kicking in. */
8732 spin->si_blocks_cnt = 0;
8733
8734 /* Skip over any other NUL bytes (same word with different
8735 * flags). */
8736 while (byts[n + 1] == 0)
8737 {
8738 ++n;
8739 ++curi[depth];
8740 }
8741 }
8742 else
8743 {
8744 /* Normal char, go one level deeper. */
8745 tword[depth++] = c;
8746 arridx[depth] = idxs[n];
8747 curi[depth] = 1;
8748 wordcount[depth] = 0;
8749 }
8750 }
8751 }
8752
8753 smsg((char_u *)_("Total number of words: %d"), words_done);
8754
8755 return OK;
8756}
8757
8758/*
8759 * Make the table that links each word in the soundfold trie to the words it
8760 * can be produced from.
8761 * This is not unlike lines in a file, thus use a memfile to be able to access
8762 * the table efficiently.
8763 * Returns FAIL when out of memory.
8764 */
8765 static int
8766sug_maketable(spin)
8767 spellinfo_T *spin;
8768{
8769 garray_T ga;
8770 int res = OK;
8771
8772 /* Allocate a buffer, open a memline for it and create the swap file
8773 * (uses a temp file, not a .swp file). */
8774 spin->si_spellbuf = open_spellbuf();
8775 if (spin->si_spellbuf == NULL)
8776 return FAIL;
8777
8778 /* Use a buffer to store the line info, avoids allocating many small
8779 * pieces of memory. */
8780 ga_init2(&ga, 1, 100);
8781
8782 /* recursively go through the tree */
8783 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8784 res = FAIL;
8785
8786 ga_clear(&ga);
8787 return res;
8788}
8789
8790/*
8791 * Fill the table for one node and its children.
8792 * Returns the wordnr at the start of the node.
8793 * Returns -1 when out of memory.
8794 */
8795 static int
8796sug_filltable(spin, node, startwordnr, gap)
8797 spellinfo_T *spin;
8798 wordnode_T *node;
8799 int startwordnr;
8800 garray_T *gap; /* place to store line of numbers */
8801{
8802 wordnode_T *p, *np;
8803 int wordnr = startwordnr;
8804 int nr;
8805 int prev_nr;
8806
8807 for (p = node; p != NULL; p = p->wn_sibling)
8808 {
8809 if (p->wn_byte == NUL)
8810 {
8811 gap->ga_len = 0;
8812 prev_nr = 0;
8813 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8814 {
8815 if (ga_grow(gap, 10) == FAIL)
8816 return -1;
8817
8818 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8819 /* Compute the offset from the previous nr and store the
8820 * offset in a way that it takes a minimum number of bytes.
8821 * It's a bit like utf-8, but without the need to mark
8822 * following bytes. */
8823 nr -= prev_nr;
8824 prev_nr += nr;
8825 gap->ga_len += offset2bytes(nr,
8826 (char_u *)gap->ga_data + gap->ga_len);
8827 }
8828
8829 /* add the NUL byte */
8830 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8831
8832 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8833 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8834 return -1;
8835 ++wordnr;
8836
8837 /* Remove extra NUL entries, we no longer need them. We don't
8838 * bother freeing the nodes, the won't be reused anyway. */
8839 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8840 p->wn_sibling = p->wn_sibling->wn_sibling;
8841
8842 /* Clear the flags on the remaining NUL node, so that compression
8843 * works a lot better. */
8844 p->wn_flags = 0;
8845 p->wn_region = 0;
8846 }
8847 else
8848 {
8849 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8850 if (wordnr == -1)
8851 return -1;
8852 }
8853 }
8854 return wordnr;
8855}
8856
8857/*
8858 * Convert an offset into a minimal number of bytes.
8859 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8860 * bytes.
8861 */
8862 static int
8863offset2bytes(nr, buf)
8864 int nr;
8865 char_u *buf;
8866{
8867 int rem;
8868 int b1, b2, b3, b4;
8869
8870 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8871 b1 = nr % 255 + 1;
8872 rem = nr / 255;
8873 b2 = rem % 255 + 1;
8874 rem = rem / 255;
8875 b3 = rem % 255 + 1;
8876 b4 = rem / 255 + 1;
8877
8878 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8879 {
8880 buf[0] = 0xe0 + b4;
8881 buf[1] = b3;
8882 buf[2] = b2;
8883 buf[3] = b1;
8884 return 4;
8885 }
8886 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8887 {
8888 buf[0] = 0xc0 + b3;
8889 buf[1] = b2;
8890 buf[2] = b1;
8891 return 3;
8892 }
8893 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8894 {
8895 buf[0] = 0x80 + b2;
8896 buf[1] = b1;
8897 return 2;
8898 }
8899 /* 1 byte */
8900 buf[0] = b1;
8901 return 1;
8902}
8903
8904/*
8905 * Opposite of offset2bytes().
8906 * "pp" points to the bytes and is advanced over it.
8907 * Returns the offset.
8908 */
8909 static int
8910bytes2offset(pp)
8911 char_u **pp;
8912{
8913 char_u *p = *pp;
8914 int nr;
8915 int c;
8916
8917 c = *p++;
8918 if ((c & 0x80) == 0x00) /* 1 byte */
8919 {
8920 nr = c - 1;
8921 }
8922 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8923 {
8924 nr = (c & 0x3f) - 1;
8925 nr = nr * 255 + (*p++ - 1);
8926 }
8927 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8928 {
8929 nr = (c & 0x1f) - 1;
8930 nr = nr * 255 + (*p++ - 1);
8931 nr = nr * 255 + (*p++ - 1);
8932 }
8933 else /* 4 bytes */
8934 {
8935 nr = (c & 0x0f) - 1;
8936 nr = nr * 255 + (*p++ - 1);
8937 nr = nr * 255 + (*p++ - 1);
8938 nr = nr * 255 + (*p++ - 1);
8939 }
8940
8941 *pp = p;
8942 return nr;
8943}
8944
8945/*
8946 * Write the .sug file in "fname".
8947 */
8948 static void
8949sug_write(spin, fname)
8950 spellinfo_T *spin;
8951 char_u *fname;
8952{
8953 FILE *fd;
8954 wordnode_T *tree;
8955 int nodecount;
8956 int wcount;
8957 char_u *line;
8958 linenr_T lnum;
8959 int len;
8960
8961 /* Create the file. Note that an existing file is silently overwritten! */
8962 fd = mch_fopen((char *)fname, "w");
8963 if (fd == NULL)
8964 {
8965 EMSG2(_(e_notopen), fname);
8966 return;
8967 }
8968
8969 vim_snprintf((char *)IObuff, IOSIZE,
8970 _("Writing suggestion file %s ..."), fname);
8971 spell_message(spin, IObuff);
8972
8973 /*
8974 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8975 */
8976 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8977 {
8978 EMSG(_(e_write));
8979 goto theend;
8980 }
8981 putc(VIMSUGVERSION, fd); /* <versionnr> */
8982
8983 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008984 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008985
8986 /*
8987 * <SUGWORDTREE>
8988 */
8989 spin->si_memtot = 0;
8990 tree = spin->si_foldroot->wn_sibling;
8991
8992 /* Clear the index and wnode fields in the tree. */
8993 clear_node(tree);
8994
8995 /* Count the number of nodes. Needed to be able to allocate the
8996 * memory when reading the nodes. Also fills in index for shared
8997 * nodes. */
8998 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8999
9000 /* number of nodes in 4 bytes */
9001 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9002 spin->si_memtot += nodecount + nodecount * sizeof(int);
9003
9004 /* Write the nodes. */
9005 (void)put_node(fd, tree, 0, 0, FALSE);
9006
9007 /*
9008 * <SUGTABLE>: <sugwcount> <sugline> ...
9009 */
9010 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9011 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9012
9013 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9014 {
9015 /* <sugline>: <sugnr> ... NUL */
9016 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009017 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009018 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9019 {
9020 EMSG(_(e_write));
9021 goto theend;
9022 }
9023 spin->si_memtot += len;
9024 }
9025
9026 /* Write another byte to check for errors. */
9027 if (putc(0, fd) == EOF)
9028 EMSG(_(e_write));
9029
9030 vim_snprintf((char *)IObuff, IOSIZE,
9031 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9032 spell_message(spin, IObuff);
9033
9034theend:
9035 /* close the file */
9036 fclose(fd);
9037}
9038
9039/*
9040 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9041 * list and only contains text lines. Can use a swapfile to reduce memory
9042 * use.
9043 * Most other fields are invalid! Esp. watch out for string options being
9044 * NULL and there is no undo info.
9045 * Returns NULL when out of memory.
9046 */
9047 static buf_T *
9048open_spellbuf()
9049{
9050 buf_T *buf;
9051
9052 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9053 if (buf != NULL)
9054 {
9055 buf->b_spell = TRUE;
9056 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02009057#ifdef FEAT_CRYPT
9058 buf->b_p_key = empty_option;
9059#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00009060 ml_open(buf);
9061 ml_open_file(buf); /* create swap file now */
9062 }
9063 return buf;
9064}
9065
9066/*
9067 * Close the buffer used for spell info.
9068 */
9069 static void
9070close_spellbuf(buf)
9071 buf_T *buf;
9072{
9073 if (buf != NULL)
9074 {
9075 ml_close(buf, TRUE);
9076 vim_free(buf);
9077 }
9078}
9079
9080
9081/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009082 * Create a Vim spell file from one or more word lists.
9083 * "fnames[0]" is the output file name.
9084 * "fnames[fcount - 1]" is the last input file name.
9085 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9086 * and ".spl" is appended to make the output file name.
9087 */
9088 static void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009089mkspell(fcount, fnames, ascii, over_write, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009090 int fcount;
9091 char_u **fnames;
9092 int ascii; /* -ascii argument given */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009093 int over_write; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009095{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009096 char_u *fname = NULL;
9097 char_u *wfname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009098 char_u **innames;
9099 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009100 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009101 int i;
9102 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009103 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009104 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009105 spellinfo_T spin;
9106
9107 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009109 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 spin.si_followup = TRUE;
9111 spin.si_rem_accents = TRUE;
9112 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009113 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9115 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009116 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009117 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009118 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009119 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009120
Bram Moolenaarb765d632005-06-07 21:00:02 +00009121 /* default: fnames[0] is output file, following are input files */
9122 innames = &fnames[1];
9123 incount = fcount - 1;
9124
Bram Moolenaard9462e32011-04-11 21:35:11 +02009125 wfname = alloc(MAXPATHL);
9126 if (wfname == NULL)
9127 return;
9128
Bram Moolenaarb765d632005-06-07 21:00:02 +00009129 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009130 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009131 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009132 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9133 {
9134 /* For ":mkspell path/en.latin1.add" output file is
9135 * "path/en.latin1.add.spl". */
9136 innames = &fnames[0];
9137 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009138 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009139 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009140 else if (fcount == 1)
9141 {
9142 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9143 innames = &fnames[0];
9144 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009145 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009146 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009147 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009148 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9149 {
9150 /* Name ends in ".spl", use as the file name. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009151 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009152 }
9153 else
9154 /* Name should be language, make the file name from it. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009155 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009156 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009157
9158 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009159 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009160 spin.si_ascii = TRUE;
9161
9162 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009163 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009164 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009165 }
9166
Bram Moolenaarb765d632005-06-07 21:00:02 +00009167 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009168 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009169 else if (vim_strchr(gettail(wfname), '_') != NULL)
9170 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009171 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009172 EMSG(_("E754: Only up to 8 regions supported"));
9173 else
9174 {
9175 /* Check for overwriting before doing things that may take a lot of
9176 * time. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009177 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009178 {
9179 EMSG(_(e_exists));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009180 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009181 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009182 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009183 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009184 EMSG2(_(e_isadir2), wfname);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009185 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009186 }
9187
Bram Moolenaard9462e32011-04-11 21:35:11 +02009188 fname = alloc(MAXPATHL);
9189 if (fname == NULL)
9190 goto theend;
9191
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009192 /*
9193 * Init the aff and dic pointers.
9194 * Get the region names if there are more than 2 arguments.
9195 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009196 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009197 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009198 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009199
Bram Moolenaar3982c542005-06-08 21:56:31 +00009200 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009201 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009202 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009203 if (STRLEN(gettail(innames[i])) < 5
9204 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009205 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009206 EMSG2(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009207 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009208 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009209 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9210 spin.si_region_name[i * 2 + 1] =
9211 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009212 }
9213 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009214 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009215
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009216 spin.si_foldroot = wordtree_alloc(&spin);
9217 spin.si_keeproot = wordtree_alloc(&spin);
9218 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009219 if (spin.si_foldroot == NULL
9220 || spin.si_keeproot == NULL
9221 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009222 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009223 free_blocks(spin.si_blocks);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009224 goto theend;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009225 }
9226
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009227 /* When not producing a .add.spl file clear the character table when
9228 * we encounter one in the .aff file. This means we dump the current
9229 * one in the .spl file if the .aff file doesn't define one. That's
9230 * better than guessing the contents, the table will match a
9231 * previously loaded spell file. */
9232 if (!spin.si_add)
9233 spin.si_clear_chartab = TRUE;
9234
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009235 /*
9236 * Read all the .aff and .dic files.
9237 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009238 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009239 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009240 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009241 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009242 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009243 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009244
Bram Moolenaard9462e32011-04-11 21:35:11 +02009245 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009246 if (mch_stat((char *)fname, &st) >= 0)
9247 {
9248 /* Read the .aff file. Will init "spin->si_conv" based on the
9249 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009250 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009251 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009252 error = TRUE;
9253 else
9254 {
9255 /* Read the .dic file and store the words in the trees. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009256 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009257 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009258 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009259 error = TRUE;
9260 }
9261 }
9262 else
9263 {
9264 /* No .aff file, try reading the file as a word list. Store
9265 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009266 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009267 error = TRUE;
9268 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009269
Bram Moolenaarb765d632005-06-07 21:00:02 +00009270#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009271 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009272 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009273#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009274 }
9275
Bram Moolenaar78622822005-08-23 21:00:13 +00009276 if (spin.si_compflags != NULL && spin.si_nobreak)
9277 MSG(_("Warning: both compounding and NOBREAK specified"));
9278
Bram Moolenaar4770d092006-01-12 23:22:24 +00009279 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009280 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009281 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009282 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009283 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009284 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009285 wordtree_compress(&spin, spin.si_foldroot);
9286 wordtree_compress(&spin, spin.si_keeproot);
9287 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009288 }
9289
Bram Moolenaar4770d092006-01-12 23:22:24 +00009290 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009291 {
9292 /*
9293 * Write the info in the spell file.
9294 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009295 vim_snprintf((char *)IObuff, IOSIZE,
9296 _("Writing spell file %s ..."), wfname);
9297 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009298
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009299 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009300
Bram Moolenaar4770d092006-01-12 23:22:24 +00009301 spell_message(&spin, (char_u *)_("Done!"));
9302 vim_snprintf((char *)IObuff, IOSIZE,
9303 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9304 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009305
Bram Moolenaar4770d092006-01-12 23:22:24 +00009306 /*
9307 * If the file is loaded need to reload it.
9308 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009309 if (!error)
9310 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009311 }
9312
9313 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009314 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009315 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009316 ga_clear(&spin.si_sal);
9317 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009318 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009319 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009320 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009321
9322 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009323 for (i = 0; i < incount; ++i)
9324 if (afile[i] != NULL)
9325 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009326
9327 /* Free all the bits and pieces at once. */
9328 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009329
9330 /*
9331 * If there is soundfolding info and no NOSUGFILE item create the
9332 * .sug file with the soundfolded word trie.
9333 */
9334 if (spin.si_sugtime != 0 && !error && !got_int)
9335 spell_make_sugfile(&spin, wfname);
9336
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009337 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009338
9339theend:
9340 vim_free(fname);
9341 vim_free(wfname);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009342}
9343
Bram Moolenaar4770d092006-01-12 23:22:24 +00009344/*
9345 * Display a message for spell file processing when 'verbose' is set or using
9346 * ":mkspell". "str" can be IObuff.
9347 */
9348 static void
9349spell_message(spin, str)
9350 spellinfo_T *spin;
9351 char_u *str;
9352{
9353 if (spin->si_verbose || p_verbose > 2)
9354 {
9355 if (!spin->si_verbose)
9356 verbose_enter();
9357 MSG(str);
9358 out_flush();
9359 if (!spin->si_verbose)
9360 verbose_leave();
9361 }
9362}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009363
9364/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009365 * ":[count]spellgood {word}"
9366 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009367 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009368 */
9369 void
9370ex_spell(eap)
9371 exarg_T *eap;
9372{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009373 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009374 eap->forceit ? 0 : (int)eap->line2,
9375 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009376}
9377
9378/*
9379 * Add "word[len]" to 'spellfile' as a good or bad word.
9380 */
9381 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009382spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009383 char_u *word;
9384 int len;
9385 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009386 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009387 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009388 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009389{
Bram Moolenaara3917072006-09-14 08:48:14 +00009390 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009391 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009392 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009393 char_u *fname;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009394 char_u *fnamebuf = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009395 char_u line[MAXWLEN * 2];
9396 long fpos, fpos_next = 0;
9397 int i;
9398 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009399
Bram Moolenaar89d40322006-08-29 15:30:07 +00009400 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009401 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009402 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009403 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009404 int_wordlist = vim_tempname('s');
9405 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009406 return;
9407 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009408 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009409 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009410 else
9411 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009412 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009413 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009414 {
9415 init_spellfile();
9416 new_spf = TRUE;
9417 }
9418
Bram Moolenaar860cae12010-06-05 23:22:07 +02009419 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009420 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009421 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009422 return;
9423 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009424 fnamebuf = alloc(MAXPATHL);
9425 if (fnamebuf == NULL)
9426 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009427
Bram Moolenaar860cae12010-06-05 23:22:07 +02009428 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009429 {
9430 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009431 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009432 break;
9433 if (*spf == NUL)
9434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009435 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009436 vim_free(fnamebuf);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009437 return;
9438 }
9439 }
9440
Bram Moolenaarb765d632005-06-07 21:00:02 +00009441 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009442 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009443 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9444 buf = NULL;
9445 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009446 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009447 EMSG(_(e_bufloaded));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009448 vim_free(fnamebuf);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009449 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009450 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009451
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009452 fname = fnamebuf;
9453 }
9454
Bram Moolenaard0131a82006-03-04 21:46:13 +00009455 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009456 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009457 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009458 * since its flags sort before the one with WF_BANNED. */
9459 fd = mch_fopen((char *)fname, "r");
9460 if (fd != NULL)
9461 {
9462 while (!vim_fgets(line, MAXWLEN * 2, fd))
9463 {
9464 fpos = fpos_next;
9465 fpos_next = ftell(fd);
9466 if (STRNCMP(word, line, len) == 0
9467 && (line[len] == '/' || line[len] < ' '))
9468 {
9469 /* Found duplicate word. Remove it by writing a '#' at
9470 * the start of the line. Mixing reading and writing
9471 * doesn't work for all systems, close the file first. */
9472 fclose(fd);
9473 fd = mch_fopen((char *)fname, "r+");
9474 if (fd == NULL)
9475 break;
9476 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009477 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009478 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009479 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009480 {
9481 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009482 smsg((char_u *)_("Word '%.*s' removed from %s"),
9483 len, word, NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009484 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009485 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009486 fseek(fd, fpos_next, SEEK_SET);
9487 }
9488 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009489 if (fd != NULL)
9490 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009491 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009492 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009493
9494 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009495 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009496 fd = mch_fopen((char *)fname, "a");
9497 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009498 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009499 char_u *p;
9500
Bram Moolenaard0131a82006-03-04 21:46:13 +00009501 /* We just initialized the 'spellfile' option and can't open the
9502 * file. We may need to create the "spell" directory first. We
9503 * already checked the runtime directory is writable in
9504 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009505 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009506 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009507 int c = *p;
9508
Bram Moolenaard0131a82006-03-04 21:46:13 +00009509 /* The directory doesn't exist. Try creating it and opening
9510 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009511 *p = NUL;
9512 vim_mkdir(fname, 0755);
9513 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009514 fd = mch_fopen((char *)fname, "a");
9515 }
9516 }
9517
9518 if (fd == NULL)
9519 EMSG2(_(e_notopen), fname);
9520 else
9521 {
9522 if (bad)
9523 fprintf(fd, "%.*s/!\n", len, word);
9524 else
9525 fprintf(fd, "%.*s\n", len, word);
9526 fclose(fd);
9527
9528 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009529 smsg((char_u *)_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009530 }
9531 }
9532
Bram Moolenaard0131a82006-03-04 21:46:13 +00009533 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009534 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009535 /* Update the .add.spl file. */
9536 mkspell(1, &fname, FALSE, TRUE, TRUE);
9537
9538 /* If the .add file is edited somewhere, reload it. */
9539 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009540 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009541
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009542 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009543 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009544 vim_free(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009545}
9546
9547/*
9548 * Initialize 'spellfile' for the current buffer.
9549 */
9550 static void
9551init_spellfile()
9552{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009553 char_u *buf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009554 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009555 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009556 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009557 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009558 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009559 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009560
Bram Moolenaar860cae12010-06-05 23:22:07 +02009561 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009562 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009563 buf = alloc(MAXPATHL);
9564 if (buf == NULL)
9565 return;
9566
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009567 /* Find the end of the language name. Exclude the region. If there
9568 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009569 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009570 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009571 if (vim_ispathsep(*lend))
9572 {
9573 aspath = TRUE;
9574 lstart = lend + 1;
9575 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009576
9577 /* Loop over all entries in 'runtimepath'. Use the first one where we
9578 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009579 rtp = p_rtp;
9580 while (*rtp != NUL)
9581 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009582 if (aspath)
9583 /* Use directory of an entry with path, e.g., for
9584 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009585 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9586 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009587 else
9588 /* Copy the path from 'runtimepath' to buf[]. */
9589 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009590 if (filewritable(buf) == 2)
9591 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009592 /* Use the first language name from 'spelllang' and the
9593 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009594 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009595 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9596 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009597 else
9598 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009599 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009600 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009601 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009602 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009603 vim_mkdir(buf, 0755);
9604
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009605 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009606 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009607 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009608 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009609 l = (int)STRLEN(buf);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009610 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9611 ->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009612 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9613 fname != NULL
9614 && strstr((char *)gettail(fname), ".ascii.") != NULL
9615 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009616 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9617 break;
9618 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009619 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009620 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009621
9622 vim_free(buf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009623 }
9624}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009625
Bram Moolenaar51485f02005-06-04 21:55:20 +00009626
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009627/*
9628 * Init the chartab used for spelling for ASCII.
9629 * EBCDIC is not supported!
9630 */
9631 static void
9632clear_spell_chartab(sp)
9633 spelltab_T *sp;
9634{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009635 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009636
9637 /* Init everything to FALSE. */
9638 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9639 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9640 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009641 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009642 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009643 sp->st_upper[i] = i;
9644 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009645
9646 /* We include digits. A word shouldn't start with a digit, but handling
9647 * that is done separately. */
9648 for (i = '0'; i <= '9'; ++i)
9649 sp->st_isw[i] = TRUE;
9650 for (i = 'A'; i <= 'Z'; ++i)
9651 {
9652 sp->st_isw[i] = TRUE;
9653 sp->st_isu[i] = TRUE;
9654 sp->st_fold[i] = i + 0x20;
9655 }
9656 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009657 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009658 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009659 sp->st_upper[i] = i - 0x20;
9660 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009661}
9662
9663/*
9664 * Init the chartab used for spelling. Only depends on 'encoding'.
9665 * Called once while starting up and when 'encoding' changes.
9666 * The default is to use isalpha(), but the spell file should define the word
9667 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009668 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009669 */
9670 void
9671init_spell_chartab()
9672{
9673 int i;
9674
9675 did_set_spelltab = FALSE;
9676 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009677#ifdef FEAT_MBYTE
9678 if (enc_dbcs)
9679 {
9680 /* DBCS: assume double-wide characters are word characters. */
9681 for (i = 128; i <= 255; ++i)
9682 if (MB_BYTE2LEN(i) == 2)
9683 spelltab.st_isw[i] = TRUE;
9684 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009685 else if (enc_utf8)
9686 {
9687 for (i = 128; i < 256; ++i)
9688 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009689 int f = utf_fold(i);
9690 int u = utf_toupper(i);
9691
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009692 spelltab.st_isu[i] = utf_isupper(i);
9693 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009694 /* The folded/upper-cased value is different between latin1 and
9695 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9696 * value for utf-8 to avoid this. */
9697 spelltab.st_fold[i] = (f < 256) ? f : i;
9698 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009699 }
9700 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009701 else
9702#endif
9703 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009704 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009705 for (i = 128; i < 256; ++i)
9706 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009707 if (MB_ISUPPER(i))
9708 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009709 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009710 spelltab.st_isu[i] = TRUE;
9711 spelltab.st_fold[i] = MB_TOLOWER(i);
9712 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009713 else if (MB_ISLOWER(i))
9714 {
9715 spelltab.st_isw[i] = TRUE;
9716 spelltab.st_upper[i] = MB_TOUPPER(i);
9717 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009718 }
9719 }
9720}
9721
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009722/*
9723 * Set the spell character tables from strings in the affix file.
9724 */
9725 static int
9726set_spell_chartab(fol, low, upp)
9727 char_u *fol;
9728 char_u *low;
9729 char_u *upp;
9730{
9731 /* We build the new tables here first, so that we can compare with the
9732 * previous one. */
9733 spelltab_T new_st;
9734 char_u *pf = fol, *pl = low, *pu = upp;
9735 int f, l, u;
9736
9737 clear_spell_chartab(&new_st);
9738
9739 while (*pf != NUL)
9740 {
9741 if (*pl == NUL || *pu == NUL)
9742 {
9743 EMSG(_(e_affform));
9744 return FAIL;
9745 }
9746#ifdef FEAT_MBYTE
9747 f = mb_ptr2char_adv(&pf);
9748 l = mb_ptr2char_adv(&pl);
9749 u = mb_ptr2char_adv(&pu);
9750#else
9751 f = *pf++;
9752 l = *pl++;
9753 u = *pu++;
9754#endif
9755 /* Every character that appears is a word character. */
9756 if (f < 256)
9757 new_st.st_isw[f] = TRUE;
9758 if (l < 256)
9759 new_st.st_isw[l] = TRUE;
9760 if (u < 256)
9761 new_st.st_isw[u] = TRUE;
9762
9763 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9764 * case-folding */
9765 if (l < 256 && l != f)
9766 {
9767 if (f >= 256)
9768 {
9769 EMSG(_(e_affrange));
9770 return FAIL;
9771 }
9772 new_st.st_fold[l] = f;
9773 }
9774
9775 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009776 * case-folding, it's upper case and the "UPP" is the upper case of
9777 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009778 if (u < 256 && u != f)
9779 {
9780 if (f >= 256)
9781 {
9782 EMSG(_(e_affrange));
9783 return FAIL;
9784 }
9785 new_st.st_fold[u] = f;
9786 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009787 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009788 }
9789 }
9790
9791 if (*pl != NUL || *pu != NUL)
9792 {
9793 EMSG(_(e_affform));
9794 return FAIL;
9795 }
9796
9797 return set_spell_finish(&new_st);
9798}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009799
9800/*
9801 * Set the spell character tables from strings in the .spl file.
9802 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009803 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009804set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009805 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009806 int cnt; /* length of "flags" */
9807 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009808{
9809 /* We build the new tables here first, so that we can compare with the
9810 * previous one. */
9811 spelltab_T new_st;
9812 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009813 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009814 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009815
9816 clear_spell_chartab(&new_st);
9817
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009818 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009819 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009820 if (i < cnt)
9821 {
9822 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9823 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9824 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009825
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009826 if (*p != NUL)
9827 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009828#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009829 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009830#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009831 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009832#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009833 new_st.st_fold[i + 128] = c;
9834 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9835 new_st.st_upper[c] = i + 128;
9836 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009837 }
9838
Bram Moolenaar5195e452005-08-19 20:32:47 +00009839 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009840}
9841
9842 static int
9843set_spell_finish(new_st)
9844 spelltab_T *new_st;
9845{
9846 int i;
9847
9848 if (did_set_spelltab)
9849 {
9850 /* check that it's the same table */
9851 for (i = 0; i < 256; ++i)
9852 {
9853 if (spelltab.st_isw[i] != new_st->st_isw[i]
9854 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009855 || spelltab.st_fold[i] != new_st->st_fold[i]
9856 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009857 {
9858 EMSG(_("E763: Word characters differ between spell files"));
9859 return FAIL;
9860 }
9861 }
9862 }
9863 else
9864 {
9865 /* copy the new spelltab into the one being used */
9866 spelltab = *new_st;
9867 did_set_spelltab = TRUE;
9868 }
9869
9870 return OK;
9871}
9872
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009873/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009874 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009875 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009876 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009877 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009878 */
9879 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009880spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009881 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009882 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009883{
Bram Moolenaarea408852005-06-25 22:49:46 +00009884#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009885 char_u *s;
9886 int l;
9887 int c;
9888
9889 if (has_mbyte)
9890 {
9891 l = MB_BYTE2LEN(*p);
9892 s = p;
9893 if (l == 1)
9894 {
9895 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009896 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009897 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009898 }
9899 else
9900 {
9901 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009902 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9903 : (wp->w_s->b_spell_ismw_mb != NULL
9904 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009905 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009906 }
9907
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009908 c = mb_ptr2char(s);
9909 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009910 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009911 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009912 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009913#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009914
Bram Moolenaar860cae12010-06-05 23:22:07 +02009915 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009916}
9917
9918/*
9919 * Return TRUE if "p" points to a word character.
9920 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9921 */
9922 static int
9923spell_iswordp_nmw(p)
9924 char_u *p;
9925{
9926#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009927 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009928
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009929 if (has_mbyte)
9930 {
9931 c = mb_ptr2char(p);
9932 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009933 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009934 return spelltab.st_isw[c];
9935 }
9936#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009937 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009938}
9939
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009940#ifdef FEAT_MBYTE
9941/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009942 * Return TRUE if word class indicates a word character.
9943 * Only for characters above 255.
9944 * Unicode subscript and superscript are not considered word characters.
9945 */
9946 static int
9947spell_mb_isword_class(cl)
9948 int cl;
9949{
9950 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9951}
9952
9953/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009954 * Return TRUE if "p" points to a word character.
9955 * Wide version of spell_iswordp().
9956 */
9957 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009958spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009959 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009960 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009961{
9962 int *s;
9963
Bram Moolenaar860cae12010-06-05 23:22:07 +02009964 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9965 : (wp->w_s->b_spell_ismw_mb != NULL
9966 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009967 s = p + 1;
9968 else
9969 s = p;
9970
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009971 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009972 {
9973 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009974 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009975 if (enc_dbcs)
9976 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9977 return 0;
9978 }
9979 return spelltab.st_isw[*s];
9980}
9981#endif
9982
Bram Moolenaarea408852005-06-25 22:49:46 +00009983/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009984 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009985 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009986 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009987 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009988write_spell_prefcond(fd, gap)
9989 FILE *fd;
9990 garray_T *gap;
9991{
9992 int i;
9993 char_u *p;
9994 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009995 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009996 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009997
Bram Moolenaar5195e452005-08-19 20:32:47 +00009998 if (fd != NULL)
9999 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
10000
10001 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010002
10003 for (i = 0; i < gap->ga_len; ++i)
10004 {
10005 /* <prefcond> : <condlen> <condstr> */
10006 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +000010007 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010008 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010009 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010010 if (fd != NULL)
10011 {
10012 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +000010013 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010014 }
10015 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010016 }
Bram Moolenaar5195e452005-08-19 20:32:47 +000010017 else if (fd != NULL)
10018 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010019 }
10020
Bram Moolenaar5195e452005-08-19 20:32:47 +000010021 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010022}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010023
10024/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010025 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
10026 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010027 * When using a multi-byte 'encoding' the length may change!
10028 * Returns FAIL when something wrong.
10029 */
10030 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010031spell_casefold(str, len, buf, buflen)
10032 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010033 int len;
10034 char_u *buf;
10035 int buflen;
10036{
10037 int i;
10038
10039 if (len >= buflen)
10040 {
10041 buf[0] = NUL;
10042 return FAIL; /* result will not fit */
10043 }
10044
10045#ifdef FEAT_MBYTE
10046 if (has_mbyte)
10047 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010048 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010049 char_u *p;
10050 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010051
10052 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010053 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010054 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010055 if (outi + MB_MAXBYTES > buflen)
10056 {
10057 buf[outi] = NUL;
10058 return FAIL;
10059 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010060 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010061 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010062 }
10063 buf[outi] = NUL;
10064 }
10065 else
10066#endif
10067 {
10068 /* Be quick for non-multibyte encodings. */
10069 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010070 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010071 buf[i] = NUL;
10072 }
10073
10074 return OK;
10075}
10076
Bram Moolenaar4770d092006-01-12 23:22:24 +000010077/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010078#define SPS_BEST 1
10079#define SPS_FAST 2
10080#define SPS_DOUBLE 4
10081
Bram Moolenaar4770d092006-01-12 23:22:24 +000010082static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10083static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010084
10085/*
10086 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010087 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010088 */
10089 int
10090spell_check_sps()
10091{
10092 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010093 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010094 char_u buf[MAXPATHL];
10095 int f;
10096
10097 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010098 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010099
10100 for (p = p_sps; *p != NUL; )
10101 {
10102 copy_option_part(&p, buf, MAXPATHL, ",");
10103
10104 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010105 if (VIM_ISDIGIT(*buf))
10106 {
10107 s = buf;
10108 sps_limit = getdigits(&s);
10109 if (*s != NUL && !VIM_ISDIGIT(*s))
10110 f = -1;
10111 }
10112 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010113 f = SPS_BEST;
10114 else if (STRCMP(buf, "fast") == 0)
10115 f = SPS_FAST;
10116 else if (STRCMP(buf, "double") == 0)
10117 f = SPS_DOUBLE;
10118 else if (STRNCMP(buf, "expr:", 5) != 0
10119 && STRNCMP(buf, "file:", 5) != 0)
10120 f = -1;
10121
10122 if (f == -1 || (sps_flags != 0 && f != 0))
10123 {
10124 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010125 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010126 return FAIL;
10127 }
10128 if (f != 0)
10129 sps_flags = f;
10130 }
10131
10132 if (sps_flags == 0)
10133 sps_flags = SPS_BEST;
10134
10135 return OK;
10136}
10137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138/*
Bram Moolenaar134bf072013-09-25 18:54:24 +020010139 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010141 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010142 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 */
10144 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010145spell_suggest(count)
10146 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147{
10148 char_u *line;
10149 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 char_u wcopy[MAXWLEN + 2];
10151 char_u *p;
10152 int i;
10153 int c;
10154 suginfo_T sug;
10155 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010156 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010157 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010158 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010159 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010160 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010161 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010163 if (no_spell_checking(curwin))
10164 return;
10165
10166#ifdef FEAT_VISUAL
10167 if (VIsual_active)
10168 {
10169 /* Use the Visually selected text as the bad word. But reject
10170 * a multi-line selection. */
10171 if (curwin->w_cursor.lnum != VIsual.lnum)
10172 {
10173 vim_beep();
10174 return;
10175 }
10176 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10177 if (badlen < 0)
10178 badlen = -badlen;
10179 else
10180 curwin->w_cursor.col = VIsual.col;
10181 ++badlen;
10182 end_visual_mode();
10183 }
10184 else
10185#endif
10186 /* Find the start of the badly spelled word. */
10187 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010188 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010190 /* No bad word or it starts after the cursor: use the word under the
10191 * cursor. */
10192 curwin->w_cursor = prev_cursor;
10193 line = ml_get_curline();
10194 p = line + curwin->w_cursor.col;
10195 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010196 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010197 mb_ptr_back(line, p);
10198 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010199 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010200 mb_ptr_adv(p);
10201
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010202 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010203 {
10204 beep_flush();
10205 return;
10206 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010207 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 }
10209
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010210 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010211
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010212 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010213 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010214
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010215 /* Make a copy of current line since autocommands may free the line. */
10216 line = vim_strsave(ml_get_curline());
10217 if (line == NULL)
10218 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010219
Bram Moolenaar5195e452005-08-19 20:32:47 +000010220 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10221 * 'spellsuggest', whatever is smaller. */
10222 if (sps_limit > (int)Rows - 2)
10223 limit = (int)Rows - 2;
10224 else
10225 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010226 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010227 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228
10229 if (sug.su_ga.ga_len == 0)
10230 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010231 else if (count > 0)
10232 {
10233 if (count > sug.su_ga.ga_len)
10234 smsg((char_u *)_("Sorry, only %ld suggestions"),
10235 (long)sug.su_ga.ga_len);
10236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 else
10238 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010239 vim_free(repl_from);
10240 repl_from = NULL;
10241 vim_free(repl_to);
10242 repl_to = NULL;
10243
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010244#ifdef FEAT_RIGHTLEFT
10245 /* When 'rightleft' is set the list is drawn right-left. */
10246 cmdmsg_rl = curwin->w_p_rl;
10247 if (cmdmsg_rl)
10248 msg_col = Columns - 1;
10249#endif
10250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 /* List the suggestions. */
10252 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010253 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010254 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10256 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010257#ifdef FEAT_RIGHTLEFT
10258 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10259 {
10260 /* And now the rabbit from the high hat: Avoid showing the
10261 * untranslated message rightleft. */
10262 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10263 sug.su_badlen, sug.su_badptr);
10264 }
10265#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 msg_puts(IObuff);
10267 msg_clr_eos();
10268 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 msg_scroll = TRUE;
10271 for (i = 0; i < sug.su_ga.ga_len; ++i)
10272 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010273 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274
10275 /* The suggested word may replace only part of the bad word, add
10276 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020010277 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010279 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010280 sug.su_badptr + stp->st_orglen,
10281 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010282 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10283#ifdef FEAT_RIGHTLEFT
10284 if (cmdmsg_rl)
10285 rl_mirror(IObuff);
10286#endif
10287 msg_puts(IObuff);
10288
10289 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010290 msg_puts(IObuff);
10291
10292 /* The word may replace more than "su_badlen". */
10293 if (sug.su_badlen < stp->st_orglen)
10294 {
10295 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10296 stp->st_orglen, sug.su_badptr);
10297 msg_puts(IObuff);
10298 }
10299
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010300 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010301 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010302 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010303 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010304 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010305 stp->st_salscore ? "s " : "",
10306 stp->st_score, stp->st_altscore);
10307 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010308 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010309 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010310#ifdef FEAT_RIGHTLEFT
10311 if (cmdmsg_rl)
10312 /* Mirror the numbers, but keep the leading space. */
10313 rl_mirror(IObuff + 1);
10314#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010315 msg_advance(30);
10316 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 msg_putchar('\n');
10319 }
10320
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010321#ifdef FEAT_RIGHTLEFT
10322 cmdmsg_rl = FALSE;
10323 msg_col = 0;
10324#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010326 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010327 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010328 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010329 lines_left = Rows; /* avoid more prompt */
10330 /* don't delay for 'smd' in normal_cmd() */
10331 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 }
10333
Bram Moolenaard12a1322005-08-21 22:08:24 +000010334 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10335 {
10336 /* Save the from and to text for :spellrepall. */
10337 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010338 if (sug.su_badlen > stp->st_orglen)
10339 {
10340 /* Replacing less than "su_badlen", append the remainder to
10341 * repl_to. */
10342 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10343 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10344 sug.su_badlen - stp->st_orglen,
10345 sug.su_badptr + stp->st_orglen);
10346 repl_to = vim_strsave(IObuff);
10347 }
10348 else
10349 {
10350 /* Replacing su_badlen or more, use the whole word. */
10351 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10352 repl_to = vim_strsave(stp->st_word);
10353 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010354
10355 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010356 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10357 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010358 if (p != NULL)
10359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010360 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010361 mch_memmove(p, line, c);
10362 STRCPY(p + c, stp->st_word);
10363 STRCAT(p, sug.su_badptr + stp->st_orglen);
10364 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10365 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010366
10367 /* For redo we use a change-word command. */
10368 ResetRedobuff();
10369 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010370 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010371 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010372 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010373
10374 /* After this "p" may be invalid. */
10375 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010376 }
10377 }
10378 else
10379 curwin->w_cursor = prev_cursor;
10380
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010381 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010382skip:
10383 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010384}
10385
10386/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010387 * Check if the word at line "lnum" column "col" is required to start with a
10388 * capital. This uses 'spellcapcheck' of the current buffer.
10389 */
10390 static int
10391check_need_cap(lnum, col)
10392 linenr_T lnum;
10393 colnr_T col;
10394{
10395 int need_cap = FALSE;
10396 char_u *line;
10397 char_u *line_copy = NULL;
10398 char_u *p;
10399 colnr_T endcol;
10400 regmatch_T regmatch;
10401
Bram Moolenaar860cae12010-06-05 23:22:07 +020010402 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010403 return FALSE;
10404
10405 line = ml_get_curline();
10406 endcol = 0;
10407 if ((int)(skipwhite(line) - line) >= (int)col)
10408 {
10409 /* At start of line, check if previous line is empty or sentence
10410 * ends there. */
10411 if (lnum == 1)
10412 need_cap = TRUE;
10413 else
10414 {
10415 line = ml_get(lnum - 1);
10416 if (*skipwhite(line) == NUL)
10417 need_cap = TRUE;
10418 else
10419 {
10420 /* Append a space in place of the line break. */
10421 line_copy = concat_str(line, (char_u *)" ");
10422 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010423 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010424 }
10425 }
10426 }
10427 else
10428 endcol = col;
10429
10430 if (endcol > 0)
10431 {
10432 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010433 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010434 regmatch.rm_ic = FALSE;
10435 p = line + endcol;
10436 for (;;)
10437 {
10438 mb_ptr_back(line, p);
10439 if (p == line || spell_iswordp_nmw(p))
10440 break;
10441 if (vim_regexec(&regmatch, p, 0)
10442 && regmatch.endp[0] == line + endcol)
10443 {
10444 need_cap = TRUE;
10445 break;
10446 }
10447 }
10448 }
10449
10450 vim_free(line_copy);
10451
10452 return need_cap;
10453}
10454
10455
10456/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010457 * ":spellrepall"
10458 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010459 void
10460ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010461 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010462{
10463 pos_T pos = curwin->w_cursor;
10464 char_u *frompat;
10465 int addlen;
10466 char_u *line;
10467 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010468 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010469 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010470
10471 if (repl_from == NULL || repl_to == NULL)
10472 {
10473 EMSG(_("E752: No previous spell replacement"));
10474 return;
10475 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010476 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010477
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010478 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010479 if (frompat == NULL)
10480 return;
10481 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10482 p_ws = FALSE;
10483
Bram Moolenaar5195e452005-08-19 20:32:47 +000010484 sub_nsubs = 0;
10485 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010486 curwin->w_cursor.lnum = 0;
10487 while (!got_int)
10488 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010489 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010490 || u_save_cursor() == FAIL)
10491 break;
10492
10493 /* Only replace when the right word isn't there yet. This happens
10494 * when changing "etc" to "etc.". */
10495 line = ml_get_curline();
10496 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10497 repl_to, STRLEN(repl_to)) != 0)
10498 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010499 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010500 if (p == NULL)
10501 break;
10502 mch_memmove(p, line, curwin->w_cursor.col);
10503 STRCPY(p + curwin->w_cursor.col, repl_to);
10504 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10505 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10506 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010507
10508 if (curwin->w_cursor.lnum != prev_lnum)
10509 {
10510 ++sub_nlines;
10511 prev_lnum = curwin->w_cursor.lnum;
10512 }
10513 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010514 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010515 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010516 }
10517
10518 p_ws = save_ws;
10519 curwin->w_cursor = pos;
10520 vim_free(frompat);
10521
Bram Moolenaar5195e452005-08-19 20:32:47 +000010522 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010523 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010524 else
10525 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010526}
10527
10528/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010529 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10530 * a list of allocated strings.
10531 */
10532 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010533spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010534 garray_T *gap;
10535 char_u *word;
10536 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010537 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010538 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010539{
10540 suginfo_T sug;
10541 int i;
10542 suggest_T *stp;
10543 char_u *wcopy;
10544
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010545 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010546
10547 /* Make room in "gap". */
10548 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010549 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010550 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010551 for (i = 0; i < sug.su_ga.ga_len; ++i)
10552 {
10553 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010554
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010555 /* The suggested word may replace only part of "word", add the not
10556 * replaced part. */
10557 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010558 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010559 if (wcopy == NULL)
10560 break;
10561 STRCPY(wcopy, stp->st_word);
10562 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10563 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10564 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010565 }
10566
10567 spell_find_cleanup(&sug);
10568}
10569
10570/*
10571 * Find spell suggestions for the word at the start of "badptr".
10572 * Return the suggestions in "su->su_ga".
10573 * The maximum number of suggestions is "maxcount".
10574 * Note: does use info for the current window.
10575 * This is based on the mechanisms of Aspell, but completely reimplemented.
10576 */
10577 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010578spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010579 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010580 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010581 suginfo_T *su;
10582 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010583 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010584 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010585 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010586{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010587 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010588 char_u buf[MAXPATHL];
10589 char_u *p;
10590 int do_combine = FALSE;
10591 char_u *sps_copy;
10592#ifdef FEAT_EVAL
10593 static int expr_busy = FALSE;
10594#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010595 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010596 int i;
10597 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010598
10599 /*
10600 * Set the info in "*su".
10601 */
10602 vim_memset(su, 0, sizeof(suginfo_T));
10603 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10604 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010605 if (*badptr == NUL)
10606 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010607 hash_init(&su->su_banned);
10608
10609 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010610 if (badlen != 0)
10611 su->su_badlen = badlen;
10612 else
10613 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010614 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010615 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010616
10617 if (su->su_badlen >= MAXWLEN)
10618 su->su_badlen = MAXWLEN - 1; /* just in case */
10619 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10620 (void)spell_casefold(su->su_badptr, su->su_badlen,
10621 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010622 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010623 su->su_badflags = badword_captype(su->su_badptr,
10624 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010625 if (need_cap)
10626 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010627
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010628 /* Find the default language for sound folding. We simply use the first
10629 * one in 'spelllang' that supports sound folding. That's good for when
10630 * using multiple files for one language, it's not that bad when mixing
10631 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010632 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010633 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010634 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010635 if (lp->lp_sallang != NULL)
10636 {
10637 su->su_sallang = lp->lp_sallang;
10638 break;
10639 }
10640 }
10641
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010642 /* Soundfold the bad word with the default sound folding, so that we don't
10643 * have to do this many times. */
10644 if (su->su_sallang != NULL)
10645 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10646 su->su_sal_badword);
10647
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010648 /* If the word is not capitalised and spell_check() doesn't consider the
10649 * word to be bad then it might need to be capitalised. Add a suggestion
10650 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010651 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010652 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010653 {
10654 make_case_word(su->su_badword, buf, WF_ONECAP);
10655 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010656 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010657 }
10658
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010659 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010660 if (banbadword)
10661 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010662
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010663 /* Make a copy of 'spellsuggest', because the expression may change it. */
10664 sps_copy = vim_strsave(p_sps);
10665 if (sps_copy == NULL)
10666 return;
10667
10668 /* Loop over the items in 'spellsuggest'. */
10669 for (p = sps_copy; *p != NUL; )
10670 {
10671 copy_option_part(&p, buf, MAXPATHL, ",");
10672
10673 if (STRNCMP(buf, "expr:", 5) == 0)
10674 {
10675#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010676 /* Evaluate an expression. Skip this when called recursively,
10677 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010678 if (!expr_busy)
10679 {
10680 expr_busy = TRUE;
10681 spell_suggest_expr(su, buf + 5);
10682 expr_busy = FALSE;
10683 }
10684#endif
10685 }
10686 else if (STRNCMP(buf, "file:", 5) == 0)
10687 /* Use list of suggestions in a file. */
10688 spell_suggest_file(su, buf + 5);
10689 else
10690 {
10691 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010692 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010693 if (sps_flags & SPS_DOUBLE)
10694 do_combine = TRUE;
10695 }
10696 }
10697
10698 vim_free(sps_copy);
10699
10700 if (do_combine)
10701 /* Combine the two list of suggestions. This must be done last,
10702 * because sorting changes the order again. */
10703 score_combine(su);
10704}
10705
10706#ifdef FEAT_EVAL
10707/*
10708 * Find suggestions by evaluating expression "expr".
10709 */
10710 static void
10711spell_suggest_expr(su, expr)
10712 suginfo_T *su;
10713 char_u *expr;
10714{
10715 list_T *list;
10716 listitem_T *li;
10717 int score;
10718 char_u *p;
10719
10720 /* The work is split up in a few parts to avoid having to export
10721 * suginfo_T.
10722 * First evaluate the expression and get the resulting list. */
10723 list = eval_spell_expr(su->su_badword, expr);
10724 if (list != NULL)
10725 {
10726 /* Loop over the items in the list. */
10727 for (li = list->lv_first; li != NULL; li = li->li_next)
10728 if (li->li_tv.v_type == VAR_LIST)
10729 {
10730 /* Get the word and the score from the items. */
10731 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010732 if (score >= 0 && score <= su->su_maxscore)
10733 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10734 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010735 }
10736 list_unref(list);
10737 }
10738
Bram Moolenaar4770d092006-01-12 23:22:24 +000010739 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10740 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010741 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10742}
10743#endif
10744
10745/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010746 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010747 */
10748 static void
10749spell_suggest_file(su, fname)
10750 suginfo_T *su;
10751 char_u *fname;
10752{
10753 FILE *fd;
10754 char_u line[MAXWLEN * 2];
10755 char_u *p;
10756 int len;
10757 char_u cword[MAXWLEN];
10758
10759 /* Open the file. */
10760 fd = mch_fopen((char *)fname, "r");
10761 if (fd == NULL)
10762 {
10763 EMSG2(_(e_notopen), fname);
10764 return;
10765 }
10766
10767 /* Read it line by line. */
10768 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10769 {
10770 line_breakcheck();
10771
10772 p = vim_strchr(line, '/');
10773 if (p == NULL)
10774 continue; /* No Tab found, just skip the line. */
10775 *p++ = NUL;
10776 if (STRICMP(su->su_badword, line) == 0)
10777 {
10778 /* Match! Isolate the good word, until CR or NL. */
10779 for (len = 0; p[len] >= ' '; ++len)
10780 ;
10781 p[len] = NUL;
10782
10783 /* If the suggestion doesn't have specific case duplicate the case
10784 * of the bad word. */
10785 if (captype(p, NULL) == 0)
10786 {
10787 make_case_word(p, cword, su->su_badflags);
10788 p = cword;
10789 }
10790
10791 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010792 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010793 }
10794 }
10795
10796 fclose(fd);
10797
Bram Moolenaar4770d092006-01-12 23:22:24 +000010798 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10799 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010800 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10801}
10802
10803/*
10804 * Find suggestions for the internal method indicated by "sps_flags".
10805 */
10806 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010808 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010809 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010810{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010811 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010812 * Load the .sug file(s) that are available and not done yet.
10813 */
10814 suggest_load_files();
10815
10816 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010817 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010818 *
10819 * Set a maximum score to limit the combination of operations that is
10820 * tried.
10821 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010822 suggest_try_special(su);
10823
10824 /*
10825 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10826 * from the .aff file and inserting a space (split the word).
10827 */
10828 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010829
10830 /* For the resulting top-scorers compute the sound-a-like score. */
10831 if (sps_flags & SPS_DOUBLE)
10832 score_comp_sal(su);
10833
10834 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010835 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010836 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010837 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010838 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010839 if (sps_flags & SPS_BEST)
10840 /* Adjust the word score for the suggestions found so far for how
10841 * they sounds like. */
10842 rescore_suggestions(su);
10843
10844 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010845 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010846 * for the soundfold word, limits the changes that are being tried,
10847 * and "su_sfmaxscore" the rescored score, which is set by
10848 * cleanup_suggestions().
10849 * First find words with a small edit distance, because this is much
10850 * faster and often already finds the top-N suggestions. If we didn't
10851 * find many suggestions try again with a higher edit distance.
10852 * "sl_sounddone" is used to avoid doing the same word twice.
10853 */
10854 suggest_try_soundalike_prep();
10855 su->su_maxscore = SCORE_SFMAX1;
10856 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010857 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010858 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10859 {
10860 /* We didn't find enough matches, try again, allowing more
10861 * changes to the soundfold word. */
10862 su->su_maxscore = SCORE_SFMAX2;
10863 suggest_try_soundalike(su);
10864 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10865 {
10866 /* Still didn't find enough matches, try again, allowing even
10867 * more changes to the soundfold word. */
10868 su->su_maxscore = SCORE_SFMAX3;
10869 suggest_try_soundalike(su);
10870 }
10871 }
10872 su->su_maxscore = su->su_sfmaxscore;
10873 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010874 }
10875
Bram Moolenaar4770d092006-01-12 23:22:24 +000010876 /* When CTRL-C was hit while searching do show the results. Only clear
10877 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010878 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010879 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010880 {
10881 (void)vgetc();
10882 got_int = FALSE;
10883 }
10884
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010885 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010886 {
10887 if (sps_flags & SPS_BEST)
10888 /* Adjust the word score for how it sounds like. */
10889 rescore_suggestions(su);
10890
Bram Moolenaar4770d092006-01-12 23:22:24 +000010891 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10892 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010893 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010894 }
10895}
10896
10897/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010898 * Load the .sug files for languages that have one and weren't loaded yet.
10899 */
10900 static void
10901suggest_load_files()
10902{
10903 langp_T *lp;
10904 int lpi;
10905 slang_T *slang;
10906 char_u *dotp;
10907 FILE *fd;
10908 char_u buf[MAXWLEN];
10909 int i;
10910 time_t timestamp;
10911 int wcount;
10912 int wordnr;
10913 garray_T ga;
10914 int c;
10915
10916 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010917 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010918 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010919 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010920 slang = lp->lp_slang;
10921 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10922 {
10923 /* Change ".spl" to ".sug" and open the file. When the file isn't
10924 * found silently skip it. Do set "sl_sugloaded" so that we
10925 * don't try again and again. */
10926 slang->sl_sugloaded = TRUE;
10927
10928 dotp = vim_strrchr(slang->sl_fname, '.');
10929 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10930 continue;
10931 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010932 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010933 if (fd == NULL)
10934 goto nextone;
10935
10936 /*
10937 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10938 */
10939 for (i = 0; i < VIMSUGMAGICL; ++i)
10940 buf[i] = getc(fd); /* <fileID> */
10941 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10942 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010943 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010944 slang->sl_fname);
10945 goto nextone;
10946 }
10947 c = getc(fd); /* <versionnr> */
10948 if (c < VIMSUGVERSION)
10949 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010950 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010951 slang->sl_fname);
10952 goto nextone;
10953 }
10954 else if (c > VIMSUGVERSION)
10955 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010956 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010957 slang->sl_fname);
10958 goto nextone;
10959 }
10960
10961 /* Check the timestamp, it must be exactly the same as the one in
10962 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010963 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010964 if (timestamp != slang->sl_sugtime)
10965 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010966 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010967 slang->sl_fname);
10968 goto nextone;
10969 }
10970
10971 /*
10972 * <SUGWORDTREE>: <wordtree>
10973 * Read the trie with the soundfolded words.
10974 */
10975 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10976 FALSE, 0) != 0)
10977 {
10978someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010979 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010980 slang->sl_fname);
10981 slang_clear_sug(slang);
10982 goto nextone;
10983 }
10984
10985 /*
10986 * <SUGTABLE>: <sugwcount> <sugline> ...
10987 *
10988 * Read the table with word numbers. We use a file buffer for
10989 * this, because it's so much like a file with lines. Makes it
10990 * possible to swap the info and save on memory use.
10991 */
10992 slang->sl_sugbuf = open_spellbuf();
10993 if (slang->sl_sugbuf == NULL)
10994 goto someerror;
10995 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010996 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010997 if (wcount < 0)
10998 goto someerror;
10999
11000 /* Read all the wordnr lists into the buffer, one NUL terminated
11001 * list per line. */
11002 ga_init2(&ga, 1, 100);
11003 for (wordnr = 0; wordnr < wcount; ++wordnr)
11004 {
11005 ga.ga_len = 0;
11006 for (;;)
11007 {
11008 c = getc(fd); /* <sugline> */
11009 if (c < 0 || ga_grow(&ga, 1) == FAIL)
11010 goto someerror;
11011 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
11012 if (c == NUL)
11013 break;
11014 }
11015 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
11016 ga.ga_data, ga.ga_len, TRUE) == FAIL)
11017 goto someerror;
11018 }
11019 ga_clear(&ga);
11020
11021 /*
11022 * Need to put word counts in the word tries, so that we can find
11023 * a word by its number.
11024 */
11025 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
11026 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
11027
11028nextone:
11029 if (fd != NULL)
11030 fclose(fd);
11031 STRCPY(dotp, ".spl");
11032 }
11033 }
11034}
11035
11036
11037/*
11038 * Fill in the wordcount fields for a trie.
11039 * Returns the total number of words.
11040 */
11041 static void
11042tree_count_words(byts, idxs)
11043 char_u *byts;
11044 idx_T *idxs;
11045{
11046 int depth;
11047 idx_T arridx[MAXWLEN];
11048 int curi[MAXWLEN];
11049 int c;
11050 idx_T n;
11051 int wordcount[MAXWLEN];
11052
11053 arridx[0] = 0;
11054 curi[0] = 1;
11055 wordcount[0] = 0;
11056 depth = 0;
11057 while (depth >= 0 && !got_int)
11058 {
11059 if (curi[depth] > byts[arridx[depth]])
11060 {
11061 /* Done all bytes at this node, go up one level. */
11062 idxs[arridx[depth]] = wordcount[depth];
11063 if (depth > 0)
11064 wordcount[depth - 1] += wordcount[depth];
11065
11066 --depth;
11067 fast_breakcheck();
11068 }
11069 else
11070 {
11071 /* Do one more byte at this node. */
11072 n = arridx[depth] + curi[depth];
11073 ++curi[depth];
11074
11075 c = byts[n];
11076 if (c == 0)
11077 {
11078 /* End of word, count it. */
11079 ++wordcount[depth];
11080
11081 /* Skip over any other NUL bytes (same word with different
11082 * flags). */
11083 while (byts[n + 1] == 0)
11084 {
11085 ++n;
11086 ++curi[depth];
11087 }
11088 }
11089 else
11090 {
11091 /* Normal char, go one level deeper to count the words. */
11092 ++depth;
11093 arridx[depth] = idxs[n];
11094 curi[depth] = 1;
11095 wordcount[depth] = 0;
11096 }
11097 }
11098 }
11099}
11100
11101/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011102 * Free the info put in "*su" by spell_find_suggest().
11103 */
11104 static void
11105spell_find_cleanup(su)
11106 suginfo_T *su;
11107{
11108 int i;
11109
11110 /* Free the suggestions. */
11111 for (i = 0; i < su->su_ga.ga_len; ++i)
11112 vim_free(SUG(su->su_ga, i).st_word);
11113 ga_clear(&su->su_ga);
11114 for (i = 0; i < su->su_sga.ga_len; ++i)
11115 vim_free(SUG(su->su_sga, i).st_word);
11116 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117
11118 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011119 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120}
11121
11122/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011123 * Make a copy of "word", with the first letter upper or lower cased, to
11124 * "wcopy[MAXWLEN]". "word" must not be empty.
11125 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 */
11127 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011128onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 char_u *wcopy;
11131 int upper; /* TRUE: first letter made upper case */
11132{
11133 char_u *p;
11134 int c;
11135 int l;
11136
11137 p = word;
11138#ifdef FEAT_MBYTE
11139 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011140 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 else
11142#endif
11143 c = *p++;
11144 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011145 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011147 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148#ifdef FEAT_MBYTE
11149 if (has_mbyte)
11150 l = mb_char2bytes(c, wcopy);
11151 else
11152#endif
11153 {
11154 l = 1;
11155 wcopy[0] = c;
11156 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011157 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158}
11159
11160/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011161 * Make a copy of "word" with all the letters upper cased into
11162 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 */
11164 static void
11165allcap_copy(word, wcopy)
11166 char_u *word;
11167 char_u *wcopy;
11168{
11169 char_u *s;
11170 char_u *d;
11171 int c;
11172
11173 d = wcopy;
11174 for (s = word; *s != NUL; )
11175 {
11176#ifdef FEAT_MBYTE
11177 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011178 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 else
11180#endif
11181 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011182
11183#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +020011184 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +000011185 * would cause weird errors in other 8-bit encodings. */
11186 if (enc_latin1like && c == 0xdf)
11187 {
11188 c = 'S';
11189 if (d - wcopy >= MAXWLEN - 1)
11190 break;
11191 *d++ = c;
11192 }
11193 else
11194#endif
11195 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196
11197#ifdef FEAT_MBYTE
11198 if (has_mbyte)
11199 {
11200 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11201 break;
11202 d += mb_char2bytes(c, d);
11203 }
11204 else
11205#endif
11206 {
11207 if (d - wcopy >= MAXWLEN - 1)
11208 break;
11209 *d++ = c;
11210 }
11211 }
11212 *d = NUL;
11213}
11214
11215/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011216 * Try finding suggestions by recognizing specific situations.
11217 */
11218 static void
11219suggest_try_special(su)
11220 suginfo_T *su;
11221{
11222 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011223 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011224 int c;
11225 char_u word[MAXWLEN];
11226
11227 /*
11228 * Recognize a word that is repeated: "the the".
11229 */
11230 p = skiptowhite(su->su_fbadword);
11231 len = p - su->su_fbadword;
11232 p = skipwhite(p);
11233 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11234 {
11235 /* Include badflags: if the badword is onecap or allcap
11236 * use that for the goodword too: "The the" -> "The". */
11237 c = su->su_fbadword[len];
11238 su->su_fbadword[len] = NUL;
11239 make_case_word(su->su_fbadword, word, su->su_badflags);
11240 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011241
11242 /* Give a soundalike score of 0, compute the score as if deleting one
11243 * character. */
11244 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011245 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011246 }
11247}
11248
11249/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 * Try finding suggestions by adding/removing/swapping letters.
11251 */
11252 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011253suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 suginfo_T *su;
11255{
11256 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011257 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011258 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011259 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011260 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011261
11262 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011263 * to find matches (esp. REP items). Append some more text, changing
11264 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011266 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011267 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011268 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269
Bram Moolenaar860cae12010-06-05 23:22:07 +020011270 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011271 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011272 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011273
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011274 /* If reloading a spell file fails it's still in the list but
11275 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011276 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011277 continue;
11278
Bram Moolenaar4770d092006-01-12 23:22:24 +000011279 /* Try it for this language. Will add possible suggestions. */
11280 suggest_trie_walk(su, lp, fword, FALSE);
11281 }
11282}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283
Bram Moolenaar4770d092006-01-12 23:22:24 +000011284/* Check the maximum score, if we go over it we won't try this change. */
11285#define TRY_DEEPER(su, stack, depth, add) \
11286 (stack[depth].ts_score + (add) < su->su_maxscore)
11287
11288/*
11289 * Try finding suggestions by adding/removing/swapping letters.
11290 *
11291 * This uses a state machine. At each node in the tree we try various
11292 * operations. When trying if an operation works "depth" is increased and the
11293 * stack[] is used to store info. This allows combinations, thus insert one
11294 * character, replace one and delete another. The number of changes is
11295 * limited by su->su_maxscore.
11296 *
11297 * After implementing this I noticed an article by Kemal Oflazer that
11298 * describes something similar: "Error-tolerant Finite State Recognition with
11299 * Applications to Morphological Analysis and Spelling Correction" (1996).
11300 * The implementation in the article is simplified and requires a stack of
11301 * unknown depth. The implementation here only needs a stack depth equal to
11302 * the length of the word.
11303 *
11304 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11305 * The mechanism is the same, but we find a match with a sound-folded word
11306 * that comes from one or more original words. Each of these words may be
11307 * added, this is done by add_sound_suggest().
11308 * Don't use:
11309 * the prefix tree or the keep-case tree
11310 * "su->su_badlen"
11311 * anything to do with upper and lower case
11312 * anything to do with word or non-word characters ("spell_iswordp()")
11313 * banned words
11314 * word flags (rare, region, compounding)
11315 * word splitting for now
11316 * "similar_chars()"
11317 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11318 */
11319 static void
11320suggest_trie_walk(su, lp, fword, soundfold)
11321 suginfo_T *su;
11322 langp_T *lp;
11323 char_u *fword;
11324 int soundfold;
11325{
11326 char_u tword[MAXWLEN]; /* good word collected so far */
11327 trystate_T stack[MAXWLEN];
11328 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011329 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011330 * words and split word. NUL terminated
11331 * when going deeper but not when coming
11332 * back. */
11333 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11334 trystate_T *sp;
11335 int newscore;
11336 int score;
11337 char_u *byts, *fbyts, *pbyts;
11338 idx_T *idxs, *fidxs, *pidxs;
11339 int depth;
11340 int c, c2, c3;
11341 int n = 0;
11342 int flags;
11343 garray_T *gap;
11344 idx_T arridx;
11345 int len;
11346 char_u *p;
11347 fromto_T *ftp;
11348 int fl = 0, tl;
11349 int repextra = 0; /* extra bytes in fword[] from REP item */
11350 slang_T *slang = lp->lp_slang;
11351 int fword_ends;
11352 int goodword_ends;
11353#ifdef DEBUG_TRIEWALK
11354 /* Stores the name of the change made at each level. */
11355 char_u changename[MAXWLEN][80];
11356#endif
11357 int breakcheckcount = 1000;
11358 int compound_ok;
11359
11360 /*
11361 * Go through the whole case-fold tree, try changes at each node.
11362 * "tword[]" contains the word collected from nodes in the tree.
11363 * "fword[]" the word we are trying to match with (initially the bad
11364 * word).
11365 */
11366 depth = 0;
11367 sp = &stack[0];
11368 vim_memset(sp, 0, sizeof(trystate_T));
11369 sp->ts_curi = 1;
11370
11371 if (soundfold)
11372 {
11373 /* Going through the soundfold tree. */
11374 byts = fbyts = slang->sl_sbyts;
11375 idxs = fidxs = slang->sl_sidxs;
11376 pbyts = NULL;
11377 pidxs = NULL;
11378 sp->ts_prefixdepth = PFD_NOPREFIX;
11379 sp->ts_state = STATE_START;
11380 }
11381 else
11382 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011383 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011384 * When there are postponed prefixes we need to use these first. At
11385 * the end of the prefix we continue in the case-fold tree.
11386 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011387 fbyts = slang->sl_fbyts;
11388 fidxs = slang->sl_fidxs;
11389 pbyts = slang->sl_pbyts;
11390 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011391 if (pbyts != NULL)
11392 {
11393 byts = pbyts;
11394 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011395 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011396 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11397 }
11398 else
11399 {
11400 byts = fbyts;
11401 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011402 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011403 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011404 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011405 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011406
Bram Moolenaar4770d092006-01-12 23:22:24 +000011407 /*
11408 * Loop to find all suggestions. At each round we either:
11409 * - For the current state try one operation, advance "ts_curi",
11410 * increase "depth".
11411 * - When a state is done go to the next, set "ts_state".
11412 * - When all states are tried decrease "depth".
11413 */
11414 while (depth >= 0 && !got_int)
11415 {
11416 sp = &stack[depth];
11417 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011418 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011419 case STATE_START:
11420 case STATE_NOPREFIX:
11421 /*
11422 * Start of node: Deal with NUL bytes, which means
11423 * tword[] may end here.
11424 */
11425 arridx = sp->ts_arridx; /* current node in the tree */
11426 len = byts[arridx]; /* bytes in this node */
11427 arridx += sp->ts_curi; /* index of current byte */
11428
11429 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011431 /* Skip over the NUL bytes, we use them later. */
11432 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11433 ;
11434 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435
Bram Moolenaar4770d092006-01-12 23:22:24 +000011436 /* Always past NUL bytes now. */
11437 n = (int)sp->ts_state;
11438 sp->ts_state = STATE_ENDNUL;
11439 sp->ts_save_badflags = su->su_badflags;
11440
11441 /* At end of a prefix or at start of prefixtree: check for
11442 * following word. */
11443 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011444 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011445 /* Set su->su_badflags to the caps type at this position.
11446 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011447#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011448 if (has_mbyte)
11449 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11450 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011451#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011452 n = sp->ts_fidx;
11453 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11454 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011455 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011456#ifdef DEBUG_TRIEWALK
11457 sprintf(changename[depth], "prefix");
11458#endif
11459 go_deeper(stack, depth, 0);
11460 ++depth;
11461 sp = &stack[depth];
11462 sp->ts_prefixdepth = depth - 1;
11463 byts = fbyts;
11464 idxs = fidxs;
11465 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011466
Bram Moolenaar4770d092006-01-12 23:22:24 +000011467 /* Move the prefix to preword[] with the right case
11468 * and make find_keepcap_word() works. */
11469 tword[sp->ts_twordlen] = NUL;
11470 make_case_word(tword + sp->ts_splitoff,
11471 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011472 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011474 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011475 break;
11476 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011477
Bram Moolenaar4770d092006-01-12 23:22:24 +000011478 if (sp->ts_curi > len || byts[arridx] != 0)
11479 {
11480 /* Past bytes in node and/or past NUL bytes. */
11481 sp->ts_state = STATE_ENDNUL;
11482 sp->ts_save_badflags = su->su_badflags;
11483 break;
11484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011485
Bram Moolenaar4770d092006-01-12 23:22:24 +000011486 /*
11487 * End of word in tree.
11488 */
11489 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490
Bram Moolenaar4770d092006-01-12 23:22:24 +000011491 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011492
11493 /* Skip words with the NOSUGGEST flag. */
11494 if (flags & WF_NOSUGGEST)
11495 break;
11496
Bram Moolenaar4770d092006-01-12 23:22:24 +000011497 fword_ends = (fword[sp->ts_fidx] == NUL
11498 || (soundfold
11499 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011500 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011501 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502
Bram Moolenaar4770d092006-01-12 23:22:24 +000011503 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011504 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 {
11506 /* There was a prefix before the word. Check that the prefix
11507 * can be used with this word. */
11508 /* Count the length of the NULs in the prefix. If there are
11509 * none this must be the first try without a prefix. */
11510 n = stack[sp->ts_prefixdepth].ts_arridx;
11511 len = pbyts[n++];
11512 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11513 ;
11514 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011515 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011516 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011517 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011518 if (c == 0)
11519 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011520
Bram Moolenaar4770d092006-01-12 23:22:24 +000011521 /* Use the WF_RARE flag for a rare prefix. */
11522 if (c & WF_RAREPFX)
11523 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011524
Bram Moolenaar4770d092006-01-12 23:22:24 +000011525 /* Tricky: when checking for both prefix and compounding
11526 * we run into the prefix flag first.
11527 * Remember that it's OK, so that we accept the prefix
11528 * when arriving at a compound flag. */
11529 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011530 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011531 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011532
Bram Moolenaar4770d092006-01-12 23:22:24 +000011533 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11534 * appending another compound word below. */
11535 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011536 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011537 goodword_ends = FALSE;
11538 else
11539 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011540
Bram Moolenaar4770d092006-01-12 23:22:24 +000011541 p = NULL;
11542 compound_ok = TRUE;
11543 if (sp->ts_complen > sp->ts_compsplit)
11544 {
11545 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011546 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011547 /* There was a word before this word. When there was no
11548 * change in this word (it was correct) add the first word
11549 * as a suggestion. If this word was corrected too, we
11550 * need to check if a correct word follows. */
11551 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011552 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011553 && STRNCMP(fword + sp->ts_splitfidx,
11554 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011555 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011556 {
11557 preword[sp->ts_prewordlen] = NUL;
11558 newscore = score_wordcount_adj(slang, sp->ts_score,
11559 preword + sp->ts_prewordlen,
11560 sp->ts_prewordlen > 0);
11561 /* Add the suggestion if the score isn't too bad. */
11562 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011563 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011564 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011565 newscore, 0, FALSE,
11566 lp->lp_sallang, FALSE);
11567 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011568 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011569 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011570 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011571 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011572 /* There was a compound word before this word. If this
11573 * word does not support compounding then give up
11574 * (splitting is tried for the word without compound
11575 * flag). */
11576 if (((unsigned)flags >> 24) == 0
11577 || sp->ts_twordlen - sp->ts_splitoff
11578 < slang->sl_compminlen)
11579 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011580#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011581 /* For multi-byte chars check character length against
11582 * COMPOUNDMIN. */
11583 if (has_mbyte
11584 && slang->sl_compminlen > 0
11585 && mb_charlen(tword + sp->ts_splitoff)
11586 < slang->sl_compminlen)
11587 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011588#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011589
Bram Moolenaar4770d092006-01-12 23:22:24 +000011590 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11591 compflags[sp->ts_complen + 1] = NUL;
11592 vim_strncpy(preword + sp->ts_prewordlen,
11593 tword + sp->ts_splitoff,
11594 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011595
11596 /* Verify CHECKCOMPOUNDPATTERN rules. */
11597 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11598 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011599 compound_ok = FALSE;
11600
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011601 if (compound_ok)
11602 {
11603 p = preword;
11604 while (*skiptowhite(p) != NUL)
11605 p = skipwhite(skiptowhite(p));
11606 if (fword_ends && !can_compound(slang, p,
11607 compflags + sp->ts_compsplit))
11608 /* Compound is not allowed. But it may still be
11609 * possible if we add another (short) word. */
11610 compound_ok = FALSE;
11611 }
11612
Bram Moolenaar4770d092006-01-12 23:22:24 +000011613 /* Get pointer to last char of previous word. */
11614 p = preword + sp->ts_prewordlen;
11615 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011616 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011617 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011618
Bram Moolenaar4770d092006-01-12 23:22:24 +000011619 /*
11620 * Form the word with proper case in preword.
11621 * If there is a word from a previous split, append.
11622 * For the soundfold tree don't change the case, simply append.
11623 */
11624 if (soundfold)
11625 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11626 else if (flags & WF_KEEPCAP)
11627 /* Must find the word in the keep-case tree. */
11628 find_keepcap_word(slang, tword + sp->ts_splitoff,
11629 preword + sp->ts_prewordlen);
11630 else
11631 {
11632 /* Include badflags: If the badword is onecap or allcap
11633 * use that for the goodword too. But if the badword is
11634 * allcap and it's only one char long use onecap. */
11635 c = su->su_badflags;
11636 if ((c & WF_ALLCAP)
11637#ifdef FEAT_MBYTE
11638 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11639#else
11640 && su->su_badlen == 1
11641#endif
11642 )
11643 c = WF_ONECAP;
11644 c |= flags;
11645
11646 /* When appending a compound word after a word character don't
11647 * use Onecap. */
11648 if (p != NULL && spell_iswordp_nmw(p))
11649 c &= ~WF_ONECAP;
11650 make_case_word(tword + sp->ts_splitoff,
11651 preword + sp->ts_prewordlen, c);
11652 }
11653
11654 if (!soundfold)
11655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 /* Don't use a banned word. It may appear again as a good
11657 * word, thus remember it. */
11658 if (flags & WF_BANNED)
11659 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011660 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 break;
11662 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011663 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011664 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11665 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011666 {
11667 if (slang->sl_compprog == NULL)
11668 break;
11669 /* the word so far was banned but we may try compounding */
11670 goodword_ends = FALSE;
11671 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673
Bram Moolenaar4770d092006-01-12 23:22:24 +000011674 newscore = 0;
11675 if (!soundfold) /* soundfold words don't have flags */
11676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011678 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 newscore += SCORE_REGION;
11680 if (flags & WF_RARE)
11681 newscore += SCORE_RARE;
11682
Bram Moolenaar0c405862005-06-22 22:26:26 +000011683 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011684 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011685 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687
Bram Moolenaar4770d092006-01-12 23:22:24 +000011688 /* TODO: how about splitting in the soundfold tree? */
11689 if (fword_ends
11690 && goodword_ends
11691 && sp->ts_fidx >= sp->ts_fidxtry
11692 && compound_ok)
11693 {
11694 /* The badword also ends: add suggestions. */
11695#ifdef DEBUG_TRIEWALK
11696 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011698 int j;
11699
11700 /* print the stack of changes that brought us here */
11701 smsg("------ %s -------", fword);
11702 for (j = 0; j < depth; ++j)
11703 smsg("%s", changename[j]);
11704 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011705#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011706 if (soundfold)
11707 {
11708 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011709 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011710 add_sound_suggest(su, preword, sp->ts_score, lp);
11711 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011712 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011713 {
11714 /* Give a penalty when changing non-word char to word
11715 * char, e.g., "thes," -> "these". */
11716 p = fword + sp->ts_fidx;
11717 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011718 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011719 {
11720 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011721 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011722 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011723 newscore += SCORE_NONWORD;
11724 }
11725
Bram Moolenaar4770d092006-01-12 23:22:24 +000011726 /* Give a bonus to words seen before. */
11727 score = score_wordcount_adj(slang,
11728 sp->ts_score + newscore,
11729 preword + sp->ts_prewordlen,
11730 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011731
Bram Moolenaar4770d092006-01-12 23:22:24 +000011732 /* Add the suggestion if the score isn't too bad. */
11733 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011734 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011735 add_suggestion(su, &su->su_ga, preword,
11736 sp->ts_fidx - repextra,
11737 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011738
11739 if (su->su_badflags & WF_MIXCAP)
11740 {
11741 /* We really don't know if the word should be
11742 * upper or lower case, add both. */
11743 c = captype(preword, NULL);
11744 if (c == 0 || c == WF_ALLCAP)
11745 {
11746 make_case_word(tword + sp->ts_splitoff,
11747 preword + sp->ts_prewordlen,
11748 c == 0 ? WF_ALLCAP : 0);
11749
11750 add_suggestion(su, &su->su_ga, preword,
11751 sp->ts_fidx - repextra,
11752 score + SCORE_ICASE, 0, FALSE,
11753 lp->lp_sallang, FALSE);
11754 }
11755 }
11756 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011757 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011758 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011759
Bram Moolenaar4770d092006-01-12 23:22:24 +000011760 /*
11761 * Try word split and/or compounding.
11762 */
11763 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011764#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011765 /* Don't split halfway a character. */
11766 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011767#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011768 )
11769 {
11770 int try_compound;
11771 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011772
Bram Moolenaar4770d092006-01-12 23:22:24 +000011773 /* If past the end of the bad word don't try a split.
11774 * Otherwise try changing the next word. E.g., find
11775 * suggestions for "the the" where the second "the" is
11776 * different. It's done like a split.
11777 * TODO: word split for soundfold words */
11778 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11779 && !soundfold;
11780
11781 /* Get here in several situations:
11782 * 1. The word in the tree ends:
11783 * If the word allows compounding try that. Otherwise try
11784 * a split by inserting a space. For both check that a
11785 * valid words starts at fword[sp->ts_fidx].
11786 * For NOBREAK do like compounding to be able to check if
11787 * the next word is valid.
11788 * 2. The badword does end, but it was due to a change (e.g.,
11789 * a swap). No need to split, but do check that the
11790 * following word is valid.
11791 * 3. The badword and the word in the tree end. It may still
11792 * be possible to compound another (short) word.
11793 */
11794 try_compound = FALSE;
11795 if (!soundfold
11796 && slang->sl_compprog != NULL
11797 && ((unsigned)flags >> 24) != 0
11798 && sp->ts_twordlen - sp->ts_splitoff
11799 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011800#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011801 && (!has_mbyte
11802 || slang->sl_compminlen == 0
11803 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011804 >= slang->sl_compminlen)
11805#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011806 && (slang->sl_compsylmax < MAXWLEN
11807 || sp->ts_complen + 1 - sp->ts_compsplit
11808 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011809 && (can_be_compound(sp, slang,
11810 compflags, ((unsigned)flags >> 24))))
11811
Bram Moolenaar4770d092006-01-12 23:22:24 +000011812 {
11813 try_compound = TRUE;
11814 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11815 compflags[sp->ts_complen + 1] = NUL;
11816 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011817
Bram Moolenaar4770d092006-01-12 23:22:24 +000011818 /* For NOBREAK we never try splitting, it won't make any word
11819 * valid. */
11820 if (slang->sl_nobreak)
11821 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011822
Bram Moolenaar4770d092006-01-12 23:22:24 +000011823 /* If we could add a compound word, and it's also possible to
11824 * split at this point, do the split first and set
11825 * TSF_DIDSPLIT to avoid doing it again. */
11826 else if (!fword_ends
11827 && try_compound
11828 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11829 {
11830 try_compound = FALSE;
11831 sp->ts_flags |= TSF_DIDSPLIT;
11832 --sp->ts_curi; /* do the same NUL again */
11833 compflags[sp->ts_complen] = NUL;
11834 }
11835 else
11836 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011837
Bram Moolenaar4770d092006-01-12 23:22:24 +000011838 if (try_split || try_compound)
11839 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011840 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011841 {
11842 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011843 * words so far are valid for compounding. If there
11844 * is only one word it must not have the NEEDCOMPOUND
11845 * flag. */
11846 if (sp->ts_complen == sp->ts_compsplit
11847 && (flags & WF_NEEDCOMP))
11848 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011849 p = preword;
11850 while (*skiptowhite(p) != NUL)
11851 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011852 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011853 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011854 compflags + sp->ts_compsplit))
11855 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011856
11857 if (slang->sl_nosplitsugs)
11858 newscore += SCORE_SPLIT_NO;
11859 else
11860 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011861
11862 /* Give a bonus to words seen before. */
11863 newscore = score_wordcount_adj(slang, newscore,
11864 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011865 }
11866
Bram Moolenaar4770d092006-01-12 23:22:24 +000011867 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011869 go_deeper(stack, depth, newscore);
11870#ifdef DEBUG_TRIEWALK
11871 if (!try_compound && !fword_ends)
11872 sprintf(changename[depth], "%.*s-%s: split",
11873 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11874 else
11875 sprintf(changename[depth], "%.*s-%s: compound",
11876 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011878 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011879 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011880 sp->ts_state = STATE_SPLITUNDO;
11881
11882 ++depth;
11883 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011884
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011885 /* Append a space to preword when splitting. */
11886 if (!try_compound && !fword_ends)
11887 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011888 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011889 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011890 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011891
11892 /* If the badword has a non-word character at this
11893 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011894 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011895 * character when the word ends. But only when the
11896 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011897 if (((!try_compound && !spell_iswordp_nmw(fword
11898 + sp->ts_fidx))
11899 || fword_ends)
11900 && fword[sp->ts_fidx] != NUL
11901 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011902 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011903 int l;
11904
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011905#ifdef FEAT_MBYTE
11906 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011907 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011908 else
11909#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011910 l = 1;
11911 if (fword_ends)
11912 {
11913 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011914 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011915 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011916 sp->ts_prewordlen += l;
11917 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011918 }
11919 else
11920 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11921 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011922 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011923
Bram Moolenaard12a1322005-08-21 22:08:24 +000011924 /* When compounding include compound flag in
11925 * compflags[] (already set above). When splitting we
11926 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011927 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011928 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011929 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011930 sp->ts_compsplit = sp->ts_complen;
11931 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011932
Bram Moolenaar53805d12005-08-01 07:08:33 +000011933 /* set su->su_badflags to the caps type at this
11934 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011935#ifdef FEAT_MBYTE
11936 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011937 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011938 else
11939#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011940 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011941 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011942 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011945 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011946
11947 /* If there are postponed prefixes, try these too. */
11948 if (pbyts != NULL)
11949 {
11950 byts = pbyts;
11951 idxs = pidxs;
11952 sp->ts_prefixdepth = PFD_PREFIXTREE;
11953 sp->ts_state = STATE_NOPREFIX;
11954 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955 }
11956 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011957 }
11958 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959
Bram Moolenaar4770d092006-01-12 23:22:24 +000011960 case STATE_SPLITUNDO:
11961 /* Undo the changes done for word split or compound word. */
11962 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 /* Continue looking for NUL bytes. */
11965 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011966
Bram Moolenaar4770d092006-01-12 23:22:24 +000011967 /* In case we went into the prefix tree. */
11968 byts = fbyts;
11969 idxs = fidxs;
11970 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971
Bram Moolenaar4770d092006-01-12 23:22:24 +000011972 case STATE_ENDNUL:
11973 /* Past the NUL bytes in the node. */
11974 su->su_badflags = sp->ts_save_badflags;
11975 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011976#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011977 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011978#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011979 )
11980 {
11981 /* The badword ends, can't use STATE_PLAIN. */
11982 sp->ts_state = STATE_DEL;
11983 break;
11984 }
11985 sp->ts_state = STATE_PLAIN;
11986 /*FALLTHROUGH*/
11987
11988 case STATE_PLAIN:
11989 /*
11990 * Go over all possible bytes at this node, add each to tword[]
11991 * and use child node. "ts_curi" is the index.
11992 */
11993 arridx = sp->ts_arridx;
11994 if (sp->ts_curi > byts[arridx])
11995 {
11996 /* Done all bytes at this node, do next state. When still at
11997 * already changed bytes skip the other tricks. */
11998 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011999 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012001 sp->ts_state = STATE_FINAL;
12002 }
12003 else
12004 {
12005 arridx += sp->ts_curi++;
12006 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012007
Bram Moolenaar4770d092006-01-12 23:22:24 +000012008 /* Normal byte, go one level deeper. If it's not equal to the
12009 * byte in the bad word adjust the score. But don't even try
12010 * when the byte was already changed. And don't try when we
12011 * just deleted this byte, accepting it is always cheaper then
12012 * delete + substitute. */
12013 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000012014#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012015 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012016#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012017 )
12018 newscore = 0;
12019 else
12020 newscore = SCORE_SUBST;
12021 if ((newscore == 0
12022 || (sp->ts_fidx >= sp->ts_fidxtry
12023 && ((sp->ts_flags & TSF_DIDDEL) == 0
12024 || c != fword[sp->ts_delidx])))
12025 && TRY_DEEPER(su, stack, depth, newscore))
12026 {
12027 go_deeper(stack, depth, newscore);
12028#ifdef DEBUG_TRIEWALK
12029 if (newscore > 0)
12030 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12031 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12032 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012033 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012034 sprintf(changename[depth], "%.*s-%s: accept %c",
12035 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12036 fword[sp->ts_fidx]);
12037#endif
12038 ++depth;
12039 sp = &stack[depth];
12040 ++sp->ts_fidx;
12041 tword[sp->ts_twordlen++] = c;
12042 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012043#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012044 if (newscore == SCORE_SUBST)
12045 sp->ts_isdiff = DIFF_YES;
12046 if (has_mbyte)
12047 {
12048 /* Multi-byte characters are a bit complicated to
12049 * handle: They differ when any of the bytes differ
12050 * and then their length may also differ. */
12051 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012052 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012053 /* First byte. */
12054 sp->ts_tcharidx = 0;
12055 sp->ts_tcharlen = MB_BYTE2LEN(c);
12056 sp->ts_fcharstart = sp->ts_fidx - 1;
12057 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012058 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012059 }
12060 else if (sp->ts_isdiff == DIFF_INSERT)
12061 /* When inserting trail bytes don't advance in the
12062 * bad word. */
12063 --sp->ts_fidx;
12064 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12065 {
12066 /* Last byte of character. */
12067 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012068 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012069 /* Correct ts_fidx for the byte length of the
12070 * character (we didn't check that before). */
12071 sp->ts_fidx = sp->ts_fcharstart
12072 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012073 fword[sp->ts_fcharstart]);
12074
Bram Moolenaar4770d092006-01-12 23:22:24 +000012075 /* For changing a composing character adjust
12076 * the score from SCORE_SUBST to
12077 * SCORE_SUBCOMP. */
12078 if (enc_utf8
12079 && utf_iscomposing(
12080 mb_ptr2char(tword
12081 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012082 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012083 && utf_iscomposing(
12084 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012085 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012086 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012087 SCORE_SUBST - SCORE_SUBCOMP;
12088
Bram Moolenaar4770d092006-01-12 23:22:24 +000012089 /* For a similar character adjust score from
12090 * SCORE_SUBST to SCORE_SIMILAR. */
12091 else if (!soundfold
12092 && slang->sl_has_map
12093 && similar_chars(slang,
12094 mb_ptr2char(tword
12095 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012096 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012097 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012098 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012099 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012100 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012101 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012102 else if (sp->ts_isdiff == DIFF_INSERT
12103 && sp->ts_twordlen > sp->ts_tcharlen)
12104 {
12105 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12106 c = mb_ptr2char(p);
12107 if (enc_utf8 && utf_iscomposing(c))
12108 {
12109 /* Inserting a composing char doesn't
12110 * count that much. */
12111 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12112 }
12113 else
12114 {
12115 /* If the previous character was the same,
12116 * thus doubling a character, give a bonus
12117 * to the score. Also for the soundfold
12118 * tree (might seem illogical but does
12119 * give better scores). */
12120 mb_ptr_back(tword, p);
12121 if (c == mb_ptr2char(p))
12122 sp->ts_score -= SCORE_INS
12123 - SCORE_INSDUP;
12124 }
12125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012126
Bram Moolenaar4770d092006-01-12 23:22:24 +000012127 /* Starting a new char, reset the length. */
12128 sp->ts_tcharlen = 0;
12129 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012130 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012131 else
12132#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012133 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012134 /* If we found a similar char adjust the score.
12135 * We do this after calling go_deeper() because
12136 * it's slow. */
12137 if (newscore != 0
12138 && !soundfold
12139 && slang->sl_has_map
12140 && similar_chars(slang,
12141 c, fword[sp->ts_fidx - 1]))
12142 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012145 }
12146 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147
Bram Moolenaar4770d092006-01-12 23:22:24 +000012148 case STATE_DEL:
12149#ifdef FEAT_MBYTE
12150 /* When past the first byte of a multi-byte char don't try
12151 * delete/insert/swap a character. */
12152 if (has_mbyte && sp->ts_tcharlen > 0)
12153 {
12154 sp->ts_state = STATE_FINAL;
12155 break;
12156 }
12157#endif
12158 /*
12159 * Try skipping one character in the bad word (delete it).
12160 */
12161 sp->ts_state = STATE_INS_PREP;
12162 sp->ts_curi = 1;
12163 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12164 /* Deleting a vowel at the start of a word counts less, see
12165 * soundalike_score(). */
12166 newscore = 2 * SCORE_DEL / 3;
12167 else
12168 newscore = SCORE_DEL;
12169 if (fword[sp->ts_fidx] != NUL
12170 && TRY_DEEPER(su, stack, depth, newscore))
12171 {
12172 go_deeper(stack, depth, newscore);
12173#ifdef DEBUG_TRIEWALK
12174 sprintf(changename[depth], "%.*s-%s: delete %c",
12175 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12176 fword[sp->ts_fidx]);
12177#endif
12178 ++depth;
12179
12180 /* Remember what character we deleted, so that we can avoid
12181 * inserting it again. */
12182 stack[depth].ts_flags |= TSF_DIDDEL;
12183 stack[depth].ts_delidx = sp->ts_fidx;
12184
12185 /* Advance over the character in fword[]. Give a bonus to the
12186 * score if the same character is following "nn" -> "n". It's
12187 * a bit illogical for soundfold tree but it does give better
12188 * results. */
12189#ifdef FEAT_MBYTE
12190 if (has_mbyte)
12191 {
12192 c = mb_ptr2char(fword + sp->ts_fidx);
12193 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12194 if (enc_utf8 && utf_iscomposing(c))
12195 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12196 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12197 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12198 }
12199 else
12200#endif
12201 {
12202 ++stack[depth].ts_fidx;
12203 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12204 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12205 }
12206 break;
12207 }
12208 /*FALLTHROUGH*/
12209
12210 case STATE_INS_PREP:
12211 if (sp->ts_flags & TSF_DIDDEL)
12212 {
12213 /* If we just deleted a byte then inserting won't make sense,
12214 * a substitute is always cheaper. */
12215 sp->ts_state = STATE_SWAP;
12216 break;
12217 }
12218
12219 /* skip over NUL bytes */
12220 n = sp->ts_arridx;
12221 for (;;)
12222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 if (sp->ts_curi > byts[n])
12224 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012225 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012226 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012227 break;
12228 }
12229 if (byts[n + sp->ts_curi] != NUL)
12230 {
12231 /* Found a byte to insert. */
12232 sp->ts_state = STATE_INS;
12233 break;
12234 }
12235 ++sp->ts_curi;
12236 }
12237 break;
12238
12239 /*FALLTHROUGH*/
12240
12241 case STATE_INS:
12242 /* Insert one byte. Repeat this for each possible byte at this
12243 * node. */
12244 n = sp->ts_arridx;
12245 if (sp->ts_curi > byts[n])
12246 {
12247 /* Done all bytes at this node, go to next state. */
12248 sp->ts_state = STATE_SWAP;
12249 break;
12250 }
12251
12252 /* Do one more byte at this node, but:
12253 * - Skip NUL bytes.
12254 * - Skip the byte if it's equal to the byte in the word,
12255 * accepting that byte is always better.
12256 */
12257 n += sp->ts_curi++;
12258 c = byts[n];
12259 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12260 /* Inserting a vowel at the start of a word counts less,
12261 * see soundalike_score(). */
12262 newscore = 2 * SCORE_INS / 3;
12263 else
12264 newscore = SCORE_INS;
12265 if (c != fword[sp->ts_fidx]
12266 && TRY_DEEPER(su, stack, depth, newscore))
12267 {
12268 go_deeper(stack, depth, newscore);
12269#ifdef DEBUG_TRIEWALK
12270 sprintf(changename[depth], "%.*s-%s: insert %c",
12271 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12272 c);
12273#endif
12274 ++depth;
12275 sp = &stack[depth];
12276 tword[sp->ts_twordlen++] = c;
12277 sp->ts_arridx = idxs[n];
12278#ifdef FEAT_MBYTE
12279 if (has_mbyte)
12280 {
12281 fl = MB_BYTE2LEN(c);
12282 if (fl > 1)
12283 {
12284 /* There are following bytes for the same character.
12285 * We must find all bytes before trying
12286 * delete/insert/swap/etc. */
12287 sp->ts_tcharlen = fl;
12288 sp->ts_tcharidx = 1;
12289 sp->ts_isdiff = DIFF_INSERT;
12290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 }
12292 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012293 fl = 1;
12294 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012295#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012296 {
12297 /* If the previous character was the same, thus doubling a
12298 * character, give a bonus to the score. Also for
12299 * soundfold words (illogical but does give a better
12300 * score). */
12301 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012302 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012303 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012304 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012305 }
12306 break;
12307
12308 case STATE_SWAP:
12309 /*
12310 * Swap two bytes in the bad word: "12" -> "21".
12311 * We change "fword" here, it's changed back afterwards at
12312 * STATE_UNSWAP.
12313 */
12314 p = fword + sp->ts_fidx;
12315 c = *p;
12316 if (c == NUL)
12317 {
12318 /* End of word, can't swap or replace. */
12319 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012322
Bram Moolenaar4770d092006-01-12 23:22:24 +000012323 /* Don't swap if the first character is not a word character.
12324 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012325 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012326 {
12327 sp->ts_state = STATE_REP_INI;
12328 break;
12329 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012330
Bram Moolenaar4770d092006-01-12 23:22:24 +000012331#ifdef FEAT_MBYTE
12332 if (has_mbyte)
12333 {
12334 n = mb_cptr2len(p);
12335 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012336 if (p[n] == NUL)
12337 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012338 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012339 c2 = c; /* don't swap non-word char */
12340 else
12341 c2 = mb_ptr2char(p + n);
12342 }
12343 else
12344#endif
12345 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012346 if (p[1] == NUL)
12347 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012348 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012349 c2 = c; /* don't swap non-word char */
12350 else
12351 c2 = p[1];
12352 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012353
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012354 /* When the second character is NUL we can't swap. */
12355 if (c2 == NUL)
12356 {
12357 sp->ts_state = STATE_REP_INI;
12358 break;
12359 }
12360
Bram Moolenaar4770d092006-01-12 23:22:24 +000012361 /* When characters are identical, swap won't do anything.
12362 * Also get here if the second char is not a word character. */
12363 if (c == c2)
12364 {
12365 sp->ts_state = STATE_SWAP3;
12366 break;
12367 }
12368 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12369 {
12370 go_deeper(stack, depth, SCORE_SWAP);
12371#ifdef DEBUG_TRIEWALK
12372 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12373 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12374 c, c2);
12375#endif
12376 sp->ts_state = STATE_UNSWAP;
12377 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012378#ifdef FEAT_MBYTE
12379 if (has_mbyte)
12380 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012381 fl = mb_char2len(c2);
12382 mch_memmove(p, p + n, fl);
12383 mb_char2bytes(c, p + fl);
12384 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012385 }
12386 else
12387#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012388 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012389 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012390 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012391 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012392 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012393 }
12394 else
12395 /* If this swap doesn't work then SWAP3 won't either. */
12396 sp->ts_state = STATE_REP_INI;
12397 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012398
Bram Moolenaar4770d092006-01-12 23:22:24 +000012399 case STATE_UNSWAP:
12400 /* Undo the STATE_SWAP swap: "21" -> "12". */
12401 p = fword + sp->ts_fidx;
12402#ifdef FEAT_MBYTE
12403 if (has_mbyte)
12404 {
12405 n = MB_BYTE2LEN(*p);
12406 c = mb_ptr2char(p + n);
12407 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12408 mb_char2bytes(c, p);
12409 }
12410 else
12411#endif
12412 {
12413 c = *p;
12414 *p = p[1];
12415 p[1] = c;
12416 }
12417 /*FALLTHROUGH*/
12418
12419 case STATE_SWAP3:
12420 /* Swap two bytes, skipping one: "123" -> "321". We change
12421 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12422 p = fword + sp->ts_fidx;
12423#ifdef FEAT_MBYTE
12424 if (has_mbyte)
12425 {
12426 n = mb_cptr2len(p);
12427 c = mb_ptr2char(p);
12428 fl = mb_cptr2len(p + n);
12429 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012430 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012431 c3 = c; /* don't swap non-word char */
12432 else
12433 c3 = mb_ptr2char(p + n + fl);
12434 }
12435 else
12436#endif
12437 {
12438 c = *p;
12439 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012440 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012441 c3 = c; /* don't swap non-word char */
12442 else
12443 c3 = p[2];
12444 }
12445
12446 /* When characters are identical: "121" then SWAP3 result is
12447 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12448 * same as SWAP on next char: "112". Thus skip all swapping.
12449 * Also skip when c3 is NUL.
12450 * Also get here when the third character is not a word character.
12451 * Second character may any char: "a.b" -> "b.a" */
12452 if (c == c3 || c3 == NUL)
12453 {
12454 sp->ts_state = STATE_REP_INI;
12455 break;
12456 }
12457 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12458 {
12459 go_deeper(stack, depth, SCORE_SWAP3);
12460#ifdef DEBUG_TRIEWALK
12461 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12462 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12463 c, c3);
12464#endif
12465 sp->ts_state = STATE_UNSWAP3;
12466 ++depth;
12467#ifdef FEAT_MBYTE
12468 if (has_mbyte)
12469 {
12470 tl = mb_char2len(c3);
12471 mch_memmove(p, p + n + fl, tl);
12472 mb_char2bytes(c2, p + tl);
12473 mb_char2bytes(c, p + fl + tl);
12474 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12475 }
12476 else
12477#endif
12478 {
12479 p[0] = p[2];
12480 p[2] = c;
12481 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12482 }
12483 }
12484 else
12485 sp->ts_state = STATE_REP_INI;
12486 break;
12487
12488 case STATE_UNSWAP3:
12489 /* Undo STATE_SWAP3: "321" -> "123" */
12490 p = fword + sp->ts_fidx;
12491#ifdef FEAT_MBYTE
12492 if (has_mbyte)
12493 {
12494 n = MB_BYTE2LEN(*p);
12495 c2 = mb_ptr2char(p + n);
12496 fl = MB_BYTE2LEN(p[n]);
12497 c = mb_ptr2char(p + n + fl);
12498 tl = MB_BYTE2LEN(p[n + fl]);
12499 mch_memmove(p + fl + tl, p, n);
12500 mb_char2bytes(c, p);
12501 mb_char2bytes(c2, p + tl);
12502 p = p + tl;
12503 }
12504 else
12505#endif
12506 {
12507 c = *p;
12508 *p = p[2];
12509 p[2] = c;
12510 ++p;
12511 }
12512
Bram Moolenaar860cae12010-06-05 23:22:07 +020012513 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012514 {
12515 /* Middle char is not a word char, skip the rotate. First and
12516 * third char were already checked at swap and swap3. */
12517 sp->ts_state = STATE_REP_INI;
12518 break;
12519 }
12520
12521 /* Rotate three characters left: "123" -> "231". We change
12522 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12523 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12524 {
12525 go_deeper(stack, depth, SCORE_SWAP3);
12526#ifdef DEBUG_TRIEWALK
12527 p = fword + sp->ts_fidx;
12528 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12529 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12530 p[0], p[1], p[2]);
12531#endif
12532 sp->ts_state = STATE_UNROT3L;
12533 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012534 p = fword + sp->ts_fidx;
12535#ifdef FEAT_MBYTE
12536 if (has_mbyte)
12537 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012538 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012539 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012540 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012541 fl += mb_cptr2len(p + n + fl);
12542 mch_memmove(p, p + n, fl);
12543 mb_char2bytes(c, p + fl);
12544 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012545 }
12546 else
12547#endif
12548 {
12549 c = *p;
12550 *p = p[1];
12551 p[1] = p[2];
12552 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012553 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012554 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012555 }
12556 else
12557 sp->ts_state = STATE_REP_INI;
12558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559
Bram Moolenaar4770d092006-01-12 23:22:24 +000012560 case STATE_UNROT3L:
12561 /* Undo ROT3L: "231" -> "123" */
12562 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012563#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012564 if (has_mbyte)
12565 {
12566 n = MB_BYTE2LEN(*p);
12567 n += MB_BYTE2LEN(p[n]);
12568 c = mb_ptr2char(p + n);
12569 tl = MB_BYTE2LEN(p[n]);
12570 mch_memmove(p + tl, p, n);
12571 mb_char2bytes(c, p);
12572 }
12573 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012574#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012575 {
12576 c = p[2];
12577 p[2] = p[1];
12578 p[1] = *p;
12579 *p = c;
12580 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012581
Bram Moolenaar4770d092006-01-12 23:22:24 +000012582 /* Rotate three bytes right: "123" -> "312". We change "fword"
12583 * here, it's changed back afterwards at STATE_UNROT3R. */
12584 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12585 {
12586 go_deeper(stack, depth, SCORE_SWAP3);
12587#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012588 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012589 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12590 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12591 p[0], p[1], p[2]);
12592#endif
12593 sp->ts_state = STATE_UNROT3R;
12594 ++depth;
12595 p = fword + sp->ts_fidx;
12596#ifdef FEAT_MBYTE
12597 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012598 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012599 n = mb_cptr2len(p);
12600 n += mb_cptr2len(p + n);
12601 c = mb_ptr2char(p + n);
12602 tl = mb_cptr2len(p + n);
12603 mch_memmove(p + tl, p, n);
12604 mb_char2bytes(c, p);
12605 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012606 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012607 else
12608#endif
12609 {
12610 c = p[2];
12611 p[2] = p[1];
12612 p[1] = *p;
12613 *p = c;
12614 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12615 }
12616 }
12617 else
12618 sp->ts_state = STATE_REP_INI;
12619 break;
12620
12621 case STATE_UNROT3R:
12622 /* Undo ROT3R: "312" -> "123" */
12623 p = fword + sp->ts_fidx;
12624#ifdef FEAT_MBYTE
12625 if (has_mbyte)
12626 {
12627 c = mb_ptr2char(p);
12628 tl = MB_BYTE2LEN(*p);
12629 n = MB_BYTE2LEN(p[tl]);
12630 n += MB_BYTE2LEN(p[tl + n]);
12631 mch_memmove(p, p + tl, n);
12632 mb_char2bytes(c, p + n);
12633 }
12634 else
12635#endif
12636 {
12637 c = *p;
12638 *p = p[1];
12639 p[1] = p[2];
12640 p[2] = c;
12641 }
12642 /*FALLTHROUGH*/
12643
12644 case STATE_REP_INI:
12645 /* Check if matching with REP items from the .aff file would work.
12646 * Quickly skip if:
12647 * - there are no REP items and we are not in the soundfold trie
12648 * - the score is going to be too high anyway
12649 * - already applied a REP item or swapped here */
12650 if ((lp->lp_replang == NULL && !soundfold)
12651 || sp->ts_score + SCORE_REP >= su->su_maxscore
12652 || sp->ts_fidx < sp->ts_fidxtry)
12653 {
12654 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012656 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012657
Bram Moolenaar4770d092006-01-12 23:22:24 +000012658 /* Use the first byte to quickly find the first entry that may
12659 * match. If the index is -1 there is none. */
12660 if (soundfold)
12661 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12662 else
12663 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664
Bram Moolenaar4770d092006-01-12 23:22:24 +000012665 if (sp->ts_curi < 0)
12666 {
12667 sp->ts_state = STATE_FINAL;
12668 break;
12669 }
12670
12671 sp->ts_state = STATE_REP;
12672 /*FALLTHROUGH*/
12673
12674 case STATE_REP:
12675 /* Try matching with REP items from the .aff file. For each match
12676 * replace the characters and check if the resulting word is
12677 * valid. */
12678 p = fword + sp->ts_fidx;
12679
12680 if (soundfold)
12681 gap = &slang->sl_repsal;
12682 else
12683 gap = &lp->lp_replang->sl_rep;
12684 while (sp->ts_curi < gap->ga_len)
12685 {
12686 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12687 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012688 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012689 /* past possible matching entries */
12690 sp->ts_curi = gap->ga_len;
12691 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012692 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012693 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12694 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12695 {
12696 go_deeper(stack, depth, SCORE_REP);
12697#ifdef DEBUG_TRIEWALK
12698 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12699 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12700 ftp->ft_from, ftp->ft_to);
12701#endif
12702 /* Need to undo this afterwards. */
12703 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012704
Bram Moolenaar4770d092006-01-12 23:22:24 +000012705 /* Change the "from" to the "to" string. */
12706 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012707 fl = (int)STRLEN(ftp->ft_from);
12708 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012709 if (fl != tl)
12710 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012711 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012712 repextra += tl - fl;
12713 }
12714 mch_memmove(p, ftp->ft_to, tl);
12715 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12716#ifdef FEAT_MBYTE
12717 stack[depth].ts_tcharlen = 0;
12718#endif
12719 break;
12720 }
12721 }
12722
12723 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12724 /* No (more) matches. */
12725 sp->ts_state = STATE_FINAL;
12726
12727 break;
12728
12729 case STATE_REP_UNDO:
12730 /* Undo a REP replacement and continue with the next one. */
12731 if (soundfold)
12732 gap = &slang->sl_repsal;
12733 else
12734 gap = &lp->lp_replang->sl_rep;
12735 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012736 fl = (int)STRLEN(ftp->ft_from);
12737 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012738 p = fword + sp->ts_fidx;
12739 if (fl != tl)
12740 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012741 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012742 repextra -= tl - fl;
12743 }
12744 mch_memmove(p, ftp->ft_from, fl);
12745 sp->ts_state = STATE_REP;
12746 break;
12747
12748 default:
12749 /* Did all possible states at this level, go up one level. */
12750 --depth;
12751
12752 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12753 {
12754 /* Continue in or go back to the prefix tree. */
12755 byts = pbyts;
12756 idxs = pidxs;
12757 }
12758
12759 /* Don't check for CTRL-C too often, it takes time. */
12760 if (--breakcheckcount == 0)
12761 {
12762 ui_breakcheck();
12763 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012765 }
12766 }
12767}
12768
Bram Moolenaar4770d092006-01-12 23:22:24 +000012769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012771 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012773 static void
12774go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 trystate_T *stack;
12776 int depth;
12777 int score_add;
12778{
Bram Moolenaarea424162005-06-16 21:51:00 +000012779 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012780 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012781 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012783 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784}
12785
Bram Moolenaar53805d12005-08-01 07:08:33 +000012786#ifdef FEAT_MBYTE
12787/*
12788 * Case-folding may change the number of bytes: Count nr of chars in
12789 * fword[flen] and return the byte length of that many chars in "word".
12790 */
12791 static int
12792nofold_len(fword, flen, word)
12793 char_u *fword;
12794 int flen;
12795 char_u *word;
12796{
12797 char_u *p;
12798 int i = 0;
12799
12800 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12801 ++i;
12802 for (p = word; i > 0; mb_ptr_adv(p))
12803 --i;
12804 return (int)(p - word);
12805}
12806#endif
12807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012808/*
12809 * "fword" is a good word with case folded. Find the matching keep-case
12810 * words and put it in "kword".
12811 * Theoretically there could be several keep-case words that result in the
12812 * same case-folded word, but we only find one...
12813 */
12814 static void
12815find_keepcap_word(slang, fword, kword)
12816 slang_T *slang;
12817 char_u *fword;
12818 char_u *kword;
12819{
12820 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12821 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012822 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823
12824 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012825 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 int round[MAXWLEN];
12827 int fwordidx[MAXWLEN];
12828 int uwordidx[MAXWLEN];
12829 int kwordlen[MAXWLEN];
12830
12831 int flen, ulen;
12832 int l;
12833 int len;
12834 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012835 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 char_u *p;
12837 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012838 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839
12840 if (byts == NULL)
12841 {
12842 /* array is empty: "cannot happen" */
12843 *kword = NUL;
12844 return;
12845 }
12846
12847 /* Make an all-cap version of "fword". */
12848 allcap_copy(fword, uword);
12849
12850 /*
12851 * Each character needs to be tried both case-folded and upper-case.
12852 * All this gets very complicated if we keep in mind that changing case
12853 * may change the byte length of a multi-byte character...
12854 */
12855 depth = 0;
12856 arridx[0] = 0;
12857 round[0] = 0;
12858 fwordidx[0] = 0;
12859 uwordidx[0] = 0;
12860 kwordlen[0] = 0;
12861 while (depth >= 0)
12862 {
12863 if (fword[fwordidx[depth]] == NUL)
12864 {
12865 /* We are at the end of "fword". If the tree allows a word to end
12866 * here we have found a match. */
12867 if (byts[arridx[depth] + 1] == 0)
12868 {
12869 kword[kwordlen[depth]] = NUL;
12870 return;
12871 }
12872
12873 /* kword is getting too long, continue one level up */
12874 --depth;
12875 }
12876 else if (++round[depth] > 2)
12877 {
12878 /* tried both fold-case and upper-case character, continue one
12879 * level up */
12880 --depth;
12881 }
12882 else
12883 {
12884 /*
12885 * round[depth] == 1: Try using the folded-case character.
12886 * round[depth] == 2: Try using the upper-case character.
12887 */
12888#ifdef FEAT_MBYTE
12889 if (has_mbyte)
12890 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012891 flen = mb_cptr2len(fword + fwordidx[depth]);
12892 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 }
12894 else
12895#endif
12896 ulen = flen = 1;
12897 if (round[depth] == 1)
12898 {
12899 p = fword + fwordidx[depth];
12900 l = flen;
12901 }
12902 else
12903 {
12904 p = uword + uwordidx[depth];
12905 l = ulen;
12906 }
12907
12908 for (tryidx = arridx[depth]; l > 0; --l)
12909 {
12910 /* Perform a binary search in the list of accepted bytes. */
12911 len = byts[tryidx++];
12912 c = *p++;
12913 lo = tryidx;
12914 hi = tryidx + len - 1;
12915 while (lo < hi)
12916 {
12917 m = (lo + hi) / 2;
12918 if (byts[m] > c)
12919 hi = m - 1;
12920 else if (byts[m] < c)
12921 lo = m + 1;
12922 else
12923 {
12924 lo = hi = m;
12925 break;
12926 }
12927 }
12928
12929 /* Stop if there is no matching byte. */
12930 if (hi < lo || byts[lo] != c)
12931 break;
12932
12933 /* Continue at the child (if there is one). */
12934 tryidx = idxs[lo];
12935 }
12936
12937 if (l == 0)
12938 {
12939 /*
12940 * Found the matching char. Copy it to "kword" and go a
12941 * level deeper.
12942 */
12943 if (round[depth] == 1)
12944 {
12945 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12946 flen);
12947 kwordlen[depth + 1] = kwordlen[depth] + flen;
12948 }
12949 else
12950 {
12951 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12952 ulen);
12953 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12954 }
12955 fwordidx[depth + 1] = fwordidx[depth] + flen;
12956 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12957
12958 ++depth;
12959 arridx[depth] = tryidx;
12960 round[depth] = 0;
12961 }
12962 }
12963 }
12964
12965 /* Didn't find it: "cannot happen". */
12966 *kword = NUL;
12967}
12968
12969/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012970 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12971 * su->su_sga.
12972 */
12973 static void
12974score_comp_sal(su)
12975 suginfo_T *su;
12976{
12977 langp_T *lp;
12978 char_u badsound[MAXWLEN];
12979 int i;
12980 suggest_T *stp;
12981 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012982 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012983 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012984
12985 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12986 return;
12987
12988 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012989 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012990 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012991 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012992 if (lp->lp_slang->sl_sal.ga_len > 0)
12993 {
12994 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012995 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012996
12997 for (i = 0; i < su->su_ga.ga_len; ++i)
12998 {
12999 stp = &SUG(su->su_ga, i);
13000
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013001 /* Case-fold the suggested word, sound-fold it and compute the
13002 * sound-a-like score. */
13003 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013004 if (score < SCORE_MAXMAX)
13005 {
13006 /* Add the suggestion. */
13007 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13008 sstp->st_word = vim_strsave(stp->st_word);
13009 if (sstp->st_word != NULL)
13010 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013011 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013012 sstp->st_score = score;
13013 sstp->st_altscore = 0;
13014 sstp->st_orglen = stp->st_orglen;
13015 ++su->su_sga.ga_len;
13016 }
13017 }
13018 }
13019 break;
13020 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013021 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013022}
13023
13024/*
13025 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013026 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013027 */
13028 static void
13029score_combine(su)
13030 suginfo_T *su;
13031{
13032 int i;
13033 int j;
13034 garray_T ga;
13035 garray_T *gap;
13036 langp_T *lp;
13037 suggest_T *stp;
13038 char_u *p;
13039 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013040 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013041 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013042 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013043
13044 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013045 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013046 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013047 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013048 if (lp->lp_slang->sl_sal.ga_len > 0)
13049 {
13050 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013051 slang = lp->lp_slang;
13052 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013053
13054 for (i = 0; i < su->su_ga.ga_len; ++i)
13055 {
13056 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013057 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013058 if (stp->st_altscore == SCORE_MAXMAX)
13059 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13060 else
13061 stp->st_score = (stp->st_score * 3
13062 + stp->st_altscore) / 4;
13063 stp->st_salscore = FALSE;
13064 }
13065 break;
13066 }
13067 }
13068
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013069 if (slang == NULL) /* Using "double" without sound folding. */
13070 {
13071 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13072 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013073 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013074 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013075
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013076 /* Add the alternate score to su_sga. */
13077 for (i = 0; i < su->su_sga.ga_len; ++i)
13078 {
13079 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013080 stp->st_altscore = spell_edit_score(slang,
13081 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013082 if (stp->st_score == SCORE_MAXMAX)
13083 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13084 else
13085 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13086 stp->st_salscore = TRUE;
13087 }
13088
Bram Moolenaar4770d092006-01-12 23:22:24 +000013089 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13090 * for both lists. */
13091 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013092 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013093 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013094 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13095
13096 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13097 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13098 return;
13099
13100 stp = &SUG(ga, 0);
13101 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13102 {
13103 /* round 1: get a suggestion from su_ga
13104 * round 2: get a suggestion from su_sga */
13105 for (round = 1; round <= 2; ++round)
13106 {
13107 gap = round == 1 ? &su->su_ga : &su->su_sga;
13108 if (i < gap->ga_len)
13109 {
13110 /* Don't add a word if it's already there. */
13111 p = SUG(*gap, i).st_word;
13112 for (j = 0; j < ga.ga_len; ++j)
13113 if (STRCMP(stp[j].st_word, p) == 0)
13114 break;
13115 if (j == ga.ga_len)
13116 stp[ga.ga_len++] = SUG(*gap, i);
13117 else
13118 vim_free(p);
13119 }
13120 }
13121 }
13122
13123 ga_clear(&su->su_ga);
13124 ga_clear(&su->su_sga);
13125
13126 /* Truncate the list to the number of suggestions that will be displayed. */
13127 if (ga.ga_len > su->su_maxcount)
13128 {
13129 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13130 vim_free(stp[i].st_word);
13131 ga.ga_len = su->su_maxcount;
13132 }
13133
13134 su->su_ga = ga;
13135}
13136
13137/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013138 * For the goodword in "stp" compute the soundalike score compared to the
13139 * badword.
13140 */
13141 static int
13142stp_sal_score(stp, su, slang, badsound)
13143 suggest_T *stp;
13144 suginfo_T *su;
13145 slang_T *slang;
13146 char_u *badsound; /* sound-folded badword */
13147{
13148 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013149 char_u *pbad;
13150 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013151 char_u badsound2[MAXWLEN];
13152 char_u fword[MAXWLEN];
13153 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013154 char_u goodword[MAXWLEN];
13155 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013156
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013157 lendiff = (int)(su->su_badlen - stp->st_orglen);
13158 if (lendiff >= 0)
13159 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013160 else
13161 {
13162 /* soundfold the bad word with more characters following */
13163 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13164
13165 /* When joining two words the sound often changes a lot. E.g., "t he"
13166 * sounds like "t h" while "the" sounds like "@". Avoid that by
13167 * removing the space. Don't do it when the good word also contains a
13168 * space. */
13169 if (vim_iswhite(su->su_badptr[su->su_badlen])
13170 && *skiptowhite(stp->st_word) == NUL)
13171 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013172 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013173
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013174 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013175 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013176 }
13177
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013178 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013179 {
13180 /* Add part of the bad word to the good word, so that we soundfold
13181 * what replaces the bad word. */
13182 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013183 vim_strncpy(goodword + stp->st_wordlen,
13184 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013185 pgood = goodword;
13186 }
13187 else
13188 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013189
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013190 /* Sound-fold the word and compute the score for the difference. */
13191 spell_soundfold(slang, pgood, FALSE, goodsound);
13192
13193 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013194}
13195
Bram Moolenaar4770d092006-01-12 23:22:24 +000013196/* structure used to store soundfolded words that add_sound_suggest() has
13197 * handled already. */
13198typedef struct
13199{
13200 short sft_score; /* lowest score used */
13201 char_u sft_word[1]; /* soundfolded word, actually longer */
13202} sftword_T;
13203
13204static sftword_T dumsft;
13205#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13206#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13207
13208/*
13209 * Prepare for calling suggest_try_soundalike().
13210 */
13211 static void
13212suggest_try_soundalike_prep()
13213{
13214 langp_T *lp;
13215 int lpi;
13216 slang_T *slang;
13217
13218 /* Do this for all languages that support sound folding and for which a
13219 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013220 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013221 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013222 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013223 slang = lp->lp_slang;
13224 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13225 /* prepare the hashtable used by add_sound_suggest() */
13226 hash_init(&slang->sl_sounddone);
13227 }
13228}
13229
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013230/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013231 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013232 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 */
13234 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013235suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 suginfo_T *su;
13237{
13238 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013239 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013240 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013241 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242
Bram Moolenaar4770d092006-01-12 23:22:24 +000013243 /* Do this for all languages that support sound folding and for which a
13244 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013245 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013246 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013247 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013248 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013249 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013250 {
13251 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013252 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253
Bram Moolenaar4770d092006-01-12 23:22:24 +000013254 /* try all kinds of inserts/deletes/swaps/etc. */
13255 /* TODO: also soundfold the next words, so that we can try joining
13256 * and splitting */
13257 suggest_trie_walk(su, lp, salword, TRUE);
13258 }
13259 }
13260}
13261
13262/*
13263 * Finish up after calling suggest_try_soundalike().
13264 */
13265 static void
13266suggest_try_soundalike_finish()
13267{
13268 langp_T *lp;
13269 int lpi;
13270 slang_T *slang;
13271 int todo;
13272 hashitem_T *hi;
13273
13274 /* Do this for all languages that support sound folding and for which a
13275 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013276 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013277 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013278 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013279 slang = lp->lp_slang;
13280 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13281 {
13282 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013283 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013284 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13285 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013286 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013287 vim_free(HI2SFT(hi));
13288 --todo;
13289 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013290
13291 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013292 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013293 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013294 }
13295 }
13296}
13297
13298/*
13299 * A match with a soundfolded word is found. Add the good word(s) that
13300 * produce this soundfolded word.
13301 */
13302 static void
13303add_sound_suggest(su, goodword, score, lp)
13304 suginfo_T *su;
13305 char_u *goodword;
13306 int score; /* soundfold score */
13307 langp_T *lp;
13308{
13309 slang_T *slang = lp->lp_slang; /* language for sound folding */
13310 int sfwordnr;
13311 char_u *nrline;
13312 int orgnr;
13313 char_u theword[MAXWLEN];
13314 int i;
13315 int wlen;
13316 char_u *byts;
13317 idx_T *idxs;
13318 int n;
13319 int wordcount;
13320 int wc;
13321 int goodscore;
13322 hash_T hash;
13323 hashitem_T *hi;
13324 sftword_T *sft;
13325 int bc, gc;
13326 int limit;
13327
13328 /*
13329 * It's very well possible that the same soundfold word is found several
13330 * times with different scores. Since the following is quite slow only do
13331 * the words that have a better score than before. Use a hashtable to
13332 * remember the words that have been done.
13333 */
13334 hash = hash_hash(goodword);
13335 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13336 if (HASHITEM_EMPTY(hi))
13337 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013338 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13339 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013340 if (sft != NULL)
13341 {
13342 sft->sft_score = score;
13343 STRCPY(sft->sft_word, goodword);
13344 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13345 }
13346 }
13347 else
13348 {
13349 sft = HI2SFT(hi);
13350 if (score >= sft->sft_score)
13351 return;
13352 sft->sft_score = score;
13353 }
13354
13355 /*
13356 * Find the word nr in the soundfold tree.
13357 */
13358 sfwordnr = soundfold_find(slang, goodword);
13359 if (sfwordnr < 0)
13360 {
13361 EMSG2(_(e_intern2), "add_sound_suggest()");
13362 return;
13363 }
13364
13365 /*
13366 * go over the list of good words that produce this soundfold word
13367 */
13368 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13369 orgnr = 0;
13370 while (*nrline != NUL)
13371 {
13372 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13373 * previous wordnr. */
13374 orgnr += bytes2offset(&nrline);
13375
13376 byts = slang->sl_fbyts;
13377 idxs = slang->sl_fidxs;
13378
13379 /* Lookup the word "orgnr" one of the two tries. */
13380 n = 0;
13381 wlen = 0;
13382 wordcount = 0;
13383 for (;;)
13384 {
13385 i = 1;
13386 if (wordcount == orgnr && byts[n + 1] == NUL)
13387 break; /* found end of word */
13388
13389 if (byts[n + 1] == NUL)
13390 ++wordcount;
13391
13392 /* skip over the NUL bytes */
13393 for ( ; byts[n + i] == NUL; ++i)
13394 if (i > byts[n]) /* safety check */
13395 {
13396 STRCPY(theword + wlen, "BAD");
13397 goto badword;
13398 }
13399
13400 /* One of the siblings must have the word. */
13401 for ( ; i < byts[n]; ++i)
13402 {
13403 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13404 if (wordcount + wc > orgnr)
13405 break;
13406 wordcount += wc;
13407 }
13408
13409 theword[wlen++] = byts[n + i];
13410 n = idxs[n + i];
13411 }
13412badword:
13413 theword[wlen] = NUL;
13414
13415 /* Go over the possible flags and regions. */
13416 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13417 {
13418 char_u cword[MAXWLEN];
13419 char_u *p;
13420 int flags = (int)idxs[n + i];
13421
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013422 /* Skip words with the NOSUGGEST flag */
13423 if (flags & WF_NOSUGGEST)
13424 continue;
13425
Bram Moolenaar4770d092006-01-12 23:22:24 +000013426 if (flags & WF_KEEPCAP)
13427 {
13428 /* Must find the word in the keep-case tree. */
13429 find_keepcap_word(slang, theword, cword);
13430 p = cword;
13431 }
13432 else
13433 {
13434 flags |= su->su_badflags;
13435 if ((flags & WF_CAPMASK) != 0)
13436 {
13437 /* Need to fix case according to "flags". */
13438 make_case_word(theword, cword, flags);
13439 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 }
13441 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013442 p = theword;
13443 }
13444
13445 /* Add the suggestion. */
13446 if (sps_flags & SPS_DOUBLE)
13447 {
13448 /* Add the suggestion if the score isn't too bad. */
13449 if (score <= su->su_maxscore)
13450 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13451 score, 0, FALSE, slang, FALSE);
13452 }
13453 else
13454 {
13455 /* Add a penalty for words in another region. */
13456 if ((flags & WF_REGION)
13457 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13458 goodscore = SCORE_REGION;
13459 else
13460 goodscore = 0;
13461
13462 /* Add a small penalty for changing the first letter from
13463 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013464 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +000013465 * letter is the same, that has already been counted. */
13466 gc = PTR2CHAR(p);
13467 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013469 bc = PTR2CHAR(su->su_badword);
13470 if (!SPELL_ISUPPER(bc)
13471 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13472 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473 }
13474
Bram Moolenaar4770d092006-01-12 23:22:24 +000013475 /* Compute the score for the good word. This only does letter
13476 * insert/delete/swap/replace. REP items are not considered,
13477 * which may make the score a bit higher.
13478 * Use a limit for the score to make it work faster. Use
13479 * MAXSCORE(), because RESCORE() will change the score.
13480 * If the limit is very high then the iterative method is
13481 * inefficient, using an array is quicker. */
13482 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13483 if (limit > SCORE_LIMITMAX)
13484 goodscore += spell_edit_score(slang, su->su_badword, p);
13485 else
13486 goodscore += spell_edit_score_limit(slang, su->su_badword,
13487 p, limit);
13488
13489 /* When going over the limit don't bother to do the rest. */
13490 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013492 /* Give a bonus to words seen before. */
13493 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013494
Bram Moolenaar4770d092006-01-12 23:22:24 +000013495 /* Add the suggestion if the score isn't too bad. */
13496 goodscore = RESCORE(goodscore, score);
13497 if (goodscore <= su->su_sfmaxscore)
13498 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13499 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013500 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501 }
13502 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013503 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013504 }
13505}
13506
13507/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013508 * Find word "word" in fold-case tree for "slang" and return the word number.
13509 */
13510 static int
13511soundfold_find(slang, word)
13512 slang_T *slang;
13513 char_u *word;
13514{
13515 idx_T arridx = 0;
13516 int len;
13517 int wlen = 0;
13518 int c;
13519 char_u *ptr = word;
13520 char_u *byts;
13521 idx_T *idxs;
13522 int wordnr = 0;
13523
13524 byts = slang->sl_sbyts;
13525 idxs = slang->sl_sidxs;
13526
13527 for (;;)
13528 {
13529 /* First byte is the number of possible bytes. */
13530 len = byts[arridx++];
13531
13532 /* If the first possible byte is a zero the word could end here.
13533 * If the word ends we found the word. If not skip the NUL bytes. */
13534 c = ptr[wlen];
13535 if (byts[arridx] == NUL)
13536 {
13537 if (c == NUL)
13538 break;
13539
13540 /* Skip over the zeros, there can be several. */
13541 while (len > 0 && byts[arridx] == NUL)
13542 {
13543 ++arridx;
13544 --len;
13545 }
13546 if (len == 0)
13547 return -1; /* no children, word should have ended here */
13548 ++wordnr;
13549 }
13550
13551 /* If the word ends we didn't find it. */
13552 if (c == NUL)
13553 return -1;
13554
13555 /* Perform a binary search in the list of accepted bytes. */
13556 if (c == TAB) /* <Tab> is handled like <Space> */
13557 c = ' ';
13558 while (byts[arridx] < c)
13559 {
13560 /* The word count is in the first idxs[] entry of the child. */
13561 wordnr += idxs[idxs[arridx]];
13562 ++arridx;
13563 if (--len == 0) /* end of the bytes, didn't find it */
13564 return -1;
13565 }
13566 if (byts[arridx] != c) /* didn't find the byte */
13567 return -1;
13568
13569 /* Continue at the child (if there is one). */
13570 arridx = idxs[arridx];
13571 ++wlen;
13572
13573 /* One space in the good word may stand for several spaces in the
13574 * checked word. */
13575 if (c == ' ')
13576 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13577 ++wlen;
13578 }
13579
13580 return wordnr;
13581}
13582
13583/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013584 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 */
13586 static void
13587make_case_word(fword, cword, flags)
13588 char_u *fword;
13589 char_u *cword;
13590 int flags;
13591{
13592 if (flags & WF_ALLCAP)
13593 /* Make it all upper-case */
13594 allcap_copy(fword, cword);
13595 else if (flags & WF_ONECAP)
13596 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013597 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013598 else
13599 /* Use goodword as-is. */
13600 STRCPY(cword, fword);
13601}
13602
Bram Moolenaarea424162005-06-16 21:51:00 +000013603/*
13604 * Use map string "map" for languages "lp".
13605 */
13606 static void
13607set_map_str(lp, map)
13608 slang_T *lp;
13609 char_u *map;
13610{
13611 char_u *p;
13612 int headc = 0;
13613 int c;
13614 int i;
13615
13616 if (*map == NUL)
13617 {
13618 lp->sl_has_map = FALSE;
13619 return;
13620 }
13621 lp->sl_has_map = TRUE;
13622
Bram Moolenaar4770d092006-01-12 23:22:24 +000013623 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013624 for (i = 0; i < 256; ++i)
13625 lp->sl_map_array[i] = 0;
13626#ifdef FEAT_MBYTE
13627 hash_init(&lp->sl_map_hash);
13628#endif
13629
13630 /*
13631 * The similar characters are stored separated with slashes:
13632 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13633 * before the same slash. For characters above 255 sl_map_hash is used.
13634 */
13635 for (p = map; *p != NUL; )
13636 {
13637#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013638 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013639#else
13640 c = *p++;
13641#endif
13642 if (c == '/')
13643 headc = 0;
13644 else
13645 {
13646 if (headc == 0)
13647 headc = c;
13648
13649#ifdef FEAT_MBYTE
13650 /* Characters above 255 don't fit in sl_map_array[], put them in
13651 * the hash table. Each entry is the char, a NUL the headchar and
13652 * a NUL. */
13653 if (c >= 256)
13654 {
13655 int cl = mb_char2len(c);
13656 int headcl = mb_char2len(headc);
13657 char_u *b;
13658 hash_T hash;
13659 hashitem_T *hi;
13660
13661 b = alloc((unsigned)(cl + headcl + 2));
13662 if (b == NULL)
13663 return;
13664 mb_char2bytes(c, b);
13665 b[cl] = NUL;
13666 mb_char2bytes(headc, b + cl + 1);
13667 b[cl + 1 + headcl] = NUL;
13668 hash = hash_hash(b);
13669 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13670 if (HASHITEM_EMPTY(hi))
13671 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13672 else
13673 {
13674 /* This should have been checked when generating the .spl
13675 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013676 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013677 vim_free(b);
13678 }
13679 }
13680 else
13681#endif
13682 lp->sl_map_array[c] = headc;
13683 }
13684 }
13685}
13686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687/*
13688 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13689 * lines in the .aff file.
13690 */
13691 static int
13692similar_chars(slang, c1, c2)
13693 slang_T *slang;
13694 int c1;
13695 int c2;
13696{
Bram Moolenaarea424162005-06-16 21:51:00 +000013697 int m1, m2;
13698#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +020013699 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +000013700 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701
Bram Moolenaarea424162005-06-16 21:51:00 +000013702 if (c1 >= 256)
13703 {
13704 buf[mb_char2bytes(c1, buf)] = 0;
13705 hi = hash_find(&slang->sl_map_hash, buf);
13706 if (HASHITEM_EMPTY(hi))
13707 m1 = 0;
13708 else
13709 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13710 }
13711 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013712#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013713 m1 = slang->sl_map_array[c1];
13714 if (m1 == 0)
13715 return FALSE;
13716
13717
13718#ifdef FEAT_MBYTE
13719 if (c2 >= 256)
13720 {
13721 buf[mb_char2bytes(c2, buf)] = 0;
13722 hi = hash_find(&slang->sl_map_hash, buf);
13723 if (HASHITEM_EMPTY(hi))
13724 m2 = 0;
13725 else
13726 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13727 }
13728 else
13729#endif
13730 m2 = slang->sl_map_array[c2];
13731
13732 return m1 == m2;
13733}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734
13735/*
13736 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013737 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 */
13739 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013740add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13741 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013743 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013745 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013746 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013747 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013748 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013749 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013750 int maxsf; /* su_maxscore applies to soundfold score,
13751 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013753 int goodlen; /* len of goodword changed */
13754 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013756 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013758 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013759
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013760 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13761 * "thee the" is added next to changing the first "the" the "thee". */
13762 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013763 pbad = su->su_badptr + badlenarg;
13764 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013765 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013766 goodlen = (int)(pgood - goodword);
13767 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013768 if (goodlen <= 0 || badlen <= 0)
13769 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013770 mb_ptr_back(goodword, pgood);
13771 mb_ptr_back(su->su_badptr, pbad);
13772#ifdef FEAT_MBYTE
13773 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013774 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013775 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13776 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013777 }
13778 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013779#endif
13780 if (*pgood != *pbad)
13781 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013782 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013783
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013784 if (badlen == 0 && goodlen == 0)
13785 /* goodword doesn't change anything; may happen for "the the" changing
13786 * the first "the" to itself. */
13787 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013788
Bram Moolenaar89d40322006-08-29 15:30:07 +000013789 if (gap->ga_len == 0)
13790 i = -1;
13791 else
13792 {
13793 /* Check if the word is already there. Also check the length that is
13794 * being replaced "thes," -> "these" is a different suggestion from
13795 * "thes" -> "these". */
13796 stp = &SUG(*gap, 0);
13797 for (i = gap->ga_len; --i >= 0; ++stp)
13798 if (stp->st_wordlen == goodlen
13799 && stp->st_orglen == badlen
13800 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013802 /*
13803 * Found it. Remember the word with the lowest score.
13804 */
13805 if (stp->st_slang == NULL)
13806 stp->st_slang = slang;
13807
13808 new_sug.st_score = score;
13809 new_sug.st_altscore = altscore;
13810 new_sug.st_had_bonus = had_bonus;
13811
13812 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013813 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013814 /* Only one of the two had the soundalike score computed.
13815 * Need to do that for the other one now, otherwise the
13816 * scores can't be compared. This happens because
13817 * suggest_try_change() doesn't compute the soundalike
13818 * word to keep it fast, while some special methods set
13819 * the soundalike score to zero. */
13820 if (had_bonus)
13821 rescore_one(su, stp);
13822 else
13823 {
13824 new_sug.st_word = stp->st_word;
13825 new_sug.st_wordlen = stp->st_wordlen;
13826 new_sug.st_slang = stp->st_slang;
13827 new_sug.st_orglen = badlen;
13828 rescore_one(su, &new_sug);
13829 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013830 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831
Bram Moolenaar89d40322006-08-29 15:30:07 +000013832 if (stp->st_score > new_sug.st_score)
13833 {
13834 stp->st_score = new_sug.st_score;
13835 stp->st_altscore = new_sug.st_altscore;
13836 stp->st_had_bonus = new_sug.st_had_bonus;
13837 }
13838 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013839 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841
Bram Moolenaar4770d092006-01-12 23:22:24 +000013842 if (i < 0 && ga_grow(gap, 1) == OK)
13843 {
13844 /* Add a suggestion. */
13845 stp = &SUG(*gap, gap->ga_len);
13846 stp->st_word = vim_strnsave(goodword, goodlen);
13847 if (stp->st_word != NULL)
13848 {
13849 stp->st_wordlen = goodlen;
13850 stp->st_score = score;
13851 stp->st_altscore = altscore;
13852 stp->st_had_bonus = had_bonus;
13853 stp->st_orglen = badlen;
13854 stp->st_slang = slang;
13855 ++gap->ga_len;
13856
13857 /* If we have too many suggestions now, sort the list and keep
13858 * the best suggestions. */
13859 if (gap->ga_len > SUG_MAX_COUNT(su))
13860 {
13861 if (maxsf)
13862 su->su_sfmaxscore = cleanup_suggestions(gap,
13863 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13864 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013865 su->su_maxscore = cleanup_suggestions(gap,
13866 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 }
13868 }
13869 }
13870}
13871
13872/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013873 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13874 * for split words, such as "the the". Remove these from the list here.
13875 */
13876 static void
13877check_suggestions(su, gap)
13878 suginfo_T *su;
13879 garray_T *gap; /* either su_ga or su_sga */
13880{
13881 suggest_T *stp;
13882 int i;
13883 char_u longword[MAXWLEN + 1];
13884 int len;
13885 hlf_T attr;
13886
13887 stp = &SUG(*gap, 0);
13888 for (i = gap->ga_len - 1; i >= 0; --i)
13889 {
13890 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013891 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013892 len = stp[i].st_wordlen;
13893 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13894 MAXWLEN - len);
13895 attr = HLF_COUNT;
13896 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13897 if (attr != HLF_COUNT)
13898 {
13899 /* Remove this entry. */
13900 vim_free(stp[i].st_word);
13901 --gap->ga_len;
13902 if (i < gap->ga_len)
13903 mch_memmove(stp + i, stp + i + 1,
13904 sizeof(suggest_T) * (gap->ga_len - i));
13905 }
13906 }
13907}
13908
13909
13910/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911 * Add a word to be banned.
13912 */
13913 static void
13914add_banned(su, word)
13915 suginfo_T *su;
13916 char_u *word;
13917{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013918 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 hash_T hash;
13920 hashitem_T *hi;
13921
Bram Moolenaar4770d092006-01-12 23:22:24 +000013922 hash = hash_hash(word);
13923 hi = hash_lookup(&su->su_banned, word, hash);
13924 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013926 s = vim_strsave(word);
13927 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013928 hash_add_item(&su->su_banned, hi, s, hash);
13929 }
13930}
13931
13932/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013933 * Recompute the score for all suggestions if sound-folding is possible. This
13934 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013935 */
13936 static void
13937rescore_suggestions(su)
13938 suginfo_T *su;
13939{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013940 int i;
13941
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013942 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013943 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013944 rescore_one(su, &SUG(su->su_ga, i));
13945}
13946
13947/*
13948 * Recompute the score for one suggestion if sound-folding is possible.
13949 */
13950 static void
13951rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013952 suginfo_T *su;
13953 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013954{
13955 slang_T *slang = stp->st_slang;
13956 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013957 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013958
13959 /* Only rescore suggestions that have no sal score yet and do have a
13960 * language. */
13961 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13962 {
13963 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013964 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013965 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013966 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013967 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013968 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013969 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013970
13971 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013972 if (stp->st_altscore == SCORE_MAXMAX)
13973 stp->st_altscore = SCORE_BIG;
13974 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13975 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013976 }
13977}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013979static int
13980#ifdef __BORLANDC__
13981_RTLENTRYF
13982#endif
13983sug_compare __ARGS((const void *s1, const void *s2));
13984
13985/*
13986 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013987 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 */
13989 static int
13990#ifdef __BORLANDC__
13991_RTLENTRYF
13992#endif
13993sug_compare(s1, s2)
13994 const void *s1;
13995 const void *s2;
13996{
13997 suggest_T *p1 = (suggest_T *)s1;
13998 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013999 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014001 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014002 {
14003 n = p1->st_altscore - p2->st_altscore;
14004 if (n == 0)
14005 n = STRICMP(p1->st_word, p2->st_word);
14006 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014007 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008}
14009
14010/*
14011 * Cleanup the suggestions:
14012 * - Sort on score.
14013 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014014 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014016 static int
14017cleanup_suggestions(gap, maxscore, keep)
14018 garray_T *gap;
14019 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014020 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014022 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014023 int i;
14024
14025 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014026 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027
14028 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014029 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014031 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014033 gap->ga_len = keep;
14034 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014036 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037}
14038
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014039#if defined(FEAT_EVAL) || defined(PROTO)
14040/*
14041 * Soundfold a string, for soundfold().
14042 * Result is in allocated memory, NULL for an error.
14043 */
14044 char_u *
14045eval_soundfold(word)
14046 char_u *word;
14047{
14048 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014049 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014050 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014051
Bram Moolenaar860cae12010-06-05 23:22:07 +020014052 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014053 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014054 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014055 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014056 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014057 if (lp->lp_slang->sl_sal.ga_len > 0)
14058 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014059 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014060 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014061 return vim_strsave(sound);
14062 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014063 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014064
14065 /* No language with sound folding, return word as-is. */
14066 return vim_strsave(word);
14067}
14068#endif
14069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070/*
14071 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014072 *
14073 * There are many ways to turn a word into a sound-a-like representation. The
14074 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14075 * swedish name matching - survey and test of different algorithms" by Klas
14076 * Erikson.
14077 *
14078 * We support two methods:
14079 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14080 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014081 */
14082 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014083spell_soundfold(slang, inword, folded, res)
14084 slang_T *slang;
14085 char_u *inword;
14086 int folded; /* "inword" is already case-folded */
14087 char_u *res;
14088{
14089 char_u fword[MAXWLEN];
14090 char_u *word;
14091
14092 if (slang->sl_sofo)
14093 /* SOFOFROM and SOFOTO used */
14094 spell_soundfold_sofo(slang, inword, res);
14095 else
14096 {
14097 /* SAL items used. Requires the word to be case-folded. */
14098 if (folded)
14099 word = inword;
14100 else
14101 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014102 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014103 word = fword;
14104 }
14105
14106#ifdef FEAT_MBYTE
14107 if (has_mbyte)
14108 spell_soundfold_wsal(slang, word, res);
14109 else
14110#endif
14111 spell_soundfold_sal(slang, word, res);
14112 }
14113}
14114
14115/*
14116 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14117 * SOFOTO lines.
14118 */
14119 static void
14120spell_soundfold_sofo(slang, inword, res)
14121 slang_T *slang;
14122 char_u *inword;
14123 char_u *res;
14124{
14125 char_u *s;
14126 int ri = 0;
14127 int c;
14128
14129#ifdef FEAT_MBYTE
14130 if (has_mbyte)
14131 {
14132 int prevc = 0;
14133 int *ip;
14134
14135 /* The sl_sal_first[] table contains the translation for chars up to
14136 * 255, sl_sal the rest. */
14137 for (s = inword; *s != NUL; )
14138 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014139 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014140 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14141 c = ' ';
14142 else if (c < 256)
14143 c = slang->sl_sal_first[c];
14144 else
14145 {
14146 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14147 if (ip == NULL) /* empty list, can't match */
14148 c = NUL;
14149 else
14150 for (;;) /* find "c" in the list */
14151 {
14152 if (*ip == 0) /* not found */
14153 {
14154 c = NUL;
14155 break;
14156 }
14157 if (*ip == c) /* match! */
14158 {
14159 c = ip[1];
14160 break;
14161 }
14162 ip += 2;
14163 }
14164 }
14165
14166 if (c != NUL && c != prevc)
14167 {
14168 ri += mb_char2bytes(c, res + ri);
14169 if (ri + MB_MAXBYTES > MAXWLEN)
14170 break;
14171 prevc = c;
14172 }
14173 }
14174 }
14175 else
14176#endif
14177 {
14178 /* The sl_sal_first[] table contains the translation. */
14179 for (s = inword; (c = *s) != NUL; ++s)
14180 {
14181 if (vim_iswhite(c))
14182 c = ' ';
14183 else
14184 c = slang->sl_sal_first[c];
14185 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14186 res[ri++] = c;
14187 }
14188 }
14189
14190 res[ri] = NUL;
14191}
14192
14193 static void
14194spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 slang_T *slang;
14196 char_u *inword;
14197 char_u *res;
14198{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014199 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014201 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014203 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014205 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 int n, k = 0;
14207 int z0;
14208 int k0;
14209 int n0;
14210 int c;
14211 int pri;
14212 int p0 = -333;
14213 int c0;
14214
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014215 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014216 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 if (slang->sl_rem_accents)
14218 {
14219 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014220 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014222 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014223 {
14224 *t++ = ' ';
14225 s = skipwhite(s);
14226 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014227 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014229 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 *t++ = *s;
14231 ++s;
14232 }
14233 }
14234 *t = NUL;
14235 }
14236 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014237 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014239 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240
14241 /*
14242 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014243 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014245 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 while ((c = word[i]) != NUL)
14247 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014248 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 n = slang->sl_sal_first[c];
14250 z0 = 0;
14251
14252 if (n >= 0)
14253 {
14254 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014255 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014257 /* Quickly skip entries that don't match the word. Most
14258 * entries are less then three chars, optimize for that. */
14259 k = smp[n].sm_leadlen;
14260 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014262 if (word[i + 1] != s[1])
14263 continue;
14264 if (k > 2)
14265 {
14266 for (j = 2; j < k; ++j)
14267 if (word[i + j] != s[j])
14268 break;
14269 if (j < k)
14270 continue;
14271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272 }
14273
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014274 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014276 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014277 while (*pf != NUL && *pf != word[i + k])
14278 ++pf;
14279 if (*pf == NUL)
14280 continue;
14281 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014283 s = smp[n].sm_rules;
14284 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285
14286 p0 = *s;
14287 k0 = k;
14288 while (*s == '-' && k > 1)
14289 {
14290 k--;
14291 s++;
14292 }
14293 if (*s == '<')
14294 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014295 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 {
14297 /* determine priority */
14298 pri = *s - '0';
14299 s++;
14300 }
14301 if (*s == '^' && *(s + 1) == '^')
14302 s++;
14303
14304 if (*s == NUL
14305 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014306 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014307 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014309 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014311 && spell_iswordp(word + i - 1, curwin)
14312 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 {
14314 /* search for followup rules, if: */
14315 /* followup and k > 1 and NO '-' in searchstring */
14316 c0 = word[i + k - 1];
14317 n0 = slang->sl_sal_first[c0];
14318
14319 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014320 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 {
14322 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014323 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014324 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014325 /* Quickly skip entries that don't match the word.
14326 * */
14327 k0 = smp[n0].sm_leadlen;
14328 if (k0 > 1)
14329 {
14330 if (word[i + k] != s[1])
14331 continue;
14332 if (k0 > 2)
14333 {
14334 pf = word + i + k + 1;
14335 for (j = 2; j < k0; ++j)
14336 if (*pf++ != s[j])
14337 break;
14338 if (j < k0)
14339 continue;
14340 }
14341 }
14342 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014344 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014345 {
14346 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014347 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014348 while (*pf != NUL && *pf != word[i + k0])
14349 ++pf;
14350 if (*pf == NUL)
14351 continue;
14352 ++k0;
14353 }
14354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014356 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 while (*s == '-')
14358 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014359 /* "k0" gets NOT reduced because
14360 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 s++;
14362 }
14363 if (*s == '<')
14364 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014365 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 {
14367 p0 = *s - '0';
14368 s++;
14369 }
14370
14371 if (*s == NUL
14372 /* *s == '^' cuts */
14373 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014374 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014375 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 {
14377 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380
14381 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014382 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384 /* rule fits; stop search */
14385 break;
14386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 }
14388
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 }
14392
14393 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014394 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014395 if (s == NULL)
14396 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014397 pf = smp[n].sm_rules;
14398 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 if (p0 == 1 && z == 0)
14400 {
14401 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014402 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14403 || res[reslen - 1] == *s))
14404 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 z0 = 1;
14406 z = 1;
14407 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014408 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 {
14410 word[i + k0] = *s;
14411 k0++;
14412 s++;
14413 }
14414 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014415 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416
14417 /* new "actual letter" */
14418 c = word[i];
14419 }
14420 else
14421 {
14422 /* no '<' rule used */
14423 i += k - 1;
14424 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014425 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014427 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014428 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 s++;
14430 }
14431 /* new "actual letter" */
14432 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014433 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 {
14435 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014436 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014437 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438 i = 0;
14439 z0 = 1;
14440 }
14441 }
14442 break;
14443 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 }
14445 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014446 else if (vim_iswhite(c))
14447 {
14448 c = ' ';
14449 k = 1;
14450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451
14452 if (z0 == 0)
14453 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014454 if (k && !p0 && reslen < MAXWLEN && c != NUL
14455 && (!slang->sl_collapse || reslen == 0
14456 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014458 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459
14460 i++;
14461 z = 0;
14462 k = 0;
14463 }
14464 }
14465
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014466 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014467}
14468
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014469#ifdef FEAT_MBYTE
14470/*
14471 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14472 * Multi-byte version of spell_soundfold().
14473 */
14474 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014475spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014476 slang_T *slang;
14477 char_u *inword;
14478 char_u *res;
14479{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014480 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014481 int word[MAXWLEN];
14482 int wres[MAXWLEN];
14483 int l;
14484 char_u *s;
14485 int *ws;
14486 char_u *t;
14487 int *pf;
14488 int i, j, z;
14489 int reslen;
14490 int n, k = 0;
14491 int z0;
14492 int k0;
14493 int n0;
14494 int c;
14495 int pri;
14496 int p0 = -333;
14497 int c0;
14498 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014499 int wordlen;
14500
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014501
14502 /*
14503 * Convert the multi-byte string to a wide-character string.
14504 * Remove accents, if wanted. We actually remove all non-word characters.
14505 * But keep white space.
14506 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014507 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014508 for (s = inword; *s != NUL; )
14509 {
14510 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014511 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014512 if (slang->sl_rem_accents)
14513 {
14514 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14515 {
14516 if (did_white)
14517 continue;
14518 c = ' ';
14519 did_white = TRUE;
14520 }
14521 else
14522 {
14523 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014524 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014525 continue;
14526 }
14527 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014528 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014529 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014530 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014531
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014532 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014533 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014534 * Converted from C++ to C. Added support for multi-byte chars.
14535 * Changed to keep spaces.
14536 */
14537 i = reslen = z = 0;
14538 while ((c = word[i]) != NUL)
14539 {
14540 /* Start with the first rule that has the character in the word. */
14541 n = slang->sl_sal_first[c & 0xff];
14542 z0 = 0;
14543
14544 if (n >= 0)
14545 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014546 /* Check all rules for the same index byte.
14547 * If c is 0x300 need extra check for the end of the array, as
14548 * (c & 0xff) is NUL. */
14549 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14550 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014551 {
14552 /* Quickly skip entries that don't match the word. Most
14553 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014554 if (c != ws[0])
14555 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014556 k = smp[n].sm_leadlen;
14557 if (k > 1)
14558 {
14559 if (word[i + 1] != ws[1])
14560 continue;
14561 if (k > 2)
14562 {
14563 for (j = 2; j < k; ++j)
14564 if (word[i + j] != ws[j])
14565 break;
14566 if (j < k)
14567 continue;
14568 }
14569 }
14570
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014571 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014572 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014573 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014574 while (*pf != NUL && *pf != word[i + k])
14575 ++pf;
14576 if (*pf == NUL)
14577 continue;
14578 ++k;
14579 }
14580 s = smp[n].sm_rules;
14581 pri = 5; /* default priority */
14582
14583 p0 = *s;
14584 k0 = k;
14585 while (*s == '-' && k > 1)
14586 {
14587 k--;
14588 s++;
14589 }
14590 if (*s == '<')
14591 s++;
14592 if (VIM_ISDIGIT(*s))
14593 {
14594 /* determine priority */
14595 pri = *s - '0';
14596 s++;
14597 }
14598 if (*s == '^' && *(s + 1) == '^')
14599 s++;
14600
14601 if (*s == NUL
14602 || (*s == '^'
14603 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014604 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014605 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014606 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014607 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014608 && spell_iswordp_w(word + i - 1, curwin)
14609 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014610 {
14611 /* search for followup rules, if: */
14612 /* followup and k > 1 and NO '-' in searchstring */
14613 c0 = word[i + k - 1];
14614 n0 = slang->sl_sal_first[c0 & 0xff];
14615
14616 if (slang->sl_followup && k > 1 && n0 >= 0
14617 && p0 != '-' && word[i + k] != NUL)
14618 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014619 /* Test follow-up rule for "word[i + k]"; loop over
14620 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014621 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14622 == (c0 & 0xff); ++n0)
14623 {
14624 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014625 */
14626 if (c0 != ws[0])
14627 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014628 k0 = smp[n0].sm_leadlen;
14629 if (k0 > 1)
14630 {
14631 if (word[i + k] != ws[1])
14632 continue;
14633 if (k0 > 2)
14634 {
14635 pf = word + i + k + 1;
14636 for (j = 2; j < k0; ++j)
14637 if (*pf++ != ws[j])
14638 break;
14639 if (j < k0)
14640 continue;
14641 }
14642 }
14643 k0 += k - 1;
14644
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014645 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014646 {
14647 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014648 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014649 while (*pf != NUL && *pf != word[i + k0])
14650 ++pf;
14651 if (*pf == NUL)
14652 continue;
14653 ++k0;
14654 }
14655
14656 p0 = 5;
14657 s = smp[n0].sm_rules;
14658 while (*s == '-')
14659 {
14660 /* "k0" gets NOT reduced because
14661 * "if (k0 == k)" */
14662 s++;
14663 }
14664 if (*s == '<')
14665 s++;
14666 if (VIM_ISDIGIT(*s))
14667 {
14668 p0 = *s - '0';
14669 s++;
14670 }
14671
14672 if (*s == NUL
14673 /* *s == '^' cuts */
14674 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014675 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014676 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014677 {
14678 if (k0 == k)
14679 /* this is just a piece of the string */
14680 continue;
14681
14682 if (p0 < pri)
14683 /* priority too low */
14684 continue;
14685 /* rule fits; stop search */
14686 break;
14687 }
14688 }
14689
14690 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14691 == (c0 & 0xff))
14692 continue;
14693 }
14694
14695 /* replace string */
14696 ws = smp[n].sm_to_w;
14697 s = smp[n].sm_rules;
14698 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14699 if (p0 == 1 && z == 0)
14700 {
14701 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014702 if (reslen > 0 && ws != NULL && *ws != NUL
14703 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014704 || wres[reslen - 1] == *ws))
14705 reslen--;
14706 z0 = 1;
14707 z = 1;
14708 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014709 if (ws != NULL)
14710 while (*ws != NUL && word[i + k0] != NUL)
14711 {
14712 word[i + k0] = *ws;
14713 k0++;
14714 ws++;
14715 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014716 if (k > k0)
14717 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014718 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014719
14720 /* new "actual letter" */
14721 c = word[i];
14722 }
14723 else
14724 {
14725 /* no '<' rule used */
14726 i += k - 1;
14727 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014728 if (ws != NULL)
14729 while (*ws != NUL && ws[1] != NUL
14730 && reslen < MAXWLEN)
14731 {
14732 if (reslen == 0 || wres[reslen - 1] != *ws)
14733 wres[reslen++] = *ws;
14734 ws++;
14735 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014736 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014737 if (ws == NULL)
14738 c = NUL;
14739 else
14740 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014741 if (strstr((char *)s, "^^") != NULL)
14742 {
14743 if (c != NUL)
14744 wres[reslen++] = c;
14745 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014746 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014747 i = 0;
14748 z0 = 1;
14749 }
14750 }
14751 break;
14752 }
14753 }
14754 }
14755 else if (vim_iswhite(c))
14756 {
14757 c = ' ';
14758 k = 1;
14759 }
14760
14761 if (z0 == 0)
14762 {
14763 if (k && !p0 && reslen < MAXWLEN && c != NUL
14764 && (!slang->sl_collapse || reslen == 0
14765 || wres[reslen - 1] != c))
14766 /* condense only double letters */
14767 wres[reslen++] = c;
14768
14769 i++;
14770 z = 0;
14771 k = 0;
14772 }
14773 }
14774
14775 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14776 l = 0;
14777 for (n = 0; n < reslen; ++n)
14778 {
14779 l += mb_char2bytes(wres[n], res + l);
14780 if (l + MB_MAXBYTES > MAXWLEN)
14781 break;
14782 }
14783 res[l] = NUL;
14784}
14785#endif
14786
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014787/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014788 * Compute a score for two sound-a-like words.
14789 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14790 * Instead of a generic loop we write out the code. That keeps it fast by
14791 * avoiding checks that will not be possible.
14792 */
14793 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014794soundalike_score(goodstart, badstart)
14795 char_u *goodstart; /* sound-folded good word */
14796 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014797{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014798 char_u *goodsound = goodstart;
14799 char_u *badsound = badstart;
14800 int goodlen;
14801 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014802 int n;
14803 char_u *pl, *ps;
14804 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014805 int score = 0;
14806
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014807 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014808 * counted so much, vowels halfway the word aren't counted at all. */
14809 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14810 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014811 if ((badsound[0] == NUL && goodsound[1] == NUL)
14812 || (goodsound[0] == NUL && badsound[1] == NUL))
14813 /* changing word with vowel to word without a sound */
14814 return SCORE_DEL;
14815 if (badsound[0] == NUL || goodsound[0] == NUL)
14816 /* more than two changes */
14817 return SCORE_MAXMAX;
14818
Bram Moolenaar4770d092006-01-12 23:22:24 +000014819 if (badsound[1] == goodsound[1]
14820 || (badsound[1] != NUL
14821 && goodsound[1] != NUL
14822 && badsound[2] == goodsound[2]))
14823 {
14824 /* handle like a substitute */
14825 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014826 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014827 {
14828 score = 2 * SCORE_DEL / 3;
14829 if (*badsound == '*')
14830 ++badsound;
14831 else
14832 ++goodsound;
14833 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014834 }
14835
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014836 goodlen = (int)STRLEN(goodsound);
14837 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014838
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014839 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014840 * changes. */
14841 n = goodlen - badlen;
14842 if (n < -2 || n > 2)
14843 return SCORE_MAXMAX;
14844
14845 if (n > 0)
14846 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014847 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014848 ps = badsound;
14849 }
14850 else
14851 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014852 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014853 ps = goodsound;
14854 }
14855
14856 /* Skip over the identical part. */
14857 while (*pl == *ps && *pl != NUL)
14858 {
14859 ++pl;
14860 ++ps;
14861 }
14862
14863 switch (n)
14864 {
14865 case -2:
14866 case 2:
14867 /*
14868 * Must delete two characters from "pl".
14869 */
14870 ++pl; /* first delete */
14871 while (*pl == *ps)
14872 {
14873 ++pl;
14874 ++ps;
14875 }
14876 /* strings must be equal after second delete */
14877 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014878 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014879
14880 /* Failed to compare. */
14881 break;
14882
14883 case -1:
14884 case 1:
14885 /*
14886 * Minimal one delete from "pl" required.
14887 */
14888
14889 /* 1: delete */
14890 pl2 = pl + 1;
14891 ps2 = ps;
14892 while (*pl2 == *ps2)
14893 {
14894 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014895 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014896 ++pl2;
14897 ++ps2;
14898 }
14899
14900 /* 2: delete then swap, then rest must be equal */
14901 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14902 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014903 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014904
14905 /* 3: delete then substitute, then the rest must be equal */
14906 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014907 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014908
14909 /* 4: first swap then delete */
14910 if (pl[0] == ps[1] && pl[1] == ps[0])
14911 {
14912 pl2 = pl + 2; /* swap, skip two chars */
14913 ps2 = ps + 2;
14914 while (*pl2 == *ps2)
14915 {
14916 ++pl2;
14917 ++ps2;
14918 }
14919 /* delete a char and then strings must be equal */
14920 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014921 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014922 }
14923
14924 /* 5: first substitute then delete */
14925 pl2 = pl + 1; /* substitute, skip one char */
14926 ps2 = ps + 1;
14927 while (*pl2 == *ps2)
14928 {
14929 ++pl2;
14930 ++ps2;
14931 }
14932 /* delete a char and then strings must be equal */
14933 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014934 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014935
14936 /* Failed to compare. */
14937 break;
14938
14939 case 0:
14940 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014941 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014942 * insert is only possible in combination with a delete.
14943 * 1: check if for identical strings
14944 */
14945 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014946 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014947
14948 /* 2: swap */
14949 if (pl[0] == ps[1] && pl[1] == ps[0])
14950 {
14951 pl2 = pl + 2; /* swap, skip two chars */
14952 ps2 = ps + 2;
14953 while (*pl2 == *ps2)
14954 {
14955 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014956 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014957 ++pl2;
14958 ++ps2;
14959 }
14960 /* 3: swap and swap again */
14961 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14962 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014963 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014964
14965 /* 4: swap and substitute */
14966 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014967 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014968 }
14969
14970 /* 5: substitute */
14971 pl2 = pl + 1;
14972 ps2 = ps + 1;
14973 while (*pl2 == *ps2)
14974 {
14975 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014976 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014977 ++pl2;
14978 ++ps2;
14979 }
14980
14981 /* 6: substitute and swap */
14982 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14983 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014984 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014985
14986 /* 7: substitute and substitute */
14987 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014988 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014989
14990 /* 8: insert then delete */
14991 pl2 = pl;
14992 ps2 = ps + 1;
14993 while (*pl2 == *ps2)
14994 {
14995 ++pl2;
14996 ++ps2;
14997 }
14998 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014999 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015000
15001 /* 9: delete then insert */
15002 pl2 = pl + 1;
15003 ps2 = ps;
15004 while (*pl2 == *ps2)
15005 {
15006 ++pl2;
15007 ++ps2;
15008 }
15009 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015010 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015011
15012 /* Failed to compare. */
15013 break;
15014 }
15015
15016 return SCORE_MAXMAX;
15017}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019/*
15020 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015021 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015022 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015023 * The algorithm is described by Du and Chang, 1992.
15024 * The implementation of the algorithm comes from Aspell editdist.cpp,
15025 * edit_distance(). It has been converted from C++ to C and modified to
15026 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015027 */
15028 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000015029spell_edit_score(slang, badword, goodword)
15030 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 char_u *badword;
15032 char_u *goodword;
15033{
15034 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015035 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 int j, i;
15037 int t;
15038 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015039 int pbc, pgc;
15040#ifdef FEAT_MBYTE
15041 char_u *p;
15042 int wbadword[MAXWLEN];
15043 int wgoodword[MAXWLEN];
15044
15045 if (has_mbyte)
15046 {
15047 /* Get the characters from the multi-byte strings and put them in an
15048 * int array for easy access. */
15049 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015050 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015051 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015052 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015053 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015054 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015055 }
15056 else
15057#endif
15058 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015059 badlen = (int)STRLEN(badword) + 1;
15060 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015061 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015062
15063 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15064#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015065 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15066 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015067 if (cnt == NULL)
15068 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015069
15070 CNT(0, 0) = 0;
15071 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015072 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073
15074 for (i = 1; i <= badlen; ++i)
15075 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015076 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015077 for (j = 1; j <= goodlen; ++j)
15078 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015079#ifdef FEAT_MBYTE
15080 if (has_mbyte)
15081 {
15082 bc = wbadword[i - 1];
15083 gc = wgoodword[j - 1];
15084 }
15085 else
15086#endif
15087 {
15088 bc = badword[i - 1];
15089 gc = goodword[j - 1];
15090 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 if (bc == gc)
15092 CNT(i, j) = CNT(i - 1, j - 1);
15093 else
15094 {
15095 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015096 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15098 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015099 {
15100 /* For a similar character use SCORE_SIMILAR. */
15101 if (slang != NULL
15102 && slang->sl_has_map
15103 && similar_chars(slang, gc, bc))
15104 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15105 else
15106 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015108
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015109 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015110 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015111#ifdef FEAT_MBYTE
15112 if (has_mbyte)
15113 {
15114 pbc = wbadword[i - 2];
15115 pgc = wgoodword[j - 2];
15116 }
15117 else
15118#endif
15119 {
15120 pbc = badword[i - 2];
15121 pgc = goodword[j - 2];
15122 }
15123 if (bc == pgc && pbc == gc)
15124 {
15125 t = SCORE_SWAP + CNT(i - 2, j - 2);
15126 if (t < CNT(i, j))
15127 CNT(i, j) = t;
15128 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015129 }
15130 t = SCORE_DEL + CNT(i - 1, j);
15131 if (t < CNT(i, j))
15132 CNT(i, j) = t;
15133 t = SCORE_INS + CNT(i, j - 1);
15134 if (t < CNT(i, j))
15135 CNT(i, j) = t;
15136 }
15137 }
15138 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015139
15140 i = CNT(badlen - 1, goodlen - 1);
15141 vim_free(cnt);
15142 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015144
Bram Moolenaar4770d092006-01-12 23:22:24 +000015145typedef struct
15146{
15147 int badi;
15148 int goodi;
15149 int score;
15150} limitscore_T;
15151
15152/*
15153 * Like spell_edit_score(), but with a limit on the score to make it faster.
15154 * May return SCORE_MAXMAX when the score is higher than "limit".
15155 *
15156 * This uses a stack for the edits still to be tried.
15157 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15158 * for multi-byte characters.
15159 */
15160 static int
15161spell_edit_score_limit(slang, badword, goodword, limit)
15162 slang_T *slang;
15163 char_u *badword;
15164 char_u *goodword;
15165 int limit;
15166{
15167 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15168 int stackidx;
15169 int bi, gi;
15170 int bi2, gi2;
15171 int bc, gc;
15172 int score;
15173 int score_off;
15174 int minscore;
15175 int round;
15176
15177#ifdef FEAT_MBYTE
15178 /* Multi-byte characters require a bit more work, use a different function
15179 * to avoid testing "has_mbyte" quite often. */
15180 if (has_mbyte)
15181 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15182#endif
15183
15184 /*
15185 * The idea is to go from start to end over the words. So long as
15186 * characters are equal just continue, this always gives the lowest score.
15187 * When there is a difference try several alternatives. Each alternative
15188 * increases "score" for the edit distance. Some of the alternatives are
15189 * pushed unto a stack and tried later, some are tried right away. At the
15190 * end of the word the score for one alternative is known. The lowest
15191 * possible score is stored in "minscore".
15192 */
15193 stackidx = 0;
15194 bi = 0;
15195 gi = 0;
15196 score = 0;
15197 minscore = limit + 1;
15198
15199 for (;;)
15200 {
15201 /* Skip over an equal part, score remains the same. */
15202 for (;;)
15203 {
15204 bc = badword[bi];
15205 gc = goodword[gi];
15206 if (bc != gc) /* stop at a char that's different */
15207 break;
15208 if (bc == NUL) /* both words end */
15209 {
15210 if (score < minscore)
15211 minscore = score;
15212 goto pop; /* do next alternative */
15213 }
15214 ++bi;
15215 ++gi;
15216 }
15217
15218 if (gc == NUL) /* goodword ends, delete badword chars */
15219 {
15220 do
15221 {
15222 if ((score += SCORE_DEL) >= minscore)
15223 goto pop; /* do next alternative */
15224 } while (badword[++bi] != NUL);
15225 minscore = score;
15226 }
15227 else if (bc == NUL) /* badword ends, insert badword chars */
15228 {
15229 do
15230 {
15231 if ((score += SCORE_INS) >= minscore)
15232 goto pop; /* do next alternative */
15233 } while (goodword[++gi] != NUL);
15234 minscore = score;
15235 }
15236 else /* both words continue */
15237 {
15238 /* If not close to the limit, perform a change. Only try changes
15239 * that may lead to a lower score than "minscore".
15240 * round 0: try deleting a char from badword
15241 * round 1: try inserting a char in badword */
15242 for (round = 0; round <= 1; ++round)
15243 {
15244 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15245 if (score_off < minscore)
15246 {
15247 if (score_off + SCORE_EDIT_MIN >= minscore)
15248 {
15249 /* Near the limit, rest of the words must match. We
15250 * can check that right now, no need to push an item
15251 * onto the stack. */
15252 bi2 = bi + 1 - round;
15253 gi2 = gi + round;
15254 while (goodword[gi2] == badword[bi2])
15255 {
15256 if (goodword[gi2] == NUL)
15257 {
15258 minscore = score_off;
15259 break;
15260 }
15261 ++bi2;
15262 ++gi2;
15263 }
15264 }
15265 else
15266 {
15267 /* try deleting/inserting a character later */
15268 stack[stackidx].badi = bi + 1 - round;
15269 stack[stackidx].goodi = gi + round;
15270 stack[stackidx].score = score_off;
15271 ++stackidx;
15272 }
15273 }
15274 }
15275
15276 if (score + SCORE_SWAP < minscore)
15277 {
15278 /* If swapping two characters makes a match then the
15279 * substitution is more expensive, thus there is no need to
15280 * try both. */
15281 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15282 {
15283 /* Swap two characters, that is: skip them. */
15284 gi += 2;
15285 bi += 2;
15286 score += SCORE_SWAP;
15287 continue;
15288 }
15289 }
15290
15291 /* Substitute one character for another which is the same
15292 * thing as deleting a character from both goodword and badword.
15293 * Use a better score when there is only a case difference. */
15294 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15295 score += SCORE_ICASE;
15296 else
15297 {
15298 /* For a similar character use SCORE_SIMILAR. */
15299 if (slang != NULL
15300 && slang->sl_has_map
15301 && similar_chars(slang, gc, bc))
15302 score += SCORE_SIMILAR;
15303 else
15304 score += SCORE_SUBST;
15305 }
15306
15307 if (score < minscore)
15308 {
15309 /* Do the substitution. */
15310 ++gi;
15311 ++bi;
15312 continue;
15313 }
15314 }
15315pop:
15316 /*
15317 * Get here to try the next alternative, pop it from the stack.
15318 */
15319 if (stackidx == 0) /* stack is empty, finished */
15320 break;
15321
15322 /* pop an item from the stack */
15323 --stackidx;
15324 gi = stack[stackidx].goodi;
15325 bi = stack[stackidx].badi;
15326 score = stack[stackidx].score;
15327 }
15328
15329 /* When the score goes over "limit" it may actually be much higher.
15330 * Return a very large number to avoid going below the limit when giving a
15331 * bonus. */
15332 if (minscore > limit)
15333 return SCORE_MAXMAX;
15334 return minscore;
15335}
15336
15337#ifdef FEAT_MBYTE
15338/*
15339 * Multi-byte version of spell_edit_score_limit().
15340 * Keep it in sync with the above!
15341 */
15342 static int
15343spell_edit_score_limit_w(slang, badword, goodword, limit)
15344 slang_T *slang;
15345 char_u *badword;
15346 char_u *goodword;
15347 int limit;
15348{
15349 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15350 int stackidx;
15351 int bi, gi;
15352 int bi2, gi2;
15353 int bc, gc;
15354 int score;
15355 int score_off;
15356 int minscore;
15357 int round;
15358 char_u *p;
15359 int wbadword[MAXWLEN];
15360 int wgoodword[MAXWLEN];
15361
15362 /* Get the characters from the multi-byte strings and put them in an
15363 * int array for easy access. */
15364 bi = 0;
15365 for (p = badword; *p != NUL; )
15366 wbadword[bi++] = mb_cptr2char_adv(&p);
15367 wbadword[bi++] = 0;
15368 gi = 0;
15369 for (p = goodword; *p != NUL; )
15370 wgoodword[gi++] = mb_cptr2char_adv(&p);
15371 wgoodword[gi++] = 0;
15372
15373 /*
15374 * The idea is to go from start to end over the words. So long as
15375 * characters are equal just continue, this always gives the lowest score.
15376 * When there is a difference try several alternatives. Each alternative
15377 * increases "score" for the edit distance. Some of the alternatives are
15378 * pushed unto a stack and tried later, some are tried right away. At the
15379 * end of the word the score for one alternative is known. The lowest
15380 * possible score is stored in "minscore".
15381 */
15382 stackidx = 0;
15383 bi = 0;
15384 gi = 0;
15385 score = 0;
15386 minscore = limit + 1;
15387
15388 for (;;)
15389 {
15390 /* Skip over an equal part, score remains the same. */
15391 for (;;)
15392 {
15393 bc = wbadword[bi];
15394 gc = wgoodword[gi];
15395
15396 if (bc != gc) /* stop at a char that's different */
15397 break;
15398 if (bc == NUL) /* both words end */
15399 {
15400 if (score < minscore)
15401 minscore = score;
15402 goto pop; /* do next alternative */
15403 }
15404 ++bi;
15405 ++gi;
15406 }
15407
15408 if (gc == NUL) /* goodword ends, delete badword chars */
15409 {
15410 do
15411 {
15412 if ((score += SCORE_DEL) >= minscore)
15413 goto pop; /* do next alternative */
15414 } while (wbadword[++bi] != NUL);
15415 minscore = score;
15416 }
15417 else if (bc == NUL) /* badword ends, insert badword chars */
15418 {
15419 do
15420 {
15421 if ((score += SCORE_INS) >= minscore)
15422 goto pop; /* do next alternative */
15423 } while (wgoodword[++gi] != NUL);
15424 minscore = score;
15425 }
15426 else /* both words continue */
15427 {
15428 /* If not close to the limit, perform a change. Only try changes
15429 * that may lead to a lower score than "minscore".
15430 * round 0: try deleting a char from badword
15431 * round 1: try inserting a char in badword */
15432 for (round = 0; round <= 1; ++round)
15433 {
15434 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15435 if (score_off < minscore)
15436 {
15437 if (score_off + SCORE_EDIT_MIN >= minscore)
15438 {
15439 /* Near the limit, rest of the words must match. We
15440 * can check that right now, no need to push an item
15441 * onto the stack. */
15442 bi2 = bi + 1 - round;
15443 gi2 = gi + round;
15444 while (wgoodword[gi2] == wbadword[bi2])
15445 {
15446 if (wgoodword[gi2] == NUL)
15447 {
15448 minscore = score_off;
15449 break;
15450 }
15451 ++bi2;
15452 ++gi2;
15453 }
15454 }
15455 else
15456 {
15457 /* try deleting a character from badword later */
15458 stack[stackidx].badi = bi + 1 - round;
15459 stack[stackidx].goodi = gi + round;
15460 stack[stackidx].score = score_off;
15461 ++stackidx;
15462 }
15463 }
15464 }
15465
15466 if (score + SCORE_SWAP < minscore)
15467 {
15468 /* If swapping two characters makes a match then the
15469 * substitution is more expensive, thus there is no need to
15470 * try both. */
15471 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15472 {
15473 /* Swap two characters, that is: skip them. */
15474 gi += 2;
15475 bi += 2;
15476 score += SCORE_SWAP;
15477 continue;
15478 }
15479 }
15480
15481 /* Substitute one character for another which is the same
15482 * thing as deleting a character from both goodword and badword.
15483 * Use a better score when there is only a case difference. */
15484 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15485 score += SCORE_ICASE;
15486 else
15487 {
15488 /* For a similar character use SCORE_SIMILAR. */
15489 if (slang != NULL
15490 && slang->sl_has_map
15491 && similar_chars(slang, gc, bc))
15492 score += SCORE_SIMILAR;
15493 else
15494 score += SCORE_SUBST;
15495 }
15496
15497 if (score < minscore)
15498 {
15499 /* Do the substitution. */
15500 ++gi;
15501 ++bi;
15502 continue;
15503 }
15504 }
15505pop:
15506 /*
15507 * Get here to try the next alternative, pop it from the stack.
15508 */
15509 if (stackidx == 0) /* stack is empty, finished */
15510 break;
15511
15512 /* pop an item from the stack */
15513 --stackidx;
15514 gi = stack[stackidx].goodi;
15515 bi = stack[stackidx].badi;
15516 score = stack[stackidx].score;
15517 }
15518
15519 /* When the score goes over "limit" it may actually be much higher.
15520 * Return a very large number to avoid going below the limit when giving a
15521 * bonus. */
15522 if (minscore > limit)
15523 return SCORE_MAXMAX;
15524 return minscore;
15525}
15526#endif
15527
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015528/*
15529 * ":spellinfo"
15530 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015531 void
15532ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015533 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015534{
15535 int lpi;
15536 langp_T *lp;
15537 char_u *p;
15538
15539 if (no_spell_checking(curwin))
15540 return;
15541
15542 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015543 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015544 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015545 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015546 msg_puts((char_u *)"file: ");
15547 msg_puts(lp->lp_slang->sl_fname);
15548 msg_putchar('\n');
15549 p = lp->lp_slang->sl_info;
15550 if (p != NULL)
15551 {
15552 msg_puts(p);
15553 msg_putchar('\n');
15554 }
15555 }
15556 msg_end();
15557}
15558
Bram Moolenaar4770d092006-01-12 23:22:24 +000015559#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15560#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015561#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015562#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15563#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015564
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015565/*
15566 * ":spelldump"
15567 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015568 void
15569ex_spelldump(eap)
15570 exarg_T *eap;
15571{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015572 char_u *spl;
15573 long dummy;
15574
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015575 if (no_spell_checking(curwin))
15576 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015577 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015578
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015579 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015580 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015581
15582 /* enable spelling locally in the new window */
15583 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
15584 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
15585 vim_free(spl);
15586
Bram Moolenaar860cae12010-06-05 23:22:07 +020015587 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015588 return;
15589
Bram Moolenaar860cae12010-06-05 23:22:07 +020015590 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015591
15592 /* Delete the empty line that we started with. */
15593 if (curbuf->b_ml.ml_line_count > 1)
15594 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15595
15596 redraw_later(NOT_VALID);
15597}
15598
15599/*
15600 * Go through all possible words and:
15601 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15602 * "ic" and "dir" are not used.
15603 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15604 */
15605 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015606spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015607 char_u *pat; /* leading part of the word */
15608 int ic; /* ignore case */
15609 int *dir; /* direction for adding matches */
15610 int dumpflags_arg; /* DUMPFLAG_* */
15611{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015612 langp_T *lp;
15613 slang_T *slang;
15614 idx_T arridx[MAXWLEN];
15615 int curi[MAXWLEN];
15616 char_u word[MAXWLEN];
15617 int c;
15618 char_u *byts;
15619 idx_T *idxs;
15620 linenr_T lnum = 0;
15621 int round;
15622 int depth;
15623 int n;
15624 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015625 char_u *region_names = NULL; /* region names being used */
15626 int do_region = TRUE; /* dump region names and numbers */
15627 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015628 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015629 int dumpflags = dumpflags_arg;
15630 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015631
Bram Moolenaard0131a82006-03-04 21:46:13 +000015632 /* When ignoring case or when the pattern starts with capital pass this on
15633 * to dump_word(). */
15634 if (pat != NULL)
15635 {
15636 if (ic)
15637 dumpflags |= DUMPFLAG_ICASE;
15638 else
15639 {
15640 n = captype(pat, NULL);
15641 if (n == WF_ONECAP)
15642 dumpflags |= DUMPFLAG_ONECAP;
15643 else if (n == WF_ALLCAP
15644#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015645 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015646#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015647 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015648#endif
15649 )
15650 dumpflags |= DUMPFLAG_ALLCAP;
15651 }
15652 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015653
Bram Moolenaar7887d882005-07-01 22:33:52 +000015654 /* Find out if we can support regions: All languages must support the same
15655 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015656 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015657 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015658 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015659 p = lp->lp_slang->sl_regions;
15660 if (p[0] != 0)
15661 {
15662 if (region_names == NULL) /* first language with regions */
15663 region_names = p;
15664 else if (STRCMP(region_names, p) != 0)
15665 {
15666 do_region = FALSE; /* region names are different */
15667 break;
15668 }
15669 }
15670 }
15671
15672 if (do_region && region_names != NULL)
15673 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015674 if (pat == NULL)
15675 {
15676 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15677 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15678 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015679 }
15680 else
15681 do_region = FALSE;
15682
15683 /*
15684 * Loop over all files loaded for the entries in 'spelllang'.
15685 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015686 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015687 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015688 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015689 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015690 if (slang->sl_fbyts == NULL) /* reloading failed */
15691 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015692
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015693 if (pat == NULL)
15694 {
15695 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15696 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15697 }
15698
15699 /* When matching with a pattern and there are no prefixes only use
15700 * parts of the tree that match "pat". */
15701 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015702 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015703 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015704 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015705
15706 /* round 1: case-folded tree
15707 * round 2: keep-case tree */
15708 for (round = 1; round <= 2; ++round)
15709 {
15710 if (round == 1)
15711 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015712 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015713 byts = slang->sl_fbyts;
15714 idxs = slang->sl_fidxs;
15715 }
15716 else
15717 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015718 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015719 byts = slang->sl_kbyts;
15720 idxs = slang->sl_kidxs;
15721 }
15722 if (byts == NULL)
15723 continue; /* array is empty */
15724
15725 depth = 0;
15726 arridx[0] = 0;
15727 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015728 while (depth >= 0 && !got_int
15729 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015730 {
15731 if (curi[depth] > byts[arridx[depth]])
15732 {
15733 /* Done all bytes at this node, go up one level. */
15734 --depth;
15735 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015736 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015737 }
15738 else
15739 {
15740 /* Do one more byte at this node. */
15741 n = arridx[depth] + curi[depth];
15742 ++curi[depth];
15743 c = byts[n];
15744 if (c == 0)
15745 {
15746 /* End of word, deal with the word.
15747 * Don't use keep-case words in the fold-case tree,
15748 * they will appear in the keep-case tree.
15749 * Only use the word when the region matches. */
15750 flags = (int)idxs[n];
15751 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015752 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015753 && (do_region
15754 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015755 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015756 & lp->lp_region) != 0))
15757 {
15758 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015759 if (!do_region)
15760 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015761
15762 /* Dump the basic word if there is no prefix or
15763 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015764 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015765 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015766 {
15767 dump_word(slang, word, pat, dir,
15768 dumpflags, flags, lnum);
15769 if (pat == NULL)
15770 ++lnum;
15771 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015772
15773 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015774 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015775 lnum = dump_prefixes(slang, word, pat, dir,
15776 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015777 }
15778 }
15779 else
15780 {
15781 /* Normal char, go one level deeper. */
15782 word[depth++] = c;
15783 arridx[depth] = idxs[n];
15784 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015785
15786 /* Check if this characters matches with the pattern.
15787 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015788 * Always ignore case here, dump_word() will check
15789 * proper case later. This isn't exactly right when
15790 * length changes for multi-byte characters with
15791 * ignore case... */
15792 if (depth <= patlen
15793 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015794 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015795 }
15796 }
15797 }
15798 }
15799 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015800}
15801
15802/*
15803 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015804 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015805 */
15806 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015807dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015808 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015809 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015810 char_u *pat;
15811 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015812 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015813 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015814 linenr_T lnum;
15815{
15816 int keepcap = FALSE;
15817 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015818 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015819 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015820 char_u badword[MAXWLEN + 10];
15821 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015822 int flags = wordflags;
15823
15824 if (dumpflags & DUMPFLAG_ONECAP)
15825 flags |= WF_ONECAP;
15826 if (dumpflags & DUMPFLAG_ALLCAP)
15827 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015828
Bram Moolenaar4770d092006-01-12 23:22:24 +000015829 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015830 {
15831 /* Need to fix case according to "flags". */
15832 make_case_word(word, cword, flags);
15833 p = cword;
15834 }
15835 else
15836 {
15837 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015838 if ((dumpflags & DUMPFLAG_KEEPCASE)
15839 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015840 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015841 keepcap = TRUE;
15842 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015843 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015844
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015845 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015846 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015847 /* Add flags and regions after a slash. */
15848 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015849 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015850 STRCPY(badword, p);
15851 STRCAT(badword, "/");
15852 if (keepcap)
15853 STRCAT(badword, "=");
15854 if (flags & WF_BANNED)
15855 STRCAT(badword, "!");
15856 else if (flags & WF_RARE)
15857 STRCAT(badword, "?");
15858 if (flags & WF_REGION)
15859 for (i = 0; i < 7; ++i)
15860 if (flags & (0x10000 << i))
15861 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15862 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015863 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015864
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015865 if (dumpflags & DUMPFLAG_COUNT)
15866 {
15867 hashitem_T *hi;
15868
15869 /* Include the word count for ":spelldump!". */
15870 hi = hash_find(&slang->sl_wordcount, tw);
15871 if (!HASHITEM_EMPTY(hi))
15872 {
15873 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15874 tw, HI2WC(hi)->wc_count);
15875 p = IObuff;
15876 }
15877 }
15878
15879 ml_append(lnum, p, (colnr_T)0, FALSE);
15880 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015881 else if (((dumpflags & DUMPFLAG_ICASE)
15882 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15883 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015884 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015885 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015886 /* if dir was BACKWARD then honor it just once */
15887 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015888}
15889
15890/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015891 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15892 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015893 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015894 * Return the updated line number.
15895 */
15896 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015897dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015898 slang_T *slang;
15899 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015900 char_u *pat;
15901 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015902 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015903 int flags; /* flags with prefix ID */
15904 linenr_T startlnum;
15905{
15906 idx_T arridx[MAXWLEN];
15907 int curi[MAXWLEN];
15908 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015909 char_u word_up[MAXWLEN];
15910 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015911 int c;
15912 char_u *byts;
15913 idx_T *idxs;
15914 linenr_T lnum = startlnum;
15915 int depth;
15916 int n;
15917 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015918 int i;
15919
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015920 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015921 * upper-case letter in word_up[]. */
15922 c = PTR2CHAR(word);
15923 if (SPELL_TOUPPER(c) != c)
15924 {
15925 onecap_copy(word, word_up, TRUE);
15926 has_word_up = TRUE;
15927 }
15928
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015929 byts = slang->sl_pbyts;
15930 idxs = slang->sl_pidxs;
15931 if (byts != NULL) /* array not is empty */
15932 {
15933 /*
15934 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015935 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015936 */
15937 depth = 0;
15938 arridx[0] = 0;
15939 curi[0] = 1;
15940 while (depth >= 0 && !got_int)
15941 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015942 n = arridx[depth];
15943 len = byts[n];
15944 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015945 {
15946 /* Done all bytes at this node, go up one level. */
15947 --depth;
15948 line_breakcheck();
15949 }
15950 else
15951 {
15952 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015953 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015954 ++curi[depth];
15955 c = byts[n];
15956 if (c == 0)
15957 {
15958 /* End of prefix, find out how many IDs there are. */
15959 for (i = 1; i < len; ++i)
15960 if (byts[n + i] != 0)
15961 break;
15962 curi[depth] += i - 1;
15963
Bram Moolenaar53805d12005-08-01 07:08:33 +000015964 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15965 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015966 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015967 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015968 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015969 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015970 : flags, lnum);
15971 if (lnum != 0)
15972 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015973 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015974
15975 /* Check for prefix that matches the word when the
15976 * first letter is upper-case, but only if the prefix has
15977 * a condition. */
15978 if (has_word_up)
15979 {
15980 c = valid_word_prefix(i, n, flags, word_up, slang,
15981 TRUE);
15982 if (c != 0)
15983 {
15984 vim_strncpy(prefix + depth, word_up,
15985 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015986 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015987 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015988 : flags, lnum);
15989 if (lnum != 0)
15990 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015991 }
15992 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015993 }
15994 else
15995 {
15996 /* Normal char, go one level deeper. */
15997 prefix[depth++] = c;
15998 arridx[depth] = idxs[n];
15999 curi[depth] = 1;
16000 }
16001 }
16002 }
16003 }
16004
16005 return lnum;
16006}
16007
Bram Moolenaar95529562005-08-25 21:21:38 +000016008/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016009 * Move "p" to the end of word "start".
16010 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000016011 */
16012 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020016013spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000016014 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020016015 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000016016{
16017 char_u *p = start;
16018
Bram Moolenaar860cae12010-06-05 23:22:07 +020016019 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000016020 mb_ptr_adv(p);
16021 return p;
16022}
16023
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016024#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016025/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016026 * For Insert mode completion CTRL-X s:
16027 * Find start of the word in front of column "startcol".
16028 * We don't check if it is badly spelled, with completion we can only change
16029 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016030 * Returns the column number of the word.
16031 */
16032 int
16033spell_word_start(startcol)
16034 int startcol;
16035{
16036 char_u *line;
16037 char_u *p;
16038 int col = 0;
16039
Bram Moolenaar95529562005-08-25 21:21:38 +000016040 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016041 return startcol;
16042
16043 /* Find a word character before "startcol". */
16044 line = ml_get_curline();
16045 for (p = line + startcol; p > line; )
16046 {
16047 mb_ptr_back(line, p);
16048 if (spell_iswordp_nmw(p))
16049 break;
16050 }
16051
16052 /* Go back to start of the word. */
16053 while (p > line)
16054 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016055 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016056 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016057 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016058 break;
16059 col = 0;
16060 }
16061
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016062 return col;
16063}
16064
16065/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016066 * Need to check for 'spellcapcheck' now, the word is removed before
16067 * expand_spelling() is called. Therefore the ugly global variable.
16068 */
16069static int spell_expand_need_cap;
16070
16071 void
16072spell_expand_check_cap(col)
16073 colnr_T col;
16074{
16075 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16076}
16077
16078/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016079 * Get list of spelling suggestions.
16080 * Used for Insert mode completion CTRL-X ?.
16081 * Returns the number of matches. The matches are in "matchp[]", array of
16082 * allocated strings.
16083 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016084 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016085expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016086 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016087 char_u *pat;
16088 char_u ***matchp;
16089{
16090 garray_T ga;
16091
Bram Moolenaar4770d092006-01-12 23:22:24 +000016092 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016093 *matchp = ga.ga_data;
16094 return ga.ga_len;
16095}
16096#endif
16097
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016098#endif /* FEAT_SPELL */