blob: 50636421eae664181c808f3535145db29c02256a [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000080 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#include "vim.h"
307
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000308#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000309
Bram Moolenaar4770d092006-01-12 23:22:24 +0000310#ifndef UNIX /* it's in os_unix.h for Unix */
311# include <time.h> /* for time_t */
312#endif
313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000314#define MAXWLEN 250 /* Assume max. word len is this many bytes.
315 Some places assume a word length fits in a
316 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000317
Bram Moolenaare52325c2005-08-22 22:54:29 +0000318/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000319 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaara2aa31a2014-02-23 22:52:40 +0100320#if VIM_SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000321typedef int idx_T;
322#else
323typedef long idx_T;
324#endif
325
Bram Moolenaar56f78042010-12-08 17:09:32 +0100326#ifdef VMS
327# define SPL_FNAME_TMPL "%s_%s.spl"
328# define SPL_FNAME_ADD "_add."
329# define SPL_FNAME_ASCII "_ascii."
330#else
331# define SPL_FNAME_TMPL "%s.%s.spl"
332# define SPL_FNAME_ADD ".add."
333# define SPL_FNAME_ASCII ".ascii."
334#endif
335
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000336/* Flags used for a word. Only the lowest byte can be used, the region byte
337 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000338#define WF_REGION 0x01 /* region byte follows */
339#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
340#define WF_ALLCAP 0x04 /* word must be all capitals */
341#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000342#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000343#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000344#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000345#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000347/* for <flags2>, shifted up one byte to be used in wn_flags */
348#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000349#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000350#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000351#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000352#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
353#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000354
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000355/* only used for su_badflags */
356#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
357
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000358#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000359
Bram Moolenaar53805d12005-08-01 07:08:33 +0000360/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000361#define WFP_RARE 0x01 /* rare prefix */
362#define WFP_NC 0x02 /* prefix is not combining */
363#define WFP_UP 0x04 /* to-upper prefix */
364#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
365#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000366
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000367/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
368 * byte) and prefcondnr (two bytes). */
369#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
370#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
371#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
372#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
373 * COMPOUNDPERMITFLAG */
374#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
375 * COMPOUNDFORBIDFLAG */
376
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000377
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000378/* flags for <compoptions> */
379#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
380#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
381#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
382#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
383
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000384/* Special byte values for <byte>. Some are only used in the tree for
385 * postponed prefixes, some only in the other trees. This is a bit messy... */
386#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000387 * postponed prefix: no <pflags> */
388#define BY_INDEX 1 /* child is shared, index follows */
389#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
390 * postponed prefix: <pflags> follows */
391#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
392 * follow; never used in prefix tree */
393#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000394
Bram Moolenaar4770d092006-01-12 23:22:24 +0000395/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
396 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000397 * One replacement: from "ft_from" to "ft_to". */
398typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000399{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400 char_u *ft_from;
401 char_u *ft_to;
402} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000403
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000404/* Info from "SAL" entries in ".aff" file used in sl_sal.
405 * The info is split for quick processing by spell_soundfold().
406 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
407typedef struct salitem_S
408{
409 char_u *sm_lead; /* leading letters */
410 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000411 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000412 char_u *sm_rules; /* rules like ^, $, priority */
413 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000414#ifdef FEAT_MBYTE
415 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000416 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000417 int *sm_to_w; /* wide character copy of "sm_to" */
418#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000419} salitem_T;
420
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000421#ifdef FEAT_MBYTE
422typedef int salfirst_T;
423#else
424typedef short salfirst_T;
425#endif
426
Bram Moolenaar5195e452005-08-19 20:32:47 +0000427/* Values for SP_*ERROR are negative, positive values are used by
428 * read_cnt_string(). */
429#define SP_TRUNCERROR -1 /* spell file truncated error */
430#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000431#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000432
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000433/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000434 * Structure used to store words and other info for one language, loaded from
435 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000436 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
437 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
438 *
439 * The "byts" array stores the possible bytes in each tree node, preceded by
440 * the number of possible bytes, sorted on byte value:
441 * <len> <byte1> <byte2> ...
442 * The "idxs" array stores the index of the child node corresponding to the
443 * byte in "byts".
444 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000445 * the flags, region mask and affixID for the word. There may be several
446 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000447 */
448typedef struct slang_S slang_T;
449struct slang_S
450{
451 slang_T *sl_next; /* next language */
452 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000453 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000455
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000458 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000459 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000460 char_u *sl_pbyts; /* prefix tree word bytes */
461 idx_T *sl_pidxs; /* prefix tree word indexes */
462
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000463 char_u *sl_info; /* infotext string or NULL */
464
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000465 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000466
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000467 char_u *sl_midword; /* MIDWORD string or NULL */
468
Bram Moolenaar4770d092006-01-12 23:22:24 +0000469 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
470
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000471 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000472 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000473 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000474 int sl_compoptions; /* COMP_* flags */
475 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000476 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000477 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000478 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000480 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000481 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000482 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
483 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000484
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000485 int sl_prefixcnt; /* number of items in "sl_prefprog" */
486 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488 garray_T sl_rep; /* list of fromto_T entries from REP lines */
489 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
490 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000492 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493 there is none */
494 int sl_followup; /* SAL followup */
495 int sl_collapse; /* SAL collapse_result */
496 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000497 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
498 * "sl_sal_first" maps chars, when has_mbyte
499 * "sl_sal" is a list of wide char lists. */
500 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
501 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000502 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000503
504 /* Info from the .sug file. Loaded on demand. */
505 time_t sl_sugtime; /* timestamp for .sug file */
506 char_u *sl_sbyts; /* soundfolded word bytes */
507 idx_T *sl_sidxs; /* soundfolded word indexes */
508 buf_T *sl_sugbuf; /* buffer with word number table */
509 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
510 load */
511
Bram Moolenaarea424162005-06-16 21:51:00 +0000512 int sl_has_map; /* TRUE if there is a MAP line */
513#ifdef FEAT_MBYTE
514 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
515 int sl_map_array[256]; /* MAP for first 256 chars */
516#else
517 char_u sl_map_array[256]; /* MAP for first 256 chars */
518#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000519 hashtab_T sl_sounddone; /* table with soundfolded words that have
520 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000521};
522
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000523/* First language that is loaded, start of the linked list of loaded
524 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525static slang_T *first_lang = NULL;
526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527/* Flags used in .spl file for soundsalike flags. */
528#define SAL_F0LLOWUP 1
529#define SAL_COLLAPSE 2
530#define SAL_REM_ACCENTS 4
531
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000532/*
533 * Structure used in "b_langp", filled from 'spelllang'.
534 */
535typedef struct langp_S
536{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000537 slang_T *lp_slang; /* info for this language */
538 slang_T *lp_sallang; /* language used for sound folding or NULL */
539 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000540 int lp_region; /* bitmask for region or REGION_ALL */
541} langp_T;
542
543#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
544
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000545#define REGION_ALL 0xff /* word valid in all regions */
546
Bram Moolenaar5195e452005-08-19 20:32:47 +0000547#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
548#define VIMSPELLMAGICL 8
549#define VIMSPELLVERSION 50
550
Bram Moolenaar4770d092006-01-12 23:22:24 +0000551#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
552#define VIMSUGMAGICL 6
553#define VIMSUGVERSION 1
554
Bram Moolenaar5195e452005-08-19 20:32:47 +0000555/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
556#define SN_REGION 0 /* <regionname> section */
557#define SN_CHARFLAGS 1 /* charflags section */
558#define SN_MIDWORD 2 /* <midword> section */
559#define SN_PREFCOND 3 /* <prefcond> section */
560#define SN_REP 4 /* REP items section */
561#define SN_SAL 5 /* SAL items section */
562#define SN_SOFO 6 /* soundfolding section */
563#define SN_MAP 7 /* MAP items section */
564#define SN_COMPOUND 8 /* compound words section */
565#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000566#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000567#define SN_SUGFILE 11 /* timestamp for .sug file */
568#define SN_REPSAL 12 /* REPSAL items section */
569#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000570#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000571#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000572#define SN_END 255 /* end of sections */
573
574#define SNF_REQUIRED 1 /* <sectionflags>: required section */
575
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576/* Result values. Lower number is accepted over higher one. */
577#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000578#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000579#define SP_RARE 1
580#define SP_LOCAL 2
581#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000582
Bram Moolenaar7887d882005-07-01 22:33:52 +0000583/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000584static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000585
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586typedef struct wordcount_S
587{
588 short_u wc_count; /* nr of times word was seen */
589 char_u wc_word[1]; /* word, actually longer */
590} wordcount_T;
591
592static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000593#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000594#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
595#define MAXWORDCOUNT 0xffff
596
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000597/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598 * Information used when looking for suggestions.
599 */
600typedef struct suginfo_S
601{
602 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000605 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u *su_badptr; /* start of bad word in line */
608 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000609 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
611 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000612 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000613 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000614 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615} suginfo_T;
616
617/* One word suggestion. Used in "si_ga". */
618typedef struct suggest_S
619{
620 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000621 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622 int st_orglen; /* length of replaced text */
623 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624 int st_altscore; /* used when st_score compares equal */
625 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000626 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000627 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628} suggest_T;
629
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000631
Bram Moolenaar4770d092006-01-12 23:22:24 +0000632/* TRUE if a word appears in the list of banned words. */
633#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
634
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000635/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000636 * what is displayed, because when rescore_suggestions() is called the score
637 * may change and wrong suggestions may be removed later. */
638#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639
640/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
641 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000642#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643
644/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000645#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000646#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000648#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000650#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000651#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000652#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653#define SCORE_SUBST 93 /* substitute a character */
654#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000658#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000659#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000660#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000661#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000662#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000664#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000665#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
666 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000667
Bram Moolenaar4770d092006-01-12 23:22:24 +0000668#define SCORE_COMMON1 30 /* subtracted for words seen before */
669#define SCORE_COMMON2 40 /* subtracted for words often seen */
670#define SCORE_COMMON3 50 /* subtracted for words very often seen */
671#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
672#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
673
674/* When trying changed soundfold words it becomes slow when trying more than
675 * two changes. With less then two changes it's slightly faster but we miss a
676 * few good suggestions. In rare cases we need to try three of four changes.
677 */
678#define SCORE_SFMAX1 200 /* maximum score for first try */
679#define SCORE_SFMAX2 300 /* maximum score for second try */
680#define SCORE_SFMAX3 400 /* maximum score for third try */
681
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000683#define SCORE_MAXMAX 999999 /* accept any score */
684#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
685
686/* for spell_edit_score_limit() we need to know the minimum value of
687 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
688#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689
690/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691 * Structure to store info for word matching.
692 */
693typedef struct matchinf_S
694{
695 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000696
697 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000698 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000699 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000701 char_u *mi_cend; /* char after what was used for
702 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000703
704 /* case-folded text */
705 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000706 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000707
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 /* for when checking word after a prefix */
709 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000710 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000711 int mi_prefcnt; /* number of entries at mi_prefarridx */
712 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000713#ifdef FEAT_MBYTE
714 int mi_cprefixlen; /* byte length of prefix in original
715 case */
716#else
717# define mi_cprefixlen mi_prefixlen /* it's the same value */
718#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000719
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000720 /* for when checking a compound word */
721 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000722 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
723 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000724 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000725
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000726 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000727 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000728 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200729 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000730
731 /* for NOBREAK */
732 int mi_result2; /* "mi_resul" without following word */
733 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000734} matchinf_T;
735
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000736/*
737 * The tables used for recognizing word characters according to spelling.
738 * These are only used for the first 256 characters of 'encoding'.
739 */
740typedef struct spelltab_S
741{
742 char_u st_isw[256]; /* flags: is word char */
743 char_u st_isu[256]; /* flags: is uppercase char */
744 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000746} spelltab_T;
747
748static spelltab_T spelltab;
749static int did_set_spelltab;
750
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000751#define CF_WORD 0x01
752#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000753
754static void clear_spell_chartab __ARGS((spelltab_T *sp));
755static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200756static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100757static int spell_iswordp_nmw __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000758#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100759static int spell_mb_isword_class __ARGS((int cl, win_T *wp));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000761#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000762static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000763
764/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000766 */
767typedef enum
768{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_START = 0, /* At start of node check for NUL bytes (goodword
770 * ends); if badword ends there is a match, otherwise
771 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000772 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000773 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
775 STATE_PLAIN, /* Use each byte of the node. */
776 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000777 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000778 STATE_INS, /* Insert a byte in the bad word. */
779 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNSWAP, /* Undo swap two characters. */
781 STATE_SWAP3, /* Swap two characters over three. */
782 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000784 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000785 STATE_REP_INI, /* Prepare for using REP items. */
786 STATE_REP, /* Use matching REP items from the .aff file. */
787 STATE_REP_UNDO, /* Undo a REP item replacement. */
788 STATE_FINAL /* End of this node. */
789} state_T;
790
791/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000792 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 */
794typedef struct trystate_S
795{
Bram Moolenaarea424162005-06-16 21:51:00 +0000796 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000797 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000798 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000799 short ts_curi; /* index in list of child nodes */
800 char_u ts_fidx; /* index in fword[], case-folded bad word */
801 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
802 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000803 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000804 * PFD_PREFIXTREE or PFD_NOPREFIX */
805 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000806#ifdef FEAT_MBYTE
807 char_u ts_tcharlen; /* number of bytes in tword character */
808 char_u ts_tcharidx; /* current byte index in tword character */
809 char_u ts_isdiff; /* DIFF_ values */
810 char_u ts_fcharstart; /* index in fword where badword char started */
811#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000812 char_u ts_prewordlen; /* length of word in "preword[]" */
813 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000814 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000815 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000816 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000817 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000818 char_u ts_delidx; /* index in fword for char that was deleted,
819 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000820} trystate_T;
821
Bram Moolenaarea424162005-06-16 21:51:00 +0000822/* values for ts_isdiff */
823#define DIFF_NONE 0 /* no different byte (yet) */
824#define DIFF_YES 1 /* different byte found */
825#define DIFF_INSERT 2 /* inserting character */
826
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827/* values for ts_flags */
828#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
829#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000830#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000831
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000833#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000834#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000835#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000836
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000837/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000838#define FIND_FOLDWORD 0 /* find word case-folded */
839#define FIND_KEEPWORD 1 /* find keep-case word */
840#define FIND_PREFIX 2 /* find word after prefix */
841#define FIND_COMPOUND 3 /* find case-folded compound word */
842#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000843
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000844static slang_T *slang_alloc __ARGS((char_u *lang));
845static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000846static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000847static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000849static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000850static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000851static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
852static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000853static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000854static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000855static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000856static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000857static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000858static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000859static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000860static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000861static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000862static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000863static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000864static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
865static int read_charflags_section __ARGS((FILE *fd));
866static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000868static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000869static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
870static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
871static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000872static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
873static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000874static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000875static int init_syl_tab __ARGS((slang_T *slang));
876static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000877static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
878static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000879#ifdef FEAT_MBYTE
880static int *mb_str2wide __ARGS((char_u *s));
881#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000882static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
884static void clear_midword __ARGS((win_T *buf));
885static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000886static int find_region __ARGS((char_u *rp, char_u *region));
887static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000888static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000889static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000890static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000891static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000892static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000893static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000894static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000895#ifdef FEAT_EVAL
896static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
897#endif
898static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000899static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
900static void suggest_load_files __ARGS((void));
901static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000903static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000904static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000905static void suggest_try_special __ARGS((suginfo_T *su));
906static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000907static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
908static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000909#ifdef FEAT_MBYTE
910static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
911#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000912static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913static void score_comp_sal __ARGS((suginfo_T *su));
914static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000915static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000917static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static void suggest_try_soundalike_finish __ARGS((void));
919static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
920static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000922static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000923static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000924static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
925static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000926static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000927static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000928static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000929static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000930static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
931static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
932static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000934static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000935#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000937static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
938static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#ifdef FEAT_MBYTE
940static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
941#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000942static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
943static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000944static buf_T *open_spellbuf __ARGS((void));
945static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000946
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000947/*
948 * Use our own character-case definitions, because the current locale may
949 * differ from what the .spl file uses.
950 * These must not be called with negative number!
951 */
952#ifndef FEAT_MBYTE
953/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000954# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
955# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000956# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
957#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000958# if defined(HAVE_WCHAR_H)
959# include <wchar.h> /* for towupper() and towlower() */
960# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000961/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
962 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
963 * the "w" library function for characters above 255 if available. */
964# ifdef HAVE_TOWLOWER
965# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000966 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000967# else
968# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000969 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000970# endif
971
972# ifdef HAVE_TOWUPPER
973# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000974 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000975# else
976# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000977 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000978# endif
979
980# ifdef HAVE_ISWUPPER
981# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
982 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
983# else
984# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000985 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000986# endif
987#endif
988
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000989
990static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000991static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000992static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000993static char *e_affname = N_("Affix name too long in %s line %d: %s");
994static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
995static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000996static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000998/* Remember what "z?" replaced. */
999static char_u *repl_from = NULL;
1000static char_u *repl_to = NULL;
1001
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001002/*
1003 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001004 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001005 * "*attrp" is set to the highlight index for a badly spelled word. For a
1006 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001007 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001009 * "capcol" is used to check for a Capitalised word after the end of a
1010 * sentence. If it's zero then perform the check. Return the column where to
1011 * check next, or -1 when no sentence end was found. If it's NULL then don't
1012 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001013 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 * Returns the length of the word in bytes, also when it's OK, so that the
1015 * caller can skip over the word.
1016 */
1017 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001018spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001020 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001021 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001023 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001024{
1025 matchinf_T mi; /* Most things are put in "mi" so that it can
1026 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001027 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001028 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001029 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001030 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001031 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001032
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001033 /* A word never starts at a space or a control character. Return quickly
1034 * then, skipping over the character. */
1035 if (*ptr <= ' ')
1036 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001037
1038 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001039 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001040 return 1;
1041
Bram Moolenaar5195e452005-08-19 20:32:47 +00001042 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001043
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001044 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001045 * 0X99FF. But always do check spelling to find "3GPP" and "11
1046 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001049 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1050 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001051 else
1052 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001053 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055
Bram Moolenaar0c405862005-06-22 22:26:26 +00001056 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001057 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001058 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001059 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001061 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001062 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001063 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001064 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001065
Bram Moolenaar860cae12010-06-05 23:22:07 +02001066 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001067 {
1068 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001069 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001071 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001073 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001074 if (capcol != NULL)
1075 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001076
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001077 /* We always use the characters up to the next non-word character,
1078 * also for bad words. */
1079 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001081 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001082 mi.mi_capflags = 0;
1083 mi.mi_cend = NULL;
1084 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085
Bram Moolenaar5195e452005-08-19 20:32:47 +00001086 /* case-fold the word with one non-word character, so that we can check
1087 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001088 if (*mi.mi_fend != NUL)
1089 mb_ptr_adv(mi.mi_fend);
1090
1091 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1092 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001093 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /* The word is bad unless we recognize it. */
1096 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001097 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001098
1099 /*
1100 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001101 * We check them all, because a word may be matched longer in another
1102 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001103 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001104 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001105 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001106 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001107
1108 /* If reloading fails the language is still in the list but everything
1109 * has been cleared. */
1110 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1111 continue;
1112
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001113 /* Check for a matching word in case-folded words. */
1114 find_word(&mi, FIND_FOLDWORD);
1115
1116 /* Check for a matching word in keep-case words. */
1117 find_word(&mi, FIND_KEEPWORD);
1118
1119 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001120 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001121
1122 /* For a NOBREAK language, may want to use a word without a following
1123 * word as a backup. */
1124 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1125 && mi.mi_result2 != SP_BAD)
1126 {
1127 mi.mi_result = mi.mi_result2;
1128 mi.mi_end = mi.mi_end2;
1129 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001130
1131 /* Count the word in the first language where it's found to be OK. */
1132 if (count_word && mi.mi_result == SP_OK)
1133 {
1134 count_common_word(mi.mi_lp->lp_slang, ptr,
1135 (int)(mi.mi_end - ptr), 1);
1136 count_word = FALSE;
1137 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001138 }
1139
1140 if (mi.mi_result != SP_OK)
1141 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001142 /* If we found a number skip over it. Allows for "42nd". Do flag
1143 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001144 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001145 {
1146 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1147 return nrlen;
1148 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001149
1150 /* When we are at a non-word character there is no error, just
1151 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001152 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001153 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001154 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001155 {
1156 regmatch_T regmatch;
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);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01001564 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +00001565 ? 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 Moolenaar34b466e2013-11-28 17:41:46 +01002183#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002184 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002185#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002186 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002187 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002188 char_u *buf = NULL;
2189 int buflen = 0;
2190 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002191 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002192 int found_one = FALSE;
2193 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaar95529562005-08-25 21:21:38 +00002195 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002197
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002198 /*
2199 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002200 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002201 *
2202 * When searching backwards, we continue in the line to find the last
2203 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002204 *
2205 * We concatenate the start of the next line, so that wrapped words work
2206 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2207 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002208 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002209 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002210 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002211
2212 while (!got_int)
2213 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002214 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002216 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002217 if (buflen < len + MAXWLEN + 2)
2218 {
2219 vim_free(buf);
2220 buflen = len + MAXWLEN + 2;
2221 buf = alloc(buflen);
2222 if (buf == NULL)
2223 break;
2224 }
2225
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002226 /* In first line check first word for Capital. */
2227 if (lnum == 1)
2228 capcol = 0;
2229
2230 /* For checking first word with a capital skip white space. */
2231 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002232 capcol = (int)(skipwhite(line) - line);
2233 else if (curline && wp == curwin)
2234 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002235 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002236 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002237 if (check_need_cap(lnum, col))
2238 capcol = col;
2239
2240 /* Need to get the line again, may have looked at the previous
2241 * one. */
2242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2243 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002244
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 /* Copy the line into "buf" and append the start of the next line if
2246 * possible. */
2247 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002248 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002249 spell_cat_line(buf + STRLEN(buf),
2250 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251
2252 p = buf + skip;
2253 endp = buf + len;
2254 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002255 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002256 /* When searching backward don't search after the cursor. Unless
2257 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002258 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002259 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002260 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002261 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002262 break;
2263
2264 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002266 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002268 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002270 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002271 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002273 /* When searching forward only accept a bad word after
2274 * the cursor. */
2275 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002276 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002277 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002278 && (wrapped
2279 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002280 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002281 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002283#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002284 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002285 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002286 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002287 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002288 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002289 if (!can_spell)
2290 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002291 }
2292 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002293#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002294 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002295
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 if (can_spell)
2297 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002298 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002299 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002300 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002301#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002302 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002303#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002304 if (dir == FORWARD)
2305 {
2306 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002307 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002308 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002309 if (attrp != NULL)
2310 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002311 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002312 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002313 else if (curline)
2314 /* Insert mode completion: put cursor after
2315 * the bad word. */
2316 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002317 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002318 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002320 else
2321 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 }
2324
Bram Moolenaar51485f02005-06-04 21:55:20 +00002325 /* advance to character after the word */
2326 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002327 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 }
2329
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 if (dir == BACKWARD && found_pos.lnum != 0)
2331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002332 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002333 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002335 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002336 }
2337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002339 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002341 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002342 if (dir == BACKWARD)
2343 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002344 /* If we are back at the starting line and searched it again there
2345 * is no match, give up. */
2346 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002347 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002348
2349 if (lnum > 1)
2350 --lnum;
2351 else if (!p_ws)
2352 break; /* at first line and 'nowrapscan' */
2353 else
2354 {
2355 /* Wrap around to the end of the buffer. May search the
2356 * starting line again and accept the last match. */
2357 lnum = wp->w_buffer->b_ml.ml_line_count;
2358 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002359 if (!shortmess(SHM_SEARCH))
2360 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002361 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002362 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002363 }
2364 else
2365 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002366 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2367 ++lnum;
2368 else if (!p_ws)
2369 break; /* at first line and 'nowrapscan' */
2370 else
2371 {
2372 /* Wrap around to the start of the buffer. May search the
2373 * starting line again and accept the first match. */
2374 lnum = 1;
2375 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376 if (!shortmess(SHM_SEARCH))
2377 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002378 }
2379
2380 /* If we are back at the starting line and there is no match then
2381 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002382 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002383 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002384
2385 /* Skip the characters at the start of the next line that were
2386 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002387 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002389 else
2390 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002392 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002393 --capcol;
2394
2395 /* But after empty line check first word in next line */
2396 if (*skipwhite(line) == NUL)
2397 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399
2400 line_breakcheck();
2401 }
2402
Bram Moolenaar0c405862005-06-22 22:26:26 +00002403 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002404 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002405}
2406
2407/*
2408 * For spell checking: concatenate the start of the following line "line" into
2409 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002410 * Keep the blanks at the start of the next line, this is used in win_line()
2411 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002412 */
2413 void
2414spell_cat_line(buf, line, maxlen)
2415 char_u *buf;
2416 char_u *line;
2417 int maxlen;
2418{
2419 char_u *p;
2420 int n;
2421
2422 p = skipwhite(line);
2423 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2424 p = skipwhite(p + 1);
2425
2426 if (*p != NUL)
2427 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002428 /* Only worth concatenating if there is something else than spaces to
2429 * concatenate. */
2430 n = (int)(p - line) + 1;
2431 if (n < maxlen - 1)
2432 {
2433 vim_memset(buf, ' ', n);
2434 vim_strncpy(buf + n, p, maxlen - 1 - n);
2435 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002437}
2438
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002439/*
2440 * Structure used for the cookie argument of do_in_runtimepath().
2441 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002442typedef struct spelload_S
2443{
2444 char_u sl_lang[MAXWLEN + 1]; /* language name */
2445 slang_T *sl_slang; /* resulting slang_T struct */
2446 int sl_nobreak; /* NOBREAK language found */
2447} spelload_T;
2448
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002450 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002453 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454spell_load_lang(lang)
2455 char_u *lang;
2456{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002457 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002459 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002460#ifdef FEAT_AUTOCMD
2461 int round;
2462#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463
Bram Moolenaarb765d632005-06-07 21:00:02 +00002464 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002465 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002466 STRCPY(sl.sl_lang, lang);
2467 sl.sl_slang = NULL;
2468 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002470#ifdef FEAT_AUTOCMD
2471 /* We may retry when no spell file is found for the language, an
2472 * autocommand may load it then. */
2473 for (round = 1; round <= 2; ++round)
2474#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002475 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002476 /*
2477 * Find the first spell file for "lang" in 'runtimepath' and load it.
2478 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002479 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002480#ifdef VMS
2481 "spell/%s_%s.spl",
2482#else
2483 "spell/%s.%s.spl",
2484#endif
2485 lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002486 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002487
2488 if (r == FAIL && *sl.sl_lang != NUL)
2489 {
2490 /* Try loading the ASCII version. */
2491 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002492#ifdef VMS
2493 "spell/%s_ascii.spl",
2494#else
2495 "spell/%s.ascii.spl",
2496#endif
2497 lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002498 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2499
2500#ifdef FEAT_AUTOCMD
2501 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2502 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2503 curbuf->b_fname, FALSE, curbuf))
2504 continue;
2505 break;
2506#endif
2507 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002508#ifdef FEAT_AUTOCMD
2509 break;
2510#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 }
2512
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002513 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002514 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01002515 smsg((char_u *)
2516#ifdef VMS
2517 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2518#else
2519 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2520#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002522 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002523 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002524 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002525 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002526 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002527 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002528 }
2529}
2530
2531/*
2532 * Return the encoding used for spell checking: Use 'encoding', except that we
2533 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2534 */
2535 static char_u *
2536spell_enc()
2537{
2538
2539#ifdef FEAT_MBYTE
2540 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2541 return p_enc;
2542#endif
2543 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544}
2545
2546/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002547 * Get the name of the .spl file for the internal wordlist into
2548 * "fname[MAXPATHL]".
2549 */
2550 static void
2551int_wordlist_spl(fname)
2552 char_u *fname;
2553{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002554 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002555 int_wordlist, spell_enc());
2556}
2557
2558/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560 * Caller must fill "sl_next".
2561 */
2562 static slang_T *
2563slang_alloc(lang)
2564 char_u *lang;
2565{
2566 slang_T *lp;
2567
Bram Moolenaar51485f02005-06-04 21:55:20 +00002568 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 if (lp != NULL)
2570 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002571 if (lang != NULL)
2572 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002575 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002576 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002577 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 return lp;
2581}
2582
2583/*
2584 * Free the contents of an slang_T and the structure itself.
2585 */
2586 static void
2587slang_free(lp)
2588 slang_T *lp;
2589{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 vim_free(lp->sl_fname);
2592 slang_clear(lp);
2593 vim_free(lp);
2594}
2595
2596/*
2597 * Clear an slang_T so that the file can be reloaded.
2598 */
2599 static void
2600slang_clear(lp)
2601 slang_T *lp;
2602{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002603 garray_T *gap;
2604 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002605 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002606 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002607 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608
Bram Moolenaar51485f02005-06-04 21:55:20 +00002609 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002610 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002613 vim_free(lp->sl_pbyts);
2614 lp->sl_pbyts = NULL;
2615
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002617 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002620 vim_free(lp->sl_pidxs);
2621 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002624 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002625 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2626 while (gap->ga_len > 0)
2627 {
2628 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2629 vim_free(ftp->ft_from);
2630 vim_free(ftp->ft_to);
2631 }
2632 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002634
2635 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002636 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002637 {
2638 /* "ga_len" is set to 1 without adding an item for latin1 */
2639 if (gap->ga_data != NULL)
2640 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2641 for (i = 0; i < gap->ga_len; ++i)
2642 vim_free(((int **)gap->ga_data)[i]);
2643 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002644 else
2645 /* SAL items: free salitem_T items */
2646 while (gap->ga_len > 0)
2647 {
2648 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2649 vim_free(smp->sm_lead);
2650 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2651 vim_free(smp->sm_to);
2652#ifdef FEAT_MBYTE
2653 vim_free(smp->sm_lead_w);
2654 vim_free(smp->sm_oneof_w);
2655 vim_free(smp->sm_to_w);
2656#endif
2657 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002658 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002660 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002661 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002662 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002663 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002664 lp->sl_prefprog = NULL;
2665
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002666 vim_free(lp->sl_info);
2667 lp->sl_info = NULL;
2668
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002669 vim_free(lp->sl_midword);
2670 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002671
Bram Moolenaar473de612013-06-08 18:19:48 +02002672 vim_regfree(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002673 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002674 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002675 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002677 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002679 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002680
2681 vim_free(lp->sl_syllable);
2682 lp->sl_syllable = NULL;
2683 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002684
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002685 ga_clear_strings(&lp->sl_comppat);
2686
Bram Moolenaar4770d092006-01-12 23:22:24 +00002687 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2688 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002689
Bram Moolenaar4770d092006-01-12 23:22:24 +00002690#ifdef FEAT_MBYTE
2691 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002692#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693
Bram Moolenaar4770d092006-01-12 23:22:24 +00002694 /* Clear info from .sug file. */
2695 slang_clear_sug(lp);
2696
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002698 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002699 lp->sl_compsylmax = MAXWLEN;
2700 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002701}
2702
2703/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002704 * Clear the info from the .sug file in "lp".
2705 */
2706 static void
2707slang_clear_sug(lp)
2708 slang_T *lp;
2709{
2710 vim_free(lp->sl_sbyts);
2711 lp->sl_sbyts = NULL;
2712 vim_free(lp->sl_sidxs);
2713 lp->sl_sidxs = NULL;
2714 close_spellbuf(lp->sl_sugbuf);
2715 lp->sl_sugbuf = NULL;
2716 lp->sl_sugloaded = FALSE;
2717 lp->sl_sugtime = 0;
2718}
2719
2720/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002721 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002722 * Invoked through do_in_runtimepath().
2723 */
2724 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002725spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002727 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002728{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002729 spelload_T *slp = (spelload_T *)cookie;
2730 slang_T *slang;
2731
2732 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2733 if (slang != NULL)
2734 {
2735 /* When a previously loaded file has NOBREAK also use it for the
2736 * ".add" files. */
2737 if (slp->sl_nobreak && slang->sl_add)
2738 slang->sl_nobreak = TRUE;
2739 else if (slang->sl_nobreak)
2740 slp->sl_nobreak = TRUE;
2741
2742 slp->sl_slang = slang;
2743 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002744}
2745
2746/*
2747 * Load one spell file and store the info into a slang_T.
2748 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002749 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002750 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2751 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2752 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2753 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002754 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002755 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2756 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002758 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002759 static slang_T *
2760spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002761 char_u *fname;
2762 char_u *lang;
2763 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002765{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002767 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002768 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002770 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002771 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002772 char_u *save_sourcing_name = sourcing_name;
2773 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002774 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002775 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002776 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002777
Bram Moolenaarb765d632005-06-07 21:00:02 +00002778 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002779 if (fd == NULL)
2780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 if (!silent)
2782 EMSG2(_(e_notopen), fname);
2783 else if (p_verbose > 2)
2784 {
2785 verbose_enter();
2786 smsg((char_u *)e_notopen, fname);
2787 verbose_leave();
2788 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002789 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002790 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002791 if (p_verbose > 2)
2792 {
2793 verbose_enter();
2794 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2795 verbose_leave();
2796 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797
Bram Moolenaarb765d632005-06-07 21:00:02 +00002798 if (old_lp == NULL)
2799 {
2800 lp = slang_alloc(lang);
2801 if (lp == NULL)
2802 goto endFAIL;
2803
2804 /* Remember the file name, used to reload the file when it's updated. */
2805 lp->sl_fname = vim_strsave(fname);
2806 if (lp->sl_fname == NULL)
2807 goto endFAIL;
2808
Bram Moolenaar56f78042010-12-08 17:09:32 +01002809 /* Check for .add.spl (_add.spl for VMS). */
2810 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002811 }
2812 else
2813 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002814
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002815 /* Set sourcing_name, so that error messages mention the file name. */
2816 sourcing_name = fname;
2817 sourcing_lnum = 0;
2818
Bram Moolenaar4770d092006-01-12 23:22:24 +00002819 /*
2820 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002821 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002822 for (i = 0; i < VIMSPELLMAGICL; ++i)
2823 buf[i] = getc(fd); /* <fileID> */
2824 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2825 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002826 EMSG(_("E757: This does not look like a spell file"));
2827 goto endFAIL;
2828 }
2829 c = getc(fd); /* <versionnr> */
2830 if (c < VIMSPELLVERSION)
2831 {
2832 EMSG(_("E771: Old spell file, needs to be updated"));
2833 goto endFAIL;
2834 }
2835 else if (c > VIMSPELLVERSION)
2836 {
2837 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002838 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002839 }
2840
Bram Moolenaar5195e452005-08-19 20:32:47 +00002841
2842 /*
2843 * <SECTIONS>: <section> ... <sectionend>
2844 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2845 */
2846 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002847 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002848 n = getc(fd); /* <sectionID> or <sectionend> */
2849 if (n == SN_END)
2850 break;
2851 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002852 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002853 if (len < 0)
2854 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002855
Bram Moolenaar5195e452005-08-19 20:32:47 +00002856 res = 0;
2857 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002858 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002859 case SN_INFO:
2860 lp->sl_info = read_string(fd, len); /* <infotext> */
2861 if (lp->sl_info == NULL)
2862 goto endFAIL;
2863 break;
2864
Bram Moolenaar5195e452005-08-19 20:32:47 +00002865 case SN_REGION:
2866 res = read_region_section(fd, lp, len);
2867 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002868
Bram Moolenaar5195e452005-08-19 20:32:47 +00002869 case SN_CHARFLAGS:
2870 res = read_charflags_section(fd);
2871 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002872
Bram Moolenaar5195e452005-08-19 20:32:47 +00002873 case SN_MIDWORD:
2874 lp->sl_midword = read_string(fd, len); /* <midword> */
2875 if (lp->sl_midword == NULL)
2876 goto endFAIL;
2877 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002878
Bram Moolenaar5195e452005-08-19 20:32:47 +00002879 case SN_PREFCOND:
2880 res = read_prefcond_section(fd, lp);
2881 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002882
Bram Moolenaar5195e452005-08-19 20:32:47 +00002883 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002884 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2885 break;
2886
2887 case SN_REPSAL:
2888 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002889 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002890
Bram Moolenaar5195e452005-08-19 20:32:47 +00002891 case SN_SAL:
2892 res = read_sal_section(fd, lp);
2893 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002894
Bram Moolenaar5195e452005-08-19 20:32:47 +00002895 case SN_SOFO:
2896 res = read_sofo_section(fd, lp);
2897 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002898
Bram Moolenaar5195e452005-08-19 20:32:47 +00002899 case SN_MAP:
2900 p = read_string(fd, len); /* <mapstr> */
2901 if (p == NULL)
2902 goto endFAIL;
2903 set_map_str(lp, p);
2904 vim_free(p);
2905 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002906
Bram Moolenaar4770d092006-01-12 23:22:24 +00002907 case SN_WORDS:
2908 res = read_words_section(fd, lp, len);
2909 break;
2910
2911 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002912 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002913 break;
2914
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002915 case SN_NOSPLITSUGS:
2916 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2917 break;
2918
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 case SN_COMPOUND:
2920 res = read_compound(fd, lp, len);
2921 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002922
Bram Moolenaar78622822005-08-23 21:00:13 +00002923 case SN_NOBREAK:
2924 lp->sl_nobreak = TRUE;
2925 break;
2926
Bram Moolenaar5195e452005-08-19 20:32:47 +00002927 case SN_SYLLABLE:
2928 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2929 if (lp->sl_syllable == NULL)
2930 goto endFAIL;
2931 if (init_syl_tab(lp) == FAIL)
2932 goto endFAIL;
2933 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002934
Bram Moolenaar5195e452005-08-19 20:32:47 +00002935 default:
2936 /* Unsupported section. When it's required give an error
2937 * message. When it's not required skip the contents. */
2938 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002939 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002941 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002942 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 while (--len >= 0)
2944 if (getc(fd) < 0)
2945 goto truncerr;
2946 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002947 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002948someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002949 if (res == SP_FORMERROR)
2950 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002951 EMSG(_(e_format));
2952 goto endFAIL;
2953 }
2954 if (res == SP_TRUNCERROR)
2955 {
2956truncerr:
2957 EMSG(_(e_spell_trunc));
2958 goto endFAIL;
2959 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002960 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002961 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002962 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002963
Bram Moolenaar4770d092006-01-12 23:22:24 +00002964 /* <LWORDTREE> */
2965 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2966 if (res != 0)
2967 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002968
Bram Moolenaar4770d092006-01-12 23:22:24 +00002969 /* <KWORDTREE> */
2970 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2971 if (res != 0)
2972 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973
Bram Moolenaar4770d092006-01-12 23:22:24 +00002974 /* <PREFIXTREE> */
2975 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2976 lp->sl_prefixcnt);
2977 if (res != 0)
2978 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002979
Bram Moolenaarb765d632005-06-07 21:00:02 +00002980 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002981 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002982 {
2983 lp->sl_next = first_lang;
2984 first_lang = lp;
2985 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002986
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002987 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002988
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002989endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002990 if (lang != NULL)
2991 /* truncating the name signals the error to spell_load_lang() */
2992 *lang = NUL;
2993 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002994 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002995 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002996
2997endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002998 if (fd != NULL)
2999 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003000 sourcing_name = save_sourcing_name;
3001 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002
3003 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004}
3005
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003006/*
3007 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003008 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003009 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3011 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003012 */
3013 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003014read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003015 FILE *fd;
3016 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003017 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003018{
3019 int cnt = 0;
3020 int i;
3021 char_u *str;
3022
3023 /* read the length bytes, MSB first */
3024 for (i = 0; i < cnt_bytes; ++i)
3025 cnt = (cnt << 8) + getc(fd);
3026 if (cnt < 0)
3027 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003028 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003029 return NULL;
3030 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003031 *cntp = cnt;
3032 if (cnt == 0)
3033 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003034
Bram Moolenaar5195e452005-08-19 20:32:47 +00003035 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003036 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003037 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003038 return str;
3039}
3040
Bram Moolenaar7887d882005-07-01 22:33:52 +00003041/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003042 * Read SN_REGION: <regionname> ...
3043 * Return SP_*ERROR flags.
3044 */
3045 static int
3046read_region_section(fd, lp, len)
3047 FILE *fd;
3048 slang_T *lp;
3049 int len;
3050{
3051 int i;
3052
3053 if (len > 16)
3054 return SP_FORMERROR;
3055 for (i = 0; i < len; ++i)
3056 lp->sl_regions[i] = getc(fd); /* <regionname> */
3057 lp->sl_regions[len] = NUL;
3058 return 0;
3059}
3060
3061/*
3062 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3063 * <folcharslen> <folchars>
3064 * Return SP_*ERROR flags.
3065 */
3066 static int
3067read_charflags_section(fd)
3068 FILE *fd;
3069{
3070 char_u *flags;
3071 char_u *fol;
3072 int flagslen, follen;
3073
3074 /* <charflagslen> <charflags> */
3075 flags = read_cnt_string(fd, 1, &flagslen);
3076 if (flagslen < 0)
3077 return flagslen;
3078
3079 /* <folcharslen> <folchars> */
3080 fol = read_cnt_string(fd, 2, &follen);
3081 if (follen < 0)
3082 {
3083 vim_free(flags);
3084 return follen;
3085 }
3086
3087 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3088 if (flags != NULL && fol != NULL)
3089 set_spell_charflags(flags, flagslen, fol);
3090
3091 vim_free(flags);
3092 vim_free(fol);
3093
3094 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3095 if ((flags == NULL) != (fol == NULL))
3096 return SP_FORMERROR;
3097 return 0;
3098}
3099
3100/*
3101 * Read SN_PREFCOND section.
3102 * Return SP_*ERROR flags.
3103 */
3104 static int
3105read_prefcond_section(fd, lp)
3106 FILE *fd;
3107 slang_T *lp;
3108{
3109 int cnt;
3110 int i;
3111 int n;
3112 char_u *p;
3113 char_u buf[MAXWLEN + 1];
3114
3115 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003116 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003117 if (cnt <= 0)
3118 return SP_FORMERROR;
3119
3120 lp->sl_prefprog = (regprog_T **)alloc_clear(
3121 (unsigned)sizeof(regprog_T *) * cnt);
3122 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003123 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003124 lp->sl_prefixcnt = cnt;
3125
3126 for (i = 0; i < cnt; ++i)
3127 {
3128 /* <prefcond> : <condlen> <condstr> */
3129 n = getc(fd); /* <condlen> */
3130 if (n < 0 || n >= MAXWLEN)
3131 return SP_FORMERROR;
3132
3133 /* When <condlen> is zero we have an empty condition. Otherwise
3134 * compile the regexp program used to check for the condition. */
3135 if (n > 0)
3136 {
3137 buf[0] = '^'; /* always match at one position only */
3138 p = buf + 1;
3139 while (n-- > 0)
3140 *p++ = getc(fd); /* <condstr> */
3141 *p = NUL;
3142 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3143 }
3144 }
3145 return 0;
3146}
3147
3148/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003149 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150 * Return SP_*ERROR flags.
3151 */
3152 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003153read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003155 garray_T *gap;
3156 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003157{
3158 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003160 int i;
3161
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003162 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003163 if (cnt < 0)
3164 return SP_TRUNCERROR;
3165
Bram Moolenaar5195e452005-08-19 20:32:47 +00003166 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003167 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003168
3169 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3170 for (; gap->ga_len < cnt; ++gap->ga_len)
3171 {
3172 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3173 ftp->ft_from = read_cnt_string(fd, 1, &i);
3174 if (i < 0)
3175 return i;
3176 if (i == 0)
3177 return SP_FORMERROR;
3178 ftp->ft_to = read_cnt_string(fd, 1, &i);
3179 if (i <= 0)
3180 {
3181 vim_free(ftp->ft_from);
3182 if (i < 0)
3183 return i;
3184 return SP_FORMERROR;
3185 }
3186 }
3187
3188 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003189 for (i = 0; i < 256; ++i)
3190 first[i] = -1;
3191 for (i = 0; i < gap->ga_len; ++i)
3192 {
3193 ftp = &((fromto_T *)gap->ga_data)[i];
3194 if (first[*ftp->ft_from] == -1)
3195 first[*ftp->ft_from] = i;
3196 }
3197 return 0;
3198}
3199
3200/*
3201 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3202 * Return SP_*ERROR flags.
3203 */
3204 static int
3205read_sal_section(fd, slang)
3206 FILE *fd;
3207 slang_T *slang;
3208{
3209 int i;
3210 int cnt;
3211 garray_T *gap;
3212 salitem_T *smp;
3213 int ccnt;
3214 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003215 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003216
3217 slang->sl_sofo = FALSE;
3218
3219 i = getc(fd); /* <salflags> */
3220 if (i & SAL_F0LLOWUP)
3221 slang->sl_followup = TRUE;
3222 if (i & SAL_COLLAPSE)
3223 slang->sl_collapse = TRUE;
3224 if (i & SAL_REM_ACCENTS)
3225 slang->sl_rem_accents = TRUE;
3226
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003227 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003228 if (cnt < 0)
3229 return SP_TRUNCERROR;
3230
3231 gap = &slang->sl_sal;
3232 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003233 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003234 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003235
3236 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3237 for (; gap->ga_len < cnt; ++gap->ga_len)
3238 {
3239 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3240 ccnt = getc(fd); /* <salfromlen> */
3241 if (ccnt < 0)
3242 return SP_TRUNCERROR;
3243 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003244 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003245 smp->sm_lead = p;
3246
3247 /* Read up to the first special char into sm_lead. */
3248 for (i = 0; i < ccnt; ++i)
3249 {
3250 c = getc(fd); /* <salfrom> */
3251 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3252 break;
3253 *p++ = c;
3254 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003255 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003256 *p++ = NUL;
3257
3258 /* Put (abc) chars in sm_oneof, if any. */
3259 if (c == '(')
3260 {
3261 smp->sm_oneof = p;
3262 for (++i; i < ccnt; ++i)
3263 {
3264 c = getc(fd); /* <salfrom> */
3265 if (c == ')')
3266 break;
3267 *p++ = c;
3268 }
3269 *p++ = NUL;
3270 if (++i < ccnt)
3271 c = getc(fd);
3272 }
3273 else
3274 smp->sm_oneof = NULL;
3275
3276 /* Any following chars go in sm_rules. */
3277 smp->sm_rules = p;
3278 if (i < ccnt)
3279 /* store the char we got while checking for end of sm_lead */
3280 *p++ = c;
3281 for (++i; i < ccnt; ++i)
3282 *p++ = getc(fd); /* <salfrom> */
3283 *p++ = NUL;
3284
3285 /* <saltolen> <salto> */
3286 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3287 if (ccnt < 0)
3288 {
3289 vim_free(smp->sm_lead);
3290 return ccnt;
3291 }
3292
3293#ifdef FEAT_MBYTE
3294 if (has_mbyte)
3295 {
3296 /* convert the multi-byte strings to wide char strings */
3297 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3298 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3299 if (smp->sm_oneof == NULL)
3300 smp->sm_oneof_w = NULL;
3301 else
3302 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3303 if (smp->sm_to == NULL)
3304 smp->sm_to_w = NULL;
3305 else
3306 smp->sm_to_w = mb_str2wide(smp->sm_to);
3307 if (smp->sm_lead_w == NULL
3308 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3309 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3310 {
3311 vim_free(smp->sm_lead);
3312 vim_free(smp->sm_to);
3313 vim_free(smp->sm_lead_w);
3314 vim_free(smp->sm_oneof_w);
3315 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003316 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003317 }
3318 }
3319#endif
3320 }
3321
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003322 if (gap->ga_len > 0)
3323 {
3324 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3325 * that we need to check the index every time. */
3326 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3327 if ((p = alloc(1)) == NULL)
3328 return SP_OTHERERROR;
3329 p[0] = NUL;
3330 smp->sm_lead = p;
3331 smp->sm_leadlen = 0;
3332 smp->sm_oneof = NULL;
3333 smp->sm_rules = p;
3334 smp->sm_to = NULL;
3335#ifdef FEAT_MBYTE
3336 if (has_mbyte)
3337 {
3338 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3339 smp->sm_leadlen = 0;
3340 smp->sm_oneof_w = NULL;
3341 smp->sm_to_w = NULL;
3342 }
3343#endif
3344 ++gap->ga_len;
3345 }
3346
Bram Moolenaar5195e452005-08-19 20:32:47 +00003347 /* Fill the first-index table. */
3348 set_sal_first(slang);
3349
3350 return 0;
3351}
3352
3353/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003354 * Read SN_WORDS: <word> ...
3355 * Return SP_*ERROR flags.
3356 */
3357 static int
3358read_words_section(fd, lp, len)
3359 FILE *fd;
3360 slang_T *lp;
3361 int len;
3362{
3363 int done = 0;
3364 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003365 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003366 char_u word[MAXWLEN];
3367
3368 while (done < len)
3369 {
3370 /* Read one word at a time. */
3371 for (i = 0; ; ++i)
3372 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003373 c = getc(fd);
3374 if (c == EOF)
3375 return SP_TRUNCERROR;
3376 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003377 if (word[i] == NUL)
3378 break;
3379 if (i == MAXWLEN - 1)
3380 return SP_FORMERROR;
3381 }
3382
3383 /* Init the count to 10. */
3384 count_common_word(lp, word, -1, 10);
3385 done += i + 1;
3386 }
3387 return 0;
3388}
3389
3390/*
3391 * Add a word to the hashtable of common words.
3392 * If it's already there then the counter is increased.
3393 */
3394 static void
3395count_common_word(lp, word, len, count)
3396 slang_T *lp;
3397 char_u *word;
3398 int len; /* word length, -1 for upto NUL */
3399 int count; /* 1 to count once, 10 to init */
3400{
3401 hash_T hash;
3402 hashitem_T *hi;
3403 wordcount_T *wc;
3404 char_u buf[MAXWLEN];
3405 char_u *p;
3406
3407 if (len == -1)
3408 p = word;
3409 else
3410 {
3411 vim_strncpy(buf, word, len);
3412 p = buf;
3413 }
3414
3415 hash = hash_hash(p);
3416 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3417 if (HASHITEM_EMPTY(hi))
3418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003419 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003420 if (wc == NULL)
3421 return;
3422 STRCPY(wc->wc_word, p);
3423 wc->wc_count = count;
3424 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3425 }
3426 else
3427 {
3428 wc = HI2WC(hi);
3429 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3430 wc->wc_count = MAXWORDCOUNT;
3431 }
3432}
3433
3434/*
3435 * Adjust the score of common words.
3436 */
3437 static int
3438score_wordcount_adj(slang, score, word, split)
3439 slang_T *slang;
3440 int score;
3441 char_u *word;
3442 int split; /* word was split, less bonus */
3443{
3444 hashitem_T *hi;
3445 wordcount_T *wc;
3446 int bonus;
3447 int newscore;
3448
3449 hi = hash_find(&slang->sl_wordcount, word);
3450 if (!HASHITEM_EMPTY(hi))
3451 {
3452 wc = HI2WC(hi);
3453 if (wc->wc_count < SCORE_THRES2)
3454 bonus = SCORE_COMMON1;
3455 else if (wc->wc_count < SCORE_THRES3)
3456 bonus = SCORE_COMMON2;
3457 else
3458 bonus = SCORE_COMMON3;
3459 if (split)
3460 newscore = score - bonus / 2;
3461 else
3462 newscore = score - bonus;
3463 if (newscore < 0)
3464 return 0;
3465 return newscore;
3466 }
3467 return score;
3468}
3469
3470/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003471 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3472 * Return SP_*ERROR flags.
3473 */
3474 static int
3475read_sofo_section(fd, slang)
3476 FILE *fd;
3477 slang_T *slang;
3478{
3479 int cnt;
3480 char_u *from, *to;
3481 int res;
3482
3483 slang->sl_sofo = TRUE;
3484
3485 /* <sofofromlen> <sofofrom> */
3486 from = read_cnt_string(fd, 2, &cnt);
3487 if (cnt < 0)
3488 return cnt;
3489
3490 /* <sofotolen> <sofoto> */
3491 to = read_cnt_string(fd, 2, &cnt);
3492 if (cnt < 0)
3493 {
3494 vim_free(from);
3495 return cnt;
3496 }
3497
3498 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3499 if (from != NULL && to != NULL)
3500 res = set_sofo(slang, from, to);
3501 else if (from != NULL || to != NULL)
3502 res = SP_FORMERROR; /* only one of two strings is an error */
3503 else
3504 res = 0;
3505
3506 vim_free(from);
3507 vim_free(to);
3508 return res;
3509}
3510
3511/*
3512 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003513 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003514 * Returns SP_*ERROR flags.
3515 */
3516 static int
3517read_compound(fd, slang, len)
3518 FILE *fd;
3519 slang_T *slang;
3520 int len;
3521{
3522 int todo = len;
3523 int c;
3524 int atstart;
3525 char_u *pat;
3526 char_u *pp;
3527 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003528 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003529 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003530 int cnt;
3531 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003532
3533 if (todo < 2)
3534 return SP_FORMERROR; /* need at least two bytes */
3535
3536 --todo;
3537 c = getc(fd); /* <compmax> */
3538 if (c < 2)
3539 c = MAXWLEN;
3540 slang->sl_compmax = c;
3541
3542 --todo;
3543 c = getc(fd); /* <compminlen> */
3544 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003545 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003546 slang->sl_compminlen = c;
3547
3548 --todo;
3549 c = getc(fd); /* <compsylmax> */
3550 if (c < 1)
3551 c = MAXWLEN;
3552 slang->sl_compsylmax = c;
3553
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003554 c = getc(fd); /* <compoptions> */
3555 if (c != 0)
3556 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3557 else
3558 {
3559 --todo;
3560 c = getc(fd); /* only use the lower byte for now */
3561 --todo;
3562 slang->sl_compoptions = c;
3563
3564 gap = &slang->sl_comppat;
3565 c = get2c(fd); /* <comppatcount> */
3566 todo -= 2;
3567 ga_init2(gap, sizeof(char_u *), c);
3568 if (ga_grow(gap, c) == OK)
3569 while (--c >= 0)
3570 {
3571 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3572 read_cnt_string(fd, 1, &cnt);
3573 /* <comppatlen> <comppattext> */
3574 if (cnt < 0)
3575 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003576 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003577 }
3578 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003579 if (todo < 0)
3580 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003581
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003582 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003583 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003584 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3585 * Conversion to utf-8 may double the size. */
3586 c = todo * 2 + 7;
3587#ifdef FEAT_MBYTE
3588 if (enc_utf8)
3589 c += todo * 2;
3590#endif
3591 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003592 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003593 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003594
Bram Moolenaard12a1322005-08-21 22:08:24 +00003595 /* We also need a list of all flags that can appear at the start and one
3596 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003597 cp = alloc(todo + 1);
3598 if (cp == NULL)
3599 {
3600 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003601 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003602 }
3603 slang->sl_compstartflags = cp;
3604 *cp = NUL;
3605
Bram Moolenaard12a1322005-08-21 22:08:24 +00003606 ap = alloc(todo + 1);
3607 if (ap == NULL)
3608 {
3609 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003610 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003611 }
3612 slang->sl_compallflags = ap;
3613 *ap = NUL;
3614
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003615 /* And a list of all patterns in their original form, for checking whether
3616 * compounding may work in match_compoundrule(). This is freed when we
3617 * encounter a wildcard, the check doesn't work then. */
3618 crp = alloc(todo + 1);
3619 slang->sl_comprules = crp;
3620
Bram Moolenaar5195e452005-08-19 20:32:47 +00003621 pp = pat;
3622 *pp++ = '^';
3623 *pp++ = '\\';
3624 *pp++ = '(';
3625
3626 atstart = 1;
3627 while (todo-- > 0)
3628 {
3629 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003630 if (c == EOF)
3631 {
3632 vim_free(pat);
3633 return SP_TRUNCERROR;
3634 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003635
3636 /* Add all flags to "sl_compallflags". */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003637 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003638 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003639 {
3640 *ap++ = c;
3641 *ap = NUL;
3642 }
3643
Bram Moolenaar5195e452005-08-19 20:32:47 +00003644 if (atstart != 0)
3645 {
3646 /* At start of item: copy flags to "sl_compstartflags". For a
3647 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3648 if (c == '[')
3649 atstart = 2;
3650 else if (c == ']')
3651 atstart = 0;
3652 else
3653 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003654 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003655 {
3656 *cp++ = c;
3657 *cp = NUL;
3658 }
3659 if (atstart == 1)
3660 atstart = 0;
3661 }
3662 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003663
3664 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3665 if (crp != NULL)
3666 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003667 if (c == '?' || c == '+' || c == '*')
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003668 {
3669 vim_free(slang->sl_comprules);
3670 slang->sl_comprules = NULL;
3671 crp = NULL;
3672 }
3673 else
3674 *crp++ = c;
3675 }
3676
Bram Moolenaar5195e452005-08-19 20:32:47 +00003677 if (c == '/') /* slash separates two items */
3678 {
3679 *pp++ = '\\';
3680 *pp++ = '|';
3681 atstart = 1;
3682 }
3683 else /* normal char, "[abc]" and '*' are copied as-is */
3684 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01003685 if (c == '?' || c == '+' || c == '~')
3686 *pp++ = '\\'; /* "a?" becomes "a\?", "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003687#ifdef FEAT_MBYTE
3688 if (enc_utf8)
3689 pp += mb_char2bytes(c, pp);
3690 else
3691#endif
3692 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003693 }
3694 }
3695
3696 *pp++ = '\\';
3697 *pp++ = ')';
3698 *pp++ = '$';
3699 *pp = NUL;
3700
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003701 if (crp != NULL)
3702 *crp = NUL;
3703
Bram Moolenaar5195e452005-08-19 20:32:47 +00003704 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3705 vim_free(pat);
3706 if (slang->sl_compprog == NULL)
3707 return SP_FORMERROR;
3708
3709 return 0;
3710}
3711
Bram Moolenaar6de68532005-08-24 22:08:48 +00003712/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003713 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003714 * Like strchr() but independent of locale.
3715 */
3716 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003717byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003718 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003719 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003720{
3721 char_u *p;
3722
3723 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003724 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003725 return TRUE;
3726 return FALSE;
3727}
3728
Bram Moolenaar5195e452005-08-19 20:32:47 +00003729#define SY_MAXLEN 30
3730typedef struct syl_item_S
3731{
3732 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3733 int sy_len;
3734} syl_item_T;
3735
3736/*
3737 * Truncate "slang->sl_syllable" at the first slash and put the following items
3738 * in "slang->sl_syl_items".
3739 */
3740 static int
3741init_syl_tab(slang)
3742 slang_T *slang;
3743{
3744 char_u *p;
3745 char_u *s;
3746 int l;
3747 syl_item_T *syl;
3748
3749 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3750 p = vim_strchr(slang->sl_syllable, '/');
3751 while (p != NULL)
3752 {
3753 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003754 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003755 break;
3756 s = p;
3757 p = vim_strchr(p, '/');
3758 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003759 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003760 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003761 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003762 if (l >= SY_MAXLEN)
3763 return SP_FORMERROR;
3764 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003765 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003766 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3767 + slang->sl_syl_items.ga_len++;
3768 vim_strncpy(syl->sy_chars, s, l);
3769 syl->sy_len = l;
3770 }
3771 return OK;
3772}
3773
3774/*
3775 * Count the number of syllables in "word".
3776 * When "word" contains spaces the syllables after the last space are counted.
3777 * Returns zero if syllables are not defines.
3778 */
3779 static int
3780count_syllables(slang, word)
3781 slang_T *slang;
3782 char_u *word;
3783{
3784 int cnt = 0;
3785 int skip = FALSE;
3786 char_u *p;
3787 int len;
3788 int i;
3789 syl_item_T *syl;
3790 int c;
3791
3792 if (slang->sl_syllable == NULL)
3793 return 0;
3794
3795 for (p = word; *p != NUL; p += len)
3796 {
3797 /* When running into a space reset counter. */
3798 if (*p == ' ')
3799 {
3800 len = 1;
3801 cnt = 0;
3802 continue;
3803 }
3804
3805 /* Find longest match of syllable items. */
3806 len = 0;
3807 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3808 {
3809 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3810 if (syl->sy_len > len
3811 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3812 len = syl->sy_len;
3813 }
3814 if (len != 0) /* found a match, count syllable */
3815 {
3816 ++cnt;
3817 skip = FALSE;
3818 }
3819 else
3820 {
3821 /* No recognized syllable item, at least a syllable char then? */
3822#ifdef FEAT_MBYTE
3823 c = mb_ptr2char(p);
3824 len = (*mb_ptr2len)(p);
3825#else
3826 c = *p;
3827 len = 1;
3828#endif
3829 if (vim_strchr(slang->sl_syllable, c) == NULL)
3830 skip = FALSE; /* No, search for next syllable */
3831 else if (!skip)
3832 {
3833 ++cnt; /* Yes, count it */
3834 skip = TRUE; /* don't count following syllable chars */
3835 }
3836 }
3837 }
3838 return cnt;
3839}
3840
3841/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003842 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003843 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003844 */
3845 static int
3846set_sofo(lp, from, to)
3847 slang_T *lp;
3848 char_u *from;
3849 char_u *to;
3850{
3851 int i;
3852
3853#ifdef FEAT_MBYTE
3854 garray_T *gap;
3855 char_u *s;
3856 char_u *p;
3857 int c;
3858 int *inp;
3859
3860 if (has_mbyte)
3861 {
3862 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3863 * characters. The index is the low byte of the character.
3864 * The list contains from-to pairs with a terminating NUL.
3865 * sl_sal_first[] is used for latin1 "from" characters. */
3866 gap = &lp->sl_sal;
3867 ga_init2(gap, sizeof(int *), 1);
3868 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003869 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003870 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3871 gap->ga_len = 256;
3872
3873 /* First count the number of items for each list. Temporarily use
3874 * sl_sal_first[] for this. */
3875 for (p = from, s = to; *p != NUL && *s != NUL; )
3876 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003877 c = mb_cptr2char_adv(&p);
3878 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003879 if (c >= 256)
3880 ++lp->sl_sal_first[c & 0xff];
3881 }
3882 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003883 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003884
3885 /* Allocate the lists. */
3886 for (i = 0; i < 256; ++i)
3887 if (lp->sl_sal_first[i] > 0)
3888 {
3889 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3890 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003891 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003892 ((int **)gap->ga_data)[i] = (int *)p;
3893 *(int *)p = 0;
3894 }
3895
3896 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3897 * list. */
3898 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3899 for (p = from, s = to; *p != NUL && *s != NUL; )
3900 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003901 c = mb_cptr2char_adv(&p);
3902 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003903 if (c >= 256)
3904 {
3905 /* Append the from-to chars at the end of the list with
3906 * the low byte. */
3907 inp = ((int **)gap->ga_data)[c & 0xff];
3908 while (*inp != 0)
3909 ++inp;
3910 *inp++ = c; /* from char */
3911 *inp++ = i; /* to char */
3912 *inp++ = NUL; /* NUL at the end */
3913 }
3914 else
3915 /* mapping byte to char is done in sl_sal_first[] */
3916 lp->sl_sal_first[c] = i;
3917 }
3918 }
3919 else
3920#endif
3921 {
3922 /* mapping bytes to bytes is done in sl_sal_first[] */
3923 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003924 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003925
3926 for (i = 0; to[i] != NUL; ++i)
3927 lp->sl_sal_first[from[i]] = to[i];
3928 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3929 }
3930
Bram Moolenaar5195e452005-08-19 20:32:47 +00003931 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003932}
3933
3934/*
3935 * Fill the first-index table for "lp".
3936 */
3937 static void
3938set_sal_first(lp)
3939 slang_T *lp;
3940{
3941 salfirst_T *sfirst;
3942 int i;
3943 salitem_T *smp;
3944 int c;
3945 garray_T *gap = &lp->sl_sal;
3946
3947 sfirst = lp->sl_sal_first;
3948 for (i = 0; i < 256; ++i)
3949 sfirst[i] = -1;
3950 smp = (salitem_T *)gap->ga_data;
3951 for (i = 0; i < gap->ga_len; ++i)
3952 {
3953#ifdef FEAT_MBYTE
3954 if (has_mbyte)
3955 /* Use the lowest byte of the first character. For latin1 it's
3956 * the character, for other encodings it should differ for most
3957 * characters. */
3958 c = *smp[i].sm_lead_w & 0xff;
3959 else
3960#endif
3961 c = *smp[i].sm_lead;
3962 if (sfirst[c] == -1)
3963 {
3964 sfirst[c] = i;
3965#ifdef FEAT_MBYTE
3966 if (has_mbyte)
3967 {
3968 int n;
3969
3970 /* Make sure all entries with this byte are following each
3971 * other. Move the ones that are in the wrong position. Do
3972 * keep the same ordering! */
3973 while (i + 1 < gap->ga_len
3974 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3975 /* Skip over entry with same index byte. */
3976 ++i;
3977
3978 for (n = 1; i + n < gap->ga_len; ++n)
3979 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3980 {
3981 salitem_T tsal;
3982
3983 /* Move entry with same index byte after the entries
3984 * we already found. */
3985 ++i;
3986 --n;
3987 tsal = smp[i + n];
3988 mch_memmove(smp + i + 1, smp + i,
3989 sizeof(salitem_T) * n);
3990 smp[i] = tsal;
3991 }
3992 }
3993#endif
3994 }
3995 }
3996}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003997
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003998#ifdef FEAT_MBYTE
3999/*
4000 * Turn a multi-byte string into a wide character string.
4001 * Return it in allocated memory (NULL for out-of-memory)
4002 */
4003 static int *
4004mb_str2wide(s)
4005 char_u *s;
4006{
4007 int *res;
4008 char_u *p;
4009 int i = 0;
4010
4011 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
4012 if (res != NULL)
4013 {
4014 for (p = s; *p != NUL; )
4015 res[i++] = mb_ptr2char_adv(&p);
4016 res[i] = NUL;
4017 }
4018 return res;
4019}
4020#endif
4021
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004022/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004023 * Read a tree from the .spl or .sug file.
4024 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4025 * This is skipped when the tree has zero length.
4026 * Returns zero when OK, SP_ value for an error.
4027 */
4028 static int
4029spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4030 FILE *fd;
4031 char_u **bytsp;
4032 idx_T **idxsp;
4033 int prefixtree; /* TRUE for the prefix tree */
4034 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4035{
4036 int len;
4037 int idx;
4038 char_u *bp;
4039 idx_T *ip;
4040
4041 /* The tree size was computed when writing the file, so that we can
4042 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004043 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004044 if (len < 0)
4045 return SP_TRUNCERROR;
4046 if (len > 0)
4047 {
4048 /* Allocate the byte array. */
4049 bp = lalloc((long_u)len, TRUE);
4050 if (bp == NULL)
4051 return SP_OTHERERROR;
4052 *bytsp = bp;
4053
4054 /* Allocate the index array. */
4055 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4056 if (ip == NULL)
4057 return SP_OTHERERROR;
4058 *idxsp = ip;
4059
4060 /* Recursively read the tree and store it in the array. */
4061 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4062 if (idx < 0)
4063 return idx;
4064 }
4065 return 0;
4066}
4067
4068/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069 * Read one row of siblings from the spell file and store it in the byte array
4070 * "byts" and index array "idxs". Recursively read the children.
4071 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004072 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004074 * Returns the index (>= 0) following the siblings.
4075 * Returns SP_TRUNCERROR if the file is shorter than expected.
4076 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004078 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004079read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 FILE *fd;
4081 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004082 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004084 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004085 int prefixtree; /* TRUE for reading PREFIXTREE */
4086 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004088 int len;
4089 int i;
4090 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004091 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004093 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004094#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095
Bram Moolenaar51485f02005-06-04 21:55:20 +00004096 len = getc(fd); /* <siblingcount> */
4097 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004098 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004099
4100 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004101 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004102 byts[idx++] = len;
4103
4104 /* Read the byte values, flag/region bytes and shared indexes. */
4105 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004106 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004107 c = getc(fd); /* <byte> */
4108 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004109 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004110 if (c <= BY_SPECIAL)
4111 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004112 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004113 {
4114 /* No flags, all regions. */
4115 idxs[idx] = 0;
4116 c = 0;
4117 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004118 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004119 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004120 if (prefixtree)
4121 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004122 /* Read the optional pflags byte, the prefix ID and the
4123 * condition nr. In idxs[] store the prefix ID in the low
4124 * byte, the condition index shifted up 8 bits, the flags
4125 * shifted up 24 bits. */
4126 if (c == BY_FLAGS)
4127 c = getc(fd) << 24; /* <pflags> */
4128 else
4129 c = 0;
4130
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004131 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004132
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004133 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004134 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004135 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004136 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004137 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004138 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004139 {
4140 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004141 * idxs[] the flags go in the low two bytes, region above
4142 * that and prefix ID above the region. */
4143 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004144 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004145 if (c2 == BY_FLAGS2)
4146 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004147 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004148 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004149 if (c & WF_AFX)
4150 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004151 }
4152
Bram Moolenaar51485f02005-06-04 21:55:20 +00004153 idxs[idx] = c;
4154 c = 0;
4155 }
4156 else /* c == BY_INDEX */
4157 {
4158 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004159 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004160 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004161 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004162 idxs[idx] = n + SHARED_MASK;
4163 c = getc(fd); /* <xbyte> */
4164 }
4165 }
4166 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 }
4168
Bram Moolenaar51485f02005-06-04 21:55:20 +00004169 /* Recursively read the children for non-shared siblings.
4170 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4171 * remove SHARED_MASK) */
4172 for (i = 1; i <= len; ++i)
4173 if (byts[startidx + i] != 0)
4174 {
4175 if (idxs[startidx + i] & SHARED_MASK)
4176 idxs[startidx + i] &= ~SHARED_MASK;
4177 else
4178 {
4179 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004180 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004181 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004182 if (idx < 0)
4183 break;
4184 }
4185 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004186
Bram Moolenaar51485f02005-06-04 21:55:20 +00004187 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004188}
4189
4190/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004191 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004192 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004193 */
4194 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004195did_set_spelllang(wp)
4196 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197{
4198 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004199 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004200 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004201 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004202 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004203 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004204 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004205 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004208 int len;
4209 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004210 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004211 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004212 char_u *use_region = NULL;
4213 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004214 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004215 int i, j;
4216 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004217 static int recursive = FALSE;
4218 char_u *ret_msg = NULL;
4219 char_u *spl_copy;
4220
4221 /* We don't want to do this recursively. May happen when a language is
4222 * not available and the SpellFileMissing autocommand opens a new buffer
4223 * in which 'spell' is set. */
4224 if (recursive)
4225 return NULL;
4226 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227
4228 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004229 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004230
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004231 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004232 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004233 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004234 if (spl_copy == NULL)
4235 goto theend;
4236
Bram Moolenaar2593e032013-11-14 03:54:07 +01004237#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004238 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004239#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004240
4241 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004242 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004244 /* Get one language name. */
4245 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004246 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004248
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004249 if (STRCMP(lang, "cjk") == 0)
4250 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01004251#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004252 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01004253#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004254 continue;
4255 }
4256
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004257 /* If the name ends in ".spl" use it as the name of the spell file.
4258 * If there is a region name let "region" point to it and remove it
4259 * from the name. */
4260 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4261 {
4262 filename = TRUE;
4263
Bram Moolenaarb6356332005-07-18 21:40:44 +00004264 /* Locate a region and remove it from the file name. */
4265 p = vim_strchr(gettail(lang), '_');
4266 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4267 && !ASCII_ISALPHA(p[3]))
4268 {
4269 vim_strncpy(region_cp, p + 1, 2);
4270 mch_memmove(p, p + 3, len - (p - lang) - 2);
4271 len -= 3;
4272 region = region_cp;
4273 }
4274 else
4275 dont_use_region = TRUE;
4276
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004277 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004278 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4279 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004280 break;
4281 }
4282 else
4283 {
4284 filename = FALSE;
4285 if (len > 3 && lang[len - 3] == '_')
4286 {
4287 region = lang + len - 2;
4288 len -= 3;
4289 lang[len] = NUL;
4290 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004291 else
4292 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004293
4294 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4296 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004297 break;
4298 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004299
Bram Moolenaarb6356332005-07-18 21:40:44 +00004300 if (region != NULL)
4301 {
4302 /* If the region differs from what was used before then don't
4303 * use it for 'spellfile'. */
4304 if (use_region != NULL && STRCMP(region, use_region) != 0)
4305 dont_use_region = TRUE;
4306 use_region = region;
4307 }
4308
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004309 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004311 {
4312 if (filename)
4313 (void)spell_load_file(lang, lang, NULL, FALSE);
4314 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004315 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004316 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004317#ifdef FEAT_AUTOCMD
4318 /* SpellFileMissing autocommands may do anything, including
4319 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004320 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004321 {
4322 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4323 goto theend;
4324 }
4325#endif
4326 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004327 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004328
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004329 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004330 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004331 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4333 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4334 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004335 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004336 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004337 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004338 {
4339 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004340 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004341 if (c == REGION_ALL)
4342 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004343 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004344 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004346 /* This addition file is for other regions. */
4347 region_mask = 0;
4348 }
4349 else
4350 /* This is probably an error. Give a warning and
4351 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004352 smsg((char_u *)
4353 _("Warning: region %s not supported"),
4354 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004355 }
4356 else
4357 region_mask = 1 << c;
4358 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004359
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004360 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004361 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004362 if (ga_grow(&ga, 1) == FAIL)
4363 {
4364 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004365 ret_msg = e_outofmem;
4366 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004367 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004368 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004369 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4370 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004371 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004373 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004374 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004375 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004376 }
4377
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004378 /* round 0: load int_wordlist, if possible.
4379 * round 1: load first name in 'spellfile'.
4380 * round 2: load second name in 'spellfile.
4381 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004382 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004383 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004384 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004385 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004386 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004387 /* Internal wordlist, if there is one. */
4388 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004389 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004390 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004391 }
4392 else
4393 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004394 /* One entry in 'spellfile'. */
4395 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4396 STRCAT(spf_name, ".spl");
4397
4398 /* If it was already found above then skip it. */
4399 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004400 {
4401 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4402 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004403 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004404 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004405 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004406 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004407 }
4408
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004409 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4411 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004413 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004415 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004416 * region name, the region is ignored otherwise. for int_wordlist
4417 * use an arbitrary name. */
4418 if (round == 0)
4419 STRCPY(lang, "internal wordlist");
4420 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004421 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004422 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004423 p = vim_strchr(lang, '.');
4424 if (p != NULL)
4425 *p = NUL; /* truncate at ".encoding.add" */
4426 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004427 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004428
4429 /* If one of the languages has NOBREAK we assume the addition
4430 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 if (slang != NULL && nobreak)
4432 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004434 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004436 region_mask = REGION_ALL;
4437 if (use_region != NULL && !dont_use_region)
4438 {
4439 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004440 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004441 if (c != REGION_ALL)
4442 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004443 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004444 /* This spell file is for other regions. */
4445 region_mask = 0;
4446 }
4447
4448 if (region_mask != 0)
4449 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004450 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4451 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4452 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004453 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4454 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004455 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 }
4458 }
4459
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004460 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004461 ga_clear(&wp->w_s->b_langp);
4462 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004463
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004464 /* For each language figure out what language to use for sound folding and
4465 * REP items. If the language doesn't support it itself use another one
4466 * with the same name. E.g. for "en-math" use "en". */
4467 for (i = 0; i < ga.ga_len; ++i)
4468 {
4469 lp = LANGP_ENTRY(ga, i);
4470
4471 /* sound folding */
4472 if (lp->lp_slang->sl_sal.ga_len > 0)
4473 /* language does sound folding itself */
4474 lp->lp_sallang = lp->lp_slang;
4475 else
4476 /* find first similar language that does sound folding */
4477 for (j = 0; j < ga.ga_len; ++j)
4478 {
4479 lp2 = LANGP_ENTRY(ga, j);
4480 if (lp2->lp_slang->sl_sal.ga_len > 0
4481 && STRNCMP(lp->lp_slang->sl_name,
4482 lp2->lp_slang->sl_name, 2) == 0)
4483 {
4484 lp->lp_sallang = lp2->lp_slang;
4485 break;
4486 }
4487 }
4488
4489 /* REP items */
4490 if (lp->lp_slang->sl_rep.ga_len > 0)
4491 /* language has REP items itself */
4492 lp->lp_replang = lp->lp_slang;
4493 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004494 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004495 for (j = 0; j < ga.ga_len; ++j)
4496 {
4497 lp2 = LANGP_ENTRY(ga, j);
4498 if (lp2->lp_slang->sl_rep.ga_len > 0
4499 && STRNCMP(lp->lp_slang->sl_name,
4500 lp2->lp_slang->sl_name, 2) == 0)
4501 {
4502 lp->lp_replang = lp2->lp_slang;
4503 break;
4504 }
4505 }
4506 }
4507
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004508theend:
4509 vim_free(spl_copy);
4510 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02004511 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004512 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004513}
4514
4515/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004516 * Clear the midword characters for buffer "buf".
4517 */
4518 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519clear_midword(wp)
4520 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004521{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004522 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004523#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004524 vim_free(wp->w_s->b_spell_ismw_mb);
4525 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004526#endif
4527}
4528
4529/*
4530 * Use the "sl_midword" field of language "lp" for buffer "buf".
4531 * They add up to any currently used midword characters.
4532 */
4533 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004534use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004535 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004536 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004537{
4538 char_u *p;
4539
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004540 if (lp->sl_midword == NULL) /* there aren't any */
4541 return;
4542
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004543 for (p = lp->sl_midword; *p != NUL; )
4544#ifdef FEAT_MBYTE
4545 if (has_mbyte)
4546 {
4547 int c, l, n;
4548 char_u *bp;
4549
4550 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004551 l = (*mb_ptr2len)(p);
4552 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004553 wp->w_s->b_spell_ismw[c] = TRUE;
4554 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004555 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004556 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004557 else
4558 {
4559 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004560 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4561 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004562 if (bp != NULL)
4563 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004564 vim_free(wp->w_s->b_spell_ismw_mb);
4565 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004566 vim_strncpy(bp + n, p, l);
4567 }
4568 }
4569 p += l;
4570 }
4571 else
4572#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004573 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004574}
4575
4576/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 * Find the region "region[2]" in "rp" (points to "sl_regions").
4578 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004579 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004580 */
4581 static int
4582find_region(rp, region)
4583 char_u *rp;
4584 char_u *region;
4585{
4586 int i;
4587
4588 for (i = 0; ; i += 2)
4589 {
4590 if (rp[i] == NUL)
4591 return REGION_ALL;
4592 if (rp[i] == region[0] && rp[i + 1] == region[1])
4593 break;
4594 }
4595 return i / 2;
4596}
4597
4598/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004599 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004601 * Word WF_ONECAP
4602 * W WORD WF_ALLCAP
4603 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004604 */
4605 static int
4606captype(word, end)
4607 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004609{
4610 char_u *p;
4611 int c;
4612 int firstcap;
4613 int allcap;
4614 int past_second = FALSE; /* past second word char */
4615
4616 /* find first letter */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004617 for (p = word; !spell_iswordp_nmw(p, curwin); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004618 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004619 return 0; /* only non-word characters, illegal word */
4620#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004621 if (has_mbyte)
4622 c = mb_ptr2char_adv(&p);
4623 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004625 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004626 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004627
4628 /*
4629 * Need to check all letters to find a word with mixed upper/lower.
4630 * But a word with an upper char only at start is a ONECAP.
4631 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004633 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004635 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004636 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 {
4638 /* UUl -> KEEPCAP */
4639 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004640 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641 allcap = FALSE;
4642 }
4643 else if (!allcap)
4644 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004645 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 past_second = TRUE;
4647 }
4648
4649 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004650 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004651 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004652 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004653 return 0;
4654}
4655
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004656/*
4657 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4658 * capital. So that make_case_word() can turn WOrd into Word.
4659 * Add ALLCAP for "WOrD".
4660 */
4661 static int
4662badword_captype(word, end)
4663 char_u *word;
4664 char_u *end;
4665{
4666 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004667 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004668 int l, u;
4669 int first;
4670 char_u *p;
4671
4672 if (flags & WF_KEEPCAP)
4673 {
4674 /* Count the number of UPPER and lower case letters. */
4675 l = u = 0;
4676 first = FALSE;
4677 for (p = word; p < end; mb_ptr_adv(p))
4678 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004679 c = PTR2CHAR(p);
4680 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004681 {
4682 ++u;
4683 if (p == word)
4684 first = TRUE;
4685 }
4686 else
4687 ++l;
4688 }
4689
4690 /* If there are more UPPER than lower case letters suggest an
4691 * ALLCAP word. Otherwise, if the first letter is UPPER then
4692 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4693 * require three upper case letters. */
4694 if (u > l && u > 2)
4695 flags |= WF_ALLCAP;
4696 else if (first)
4697 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004698
4699 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4700 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004701 }
4702 return flags;
4703}
4704
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004705/*
4706 * Delete the internal wordlist and its .spl file.
4707 */
4708 void
4709spell_delete_wordlist()
4710{
4711 char_u fname[MAXPATHL];
4712
4713 if (int_wordlist != NULL)
4714 {
4715 mch_remove(int_wordlist);
4716 int_wordlist_spl(fname);
4717 mch_remove(fname);
4718 vim_free(int_wordlist);
4719 int_wordlist = NULL;
4720 }
4721}
4722
4723#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004724/*
4725 * Free all languages.
4726 */
4727 void
4728spell_free_all()
4729{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004730 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004731 buf_T *buf;
4732
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004733 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004734 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004736
4737 while (first_lang != NULL)
4738 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004739 slang = first_lang;
4740 first_lang = slang->sl_next;
4741 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004742 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004743
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004744 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00004745
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004746 vim_free(repl_to);
4747 repl_to = NULL;
4748 vim_free(repl_from);
4749 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004750}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004751#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004752
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004753#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004754/*
4755 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004756 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757 */
4758 void
4759spell_reload()
4760{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004761 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004762
Bram Moolenaarea408852005-06-25 22:49:46 +00004763 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764 init_spell_chartab();
4765
4766 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004767 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768
4769 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004770 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004771 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004772 /* Only load the wordlists when 'spelllang' is set and there is a
4773 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004774 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004775 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004776 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004777 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004778 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004779# ifdef FEAT_WINDOWS
4780 break;
4781# endif
4782 }
4783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004784 }
4785}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01004786#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004787
Bram Moolenaarb765d632005-06-07 21:00:02 +00004788/*
4789 * Reload the spell file "fname" if it's loaded.
4790 */
4791 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004793 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004795{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004796 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004798
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004799 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004800 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004801 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004802 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004803 slang_clear(slang);
4804 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004805 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004806 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004807 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004809 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811
4812 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004813 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004815 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004816}
4817
4818
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004819/*
4820 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 */
4822
Bram Moolenaar51485f02005-06-04 21:55:20 +00004823#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004824 and .dic file. */
4825/*
4826 * Main structure to store the contents of a ".aff" file.
4827 */
4828typedef struct afffile_S
4829{
4830 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004831 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004832 unsigned af_rare; /* RARE ID for rare word */
4833 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004834 unsigned af_bad; /* BAD ID for banned word */
4835 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004836 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004837 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004838 unsigned af_comproot; /* COMPOUNDROOT ID */
4839 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4840 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004841 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004842 int af_pfxpostpone; /* postpone prefixes without chop string and
4843 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004844 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4845 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004846 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004847} afffile_T;
4848
Bram Moolenaar6de68532005-08-24 22:08:48 +00004849#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004850#define AFT_LONG 1 /* flags are two characters */
4851#define AFT_CAPLONG 2 /* flags are one or two characters */
4852#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004853
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004854typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004855/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4856struct affentry_S
4857{
4858 affentry_T *ae_next; /* next affix with same name/number */
4859 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4860 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004861 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004862 char_u *ae_cond; /* condition (NULL for ".") */
4863 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004864 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4865 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866};
4867
Bram Moolenaar6de68532005-08-24 22:08:48 +00004868#ifdef FEAT_MBYTE
4869# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4870#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004871# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004872#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004873
Bram Moolenaar51485f02005-06-04 21:55:20 +00004874/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4875typedef struct affheader_S
4876{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004877 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4878 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4879 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004880 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004881 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004882 affentry_T *ah_first; /* first affix entry */
4883} affheader_T;
4884
4885#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4886
Bram Moolenaar6de68532005-08-24 22:08:48 +00004887/* Flag used in compound items. */
4888typedef struct compitem_S
4889{
4890 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4891 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4892 int ci_newID; /* affix ID after renumbering. */
4893} compitem_T;
4894
4895#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4896
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897/*
4898 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004899 * the need to keep track of each allocated thing, everything is freed all at
4900 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004901 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4902 * "sb_data" is correct for systems where pointers must be aligned on
4903 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004904 */
4905#define SBLOCKSIZE 16000 /* size of sb_data */
4906typedef struct sblock_S sblock_T;
4907struct sblock_S
4908{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004909 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004910 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004911 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004912};
4913
4914/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004915 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004916 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004917typedef struct wordnode_S wordnode_T;
4918struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004919{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004920 union /* shared to save space */
4921 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004922 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004923 int index; /* index in written nodes (valid after first
4924 round) */
4925 } wn_u1;
4926 union /* shared to save space */
4927 {
4928 wordnode_T *next; /* next node with same hash key */
4929 wordnode_T *wnode; /* parent node that will write this node */
4930 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004931 wordnode_T *wn_child; /* child (next byte in word) */
4932 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4933 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004934 int wn_refs; /* Nr. of references to this node. Only
4935 relevant for first node in a list of
4936 siblings, in following siblings it is
4937 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004938 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004939
4940 /* Info for when "wn_byte" is NUL.
4941 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4942 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4943 * "wn_region" the LSW of the wordnr. */
4944 char_u wn_affixID; /* supported/required prefix ID or 0 */
4945 short_u wn_flags; /* WF_ flags */
4946 short wn_region; /* region mask */
4947
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004948#ifdef SPELL_PRINTTREE
4949 int wn_nr; /* sequence nr for printing */
4950#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004951};
4952
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004953#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4954
Bram Moolenaar51485f02005-06-04 21:55:20 +00004955#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004956
Bram Moolenaar51485f02005-06-04 21:55:20 +00004957/*
4958 * Info used while reading the spell files.
4959 */
4960typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004961{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004962 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004963 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004964
Bram Moolenaar51485f02005-06-04 21:55:20 +00004965 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004966 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004967
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004968 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004969
Bram Moolenaar4770d092006-01-12 23:22:24 +00004970 long si_sugtree; /* creating the soundfolding trie */
4971
Bram Moolenaar51485f02005-06-04 21:55:20 +00004972 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004973 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004974 int si_did_emsg; /* TRUE when ran out of memory */
4975
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004976 long si_compress_cnt; /* words to add before lowering
4977 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004978 wordnode_T *si_first_free; /* List of nodes that have been freed during
4979 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004980 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004981#ifdef SPELL_PRINTTREE
4982 int si_wordnode_nr; /* sequence nr for nodes */
4983#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004984 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004985
Bram Moolenaar51485f02005-06-04 21:55:20 +00004986 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004987 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004988 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004989 int si_region; /* region mask */
4990 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004991 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004992 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004993 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004994 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004995 int si_region_count; /* number of regions supported (1 when there
4996 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004997 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004998 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004999
5000 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005002 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005003 char_u *si_sofofr; /* SOFOFROM text */
5004 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005005 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005006 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005007 int si_followup; /* soundsalike: ? */
5008 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005009 hashtab_T si_commonwords; /* hashtable for common words */
5010 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005011 int si_rem_accents; /* soundsalike: remove accents */
5012 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005013 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005014 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005015 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005016 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005017 int si_compoptions; /* COMP_ flags */
5018 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
5019 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005020 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00005021 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005022 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005023 garray_T si_prefcond; /* table with conditions for postponed
5024 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005025 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005026 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005027} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005028
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005029static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005030static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005031static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005032static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005033static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5034static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5035static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005036static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005037static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5038static void aff_check_number __ARGS((int spinval, int affval, char *name));
5039static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005040static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005041static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5042static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005043static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005044static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005045static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005046static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005047static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005048static 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 +00005049static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5050static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5051static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005052static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005053static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005054static 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 +00005055static 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 +00005056static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005057static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005058static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5059static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5060static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005061static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005062static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005063static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005064static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005065static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5066static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5067static int sug_maketable __ARGS((spellinfo_T *spin));
5068static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5069static int offset2bytes __ARGS((int nr, char_u *buf));
5070static int bytes2offset __ARGS((char_u **pp));
5071static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar70b2a562012-01-10 22:26:17 +01005072static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int over_write, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005073static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005074static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005075
Bram Moolenaar53805d12005-08-01 07:08:33 +00005076/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5077 * but it must be negative to indicate the prefix tree to tree_add_word().
5078 * Use a negative number with the lower 8 bits zero. */
5079#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005080
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005081/* flags for "condit" argument of store_aff_word() */
5082#define CONDIT_COMB 1 /* affix must combine */
5083#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5084#define CONDIT_SUF 4 /* add a suffix for matching flags */
5085#define CONDIT_AFF 8 /* word already has an affix */
5086
Bram Moolenaar5195e452005-08-19 20:32:47 +00005087/*
5088 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5089 */
5090static long compress_start = 30000; /* memory / SBLOCKSIZE */
5091static long compress_inc = 100; /* memory / SBLOCKSIZE */
5092static long compress_added = 500000; /* word count */
5093
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005094#ifdef SPELL_PRINTTREE
5095/*
5096 * For debugging the tree code: print the current tree in a (more or less)
5097 * readable format, so that we can see what happens when adding a word and/or
5098 * compressing the tree.
5099 * Based on code from Olaf Seibert.
5100 */
5101#define PRINTLINESIZE 1000
5102#define PRINTWIDTH 6
5103
5104#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5105 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5106
5107static char line1[PRINTLINESIZE];
5108static char line2[PRINTLINESIZE];
5109static char line3[PRINTLINESIZE];
5110
5111 static void
5112spell_clear_flags(wordnode_T *node)
5113{
5114 wordnode_T *np;
5115
5116 for (np = node; np != NULL; np = np->wn_sibling)
5117 {
5118 np->wn_u1.index = FALSE;
5119 spell_clear_flags(np->wn_child);
5120 }
5121}
5122
5123 static void
5124spell_print_node(wordnode_T *node, int depth)
5125{
5126 if (node->wn_u1.index)
5127 {
5128 /* Done this node before, print the reference. */
5129 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5130 PRINTSOME(line2, depth, " ", 0, 0);
5131 PRINTSOME(line3, depth, " ", 0, 0);
5132 msg(line1);
5133 msg(line2);
5134 msg(line3);
5135 }
5136 else
5137 {
5138 node->wn_u1.index = TRUE;
5139
5140 if (node->wn_byte != NUL)
5141 {
5142 if (node->wn_child != NULL)
5143 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5144 else
5145 /* Cannot happen? */
5146 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5147 }
5148 else
5149 PRINTSOME(line1, depth, " $ ", 0, 0);
5150
5151 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5152
5153 if (node->wn_sibling != NULL)
5154 PRINTSOME(line3, depth, " | ", 0, 0);
5155 else
5156 PRINTSOME(line3, depth, " ", 0, 0);
5157
5158 if (node->wn_byte == NUL)
5159 {
5160 msg(line1);
5161 msg(line2);
5162 msg(line3);
5163 }
5164
5165 /* do the children */
5166 if (node->wn_byte != NUL && node->wn_child != NULL)
5167 spell_print_node(node->wn_child, depth + 1);
5168
5169 /* do the siblings */
5170 if (node->wn_sibling != NULL)
5171 {
5172 /* get rid of all parent details except | */
5173 STRCPY(line1, line3);
5174 STRCPY(line2, line3);
5175 spell_print_node(node->wn_sibling, depth);
5176 }
5177 }
5178}
5179
5180 static void
5181spell_print_tree(wordnode_T *root)
5182{
5183 if (root != NULL)
5184 {
5185 /* Clear the "wn_u1.index" fields, used to remember what has been
5186 * done. */
5187 spell_clear_flags(root);
5188
5189 /* Recursively print the tree. */
5190 spell_print_node(root, 0);
5191 }
5192}
5193#endif /* SPELL_PRINTTREE */
5194
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005195/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005197 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005198 */
5199 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005200spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005201 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005202 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005203{
5204 FILE *fd;
5205 afffile_T *aff;
5206 char_u rline[MAXLINELEN];
5207 char_u *line;
5208 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005209#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005210 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005211 int itemcnt;
5212 char_u *p;
5213 int lnum = 0;
5214 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005215 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005216 int aff_todo = 0;
5217 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005218 char_u *low = NULL;
5219 char_u *fol = NULL;
5220 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005222 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005224 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005226 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005227 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005228 int compminlen = 0; /* COMPOUNDMIN value */
5229 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005230 int compoptions = 0; /* COMP_ flags */
5231 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005232 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005233 concatenated */
5234 char_u *midword = NULL; /* MIDWORD value */
5235 char_u *syllable = NULL; /* SYLLABLE value */
5236 char_u *sofofrom = NULL; /* SOFOFROM value */
5237 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005238
Bram Moolenaar51485f02005-06-04 21:55:20 +00005239 /*
5240 * Open the file.
5241 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005242 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005243 if (fd == NULL)
5244 {
5245 EMSG2(_(e_notopen), fname);
5246 return NULL;
5247 }
5248
Bram Moolenaar4770d092006-01-12 23:22:24 +00005249 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5250 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 /* Only do REP lines when not done in another .aff file already. */
5253 do_rep = spin->si_rep.ga_len == 0;
5254
Bram Moolenaar4770d092006-01-12 23:22:24 +00005255 /* Only do REPSAL lines when not done in another .aff file already. */
5256 do_repsal = spin->si_repsal.ga_len == 0;
5257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005258 /* Only do SAL lines when not done in another .aff file already. */
5259 do_sal = spin->si_sal.ga_len == 0;
5260
5261 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005262 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263
Bram Moolenaar51485f02005-06-04 21:55:20 +00005264 /*
5265 * Allocate and init the afffile_T structure.
5266 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005267 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005268 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005269 {
5270 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005271 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005272 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005273 hash_init(&aff->af_pref);
5274 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005275 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005276
5277 /*
5278 * Read all the lines in the file one by one.
5279 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005280 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005281 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005282 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005283 ++lnum;
5284
5285 /* Skip comment lines. */
5286 if (*rline == '#')
5287 continue;
5288
5289 /* Convert from "SET" to 'encoding' when needed. */
5290 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005291#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005292 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005293 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005294 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005295 if (pc == NULL)
5296 {
5297 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5298 fname, lnum, rline);
5299 continue;
5300 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005301 line = pc;
5302 }
5303 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005304#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005305 {
5306 pc = NULL;
5307 line = rline;
5308 }
5309
5310 /* Split the line up in white separated items. Put a NUL after each
5311 * item. */
5312 itemcnt = 0;
5313 for (p = line; ; )
5314 {
5315 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5316 ++p;
5317 if (*p == NUL)
5318 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005319 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005320 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005322 /* A few items have arbitrary text argument, don't split them. */
5323 if (itemcnt == 2 && spell_info_item(items[0]))
5324 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5325 ++p;
5326 else
5327 while (*p > ' ') /* skip until white space or CR/NL */
5328 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005329 if (*p == NUL)
5330 break;
5331 *p++ = NUL;
5332 }
5333
5334 /* Handle non-empty lines. */
5335 if (itemcnt > 0)
5336 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005337 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005338 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005339#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005340 /* Setup for conversion from "ENC" to 'encoding'. */
5341 aff->af_enc = enc_canonize(items[1]);
5342 if (aff->af_enc != NULL && !spin->si_ascii
5343 && convert_setup(&spin->si_conv, aff->af_enc,
5344 p_enc) == FAIL)
5345 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5346 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005347 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005348#else
5349 smsg((char_u *)_("Conversion in %s not supported"), fname);
5350#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005351 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005352 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005353 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005354 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005355 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005356 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005357 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005358 aff->af_flagtype = AFT_NUM;
5359 else if (STRCMP(items[1], "caplong") == 0)
5360 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005361 else
5362 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5363 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005364 if (aff->af_rare != 0
5365 || aff->af_keepcase != 0
5366 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005367 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005368 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005369 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005370 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005371 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005372 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005373 || aff->af_suff.ht_used > 0
5374 || aff->af_pref.ht_used > 0)
5375 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5376 fname, lnum, items[1]);
5377 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005378 else if (spell_info_item(items[0]))
5379 {
5380 p = (char_u *)getroom(spin,
5381 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5382 + STRLEN(items[0])
5383 + STRLEN(items[1]) + 3, FALSE);
5384 if (p != NULL)
5385 {
5386 if (spin->si_info != NULL)
5387 {
5388 STRCPY(p, spin->si_info);
5389 STRCAT(p, "\n");
5390 }
5391 STRCAT(p, items[0]);
5392 STRCAT(p, " ");
5393 STRCAT(p, items[1]);
5394 spin->si_info = p;
5395 }
5396 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005397 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005398 && midword == NULL)
5399 {
5400 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005401 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005402 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005404 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005405 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005406 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005407 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5408 || is_aff_rule(items, itemcnt, "RARE", 2))
5409 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005410 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005411 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005412 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005413 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005414 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005415 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5416 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005417 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005418 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005419 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005420 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005421 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005422 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5423 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5424 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005425 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005426 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5427 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005428 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005429 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005430 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005431 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005432 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5433 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005434 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005435 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005436 && aff->af_circumfix == 0)
5437 {
5438 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5439 fname, lnum);
5440 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005441 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005442 && aff->af_nosuggest == 0)
5443 {
5444 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5445 fname, lnum);
5446 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005447 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5448 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005449 && aff->af_needcomp == 0)
5450 {
5451 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5452 fname, lnum);
5453 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005454 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005455 && aff->af_comproot == 0)
5456 {
5457 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5458 fname, lnum);
5459 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005460 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5461 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005462 {
5463 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5464 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005465 if (aff->af_pref.ht_used > 0)
5466 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5467 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005468 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005469 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5470 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005471 {
5472 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5473 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005474 if (aff->af_pref.ht_used > 0)
5475 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5476 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005477 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005478 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005479 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005480 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005481 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005482 * "Na" into "Na+", "1234" into "1234+". */
5483 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005484 if (p != NULL)
5485 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005486 STRCPY(p, items[1]);
5487 STRCAT(p, "+");
5488 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005489 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005490 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005491 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5492 {
5493 /* We don't use the count, but do check that it's a number and
5494 * not COMPOUNDRULE mistyped. */
5495 if (atoi((char *)items[1]) == 0)
5496 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5497 fname, lnum, items[1]);
5498 }
5499 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005500 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005501 /* Don't use the first rule if it is a number. */
5502 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005503 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005504 /* Concatenate this string to previously defined ones,
5505 * using a slash to separate them. */
5506 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005507 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005508 l += (int)STRLEN(compflags) + 1;
5509 p = getroom(spin, l, FALSE);
5510 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005511 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005512 if (compflags != NULL)
5513 {
5514 STRCPY(p, compflags);
5515 STRCAT(p, "/");
5516 }
5517 STRCAT(p, items[1]);
5518 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005519 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005520 }
5521 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005522 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005523 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005524 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005525 compmax = atoi((char *)items[1]);
5526 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005527 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005528 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005529 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005530 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005531 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005532 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005533 compminlen = atoi((char *)items[1]);
5534 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005535 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5536 fname, lnum, items[1]);
5537 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005538 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005539 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005540 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005541 compsylmax = atoi((char *)items[1]);
5542 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005543 smsg((char_u *)_("Wrong COMPOUNDSYLMAX 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, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005547 {
5548 compoptions |= COMP_CHECKDUP;
5549 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005550 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005551 {
5552 compoptions |= COMP_CHECKREP;
5553 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005554 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005555 {
5556 compoptions |= COMP_CHECKCASE;
5557 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005558 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005559 {
5560 compoptions |= COMP_CHECKTRIPLE;
5561 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005562 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005563 {
5564 if (atoi((char *)items[1]) == 0)
5565 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5566 fname, lnum, items[1]);
5567 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005568 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005569 {
5570 garray_T *gap = &spin->si_comppat;
5571 int i;
5572
5573 /* Only add the couple if it isn't already there. */
5574 for (i = 0; i < gap->ga_len - 1; i += 2)
5575 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5576 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5577 items[2]) == 0)
5578 break;
5579 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5580 {
5581 ((char_u **)(gap->ga_data))[gap->ga_len++]
5582 = getroom_save(spin, items[1]);
5583 ((char_u **)(gap->ga_data))[gap->ga_len++]
5584 = getroom_save(spin, items[2]);
5585 }
5586 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005587 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005588 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005589 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005590 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005591 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005592 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005593 {
5594 spin->si_nobreak = TRUE;
5595 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005596 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005597 {
5598 spin->si_nosplitsugs = TRUE;
5599 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005600 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005601 {
5602 spin->si_nosugfile = TRUE;
5603 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005604 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005605 {
5606 aff->af_pfxpostpone = TRUE;
5607 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005608 else if ((STRCMP(items[0], "PFX") == 0
5609 || STRCMP(items[0], "SFX") == 0)
5610 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005611 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005612 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005613 int lasti = 4;
5614 char_u key[AH_KEY_LEN];
5615
5616 if (*items[0] == 'P')
5617 tp = &aff->af_pref;
5618 else
5619 tp = &aff->af_suff;
5620
5621 /* Myspell allows the same affix name to be used multiple
5622 * times. The affix files that do this have an undocumented
5623 * "S" flag on all but the last block, thus we check for that
5624 * and store it in ah_follows. */
5625 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5626 hi = hash_find(tp, key);
5627 if (!HASHITEM_EMPTY(hi))
5628 {
5629 cur_aff = HI2AH(hi);
5630 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5631 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5632 fname, lnum, items[1]);
5633 if (!cur_aff->ah_follows)
5634 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5635 fname, lnum, items[1]);
5636 }
5637 else
5638 {
5639 /* New affix letter. */
5640 cur_aff = (affheader_T *)getroom(spin,
5641 sizeof(affheader_T), TRUE);
5642 if (cur_aff == NULL)
5643 break;
5644 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5645 fname, lnum);
5646 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5647 break;
5648 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005649 || cur_aff->ah_flag == aff->af_rare
5650 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005651 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005652 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005653 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005654 || cur_aff->ah_flag == aff->af_needcomp
5655 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005656 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 +00005657 fname, lnum, items[1]);
5658 STRCPY(cur_aff->ah_key, items[1]);
5659 hash_add(tp, cur_aff->ah_key);
5660
5661 cur_aff->ah_combine = (*items[2] == 'Y');
5662 }
5663
5664 /* Check for the "S" flag, which apparently means that another
5665 * block with the same affix name is following. */
5666 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5667 {
5668 ++lasti;
5669 cur_aff->ah_follows = TRUE;
5670 }
5671 else
5672 cur_aff->ah_follows = FALSE;
5673
Bram Moolenaar8db73182005-06-17 21:51:16 +00005674 /* Myspell allows extra text after the item, but that might
5675 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005676 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005677 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005678
Bram Moolenaar95529562005-08-25 21:21:38 +00005679 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005680 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5681 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005682
Bram Moolenaar95529562005-08-25 21:21:38 +00005683 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005684 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005685 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005686 {
5687 /* Use a new number in the .spl file later, to be able
5688 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005689 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005690 cur_aff->ah_newID = ++spin->si_newprefID;
5691
5692 /* We only really use ah_newID if the prefix is
5693 * postponed. We know that only after handling all
5694 * the items. */
5695 did_postpone_prefix = FALSE;
5696 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005697 else
5698 /* Did use the ID in a previous block. */
5699 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005700 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005701
Bram Moolenaar51485f02005-06-04 21:55:20 +00005702 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703 }
5704 else if ((STRCMP(items[0], "PFX") == 0
5705 || STRCMP(items[0], "SFX") == 0)
5706 && aff_todo > 0
5707 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005708 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005709 {
5710 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005711 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005712 int lasti = 5;
5713
Bram Moolenaar8db73182005-06-17 21:51:16 +00005714 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005715 * mean mistakes go unnoticed. Require a comment-starter.
5716 * Hunspell uses a "-" item. */
5717 if (itemcnt > lasti && *items[lasti] != '#'
5718 && (STRCMP(items[lasti], "-") != 0
5719 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005720 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005721
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005722 /* New item for an affix letter. */
5723 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005724 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005725 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005726 if (aff_entry == NULL)
5727 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005728
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005729 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005730 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005731 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005732 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005733 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005734
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005735 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005736 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5737 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005738 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005739 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005740 aff_process_flags(aff, aff_entry);
5741 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005742 }
5743
Bram Moolenaar51485f02005-06-04 21:55:20 +00005744 /* Don't use an affix entry with non-ASCII characters when
5745 * "spin->si_ascii" is TRUE. */
5746 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005747 || has_non_ascii(aff_entry->ae_add)))
5748 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005749 aff_entry->ae_next = cur_aff->ah_first;
5750 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005751
5752 if (STRCMP(items[4], ".") != 0)
5753 {
5754 char_u buf[MAXLINELEN];
5755
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005756 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005757 if (*items[0] == 'P')
5758 sprintf((char *)buf, "^%s", items[4]);
5759 else
5760 sprintf((char *)buf, "%s$", items[4]);
5761 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005762 RE_MAGIC + RE_STRING + RE_STRICT);
5763 if (aff_entry->ae_prog == NULL)
5764 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5765 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005766 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005767
5768 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005769 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005770 * Can't be done for an affix with flags, ignoring
5771 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005772 if (*items[0] == 'P' && aff->af_pfxpostpone
5773 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005774 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005775 /* When the chop string is one lower-case letter and
5776 * the add string ends in the upper-case letter we set
5777 * the "upper" flag, clear "ae_chop" and remove the
5778 * letters from "ae_add". The condition must either
5779 * be empty or start with the same letter. */
5780 if (aff_entry->ae_chop != NULL
5781 && aff_entry->ae_add != NULL
5782#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005783 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005784 aff_entry->ae_chop)] == NUL
5785#else
5786 && aff_entry->ae_chop[1] == NUL
5787#endif
5788 )
5789 {
5790 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005791
Bram Moolenaar53805d12005-08-01 07:08:33 +00005792 c = PTR2CHAR(aff_entry->ae_chop);
5793 c_up = SPELL_TOUPPER(c);
5794 if (c_up != c
5795 && (aff_entry->ae_cond == NULL
5796 || PTR2CHAR(aff_entry->ae_cond) == c))
5797 {
5798 p = aff_entry->ae_add
5799 + STRLEN(aff_entry->ae_add);
5800 mb_ptr_back(aff_entry->ae_add, p);
5801 if (PTR2CHAR(p) == c_up)
5802 {
5803 upper = TRUE;
5804 aff_entry->ae_chop = NULL;
5805 *p = NUL;
5806
5807 /* The condition is matched with the
5808 * actual word, thus must check for the
5809 * upper-case letter. */
5810 if (aff_entry->ae_cond != NULL)
5811 {
5812 char_u buf[MAXLINELEN];
5813#ifdef FEAT_MBYTE
5814 if (has_mbyte)
5815 {
5816 onecap_copy(items[4], buf, TRUE);
5817 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005818 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005819 }
5820 else
5821#endif
5822 *aff_entry->ae_cond = c_up;
5823 if (aff_entry->ae_cond != NULL)
5824 {
5825 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005826 aff_entry->ae_cond);
Bram Moolenaar473de612013-06-08 18:19:48 +02005827 vim_regfree(aff_entry->ae_prog);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005828 aff_entry->ae_prog = vim_regcomp(
5829 buf, RE_MAGIC + RE_STRING);
5830 }
5831 }
5832 }
5833 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005834 }
5835
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005836 if (aff_entry->ae_chop == NULL
5837 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005838 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005839 int idx;
5840 char_u **pp;
5841 int n;
5842
Bram Moolenaar6de68532005-08-24 22:08:48 +00005843 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005844 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5845 --idx)
5846 {
5847 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5848 if (str_equal(p, aff_entry->ae_cond))
5849 break;
5850 }
5851 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5852 {
5853 /* Not found, add a new condition. */
5854 idx = spin->si_prefcond.ga_len++;
5855 pp = ((char_u **)spin->si_prefcond.ga_data)
5856 + idx;
5857 if (aff_entry->ae_cond == NULL)
5858 *pp = NULL;
5859 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005860 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005861 aff_entry->ae_cond);
5862 }
5863
5864 /* Add the prefix to the prefix tree. */
5865 if (aff_entry->ae_add == NULL)
5866 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005867 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005868 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005869
Bram Moolenaar53805d12005-08-01 07:08:33 +00005870 /* PFX_FLAGS is a negative number, so that
5871 * tree_add_word() knows this is the prefix tree. */
5872 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005873 if (!cur_aff->ah_combine)
5874 n |= WFP_NC;
5875 if (upper)
5876 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005877 if (aff_entry->ae_comppermit)
5878 n |= WFP_COMPPERMIT;
5879 if (aff_entry->ae_compforbid)
5880 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005881 tree_add_word(spin, p, spin->si_prefroot, n,
5882 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005883 did_postpone_prefix = TRUE;
5884 }
5885
5886 /* Didn't actually use ah_newID, backup si_newprefID. */
5887 if (aff_todo == 0 && !did_postpone_prefix)
5888 {
5889 --spin->si_newprefID;
5890 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005891 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005892 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005893 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005894 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005895 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005896 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005897 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005898 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005899 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005900 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005901 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005902 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005903 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005904 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005906 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005907 else if (is_aff_rule(items, itemcnt, "REP", 2)
5908 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005909 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005910 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005911 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005912 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005913 fname, lnum);
5914 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005915 else if ((STRCMP(items[0], "REP") == 0
5916 || STRCMP(items[0], "REPSAL") == 0)
5917 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005918 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005919 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005920 /* Myspell ignores extra arguments, we require it starts with
5921 * # to detect mistakes. */
5922 if (itemcnt > 3 && items[3][0] != '#')
5923 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005924 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005925 {
5926 /* Replace underscore with space (can't include a space
5927 * directly). */
5928 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5929 if (*p == '_')
5930 *p = ' ';
5931 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5932 if (*p == '_')
5933 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005934 add_fromto(spin, items[0][3] == 'S'
5935 ? &spin->si_repsal
5936 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005937 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005938 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005939 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005940 {
5941 /* MAP item or count */
5942 if (!found_map)
5943 {
5944 /* First line contains the count. */
5945 found_map = TRUE;
5946 if (!isdigit(*items[1]))
5947 smsg((char_u *)_("Expected MAP count in %s line %d"),
5948 fname, lnum);
5949 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005950 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005951 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005952 int c;
5953
5954 /* Check that every character appears only once. */
5955 for (p = items[1]; *p != NUL; )
5956 {
5957#ifdef FEAT_MBYTE
5958 c = mb_ptr2char_adv(&p);
5959#else
5960 c = *p++;
5961#endif
5962 if ((spin->si_map.ga_len > 0
5963 && vim_strchr(spin->si_map.ga_data, c)
5964 != NULL)
5965 || vim_strchr(p, c) != NULL)
5966 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5967 fname, lnum);
5968 }
5969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005970 /* We simply concatenate all the MAP strings, separated by
5971 * slashes. */
5972 ga_concat(&spin->si_map, items[1]);
5973 ga_append(&spin->si_map, '/');
5974 }
5975 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005976 /* Accept "SAL from to" and "SAL from to #comment". */
5977 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005978 {
5979 if (do_sal)
5980 {
5981 /* SAL item (sounds-a-like)
5982 * Either one of the known keys or a from-to pair. */
5983 if (STRCMP(items[1], "followup") == 0)
5984 spin->si_followup = sal_to_bool(items[2]);
5985 else if (STRCMP(items[1], "collapse_result") == 0)
5986 spin->si_collapse = sal_to_bool(items[2]);
5987 else if (STRCMP(items[1], "remove_accents") == 0)
5988 spin->si_rem_accents = sal_to_bool(items[2]);
5989 else
5990 /* when "to" is "_" it means empty */
5991 add_fromto(spin, &spin->si_sal, items[1],
5992 STRCMP(items[2], "_") == 0 ? (char_u *)""
5993 : items[2]);
5994 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005995 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005996 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005997 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005998 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005999 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006000 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006001 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006002 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006003 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006004 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006005 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006006 else if (STRCMP(items[0], "COMMON") == 0)
6007 {
6008 int i;
6009
6010 for (i = 1; i < itemcnt; ++i)
6011 {
6012 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
6013 items[i])))
6014 {
6015 p = vim_strsave(items[i]);
6016 if (p == NULL)
6017 break;
6018 hash_add(&spin->si_commonwords, p);
6019 }
6020 }
6021 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006022 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006023 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006024 fname, lnum, items[0]);
6025 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006026 }
6027
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006028 if (fol != NULL || low != NULL || upp != NULL)
6029 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006030 if (spin->si_clear_chartab)
6031 {
6032 /* Clear the char type tables, don't want to use any of the
6033 * currently used spell properties. */
6034 init_spell_chartab();
6035 spin->si_clear_chartab = FALSE;
6036 }
6037
Bram Moolenaar3982c542005-06-08 21:56:31 +00006038 /*
6039 * Don't write a word table for an ASCII file, so that we don't check
6040 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006041 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006042 * mb_get_class(), the list of chars in the file will be incomplete.
6043 */
6044 if (!spin->si_ascii
6045#ifdef FEAT_MBYTE
6046 && !enc_utf8
6047#endif
6048 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006049 {
6050 if (fol == NULL || low == NULL || upp == NULL)
6051 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6052 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006053 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006054 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006055
6056 vim_free(fol);
6057 vim_free(low);
6058 vim_free(upp);
6059 }
6060
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006061 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006062 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006063 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006064 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006065 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006066 }
6067
Bram Moolenaar6de68532005-08-24 22:08:48 +00006068 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006069 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006070 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6071 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006072 }
6073
Bram Moolenaar6de68532005-08-24 22:08:48 +00006074 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006075 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006076 if (syllable == NULL)
6077 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6078 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6079 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006080 }
6081
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006082 if (compoptions != 0)
6083 {
6084 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6085 spin->si_compoptions |= compoptions;
6086 }
6087
Bram Moolenaar6de68532005-08-24 22:08:48 +00006088 if (compflags != NULL)
6089 process_compflags(spin, aff, compflags);
6090
6091 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006092 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006093 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006094 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006095 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006096 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006097 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006098 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006099 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006100 }
6101
Bram Moolenaar6de68532005-08-24 22:08:48 +00006102 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006103 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006104 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6105 spin->si_syllable = syllable;
6106 }
6107
6108 if (sofofrom != NULL || sofoto != NULL)
6109 {
6110 if (sofofrom == NULL || sofoto == NULL)
6111 smsg((char_u *)_("Missing SOFO%s line in %s"),
6112 sofofrom == NULL ? "FROM" : "TO", fname);
6113 else if (spin->si_sal.ga_len > 0)
6114 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006115 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006116 {
6117 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6118 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6119 spin->si_sofofr = sofofrom;
6120 spin->si_sofoto = sofoto;
6121 }
6122 }
6123
6124 if (midword != NULL)
6125 {
6126 aff_check_string(spin->si_midword, midword, "MIDWORD");
6127 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006128 }
6129
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006130 vim_free(pc);
6131 fclose(fd);
6132 return aff;
6133}
6134
6135/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006136 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6137 * a comment is following after item "mincount".
6138 */
6139 static int
6140is_aff_rule(items, itemcnt, rulename, mincount)
6141 char_u **items;
6142 int itemcnt;
6143 char *rulename;
6144 int mincount;
6145{
6146 return (STRCMP(items[0], rulename) == 0
6147 && (itemcnt == mincount
6148 || (itemcnt > mincount && items[mincount][0] == '#')));
6149}
6150
6151/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006152 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6153 * ae_flags to ae_comppermit and ae_compforbid.
6154 */
6155 static void
6156aff_process_flags(affile, entry)
6157 afffile_T *affile;
6158 affentry_T *entry;
6159{
6160 char_u *p;
6161 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006162 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006163
6164 if (entry->ae_flags != NULL
6165 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6166 {
6167 for (p = entry->ae_flags; *p != NUL; )
6168 {
6169 prevp = p;
6170 flag = get_affitem(affile->af_flagtype, &p);
6171 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6172 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006173 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006174 p = prevp;
6175 if (flag == affile->af_comppermit)
6176 entry->ae_comppermit = TRUE;
6177 else
6178 entry->ae_compforbid = TRUE;
6179 }
6180 if (affile->af_flagtype == AFT_NUM && *p == ',')
6181 ++p;
6182 }
6183 if (*entry->ae_flags == NUL)
6184 entry->ae_flags = NULL; /* nothing left */
6185 }
6186}
6187
6188/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006189 * Return TRUE if "s" is the name of an info item in the affix file.
6190 */
6191 static int
6192spell_info_item(s)
6193 char_u *s;
6194{
6195 return STRCMP(s, "NAME") == 0
6196 || STRCMP(s, "HOME") == 0
6197 || STRCMP(s, "VERSION") == 0
6198 || STRCMP(s, "AUTHOR") == 0
6199 || STRCMP(s, "EMAIL") == 0
6200 || STRCMP(s, "COPYRIGHT") == 0;
6201}
6202
6203/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006204 * Turn an affix flag name into a number, according to the FLAG type.
6205 * returns zero for failure.
6206 */
6207 static unsigned
6208affitem2flag(flagtype, item, fname, lnum)
6209 int flagtype;
6210 char_u *item;
6211 char_u *fname;
6212 int lnum;
6213{
6214 unsigned res;
6215 char_u *p = item;
6216
6217 res = get_affitem(flagtype, &p);
6218 if (res == 0)
6219 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006220 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006221 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6222 fname, lnum, item);
6223 else
6224 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6225 fname, lnum, item);
6226 }
6227 if (*p != NUL)
6228 {
6229 smsg((char_u *)_(e_affname), fname, lnum, item);
6230 return 0;
6231 }
6232
6233 return res;
6234}
6235
6236/*
6237 * Get one affix name from "*pp" and advance the pointer.
6238 * Returns zero for an error, still advances the pointer then.
6239 */
6240 static unsigned
6241get_affitem(flagtype, pp)
6242 int flagtype;
6243 char_u **pp;
6244{
6245 int res;
6246
Bram Moolenaar95529562005-08-25 21:21:38 +00006247 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006248 {
6249 if (!VIM_ISDIGIT(**pp))
6250 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006251 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006252 return 0;
6253 }
6254 res = getdigits(pp);
6255 }
6256 else
6257 {
6258#ifdef FEAT_MBYTE
6259 res = mb_ptr2char_adv(pp);
6260#else
6261 res = *(*pp)++;
6262#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006263 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006264 && res >= 'A' && res <= 'Z'))
6265 {
6266 if (**pp == NUL)
6267 return 0;
6268#ifdef FEAT_MBYTE
6269 res = mb_ptr2char_adv(pp) + (res << 16);
6270#else
6271 res = *(*pp)++ + (res << 16);
6272#endif
6273 }
6274 }
6275 return res;
6276}
6277
6278/*
6279 * Process the "compflags" string used in an affix file and append it to
6280 * spin->si_compflags.
6281 * The processing involves changing the affix names to ID numbers, so that
6282 * they fit in one byte.
6283 */
6284 static void
6285process_compflags(spin, aff, compflags)
6286 spellinfo_T *spin;
6287 afffile_T *aff;
6288 char_u *compflags;
6289{
6290 char_u *p;
6291 char_u *prevp;
6292 unsigned flag;
6293 compitem_T *ci;
6294 int id;
6295 int len;
6296 char_u *tp;
6297 char_u key[AH_KEY_LEN];
6298 hashitem_T *hi;
6299
6300 /* Make room for the old and the new compflags, concatenated with a / in
6301 * between. Processing it makes it shorter, but we don't know by how
6302 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006303 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006304 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006305 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006306 p = getroom(spin, len, FALSE);
6307 if (p == NULL)
6308 return;
6309 if (spin->si_compflags != NULL)
6310 {
6311 STRCPY(p, spin->si_compflags);
6312 STRCAT(p, "/");
6313 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006314 spin->si_compflags = p;
6315 tp = p + STRLEN(p);
6316
6317 for (p = compflags; *p != NUL; )
6318 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006319 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006320 /* Copy non-flag characters directly. */
6321 *tp++ = *p++;
6322 else
6323 {
6324 /* First get the flag number, also checks validity. */
6325 prevp = p;
6326 flag = get_affitem(aff->af_flagtype, &p);
6327 if (flag != 0)
6328 {
6329 /* Find the flag in the hashtable. If it was used before, use
6330 * the existing ID. Otherwise add a new entry. */
6331 vim_strncpy(key, prevp, p - prevp);
6332 hi = hash_find(&aff->af_comp, key);
6333 if (!HASHITEM_EMPTY(hi))
6334 id = HI2CI(hi)->ci_newID;
6335 else
6336 {
6337 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6338 if (ci == NULL)
6339 break;
6340 STRCPY(ci->ci_key, key);
6341 ci->ci_flag = flag;
6342 /* Avoid using a flag ID that has a special meaning in a
6343 * regexp (also inside []). */
6344 do
6345 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006346 check_renumber(spin);
6347 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006348 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006349 ci->ci_newID = id;
6350 hash_add(&aff->af_comp, ci->ci_key);
6351 }
6352 *tp++ = id;
6353 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006354 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006355 ++p;
6356 }
6357 }
6358
6359 *tp = NUL;
6360}
6361
6362/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006363 * Check that the new IDs for postponed affixes and compounding don't overrun
6364 * each other. We have almost 255 available, but start at 0-127 to avoid
6365 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6366 * When that is used up an error message is given.
6367 */
6368 static void
6369check_renumber(spin)
6370 spellinfo_T *spin;
6371{
6372 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6373 {
6374 spin->si_newprefID = 127;
6375 spin->si_newcompID = 255;
6376 }
6377}
6378
6379/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006380 * Return TRUE if flag "flag" appears in affix list "afflist".
6381 */
6382 static int
6383flag_in_afflist(flagtype, afflist, flag)
6384 int flagtype;
6385 char_u *afflist;
6386 unsigned flag;
6387{
6388 char_u *p;
6389 unsigned n;
6390
6391 switch (flagtype)
6392 {
6393 case AFT_CHAR:
6394 return vim_strchr(afflist, flag) != NULL;
6395
Bram Moolenaar95529562005-08-25 21:21:38 +00006396 case AFT_CAPLONG:
6397 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006398 for (p = afflist; *p != NUL; )
6399 {
6400#ifdef FEAT_MBYTE
6401 n = mb_ptr2char_adv(&p);
6402#else
6403 n = *p++;
6404#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006405 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006406 && *p != NUL)
6407#ifdef FEAT_MBYTE
6408 n = mb_ptr2char_adv(&p) + (n << 16);
6409#else
6410 n = *p++ + (n << 16);
6411#endif
6412 if (n == flag)
6413 return TRUE;
6414 }
6415 break;
6416
Bram Moolenaar95529562005-08-25 21:21:38 +00006417 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006418 for (p = afflist; *p != NUL; )
6419 {
6420 n = getdigits(&p);
6421 if (n == flag)
6422 return TRUE;
6423 if (*p != NUL) /* skip over comma */
6424 ++p;
6425 }
6426 break;
6427 }
6428 return FALSE;
6429}
6430
6431/*
6432 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6433 */
6434 static void
6435aff_check_number(spinval, affval, name)
6436 int spinval;
6437 int affval;
6438 char *name;
6439{
6440 if (spinval != 0 && spinval != affval)
6441 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6442}
6443
6444/*
6445 * Give a warning when "spinval" and "affval" strings are set and not the same.
6446 */
6447 static void
6448aff_check_string(spinval, affval, name)
6449 char_u *spinval;
6450 char_u *affval;
6451 char *name;
6452{
6453 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6454 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6455}
6456
6457/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006458 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6459 * NULL as equal.
6460 */
6461 static int
6462str_equal(s1, s2)
6463 char_u *s1;
6464 char_u *s2;
6465{
6466 if (s1 == NULL || s2 == NULL)
6467 return s1 == s2;
6468 return STRCMP(s1, s2) == 0;
6469}
6470
6471/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006472 * Add a from-to item to "gap". Used for REP and SAL items.
6473 * They are stored case-folded.
6474 */
6475 static void
6476add_fromto(spin, gap, from, to)
6477 spellinfo_T *spin;
6478 garray_T *gap;
6479 char_u *from;
6480 char_u *to;
6481{
6482 fromto_T *ftp;
6483 char_u word[MAXWLEN];
6484
6485 if (ga_grow(gap, 1) == OK)
6486 {
6487 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006488 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006489 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006490 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006491 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006492 ++gap->ga_len;
6493 }
6494}
6495
6496/*
6497 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6498 */
6499 static int
6500sal_to_bool(s)
6501 char_u *s;
6502{
6503 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6504}
6505
6506/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006507 * Free the structure filled by spell_read_aff().
6508 */
6509 static void
6510spell_free_aff(aff)
6511 afffile_T *aff;
6512{
6513 hashtab_T *ht;
6514 hashitem_T *hi;
6515 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006517 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518
6519 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006521 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006522 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6523 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006524 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006525 for (hi = ht->ht_array; todo > 0; ++hi)
6526 {
6527 if (!HASHITEM_EMPTY(hi))
6528 {
6529 --todo;
6530 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006531 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar473de612013-06-08 18:19:48 +02006532 vim_regfree(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006533 }
6534 }
6535 if (ht == &aff->af_suff)
6536 break;
6537 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006538
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006539 hash_clear(&aff->af_pref);
6540 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006541 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006542}
6543
6544/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006545 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006546 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006547 */
6548 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006549spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006550 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006551 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006552 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006553{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006555 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006556 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006557 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006558 char_u store_afflist[MAXWLEN];
6559 int pfxlen;
6560 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562 char_u *pc;
6563 char_u *w;
6564 int l;
6565 hash_T hash;
6566 hashitem_T *hi;
6567 FILE *fd;
6568 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006569 int non_ascii = 0;
6570 int retval = OK;
6571 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006572 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006573 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006574
Bram Moolenaar51485f02005-06-04 21:55:20 +00006575 /*
6576 * Open the file.
6577 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006578 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006579 if (fd == NULL)
6580 {
6581 EMSG2(_(e_notopen), fname);
6582 return FAIL;
6583 }
6584
Bram Moolenaar51485f02005-06-04 21:55:20 +00006585 /* The hashtable is only used to detect duplicated words. */
6586 hash_init(&ht);
6587
Bram Moolenaar4770d092006-01-12 23:22:24 +00006588 vim_snprintf((char *)IObuff, IOSIZE,
6589 _("Reading dictionary file %s ..."), fname);
6590 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006591
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006592 /* start with a message for the first line */
6593 spin->si_msg_count = 999999;
6594
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006595 /* Read and ignore the first line: word count. */
6596 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006597 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006598 EMSG2(_("E760: No word count in %s"), fname);
6599
6600 /*
6601 * Read all the lines in the file one by one.
6602 * The words are converted to 'encoding' here, before being added to
6603 * the hashtable.
6604 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006605 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006606 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006607 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006608 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006609 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006610 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006611
Bram Moolenaar51485f02005-06-04 21:55:20 +00006612 /* Remove CR, LF and white space from the end. White space halfway
6613 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006614 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006615 while (l > 0 && line[l - 1] <= ' ')
6616 --l;
6617 if (l == 0)
6618 continue; /* empty line */
6619 line[l] = NUL;
6620
Bram Moolenaarb765d632005-06-07 21:00:02 +00006621#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006622 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006624 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006625 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006626 if (pc == NULL)
6627 {
6628 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6629 fname, lnum, line);
6630 continue;
6631 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006632 w = pc;
6633 }
6634 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006635#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006636 {
6637 pc = NULL;
6638 w = line;
6639 }
6640
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006641 /* Truncate the word at the "/", set "afflist" to what follows.
6642 * Replace "\/" by "/" and "\\" by "\". */
6643 afflist = NULL;
6644 for (p = w; *p != NUL; mb_ptr_adv(p))
6645 {
6646 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006647 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006648 else if (*p == '/')
6649 {
6650 *p = NUL;
6651 afflist = p + 1;
6652 break;
6653 }
6654 }
6655
6656 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6657 if (spin->si_ascii && has_non_ascii(w))
6658 {
6659 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006660 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006661 continue;
6662 }
6663
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006664 /* This takes time, print a message every 10000 words. */
6665 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006666 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006667 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006668 vim_snprintf((char *)message, sizeof(message),
6669 _("line %6d, word %6d - %s"),
6670 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6671 msg_start();
6672 msg_puts_long_attr(message, 0);
6673 msg_clr_eos();
6674 msg_didout = FALSE;
6675 msg_col = 0;
6676 out_flush();
6677 }
6678
Bram Moolenaar51485f02005-06-04 21:55:20 +00006679 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006680 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006681 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006682 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006683 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006684 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006685 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006686 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006687
Bram Moolenaar51485f02005-06-04 21:55:20 +00006688 hash = hash_hash(dw);
6689 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006690 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006691 {
6692 if (p_verbose > 0)
6693 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006694 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006695 else if (duplicate == 0)
6696 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6697 fname, lnum, dw);
6698 ++duplicate;
6699 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006701 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006702
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006703 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006704 store_afflist[0] = NUL;
6705 pfxlen = 0;
6706 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006707 if (afflist != NULL)
6708 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006709 /* Extract flags from the affix list. */
6710 flags |= get_affix_flags(affile, afflist);
6711
Bram Moolenaar6de68532005-08-24 22:08:48 +00006712 if (affile->af_needaffix != 0 && flag_in_afflist(
6713 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006714 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006715
6716 if (affile->af_pfxpostpone)
6717 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006718 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006719
Bram Moolenaar5195e452005-08-19 20:32:47 +00006720 if (spin->si_compflags != NULL)
6721 /* Need to store the list of compound flags with the word.
6722 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006723 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006724 }
6725
Bram Moolenaar51485f02005-06-04 21:55:20 +00006726 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006727 if (store_word(spin, dw, flags, spin->si_region,
6728 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006729 retval = FAIL;
6730
6731 if (afflist != NULL)
6732 {
6733 /* Find all matching suffixes and add the resulting words.
6734 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006735 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006736 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006737 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006738 retval = FAIL;
6739
6740 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006741 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006742 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006743 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006744 retval = FAIL;
6745 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006746
6747 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006748 }
6749
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006750 if (duplicate > 0)
6751 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006752 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006753 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6754 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006755 hash_clear(&ht);
6756
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006757 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006758 return retval;
6759}
6760
6761/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006762 * Check for affix flags in "afflist" that are turned into word flags.
6763 * Return WF_ flags.
6764 */
6765 static int
6766get_affix_flags(affile, afflist)
6767 afffile_T *affile;
6768 char_u *afflist;
6769{
6770 int flags = 0;
6771
6772 if (affile->af_keepcase != 0 && flag_in_afflist(
6773 affile->af_flagtype, afflist, affile->af_keepcase))
6774 flags |= WF_KEEPCAP | WF_FIXCAP;
6775 if (affile->af_rare != 0 && flag_in_afflist(
6776 affile->af_flagtype, afflist, affile->af_rare))
6777 flags |= WF_RARE;
6778 if (affile->af_bad != 0 && flag_in_afflist(
6779 affile->af_flagtype, afflist, affile->af_bad))
6780 flags |= WF_BANNED;
6781 if (affile->af_needcomp != 0 && flag_in_afflist(
6782 affile->af_flagtype, afflist, affile->af_needcomp))
6783 flags |= WF_NEEDCOMP;
6784 if (affile->af_comproot != 0 && flag_in_afflist(
6785 affile->af_flagtype, afflist, affile->af_comproot))
6786 flags |= WF_COMPROOT;
6787 if (affile->af_nosuggest != 0 && flag_in_afflist(
6788 affile->af_flagtype, afflist, affile->af_nosuggest))
6789 flags |= WF_NOSUGGEST;
6790 return flags;
6791}
6792
6793/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006794 * Get the list of prefix IDs from the affix list "afflist".
6795 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006796 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6797 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006798 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006799 static int
6800get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006801 afffile_T *affile;
6802 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006803 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006804{
6805 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006806 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006807 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006808 int id;
6809 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006810 hashitem_T *hi;
6811
Bram Moolenaar6de68532005-08-24 22:08:48 +00006812 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006813 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006814 prevp = p;
6815 if (get_affitem(affile->af_flagtype, &p) != 0)
6816 {
6817 /* A flag is a postponed prefix flag if it appears in "af_pref"
6818 * and it's ID is not zero. */
6819 vim_strncpy(key, prevp, p - prevp);
6820 hi = hash_find(&affile->af_pref, key);
6821 if (!HASHITEM_EMPTY(hi))
6822 {
6823 id = HI2AH(hi)->ah_newID;
6824 if (id != 0)
6825 store_afflist[cnt++] = id;
6826 }
6827 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006828 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006829 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006830 }
6831
Bram Moolenaar5195e452005-08-19 20:32:47 +00006832 store_afflist[cnt] = NUL;
6833 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006834}
6835
6836/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006837 * Get the list of compound IDs from the affix list "afflist" that are used
6838 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006839 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006840 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006841 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006842get_compflags(affile, afflist, store_afflist)
6843 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006844 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006845 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006846{
6847 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006848 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006849 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006850 char_u key[AH_KEY_LEN];
6851 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006852
Bram Moolenaar6de68532005-08-24 22:08:48 +00006853 for (p = afflist; *p != NUL; )
6854 {
6855 prevp = p;
6856 if (get_affitem(affile->af_flagtype, &p) != 0)
6857 {
6858 /* A flag is a compound flag if it appears in "af_comp". */
6859 vim_strncpy(key, prevp, p - prevp);
6860 hi = hash_find(&affile->af_comp, key);
6861 if (!HASHITEM_EMPTY(hi))
6862 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6863 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006864 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006865 ++p;
6866 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006867
Bram Moolenaar5195e452005-08-19 20:32:47 +00006868 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006869}
6870
6871/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006872 * Apply affixes to a word and store the resulting words.
6873 * "ht" is the hashtable with affentry_T that need to be applied, either
6874 * prefixes or suffixes.
6875 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6876 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006877 *
6878 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006879 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006880 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006881store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006882 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006883 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006884 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006885 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006886 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006887 hashtab_T *ht;
6888 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006889 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006890 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006891 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006892 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6893 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006894{
6895 int todo;
6896 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006897 affheader_T *ah;
6898 affentry_T *ae;
6899 regmatch_T regmatch;
6900 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006901 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006902 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006903 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006904 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006905 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006906 int use_pfxlen;
6907 int need_affix;
6908 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006909 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006910 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006911 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006912
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006913 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006914 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006915 {
6916 if (!HASHITEM_EMPTY(hi))
6917 {
6918 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006919 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006920
Bram Moolenaar51485f02005-06-04 21:55:20 +00006921 /* Check that the affix combines, if required, and that the word
6922 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006923 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6924 && flag_in_afflist(affile->af_flagtype, afflist,
6925 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006926 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006927 /* Loop over all affix entries with this name. */
6928 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006929 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006930 /* Check the condition. It's not logical to match case
6931 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006932 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006933 * Another requirement from Myspell is that the chop
6934 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006935 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006936 * prefixes with a chop string and/or flags.
6937 * When a previously added affix had CIRCUMFIX this one
6938 * must have it too, if it had not then this one must not
6939 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006940 regmatch.regprog = ae->ae_prog;
6941 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006942 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006943 || ae->ae_chop != NULL
6944 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006945 && (ae->ae_chop == NULL
6946 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006947 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006948 || vim_regexec(&regmatch, word, (colnr_T)0))
6949 && (((condit & CONDIT_CFIX) == 0)
6950 == ((condit & CONDIT_AFF) == 0
6951 || ae->ae_flags == NULL
6952 || !flag_in_afflist(affile->af_flagtype,
6953 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006954 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006955 /* Match. Remove the chop and add the affix. */
6956 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006957 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006958 /* prefix: chop/add at the start of the word */
6959 if (ae->ae_add == NULL)
6960 *newword = NUL;
6961 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006962 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006963 p = word;
6964 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006965 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006966 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006967#ifdef FEAT_MBYTE
6968 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006969 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006970 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006971 for ( ; i > 0; --i)
6972 mb_ptr_adv(p);
6973 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006974 else
6975#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006976 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006977 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006978 STRCAT(newword, p);
6979 }
6980 else
6981 {
6982 /* suffix: chop/add at the end of the word */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006983 vim_strncpy(newword, word, MAXWLEN - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006984 if (ae->ae_chop != NULL)
6985 {
6986 /* Remove chop string. */
6987 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006988 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006989 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006990 mb_ptr_back(newword, p);
6991 *p = NUL;
6992 }
6993 if (ae->ae_add != NULL)
6994 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006995 }
6996
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006997 use_flags = flags;
6998 use_pfxlist = pfxlist;
6999 use_pfxlen = pfxlen;
7000 need_affix = FALSE;
7001 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
7002 if (ae->ae_flags != NULL)
7003 {
7004 /* Extract flags from the affix list. */
7005 use_flags |= get_affix_flags(affile, ae->ae_flags);
7006
7007 if (affile->af_needaffix != 0 && flag_in_afflist(
7008 affile->af_flagtype, ae->ae_flags,
7009 affile->af_needaffix))
7010 need_affix = TRUE;
7011
7012 /* When there is a CIRCUMFIX flag the other affix
7013 * must also have it and we don't add the word
7014 * with one affix. */
7015 if (affile->af_circumfix != 0 && flag_in_afflist(
7016 affile->af_flagtype, ae->ae_flags,
7017 affile->af_circumfix))
7018 {
7019 use_condit |= CONDIT_CFIX;
7020 if ((condit & CONDIT_CFIX) == 0)
7021 need_affix = TRUE;
7022 }
7023
7024 if (affile->af_pfxpostpone
7025 || spin->si_compflags != NULL)
7026 {
7027 if (affile->af_pfxpostpone)
7028 /* Get prefix IDS from the affix list. */
7029 use_pfxlen = get_pfxlist(affile,
7030 ae->ae_flags, store_afflist);
7031 else
7032 use_pfxlen = 0;
7033 use_pfxlist = store_afflist;
7034
7035 /* Combine the prefix IDs. Avoid adding the
7036 * same ID twice. */
7037 for (i = 0; i < pfxlen; ++i)
7038 {
7039 for (j = 0; j < use_pfxlen; ++j)
7040 if (pfxlist[i] == use_pfxlist[j])
7041 break;
7042 if (j == use_pfxlen)
7043 use_pfxlist[use_pfxlen++] = pfxlist[i];
7044 }
7045
7046 if (spin->si_compflags != NULL)
7047 /* Get compound IDS from the affix list. */
7048 get_compflags(affile, ae->ae_flags,
7049 use_pfxlist + use_pfxlen);
7050
7051 /* Combine the list of compound flags.
7052 * Concatenate them to the prefix IDs list.
7053 * Avoid adding the same ID twice. */
7054 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7055 {
7056 for (j = use_pfxlen;
7057 use_pfxlist[j] != NUL; ++j)
7058 if (pfxlist[i] == use_pfxlist[j])
7059 break;
7060 if (use_pfxlist[j] == NUL)
7061 {
7062 use_pfxlist[j++] = pfxlist[i];
7063 use_pfxlist[j] = NUL;
7064 }
7065 }
7066 }
7067 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007069 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007070 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007071 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007072 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007073 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007074 use_pfxlist = pfx_pfxlist;
7075 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007076
7077 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007078 if (spin->si_prefroot != NULL
7079 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007080 {
7081 /* ... add a flag to indicate an affix was used. */
7082 use_flags |= WF_HAS_AFF;
7083
7084 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007085 * affixes is not allowed. But do use the
7086 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007087 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007088 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007089 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007090
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007091 /* When compounding is supported and there is no
7092 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7093 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007094 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007095 {
7096 if (xht != NULL)
7097 use_flags |= WF_NOCOMPAFT;
7098 else
7099 use_flags |= WF_NOCOMPBEF;
7100 }
7101
Bram Moolenaar51485f02005-06-04 21:55:20 +00007102 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007103 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007104 spin->si_region, use_pfxlist,
7105 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007106 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007107
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007108 /* When added a prefix or a first suffix and the affix
7109 * has flags may add a(nother) suffix. RECURSIVE! */
7110 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7111 if (store_aff_word(spin, newword, ae->ae_flags,
7112 affile, &affile->af_suff, xht,
7113 use_condit & (xht == NULL
7114 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007115 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007116 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007117
7118 /* When added a suffix and combining is allowed also
7119 * try adding a prefix additionally. Both for the
7120 * word flags and for the affix flags. RECURSIVE! */
7121 if (xht != NULL && ah->ah_combine)
7122 {
7123 if (store_aff_word(spin, newword,
7124 afflist, affile,
7125 xht, NULL, use_condit,
7126 use_flags, use_pfxlist,
7127 pfxlen) == FAIL
7128 || (ae->ae_flags != NULL
7129 && store_aff_word(spin, newword,
7130 ae->ae_flags, affile,
7131 xht, NULL, use_condit,
7132 use_flags, use_pfxlist,
7133 pfxlen) == FAIL))
7134 retval = FAIL;
7135 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007136 }
7137 }
7138 }
7139 }
7140 }
7141
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007142 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007143}
7144
7145/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007146 * Read a file with a list of words.
7147 */
7148 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007149spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007150 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007151 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007152{
7153 FILE *fd;
7154 long lnum = 0;
7155 char_u rline[MAXLINELEN];
7156 char_u *line;
7157 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007158 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007159 int l;
7160 int retval = OK;
7161 int did_word = FALSE;
7162 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007163 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007164 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007165
7166 /*
7167 * Open the file.
7168 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007169 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007170 if (fd == NULL)
7171 {
7172 EMSG2(_(e_notopen), fname);
7173 return FAIL;
7174 }
7175
Bram Moolenaar4770d092006-01-12 23:22:24 +00007176 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7177 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007178
7179 /*
7180 * Read all the lines in the file one by one.
7181 */
7182 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7183 {
7184 line_breakcheck();
7185 ++lnum;
7186
7187 /* Skip comment lines. */
7188 if (*rline == '#')
7189 continue;
7190
7191 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007192 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007193 while (l > 0 && rline[l - 1] <= ' ')
7194 --l;
7195 if (l == 0)
7196 continue; /* empty or blank line */
7197 rline[l] = NUL;
7198
Bram Moolenaar9c102382006-05-03 21:26:49 +00007199 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007200 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007201#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202 if (spin->si_conv.vc_type != CONV_NONE)
7203 {
7204 pc = string_convert(&spin->si_conv, rline, NULL);
7205 if (pc == NULL)
7206 {
7207 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7208 fname, lnum, rline);
7209 continue;
7210 }
7211 line = pc;
7212 }
7213 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007214#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007215 {
7216 pc = NULL;
7217 line = rline;
7218 }
7219
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007220 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007221 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007222 ++line;
7223 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007224 {
7225 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007226 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7227 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007228 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007229 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7230 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007231 else
7232 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007233#ifdef FEAT_MBYTE
7234 char_u *enc;
7235
Bram Moolenaar51485f02005-06-04 21:55:20 +00007236 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007237 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007238 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007239 if (enc != NULL && !spin->si_ascii
7240 && convert_setup(&spin->si_conv, enc,
7241 p_enc) == FAIL)
7242 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007243 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007244 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007245 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007246#else
7247 smsg((char_u *)_("Conversion in %s not supported"), fname);
7248#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007249 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007250 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007251 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007252
Bram Moolenaar3982c542005-06-08 21:56:31 +00007253 if (STRNCMP(line, "regions=", 8) == 0)
7254 {
7255 if (spin->si_region_count > 1)
7256 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7257 fname, lnum, line);
7258 else
7259 {
7260 line += 8;
7261 if (STRLEN(line) > 16)
7262 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7263 fname, lnum, line);
7264 else
7265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007266 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007267 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007268
7269 /* Adjust the mask for a word valid in all regions. */
7270 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007271 }
7272 }
7273 continue;
7274 }
7275
Bram Moolenaar7887d882005-07-01 22:33:52 +00007276 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7277 fname, lnum, line - 1);
7278 continue;
7279 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007280
Bram Moolenaar7887d882005-07-01 22:33:52 +00007281 flags = 0;
7282 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007283
Bram Moolenaar7887d882005-07-01 22:33:52 +00007284 /* Check for flags and region after a slash. */
7285 p = vim_strchr(line, '/');
7286 if (p != NULL)
7287 {
7288 *p++ = NUL;
7289 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007290 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007291 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007292 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007293 else if (*p == '!') /* Bad, bad, wicked word. */
7294 flags |= WF_BANNED;
7295 else if (*p == '?') /* Rare word. */
7296 flags |= WF_RARE;
7297 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007298 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007299 if ((flags & WF_REGION) == 0) /* first one */
7300 regionmask = 0;
7301 flags |= WF_REGION;
7302
7303 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007304 if (l > spin->si_region_count)
7305 {
7306 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007307 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007308 break;
7309 }
7310 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007311 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007312 else
7313 {
7314 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7315 fname, lnum, p);
7316 break;
7317 }
7318 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007319 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007320 }
7321
7322 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7323 if (spin->si_ascii && has_non_ascii(line))
7324 {
7325 ++non_ascii;
7326 continue;
7327 }
7328
7329 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007330 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007331 {
7332 retval = FAIL;
7333 break;
7334 }
7335 did_word = TRUE;
7336 }
7337
7338 vim_free(pc);
7339 fclose(fd);
7340
Bram Moolenaar4770d092006-01-12 23:22:24 +00007341 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007342 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007343 vim_snprintf((char *)IObuff, IOSIZE,
7344 _("Ignored %d words with non-ASCII characters"), non_ascii);
7345 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007346 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007347
Bram Moolenaar51485f02005-06-04 21:55:20 +00007348 return retval;
7349}
7350
7351/*
7352 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007353 * This avoids calling free() for every little struct we use (and keeping
7354 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007355 * The memory is cleared to all zeros.
7356 * Returns NULL when out of memory.
7357 */
7358 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007359getroom(spin, len, align)
7360 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007361 size_t len; /* length needed */
7362 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007363{
7364 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007365 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007366
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007367 if (align && bl != NULL)
7368 /* Round size up for alignment. On some systems structures need to be
7369 * aligned to the size of a pointer (e.g., SPARC). */
7370 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7371 & ~(sizeof(char *) - 1);
7372
Bram Moolenaar51485f02005-06-04 21:55:20 +00007373 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7374 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007375 if (len >= SBLOCKSIZE)
7376 bl = NULL;
7377 else
7378 /* Allocate a block of memory. It is not freed until much later. */
7379 bl = (sblock_T *)alloc_clear(
7380 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007381 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007382 {
7383 if (!spin->si_did_emsg)
7384 {
7385 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7386 spin->si_did_emsg = TRUE;
7387 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007388 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007389 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007390 bl->sb_next = spin->si_blocks;
7391 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007392 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007393 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007394 }
7395
7396 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007397 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007398
7399 return p;
7400}
7401
7402/*
7403 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007404 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007405 */
7406 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007407getroom_save(spin, s)
7408 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007409 char_u *s;
7410{
7411 char_u *sc;
7412
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007413 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007414 if (sc != NULL)
7415 STRCPY(sc, s);
7416 return sc;
7417}
7418
7419
7420/*
7421 * Free the list of allocated sblock_T.
7422 */
7423 static void
7424free_blocks(bl)
7425 sblock_T *bl;
7426{
7427 sblock_T *next;
7428
7429 while (bl != NULL)
7430 {
7431 next = bl->sb_next;
7432 vim_free(bl);
7433 bl = next;
7434 }
7435}
7436
7437/*
7438 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007439 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007440 */
7441 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007442wordtree_alloc(spin)
7443 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007444{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007445 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007446}
7447
7448/*
7449 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007450 * Always store it in the case-folded tree. For a keep-case word this is
7451 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7452 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007453 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007454 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7455 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007456 */
7457 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007458store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007459 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007460 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007461 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007462 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007463 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007464 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007465{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007466 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007467 int ct = captype(word, word + len);
7468 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007469 int res = OK;
7470 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007472 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007473 for (p = pfxlist; res == OK; ++p)
7474 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007475 if (!need_affix || (p != NULL && *p != NUL))
7476 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007477 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007478 if (p == NULL || *p == NUL)
7479 break;
7480 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007481 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007482
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007483 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007484 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007485 for (p = pfxlist; res == OK; ++p)
7486 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007487 if (!need_affix || (p != NULL && *p != NUL))
7488 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007489 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007490 if (p == NULL || *p == NUL)
7491 break;
7492 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007493 ++spin->si_keepwcount;
7494 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007495 return res;
7496}
7497
7498/*
7499 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007500 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007501 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007502 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007503 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007504 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007505tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007506 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007507 char_u *word;
7508 wordnode_T *root;
7509 int flags;
7510 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007511 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007512{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007513 wordnode_T *node = root;
7514 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007515 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007516 wordnode_T **prev = NULL;
7517 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007518
Bram Moolenaar51485f02005-06-04 21:55:20 +00007519 /* Add each byte of the word to the tree, including the NUL at the end. */
7520 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007521 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007522 /* When there is more than one reference to this node we need to make
7523 * a copy, so that we can modify it. Copy the whole list of siblings
7524 * (we don't optimize for a partly shared list of siblings). */
7525 if (node != NULL && node->wn_refs > 1)
7526 {
7527 --node->wn_refs;
7528 copyprev = prev;
7529 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7530 {
7531 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007532 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007533 if (np == NULL)
7534 return FAIL;
7535 np->wn_child = copyp->wn_child;
7536 if (np->wn_child != NULL)
7537 ++np->wn_child->wn_refs; /* child gets extra ref */
7538 np->wn_byte = copyp->wn_byte;
7539 if (np->wn_byte == NUL)
7540 {
7541 np->wn_flags = copyp->wn_flags;
7542 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007543 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007544 }
7545
7546 /* Link the new node in the list, there will be one ref. */
7547 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007548 if (copyprev != NULL)
7549 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007550 copyprev = &np->wn_sibling;
7551
7552 /* Let "node" point to the head of the copied list. */
7553 if (copyp == node)
7554 node = np;
7555 }
7556 }
7557
Bram Moolenaar51485f02005-06-04 21:55:20 +00007558 /* Look for the sibling that has the same character. They are sorted
7559 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007560 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007561 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007562 while (node != NULL
7563 && (node->wn_byte < word[i]
7564 || (node->wn_byte == NUL
7565 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007566 ? node->wn_affixID < (unsigned)affixID
7567 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007568 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007569 && (spin->si_sugtree
7570 ? (node->wn_region & 0xffff) < region
7571 : node->wn_affixID
7572 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007573 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007574 prev = &node->wn_sibling;
7575 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007576 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007577 if (node == NULL
7578 || node->wn_byte != word[i]
7579 || (word[i] == NUL
7580 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007581 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007582 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007583 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007584 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007585 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007586 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007587 if (np == NULL)
7588 return FAIL;
7589 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007590
7591 /* If "node" is NULL this is a new child or the end of the sibling
7592 * list: ref count is one. Otherwise use ref count of sibling and
7593 * make ref count of sibling one (matters when inserting in front
7594 * of the list of siblings). */
7595 if (node == NULL)
7596 np->wn_refs = 1;
7597 else
7598 {
7599 np->wn_refs = node->wn_refs;
7600 node->wn_refs = 1;
7601 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007602 if (prev != NULL)
7603 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007604 np->wn_sibling = node;
7605 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007606 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007607
Bram Moolenaar51485f02005-06-04 21:55:20 +00007608 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007609 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007610 node->wn_flags = flags;
7611 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007612 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007613 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007614 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007615 prev = &node->wn_child;
7616 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007617 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007618#ifdef SPELL_PRINTTREE
7619 smsg("Added \"%s\"", word);
7620 spell_print_tree(root->wn_sibling);
7621#endif
7622
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 /* count nr of words added since last message */
7624 ++spin->si_msg_count;
7625
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007626 if (spin->si_compress_cnt > 1)
7627 {
7628 if (--spin->si_compress_cnt == 1)
7629 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007630 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007631 }
7632
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007633 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007634 * When we have allocated lots of memory we need to compress the word tree
7635 * to free up some room. But compression is slow, and we might actually
7636 * need that room, thus only compress in the following situations:
7637 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007638 * "compress_start" blocks.
7639 * 2. When compressed before and used "compress_inc" blocks before
7640 * adding "compress_added" words (si_compress_cnt > 1).
7641 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007642 * (si_compress_cnt == 1) and the number of free nodes drops below the
7643 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007644 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007645#ifndef SPELL_PRINTTREE
7646 if (spin->si_compress_cnt == 1
7647 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007648 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007649#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007650 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007651 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007652 * when the freed up room has been used and another "compress_inc"
7653 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007654 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007655 spin->si_blocks_cnt -= compress_inc;
7656 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007657
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007658 if (spin->si_verbose)
7659 {
7660 msg_start();
7661 msg_puts((char_u *)_(msg_compressing));
7662 msg_clr_eos();
7663 msg_didout = FALSE;
7664 msg_col = 0;
7665 out_flush();
7666 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007667
7668 /* Compress both trees. Either they both have many nodes, which makes
7669 * compression useful, or one of them is small, which means
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007670 * compression goes fast. But when filling the soundfold word tree
Bram Moolenaar4770d092006-01-12 23:22:24 +00007671 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007672 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007673 if (affixID >= 0)
7674 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007675 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007676
7677 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007678}
7679
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007680/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007681 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7682 * Sets "sps_flags".
7683 */
7684 int
7685spell_check_msm()
7686{
7687 char_u *p = p_msm;
7688 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007689 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007690 long added = 0;
7691
7692 if (!VIM_ISDIGIT(*p))
7693 return FAIL;
7694 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7695 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7696 if (*p != ',')
7697 return FAIL;
7698 ++p;
7699 if (!VIM_ISDIGIT(*p))
7700 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007701 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007702 if (*p != ',')
7703 return FAIL;
7704 ++p;
7705 if (!VIM_ISDIGIT(*p))
7706 return FAIL;
7707 added = getdigits(&p) * 1024;
7708 if (*p != NUL)
7709 return FAIL;
7710
Bram Moolenaar89d40322006-08-29 15:30:07 +00007711 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007712 return FAIL;
7713
7714 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007715 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007716 compress_added = added;
7717 return OK;
7718}
7719
7720
7721/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007722 * Get a wordnode_T, either from the list of previously freed nodes or
7723 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007724 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007725 */
7726 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007727get_wordnode(spin)
7728 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007729{
7730 wordnode_T *n;
7731
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007732 if (spin->si_first_free == NULL)
7733 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007734 else
7735 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007736 n = spin->si_first_free;
7737 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007738 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007739 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007740 }
7741#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007742 if (n != NULL)
7743 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007744#endif
7745 return n;
7746}
7747
7748/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007749 * Decrement the reference count on a node (which is the head of a list of
7750 * siblings). If the reference count becomes zero free the node and its
7751 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007752 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007753 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007754 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007755deref_wordnode(spin, node)
7756 spellinfo_T *spin;
7757 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007758{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007759 wordnode_T *np;
7760 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007761
7762 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007763 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007764 for (np = node; np != NULL; np = np->wn_sibling)
7765 {
7766 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007767 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007768 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007769 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007770 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007771 ++cnt; /* length field */
7772 }
7773 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007774}
7775
7776/*
7777 * Free a wordnode_T for re-use later.
7778 * Only the "wn_child" field becomes invalid.
7779 */
7780 static void
7781free_wordnode(spin, n)
7782 spellinfo_T *spin;
7783 wordnode_T *n;
7784{
7785 n->wn_child = spin->si_first_free;
7786 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007787 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007788}
7789
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007790/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007791 * Compress a tree: find tails that are identical and can be shared.
7792 */
7793 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007794wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007795 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007796 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007797{
7798 hashtab_T ht;
7799 int n;
7800 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007801 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007802
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007803 /* Skip the root itself, it's not actually used. The first sibling is the
7804 * start of the tree. */
7805 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007806 {
7807 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007808 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007809
7810#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007811 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007812#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007813 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007814 if (tot > 1000000)
7815 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007816 else if (tot == 0)
7817 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007818 else
7819 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007820 vim_snprintf((char *)IObuff, IOSIZE,
7821 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7822 n, tot, tot - n, perc);
7823 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007824 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007825#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007826 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007827#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007828 hash_clear(&ht);
7829 }
7830}
7831
7832/*
7833 * Compress a node, its siblings and its children, depth first.
7834 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007835 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007836 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007837node_compress(spin, node, ht, tot)
7838 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 wordnode_T *node;
7840 hashtab_T *ht;
7841 int *tot; /* total count of nodes before compressing,
7842 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007843{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007844 wordnode_T *np;
7845 wordnode_T *tp;
7846 wordnode_T *child;
7847 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007848 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007849 int len = 0;
7850 unsigned nr, n;
7851 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007852
Bram Moolenaar51485f02005-06-04 21:55:20 +00007853 /*
7854 * Go through the list of siblings. Compress each child and then try
7855 * finding an identical child to replace it.
7856 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007857 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007858 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007859 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007860 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007861 ++len;
7862 if ((child = np->wn_child) != NULL)
7863 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007864 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007865 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007866
7867 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007868 hash = hash_hash(child->wn_u1.hashkey);
7869 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007870 if (!HASHITEM_EMPTY(hi))
7871 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007872 /* There are children we encountered before with a hash value
7873 * identical to the current child. Now check if there is one
7874 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007875 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007876 if (node_equal(child, tp))
7877 {
7878 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007879 * current one. This means the current child and all
7880 * its siblings is unlinked from the tree. */
7881 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007882 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007883 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007884 break;
7885 }
7886 if (tp == NULL)
7887 {
7888 /* No other child with this hash value equals the child of
7889 * the node, add it to the linked list after the first
7890 * item. */
7891 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007892 child->wn_u2.next = tp->wn_u2.next;
7893 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007894 }
7895 }
7896 else
7897 /* No other child has this hash value, add it to the
7898 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007899 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007900 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007901 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007902 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007903
7904 /*
7905 * Make a hash key for the node and its siblings, so that we can quickly
7906 * find a lookalike node. This must be done after compressing the sibling
7907 * list, otherwise the hash key would become invalid by the compression.
7908 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007909 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007910 nr = 0;
7911 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007912 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007913 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007914 /* end node: use wn_flags, wn_region and wn_affixID */
7915 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007916 else
7917 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007918 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007919 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007920 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007921
7922 /* Avoid NUL bytes, it terminates the hash key. */
7923 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007924 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007925 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007926 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007927 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007928 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007929 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007930 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7931 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007932
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007933 /* Check for CTRL-C pressed now and then. */
7934 fast_breakcheck();
7935
Bram Moolenaar51485f02005-06-04 21:55:20 +00007936 return compressed;
7937}
7938
7939/*
7940 * Return TRUE when two nodes have identical siblings and children.
7941 */
7942 static int
7943node_equal(n1, n2)
7944 wordnode_T *n1;
7945 wordnode_T *n2;
7946{
7947 wordnode_T *p1;
7948 wordnode_T *p2;
7949
7950 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7951 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7952 if (p1->wn_byte != p2->wn_byte
7953 || (p1->wn_byte == NUL
7954 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007955 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007956 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007957 : (p1->wn_child != p2->wn_child)))
7958 break;
7959
7960 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007961}
7962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963static int
7964#ifdef __BORLANDC__
7965_RTLENTRYF
7966#endif
7967rep_compare __ARGS((const void *s1, const void *s2));
7968
7969/*
7970 * Function given to qsort() to sort the REP items on "from" string.
7971 */
7972 static int
7973#ifdef __BORLANDC__
7974_RTLENTRYF
7975#endif
7976rep_compare(s1, s2)
7977 const void *s1;
7978 const void *s2;
7979{
7980 fromto_T *p1 = (fromto_T *)s1;
7981 fromto_T *p2 = (fromto_T *)s2;
7982
7983 return STRCMP(p1->ft_from, p2->ft_from);
7984}
7985
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007986/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007987 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007988 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007989 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007990 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007991write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007992 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007993 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007994{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007995 FILE *fd;
7996 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007997 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007998 wordnode_T *tree;
7999 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008000 int i;
8001 int l;
8002 garray_T *gap;
8003 fromto_T *ftp;
8004 char_u *p;
8005 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008006 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008007 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008008 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008009
Bram Moolenaarb765d632005-06-07 21:00:02 +00008010 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00008011 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008012 {
8013 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008014 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008015 }
8016
Bram Moolenaar5195e452005-08-19 20:32:47 +00008017 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008018 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008019 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008020 if (fwv != (size_t)1)
8021 /* Catch first write error, don't try writing more. */
8022 goto theend;
8023
Bram Moolenaar5195e452005-08-19 20:32:47 +00008024 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008025
Bram Moolenaar5195e452005-08-19 20:32:47 +00008026 /*
8027 * <SECTIONS>: <section> ... <sectionend>
8028 */
8029
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008030 /* SN_INFO: <infotext> */
8031 if (spin->si_info != NULL)
8032 {
8033 putc(SN_INFO, fd); /* <sectionID> */
8034 putc(0, fd); /* <sectionflags> */
8035
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008036 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008037 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008038 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008039 }
8040
Bram Moolenaar5195e452005-08-19 20:32:47 +00008041 /* SN_REGION: <regionname> ...
8042 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008043 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008044 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008045 putc(SN_REGION, fd); /* <sectionID> */
8046 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8047 l = spin->si_region_count * 2;
8048 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008049 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008050 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008051 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008052 }
8053 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008054 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008055
Bram Moolenaar5195e452005-08-19 20:32:47 +00008056 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8057 *
8058 * The table with character flags and the table for case folding.
8059 * This makes sure the same characters are recognized as word characters
8060 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008061 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008062 * 'encoding'.
8063 * Also skip this for an .add.spl file, the main spell file must contain
8064 * the table (avoids that it conflicts). File is shorter too.
8065 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008066 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008067 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008068 char_u folchars[128 * 8];
8069 int flags;
8070
Bram Moolenaard12a1322005-08-21 22:08:24 +00008071 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008072 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8073
8074 /* Form the <folchars> string first, we need to know its length. */
8075 l = 0;
8076 for (i = 128; i < 256; ++i)
8077 {
8078#ifdef FEAT_MBYTE
8079 if (has_mbyte)
8080 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8081 else
8082#endif
8083 folchars[l++] = spelltab.st_fold[i];
8084 }
8085 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8086
8087 fputc(128, fd); /* <charflagslen> */
8088 for (i = 128; i < 256; ++i)
8089 {
8090 flags = 0;
8091 if (spelltab.st_isw[i])
8092 flags |= CF_WORD;
8093 if (spelltab.st_isu[i])
8094 flags |= CF_UPPER;
8095 fputc(flags, fd); /* <charflags> */
8096 }
8097
8098 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008099 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008100 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008101
Bram Moolenaar5195e452005-08-19 20:32:47 +00008102 /* SN_MIDWORD: <midword> */
8103 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008104 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008105 putc(SN_MIDWORD, fd); /* <sectionID> */
8106 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008108 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008109 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008110 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8111 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008112 }
8113
Bram Moolenaar5195e452005-08-19 20:32:47 +00008114 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8115 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008116 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008117 putc(SN_PREFCOND, fd); /* <sectionID> */
8118 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8119
8120 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8121 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8122
8123 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008124 }
8125
Bram Moolenaar5195e452005-08-19 20:32:47 +00008126 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008127 * SN_SAL: <salflags> <salcount> <sal> ...
8128 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008129
Bram Moolenaar5195e452005-08-19 20:32:47 +00008130 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008131 * round 2: SN_SAL section (unless SN_SOFO is used)
8132 * round 3: SN_REPSAL section */
8133 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008134 {
8135 if (round == 1)
8136 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008137 else if (round == 2)
8138 {
8139 /* Don't write SN_SAL when using a SN_SOFO section */
8140 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8141 continue;
8142 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008144 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008145 gap = &spin->si_repsal;
8146
8147 /* Don't write the section if there are no items. */
8148 if (gap->ga_len == 0)
8149 continue;
8150
8151 /* Sort the REP/REPSAL items. */
8152 if (round != 2)
8153 qsort(gap->ga_data, (size_t)gap->ga_len,
8154 sizeof(fromto_T), rep_compare);
8155
8156 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8157 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008158
Bram Moolenaar5195e452005-08-19 20:32:47 +00008159 /* This is for making suggestions, section is not required. */
8160 putc(0, fd); /* <sectionflags> */
8161
8162 /* Compute the length of what follows. */
8163 l = 2; /* count <repcount> or <salcount> */
8164 for (i = 0; i < gap->ga_len; ++i)
8165 {
8166 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008167 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8168 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008169 }
8170 if (round == 2)
8171 ++l; /* count <salflags> */
8172 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8173
8174 if (round == 2)
8175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008176 i = 0;
8177 if (spin->si_followup)
8178 i |= SAL_F0LLOWUP;
8179 if (spin->si_collapse)
8180 i |= SAL_COLLAPSE;
8181 if (spin->si_rem_accents)
8182 i |= SAL_REM_ACCENTS;
8183 putc(i, fd); /* <salflags> */
8184 }
8185
8186 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8187 for (i = 0; i < gap->ga_len; ++i)
8188 {
8189 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8190 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8191 ftp = &((fromto_T *)gap->ga_data)[i];
8192 for (rr = 1; rr <= 2; ++rr)
8193 {
8194 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008195 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008196 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008197 if (l > 0)
8198 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008199 }
8200 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008202 }
8203
Bram Moolenaar5195e452005-08-19 20:32:47 +00008204 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8205 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008206 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8207 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008208 putc(SN_SOFO, fd); /* <sectionID> */
8209 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008210
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008211 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008212 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8213 /* <sectionlen> */
8214
8215 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008216 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008218 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008219 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008220 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008221 }
8222
Bram Moolenaar4770d092006-01-12 23:22:24 +00008223 /* SN_WORDS: <word> ...
8224 * This is for making suggestions, section is not required. */
8225 if (spin->si_commonwords.ht_used > 0)
8226 {
8227 putc(SN_WORDS, fd); /* <sectionID> */
8228 putc(0, fd); /* <sectionflags> */
8229
8230 /* round 1: count the bytes
8231 * round 2: write the bytes */
8232 for (round = 1; round <= 2; ++round)
8233 {
8234 int todo;
8235 int len = 0;
8236 hashitem_T *hi;
8237
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008238 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008239 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8240 if (!HASHITEM_EMPTY(hi))
8241 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008242 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008243 len += l;
8244 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008245 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008246 --todo;
8247 }
8248 if (round == 1)
8249 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8250 }
8251 }
8252
Bram Moolenaar5195e452005-08-19 20:32:47 +00008253 /* SN_MAP: <mapstr>
8254 * This is for making suggestions, section is not required. */
8255 if (spin->si_map.ga_len > 0)
8256 {
8257 putc(SN_MAP, fd); /* <sectionID> */
8258 putc(0, fd); /* <sectionflags> */
8259 l = spin->si_map.ga_len;
8260 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008261 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008262 /* <mapstr> */
8263 }
8264
Bram Moolenaar4770d092006-01-12 23:22:24 +00008265 /* SN_SUGFILE: <timestamp>
8266 * This is used to notify that a .sug file may be available and at the
8267 * same time allows for checking that a .sug file that is found matches
8268 * with this .spl file. That's because the word numbers must be exactly
8269 * right. */
8270 if (!spin->si_nosugfile
8271 && (spin->si_sal.ga_len > 0
8272 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8273 {
8274 putc(SN_SUGFILE, fd); /* <sectionID> */
8275 putc(0, fd); /* <sectionflags> */
8276 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8277
8278 /* Set si_sugtime and write it to the file. */
8279 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008280 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008281 }
8282
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008283 /* SN_NOSPLITSUGS: nothing
8284 * This is used to notify that no suggestions with word splits are to be
8285 * made. */
8286 if (spin->si_nosplitsugs)
8287 {
8288 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8289 putc(0, fd); /* <sectionflags> */
8290 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8291 }
8292
Bram Moolenaar5195e452005-08-19 20:32:47 +00008293 /* SN_COMPOUND: compound info.
8294 * We don't mark it required, when not supported all compound words will
8295 * be bad words. */
8296 if (spin->si_compflags != NULL)
8297 {
8298 putc(SN_COMPOUND, fd); /* <sectionID> */
8299 putc(0, fd); /* <sectionflags> */
8300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008301 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008302 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008303 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008304 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8305
Bram Moolenaar5195e452005-08-19 20:32:47 +00008306 putc(spin->si_compmax, fd); /* <compmax> */
8307 putc(spin->si_compminlen, fd); /* <compminlen> */
8308 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008309 putc(0, fd); /* for Vim 7.0b compatibility */
8310 putc(spin->si_compoptions, fd); /* <compoptions> */
8311 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8312 /* <comppatcount> */
8313 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8314 {
8315 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008316 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008317 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8318 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008319 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008320 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008321 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008322 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008323 }
8324
Bram Moolenaar78622822005-08-23 21:00:13 +00008325 /* SN_NOBREAK: NOBREAK flag */
8326 if (spin->si_nobreak)
8327 {
8328 putc(SN_NOBREAK, fd); /* <sectionID> */
8329 putc(0, fd); /* <sectionflags> */
8330
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008331 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008332 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8333 }
8334
Bram Moolenaar5195e452005-08-19 20:32:47 +00008335 /* SN_SYLLABLE: syllable info.
8336 * We don't mark it required, when not supported syllables will not be
8337 * counted. */
8338 if (spin->si_syllable != NULL)
8339 {
8340 putc(SN_SYLLABLE, fd); /* <sectionID> */
8341 putc(0, fd); /* <sectionflags> */
8342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008343 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008344 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008345 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8346 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008347 }
8348
8349 /* end of <SECTIONS> */
8350 putc(SN_END, fd); /* <sectionend> */
8351
Bram Moolenaar50cde822005-06-05 21:54:54 +00008352
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008353 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008354 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008355 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008356 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008357 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008359 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008360 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008361 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008362 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008363 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008364 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008365
Bram Moolenaar0c405862005-06-22 22:26:26 +00008366 /* Clear the index and wnode fields in the tree. */
8367 clear_node(tree);
8368
Bram Moolenaar51485f02005-06-04 21:55:20 +00008369 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008370 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008371 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008372 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008373
Bram Moolenaar51485f02005-06-04 21:55:20 +00008374 /* number of nodes in 4 bytes */
8375 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008376 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008377
Bram Moolenaar51485f02005-06-04 21:55:20 +00008378 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008379 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008380 }
8381
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008382 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008383 if (putc(0, fd) == EOF)
8384 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008385theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008386 if (fclose(fd) == EOF)
8387 retval = FAIL;
8388
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008389 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008390 retval = FAIL;
8391 if (retval == FAIL)
8392 EMSG(_(e_write));
8393
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008394 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008395}
8396
8397/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008398 * Clear the index and wnode fields of "node", it siblings and its
8399 * children. This is needed because they are a union with other items to save
8400 * space.
8401 */
8402 static void
8403clear_node(node)
8404 wordnode_T *node;
8405{
8406 wordnode_T *np;
8407
8408 if (node != NULL)
8409 for (np = node; np != NULL; np = np->wn_sibling)
8410 {
8411 np->wn_u1.index = 0;
8412 np->wn_u2.wnode = NULL;
8413
8414 if (np->wn_byte != NUL)
8415 clear_node(np->wn_child);
8416 }
8417}
8418
8419
8420/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008421 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008422 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008423 * This first writes the list of possible bytes (siblings). Then for each
8424 * byte recursively write the children.
8425 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008426 * NOTE: The code here must match the code in read_tree_node(), since
8427 * assumptions are made about the indexes (so that we don't have to write them
8428 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008429 *
8430 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008431 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008432 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008433put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008434 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008435 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008436 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008437 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008438 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008439{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008440 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008441 int siblingcount = 0;
8442 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008443 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008444
Bram Moolenaar51485f02005-06-04 21:55:20 +00008445 /* If "node" is zero the tree is empty. */
8446 if (node == NULL)
8447 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008448
Bram Moolenaar51485f02005-06-04 21:55:20 +00008449 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008450 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008451
8452 /* Count the number of siblings. */
8453 for (np = node; np != NULL; np = np->wn_sibling)
8454 ++siblingcount;
8455
8456 /* Write the sibling count. */
8457 if (fd != NULL)
8458 putc(siblingcount, fd); /* <siblingcount> */
8459
8460 /* Write each sibling byte and optionally extra info. */
8461 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008462 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008463 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008464 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008465 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008466 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008467 /* For a NUL byte (end of word) write the flags etc. */
8468 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008469 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008470 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008471 * associated condition nr (stored in wn_region). The
8472 * byte value is misused to store the "rare" and "not
8473 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008474 if (np->wn_flags == (short_u)PFX_FLAGS)
8475 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008476 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008477 {
8478 putc(BY_FLAGS, fd); /* <byte> */
8479 putc(np->wn_flags, fd); /* <pflags> */
8480 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008481 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008482 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008483 }
8484 else
8485 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008486 /* For word trees we write the flag/region items. */
8487 flags = np->wn_flags;
8488 if (regionmask != 0 && np->wn_region != regionmask)
8489 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008490 if (np->wn_affixID != 0)
8491 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008492 if (flags == 0)
8493 {
8494 /* word without flags or region */
8495 putc(BY_NOFLAGS, fd); /* <byte> */
8496 }
8497 else
8498 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008499 if (np->wn_flags >= 0x100)
8500 {
8501 putc(BY_FLAGS2, fd); /* <byte> */
8502 putc(flags, fd); /* <flags> */
8503 putc((unsigned)flags >> 8, fd); /* <flags2> */
8504 }
8505 else
8506 {
8507 putc(BY_FLAGS, fd); /* <byte> */
8508 putc(flags, fd); /* <flags> */
8509 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008510 if (flags & WF_REGION)
8511 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008512 if (flags & WF_AFX)
8513 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008514 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008515 }
8516 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008517 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008518 else
8519 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008520 if (np->wn_child->wn_u1.index != 0
8521 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008522 {
8523 /* The child is written elsewhere, write the reference. */
8524 if (fd != NULL)
8525 {
8526 putc(BY_INDEX, fd); /* <byte> */
8527 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008528 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008529 }
8530 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008531 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008532 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008533 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008534
Bram Moolenaar51485f02005-06-04 21:55:20 +00008535 if (fd != NULL)
8536 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8537 {
8538 EMSG(_(e_write));
8539 return 0;
8540 }
8541 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008542 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008543
8544 /* Space used in the array when reading: one for each sibling and one for
8545 * the count. */
8546 newindex += siblingcount + 1;
8547
8548 /* Recursively dump the children of each sibling. */
8549 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008550 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8551 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008552 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008553
8554 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008555}
8556
8557
8558/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008559 * ":mkspell [-ascii] outfile infile ..."
8560 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008561 */
8562 void
8563ex_mkspell(eap)
8564 exarg_T *eap;
8565{
8566 int fcount;
8567 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008568 char_u *arg = eap->arg;
8569 int ascii = FALSE;
8570
8571 if (STRNCMP(arg, "-ascii", 6) == 0)
8572 {
8573 ascii = TRUE;
8574 arg = skipwhite(arg + 6);
8575 }
8576
8577 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
Bram Moolenaar8f5c6f02012-06-29 12:57:06 +02008578 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008580 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008581 FreeWild(fcount, fnames);
8582 }
8583}
8584
8585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008586 * Create the .sug file.
8587 * Uses the soundfold info in "spin".
8588 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8589 */
8590 static void
8591spell_make_sugfile(spin, wfname)
8592 spellinfo_T *spin;
8593 char_u *wfname;
8594{
Bram Moolenaard9462e32011-04-11 21:35:11 +02008595 char_u *fname = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008596 int len;
8597 slang_T *slang;
8598 int free_slang = FALSE;
8599
8600 /*
8601 * Read back the .spl file that was written. This fills the required
8602 * info for soundfolding. This also uses less memory than the
8603 * pointer-linked version of the trie. And it avoids having two versions
8604 * of the code for the soundfolding stuff.
8605 * It might have been done already by spell_reload_one().
8606 */
8607 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8608 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8609 break;
8610 if (slang == NULL)
8611 {
8612 spell_message(spin, (char_u *)_("Reading back spell file..."));
8613 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8614 if (slang == NULL)
8615 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008616 free_slang = TRUE;
8617 }
8618
8619 /*
8620 * Clear the info in "spin" that is used.
8621 */
8622 spin->si_blocks = NULL;
8623 spin->si_blocks_cnt = 0;
8624 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8625 spin->si_free_count = 0;
8626 spin->si_first_free = NULL;
8627 spin->si_foldwcount = 0;
8628
8629 /*
8630 * Go through the trie of good words, soundfold each word and add it to
8631 * the soundfold trie.
8632 */
8633 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8634 if (sug_filltree(spin, slang) == FAIL)
8635 goto theend;
8636
8637 /*
8638 * Create the table which links each soundfold word with a list of the
8639 * good words it may come from. Creates buffer "spin->si_spellbuf".
8640 * This also removes the wordnr from the NUL byte entries to make
8641 * compression possible.
8642 */
8643 if (sug_maketable(spin) == FAIL)
8644 goto theend;
8645
8646 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8647 (long)spin->si_spellbuf->b_ml.ml_line_count);
8648
8649 /*
8650 * Compress the soundfold trie.
8651 */
8652 spell_message(spin, (char_u *)_(msg_compressing));
8653 wordtree_compress(spin, spin->si_foldroot);
8654
8655 /*
8656 * Write the .sug file.
8657 * Make the file name by changing ".spl" to ".sug".
8658 */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008659 fname = alloc(MAXPATHL);
8660 if (fname == NULL)
8661 goto theend;
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02008662 vim_strncpy(fname, wfname, MAXPATHL - 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008663 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008664 fname[len - 2] = 'u';
8665 fname[len - 1] = 'g';
8666 sug_write(spin, fname);
8667
8668theend:
Bram Moolenaard9462e32011-04-11 21:35:11 +02008669 vim_free(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008670 if (free_slang)
8671 slang_free(slang);
8672 free_blocks(spin->si_blocks);
8673 close_spellbuf(spin->si_spellbuf);
8674}
8675
8676/*
8677 * Build the soundfold trie for language "slang".
8678 */
8679 static int
8680sug_filltree(spin, slang)
8681 spellinfo_T *spin;
8682 slang_T *slang;
8683{
8684 char_u *byts;
8685 idx_T *idxs;
8686 int depth;
8687 idx_T arridx[MAXWLEN];
8688 int curi[MAXWLEN];
8689 char_u tword[MAXWLEN];
8690 char_u tsalword[MAXWLEN];
8691 int c;
8692 idx_T n;
8693 unsigned words_done = 0;
8694 int wordcount[MAXWLEN];
8695
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008696 /* We use si_foldroot for the soundfolded trie. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008697 spin->si_foldroot = wordtree_alloc(spin);
8698 if (spin->si_foldroot == NULL)
8699 return FAIL;
8700
8701 /* let tree_add_word() know we're adding to the soundfolded tree */
8702 spin->si_sugtree = TRUE;
8703
8704 /*
8705 * Go through the whole case-folded tree, soundfold each word and put it
8706 * in the trie.
8707 */
8708 byts = slang->sl_fbyts;
8709 idxs = slang->sl_fidxs;
8710
8711 arridx[0] = 0;
8712 curi[0] = 1;
8713 wordcount[0] = 0;
8714
8715 depth = 0;
8716 while (depth >= 0 && !got_int)
8717 {
8718 if (curi[depth] > byts[arridx[depth]])
8719 {
8720 /* Done all bytes at this node, go up one level. */
8721 idxs[arridx[depth]] = wordcount[depth];
8722 if (depth > 0)
8723 wordcount[depth - 1] += wordcount[depth];
8724
8725 --depth;
8726 line_breakcheck();
8727 }
8728 else
8729 {
8730
8731 /* Do one more byte at this node. */
8732 n = arridx[depth] + curi[depth];
8733 ++curi[depth];
8734
8735 c = byts[n];
8736 if (c == 0)
8737 {
8738 /* Sound-fold the word. */
8739 tword[depth] = NUL;
8740 spell_soundfold(slang, tword, TRUE, tsalword);
8741
8742 /* We use the "flags" field for the MSB of the wordnr,
8743 * "region" for the LSB of the wordnr. */
8744 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8745 words_done >> 16, words_done & 0xffff,
8746 0) == FAIL)
8747 return FAIL;
8748
8749 ++words_done;
8750 ++wordcount[depth];
8751
8752 /* Reset the block count each time to avoid compression
8753 * kicking in. */
8754 spin->si_blocks_cnt = 0;
8755
8756 /* Skip over any other NUL bytes (same word with different
8757 * flags). */
8758 while (byts[n + 1] == 0)
8759 {
8760 ++n;
8761 ++curi[depth];
8762 }
8763 }
8764 else
8765 {
8766 /* Normal char, go one level deeper. */
8767 tword[depth++] = c;
8768 arridx[depth] = idxs[n];
8769 curi[depth] = 1;
8770 wordcount[depth] = 0;
8771 }
8772 }
8773 }
8774
8775 smsg((char_u *)_("Total number of words: %d"), words_done);
8776
8777 return OK;
8778}
8779
8780/*
8781 * Make the table that links each word in the soundfold trie to the words it
8782 * can be produced from.
8783 * This is not unlike lines in a file, thus use a memfile to be able to access
8784 * the table efficiently.
8785 * Returns FAIL when out of memory.
8786 */
8787 static int
8788sug_maketable(spin)
8789 spellinfo_T *spin;
8790{
8791 garray_T ga;
8792 int res = OK;
8793
8794 /* Allocate a buffer, open a memline for it and create the swap file
8795 * (uses a temp file, not a .swp file). */
8796 spin->si_spellbuf = open_spellbuf();
8797 if (spin->si_spellbuf == NULL)
8798 return FAIL;
8799
8800 /* Use a buffer to store the line info, avoids allocating many small
8801 * pieces of memory. */
8802 ga_init2(&ga, 1, 100);
8803
8804 /* recursively go through the tree */
8805 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8806 res = FAIL;
8807
8808 ga_clear(&ga);
8809 return res;
8810}
8811
8812/*
8813 * Fill the table for one node and its children.
8814 * Returns the wordnr at the start of the node.
8815 * Returns -1 when out of memory.
8816 */
8817 static int
8818sug_filltable(spin, node, startwordnr, gap)
8819 spellinfo_T *spin;
8820 wordnode_T *node;
8821 int startwordnr;
8822 garray_T *gap; /* place to store line of numbers */
8823{
8824 wordnode_T *p, *np;
8825 int wordnr = startwordnr;
8826 int nr;
8827 int prev_nr;
8828
8829 for (p = node; p != NULL; p = p->wn_sibling)
8830 {
8831 if (p->wn_byte == NUL)
8832 {
8833 gap->ga_len = 0;
8834 prev_nr = 0;
8835 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8836 {
8837 if (ga_grow(gap, 10) == FAIL)
8838 return -1;
8839
8840 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8841 /* Compute the offset from the previous nr and store the
8842 * offset in a way that it takes a minimum number of bytes.
8843 * It's a bit like utf-8, but without the need to mark
8844 * following bytes. */
8845 nr -= prev_nr;
8846 prev_nr += nr;
8847 gap->ga_len += offset2bytes(nr,
8848 (char_u *)gap->ga_data + gap->ga_len);
8849 }
8850
8851 /* add the NUL byte */
8852 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8853
8854 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8855 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8856 return -1;
8857 ++wordnr;
8858
8859 /* Remove extra NUL entries, we no longer need them. We don't
8860 * bother freeing the nodes, the won't be reused anyway. */
8861 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8862 p->wn_sibling = p->wn_sibling->wn_sibling;
8863
8864 /* Clear the flags on the remaining NUL node, so that compression
8865 * works a lot better. */
8866 p->wn_flags = 0;
8867 p->wn_region = 0;
8868 }
8869 else
8870 {
8871 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8872 if (wordnr == -1)
8873 return -1;
8874 }
8875 }
8876 return wordnr;
8877}
8878
8879/*
8880 * Convert an offset into a minimal number of bytes.
8881 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8882 * bytes.
8883 */
8884 static int
8885offset2bytes(nr, buf)
8886 int nr;
8887 char_u *buf;
8888{
8889 int rem;
8890 int b1, b2, b3, b4;
8891
8892 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8893 b1 = nr % 255 + 1;
8894 rem = nr / 255;
8895 b2 = rem % 255 + 1;
8896 rem = rem / 255;
8897 b3 = rem % 255 + 1;
8898 b4 = rem / 255 + 1;
8899
8900 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8901 {
8902 buf[0] = 0xe0 + b4;
8903 buf[1] = b3;
8904 buf[2] = b2;
8905 buf[3] = b1;
8906 return 4;
8907 }
8908 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8909 {
8910 buf[0] = 0xc0 + b3;
8911 buf[1] = b2;
8912 buf[2] = b1;
8913 return 3;
8914 }
8915 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8916 {
8917 buf[0] = 0x80 + b2;
8918 buf[1] = b1;
8919 return 2;
8920 }
8921 /* 1 byte */
8922 buf[0] = b1;
8923 return 1;
8924}
8925
8926/*
8927 * Opposite of offset2bytes().
8928 * "pp" points to the bytes and is advanced over it.
8929 * Returns the offset.
8930 */
8931 static int
8932bytes2offset(pp)
8933 char_u **pp;
8934{
8935 char_u *p = *pp;
8936 int nr;
8937 int c;
8938
8939 c = *p++;
8940 if ((c & 0x80) == 0x00) /* 1 byte */
8941 {
8942 nr = c - 1;
8943 }
8944 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8945 {
8946 nr = (c & 0x3f) - 1;
8947 nr = nr * 255 + (*p++ - 1);
8948 }
8949 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8950 {
8951 nr = (c & 0x1f) - 1;
8952 nr = nr * 255 + (*p++ - 1);
8953 nr = nr * 255 + (*p++ - 1);
8954 }
8955 else /* 4 bytes */
8956 {
8957 nr = (c & 0x0f) - 1;
8958 nr = nr * 255 + (*p++ - 1);
8959 nr = nr * 255 + (*p++ - 1);
8960 nr = nr * 255 + (*p++ - 1);
8961 }
8962
8963 *pp = p;
8964 return nr;
8965}
8966
8967/*
8968 * Write the .sug file in "fname".
8969 */
8970 static void
8971sug_write(spin, fname)
8972 spellinfo_T *spin;
8973 char_u *fname;
8974{
8975 FILE *fd;
8976 wordnode_T *tree;
8977 int nodecount;
8978 int wcount;
8979 char_u *line;
8980 linenr_T lnum;
8981 int len;
8982
8983 /* Create the file. Note that an existing file is silently overwritten! */
8984 fd = mch_fopen((char *)fname, "w");
8985 if (fd == NULL)
8986 {
8987 EMSG2(_(e_notopen), fname);
8988 return;
8989 }
8990
8991 vim_snprintf((char *)IObuff, IOSIZE,
8992 _("Writing suggestion file %s ..."), fname);
8993 spell_message(spin, IObuff);
8994
8995 /*
8996 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8997 */
8998 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8999 {
9000 EMSG(_(e_write));
9001 goto theend;
9002 }
9003 putc(VIMSUGVERSION, fd); /* <versionnr> */
9004
9005 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02009006 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009007
9008 /*
9009 * <SUGWORDTREE>
9010 */
9011 spin->si_memtot = 0;
9012 tree = spin->si_foldroot->wn_sibling;
9013
9014 /* Clear the index and wnode fields in the tree. */
9015 clear_node(tree);
9016
9017 /* Count the number of nodes. Needed to be able to allocate the
9018 * memory when reading the nodes. Also fills in index for shared
9019 * nodes. */
9020 nodecount = put_node(NULL, tree, 0, 0, FALSE);
9021
9022 /* number of nodes in 4 bytes */
9023 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9024 spin->si_memtot += nodecount + nodecount * sizeof(int);
9025
9026 /* Write the nodes. */
9027 (void)put_node(fd, tree, 0, 0, FALSE);
9028
9029 /*
9030 * <SUGTABLE>: <sugwcount> <sugline> ...
9031 */
9032 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9033 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9034
9035 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9036 {
9037 /* <sugline>: <sugnr> ... NUL */
9038 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009039 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009040 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9041 {
9042 EMSG(_(e_write));
9043 goto theend;
9044 }
9045 spin->si_memtot += len;
9046 }
9047
9048 /* Write another byte to check for errors. */
9049 if (putc(0, fd) == EOF)
9050 EMSG(_(e_write));
9051
9052 vim_snprintf((char *)IObuff, IOSIZE,
9053 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9054 spell_message(spin, IObuff);
9055
9056theend:
9057 /* close the file */
9058 fclose(fd);
9059}
9060
9061/*
9062 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9063 * list and only contains text lines. Can use a swapfile to reduce memory
9064 * use.
9065 * Most other fields are invalid! Esp. watch out for string options being
9066 * NULL and there is no undo info.
9067 * Returns NULL when out of memory.
9068 */
9069 static buf_T *
9070open_spellbuf()
9071{
9072 buf_T *buf;
9073
9074 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9075 if (buf != NULL)
9076 {
9077 buf->b_spell = TRUE;
9078 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02009079#ifdef FEAT_CRYPT
9080 buf->b_p_key = empty_option;
9081#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00009082 ml_open(buf);
9083 ml_open_file(buf); /* create swap file now */
9084 }
9085 return buf;
9086}
9087
9088/*
9089 * Close the buffer used for spell info.
9090 */
9091 static void
9092close_spellbuf(buf)
9093 buf_T *buf;
9094{
9095 if (buf != NULL)
9096 {
9097 ml_close(buf, TRUE);
9098 vim_free(buf);
9099 }
9100}
9101
9102
9103/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009104 * Create a Vim spell file from one or more word lists.
9105 * "fnames[0]" is the output file name.
9106 * "fnames[fcount - 1]" is the last input file name.
9107 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9108 * and ".spl" is appended to make the output file name.
9109 */
9110 static void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009111mkspell(fcount, fnames, ascii, over_write, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009112 int fcount;
9113 char_u **fnames;
9114 int ascii; /* -ascii argument given */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009115 int over_write; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009116 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009117{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009118 char_u *fname = NULL;
9119 char_u *wfname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009120 char_u **innames;
9121 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009122 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009123 int i;
9124 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009125 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009126 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009127 spellinfo_T spin;
9128
9129 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009131 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 spin.si_followup = TRUE;
9133 spin.si_rem_accents = TRUE;
9134 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009135 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9137 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009138 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009139 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009140 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009141 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009142
Bram Moolenaarb765d632005-06-07 21:00:02 +00009143 /* default: fnames[0] is output file, following are input files */
9144 innames = &fnames[1];
9145 incount = fcount - 1;
9146
Bram Moolenaard9462e32011-04-11 21:35:11 +02009147 wfname = alloc(MAXPATHL);
9148 if (wfname == NULL)
9149 return;
9150
Bram Moolenaarb765d632005-06-07 21:00:02 +00009151 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009152 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009153 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009154 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9155 {
9156 /* For ":mkspell path/en.latin1.add" output file is
9157 * "path/en.latin1.add.spl". */
9158 innames = &fnames[0];
9159 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009160 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009161 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009162 else if (fcount == 1)
9163 {
9164 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9165 innames = &fnames[0];
9166 incount = 1;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009167 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009168 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009169 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009170 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9171 {
9172 /* Name ends in ".spl", use as the file name. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009173 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009174 }
9175 else
9176 /* Name should be language, make the file name from it. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009177 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaar56f78042010-12-08 17:09:32 +01009178 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009179
9180 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009181 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009182 spin.si_ascii = TRUE;
9183
9184 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009185 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009186 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009187 }
9188
Bram Moolenaarb765d632005-06-07 21:00:02 +00009189 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009190 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009191 else if (vim_strchr(gettail(wfname), '_') != NULL)
9192 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009193 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009194 EMSG(_("E754: Only up to 8 regions supported"));
9195 else
9196 {
9197 /* Check for overwriting before doing things that may take a lot of
9198 * time. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009199 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009200 {
9201 EMSG(_(e_exists));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009202 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009203 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009204 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009205 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009206 EMSG2(_(e_isadir2), wfname);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009207 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009208 }
9209
Bram Moolenaard9462e32011-04-11 21:35:11 +02009210 fname = alloc(MAXPATHL);
9211 if (fname == NULL)
9212 goto theend;
9213
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009214 /*
9215 * Init the aff and dic pointers.
9216 * Get the region names if there are more than 2 arguments.
9217 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009218 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009219 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009220 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009221
Bram Moolenaar3982c542005-06-08 21:56:31 +00009222 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009224 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009225 if (STRLEN(gettail(innames[i])) < 5
9226 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009227 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009228 EMSG2(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009229 goto theend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009230 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009231 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9232 spin.si_region_name[i * 2 + 1] =
9233 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009234 }
9235 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009236 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009237
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009238 spin.si_foldroot = wordtree_alloc(&spin);
9239 spin.si_keeproot = wordtree_alloc(&spin);
9240 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009241 if (spin.si_foldroot == NULL
9242 || spin.si_keeproot == NULL
9243 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009244 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009245 free_blocks(spin.si_blocks);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009246 goto theend;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009247 }
9248
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009249 /* When not producing a .add.spl file clear the character table when
9250 * we encounter one in the .aff file. This means we dump the current
9251 * one in the .spl file if the .aff file doesn't define one. That's
9252 * better than guessing the contents, the table will match a
9253 * previously loaded spell file. */
9254 if (!spin.si_add)
9255 spin.si_clear_chartab = TRUE;
9256
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009257 /*
9258 * Read all the .aff and .dic files.
9259 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009260 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009261 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009262 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009263 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009264 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009265 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009266
Bram Moolenaard9462e32011-04-11 21:35:11 +02009267 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009268 if (mch_stat((char *)fname, &st) >= 0)
9269 {
9270 /* Read the .aff file. Will init "spin->si_conv" based on the
9271 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009272 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009273 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009274 error = TRUE;
9275 else
9276 {
9277 /* Read the .dic file and store the words in the trees. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009278 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009279 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009280 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009281 error = TRUE;
9282 }
9283 }
9284 else
9285 {
9286 /* No .aff file, try reading the file as a word list. Store
9287 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009288 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009289 error = TRUE;
9290 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009291
Bram Moolenaarb765d632005-06-07 21:00:02 +00009292#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009293 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009294 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009295#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009296 }
9297
Bram Moolenaar78622822005-08-23 21:00:13 +00009298 if (spin.si_compflags != NULL && spin.si_nobreak)
9299 MSG(_("Warning: both compounding and NOBREAK specified"));
9300
Bram Moolenaar4770d092006-01-12 23:22:24 +00009301 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009302 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009303 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009304 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009305 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009306 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009307 wordtree_compress(&spin, spin.si_foldroot);
9308 wordtree_compress(&spin, spin.si_keeproot);
9309 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009310 }
9311
Bram Moolenaar4770d092006-01-12 23:22:24 +00009312 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009313 {
9314 /*
9315 * Write the info in the spell file.
9316 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009317 vim_snprintf((char *)IObuff, IOSIZE,
9318 _("Writing spell file %s ..."), wfname);
9319 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009320
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009321 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009322
Bram Moolenaar4770d092006-01-12 23:22:24 +00009323 spell_message(&spin, (char_u *)_("Done!"));
9324 vim_snprintf((char *)IObuff, IOSIZE,
9325 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9326 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009327
Bram Moolenaar4770d092006-01-12 23:22:24 +00009328 /*
9329 * If the file is loaded need to reload it.
9330 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009331 if (!error)
9332 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009333 }
9334
9335 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009336 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009337 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 ga_clear(&spin.si_sal);
9339 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009340 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009341 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009342 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009343
9344 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009345 for (i = 0; i < incount; ++i)
9346 if (afile[i] != NULL)
9347 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009348
9349 /* Free all the bits and pieces at once. */
9350 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009351
9352 /*
9353 * If there is soundfolding info and no NOSUGFILE item create the
9354 * .sug file with the soundfolded word trie.
9355 */
9356 if (spin.si_sugtime != 0 && !error && !got_int)
9357 spell_make_sugfile(&spin, wfname);
9358
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009359 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009360
9361theend:
9362 vim_free(fname);
9363 vim_free(wfname);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009364}
9365
Bram Moolenaar4770d092006-01-12 23:22:24 +00009366/*
9367 * Display a message for spell file processing when 'verbose' is set or using
9368 * ":mkspell". "str" can be IObuff.
9369 */
9370 static void
9371spell_message(spin, str)
9372 spellinfo_T *spin;
9373 char_u *str;
9374{
9375 if (spin->si_verbose || p_verbose > 2)
9376 {
9377 if (!spin->si_verbose)
9378 verbose_enter();
9379 MSG(str);
9380 out_flush();
9381 if (!spin->si_verbose)
9382 verbose_leave();
9383 }
9384}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009385
9386/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009387 * ":[count]spellgood {word}"
9388 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009389 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009390 */
9391 void
9392ex_spell(eap)
9393 exarg_T *eap;
9394{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009395 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009396 eap->forceit ? 0 : (int)eap->line2,
9397 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009398}
9399
9400/*
9401 * Add "word[len]" to 'spellfile' as a good or bad word.
9402 */
9403 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009404spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009405 char_u *word;
9406 int len;
9407 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009408 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009409 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009410 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009411{
Bram Moolenaara3917072006-09-14 08:48:14 +00009412 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009413 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009414 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009415 char_u *fname;
Bram Moolenaard9462e32011-04-11 21:35:11 +02009416 char_u *fnamebuf = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009417 char_u line[MAXWLEN * 2];
9418 long fpos, fpos_next = 0;
9419 int i;
9420 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009421
Bram Moolenaar89d40322006-08-29 15:30:07 +00009422 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009423 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009424 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009425 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009426 int_wordlist = vim_tempname('s');
9427 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009428 return;
9429 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009430 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009431 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009432 else
9433 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009434 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009435 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009436 {
9437 init_spellfile();
9438 new_spf = TRUE;
9439 }
9440
Bram Moolenaar860cae12010-06-05 23:22:07 +02009441 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009442 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009443 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009444 return;
9445 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009446 fnamebuf = alloc(MAXPATHL);
9447 if (fnamebuf == NULL)
9448 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009449
Bram Moolenaar860cae12010-06-05 23:22:07 +02009450 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009451 {
9452 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009453 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009454 break;
9455 if (*spf == NUL)
9456 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009457 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009458 vim_free(fnamebuf);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009459 return;
9460 }
9461 }
9462
Bram Moolenaarb765d632005-06-07 21:00:02 +00009463 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009464 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009465 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9466 buf = NULL;
9467 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009468 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009469 EMSG(_(e_bufloaded));
Bram Moolenaard9462e32011-04-11 21:35:11 +02009470 vim_free(fnamebuf);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009471 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009472 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009473
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009474 fname = fnamebuf;
9475 }
9476
Bram Moolenaard0131a82006-03-04 21:46:13 +00009477 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009478 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009479 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009480 * since its flags sort before the one with WF_BANNED. */
9481 fd = mch_fopen((char *)fname, "r");
9482 if (fd != NULL)
9483 {
9484 while (!vim_fgets(line, MAXWLEN * 2, fd))
9485 {
9486 fpos = fpos_next;
9487 fpos_next = ftell(fd);
9488 if (STRNCMP(word, line, len) == 0
9489 && (line[len] == '/' || line[len] < ' '))
9490 {
9491 /* Found duplicate word. Remove it by writing a '#' at
9492 * the start of the line. Mixing reading and writing
9493 * doesn't work for all systems, close the file first. */
9494 fclose(fd);
9495 fd = mch_fopen((char *)fname, "r+");
9496 if (fd == NULL)
9497 break;
9498 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009499 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009500 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009501 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009502 {
9503 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009504 smsg((char_u *)_("Word '%.*s' removed from %s"),
9505 len, word, NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009506 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009507 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009508 fseek(fd, fpos_next, SEEK_SET);
9509 }
9510 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009511 if (fd != NULL)
9512 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009513 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009514 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009515
9516 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009517 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009518 fd = mch_fopen((char *)fname, "a");
9519 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009520 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009521 char_u *p;
9522
Bram Moolenaard0131a82006-03-04 21:46:13 +00009523 /* We just initialized the 'spellfile' option and can't open the
9524 * file. We may need to create the "spell" directory first. We
9525 * already checked the runtime directory is writable in
9526 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009527 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009528 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009529 int c = *p;
9530
Bram Moolenaard0131a82006-03-04 21:46:13 +00009531 /* The directory doesn't exist. Try creating it and opening
9532 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009533 *p = NUL;
9534 vim_mkdir(fname, 0755);
9535 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009536 fd = mch_fopen((char *)fname, "a");
9537 }
9538 }
9539
9540 if (fd == NULL)
9541 EMSG2(_(e_notopen), fname);
9542 else
9543 {
9544 if (bad)
9545 fprintf(fd, "%.*s/!\n", len, word);
9546 else
9547 fprintf(fd, "%.*s\n", len, word);
9548 fclose(fd);
9549
9550 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar134bf072013-09-25 18:54:24 +02009551 smsg((char_u *)_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009552 }
9553 }
9554
Bram Moolenaard0131a82006-03-04 21:46:13 +00009555 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009556 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009557 /* Update the .add.spl file. */
9558 mkspell(1, &fname, FALSE, TRUE, TRUE);
9559
9560 /* If the .add file is edited somewhere, reload it. */
9561 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009562 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009563
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009564 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009565 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009566 vim_free(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009567}
9568
9569/*
9570 * Initialize 'spellfile' for the current buffer.
9571 */
9572 static void
9573init_spellfile()
9574{
Bram Moolenaard9462e32011-04-11 21:35:11 +02009575 char_u *buf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009576 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009577 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009578 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009579 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009580 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009581 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009582
Bram Moolenaar860cae12010-06-05 23:22:07 +02009583 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009584 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009585 buf = alloc(MAXPATHL);
9586 if (buf == NULL)
9587 return;
9588
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009589 /* Find the end of the language name. Exclude the region. If there
9590 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009591 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009592 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009593 if (vim_ispathsep(*lend))
9594 {
9595 aspath = TRUE;
9596 lstart = lend + 1;
9597 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009598
9599 /* Loop over all entries in 'runtimepath'. Use the first one where we
9600 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009601 rtp = p_rtp;
9602 while (*rtp != NUL)
9603 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009604 if (aspath)
9605 /* Use directory of an entry with path, e.g., for
9606 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009607 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9608 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009609 else
9610 /* Copy the path from 'runtimepath' to buf[]. */
9611 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009612 if (filewritable(buf) == 2)
9613 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009614 /* Use the first language name from 'spelllang' and the
9615 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009616 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009617 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9618 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009619 else
9620 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009621 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009622 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009623 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009624 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009625 vim_mkdir(buf, 0755);
9626
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009627 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009628 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009629 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009630 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009631 l = (int)STRLEN(buf);
Bram Moolenaard9462e32011-04-11 21:35:11 +02009632 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9633 ->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009634 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9635 fname != NULL
9636 && strstr((char *)gettail(fname), ".ascii.") != NULL
9637 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009638 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9639 break;
9640 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009641 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009642 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02009643
9644 vim_free(buf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009645 }
9646}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009647
Bram Moolenaar51485f02005-06-04 21:55:20 +00009648
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009649/*
9650 * Init the chartab used for spelling for ASCII.
9651 * EBCDIC is not supported!
9652 */
9653 static void
9654clear_spell_chartab(sp)
9655 spelltab_T *sp;
9656{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009657 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009658
9659 /* Init everything to FALSE. */
9660 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9661 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9662 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009663 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009664 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009665 sp->st_upper[i] = i;
9666 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009667
9668 /* We include digits. A word shouldn't start with a digit, but handling
9669 * that is done separately. */
9670 for (i = '0'; i <= '9'; ++i)
9671 sp->st_isw[i] = TRUE;
9672 for (i = 'A'; i <= 'Z'; ++i)
9673 {
9674 sp->st_isw[i] = TRUE;
9675 sp->st_isu[i] = TRUE;
9676 sp->st_fold[i] = i + 0x20;
9677 }
9678 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009679 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009680 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009681 sp->st_upper[i] = i - 0x20;
9682 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009683}
9684
9685/*
9686 * Init the chartab used for spelling. Only depends on 'encoding'.
9687 * Called once while starting up and when 'encoding' changes.
9688 * The default is to use isalpha(), but the spell file should define the word
9689 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009690 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009691 */
9692 void
9693init_spell_chartab()
9694{
9695 int i;
9696
9697 did_set_spelltab = FALSE;
9698 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009699#ifdef FEAT_MBYTE
9700 if (enc_dbcs)
9701 {
9702 /* DBCS: assume double-wide characters are word characters. */
9703 for (i = 128; i <= 255; ++i)
9704 if (MB_BYTE2LEN(i) == 2)
9705 spelltab.st_isw[i] = TRUE;
9706 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009707 else if (enc_utf8)
9708 {
9709 for (i = 128; i < 256; ++i)
9710 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009711 int f = utf_fold(i);
9712 int u = utf_toupper(i);
9713
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009714 spelltab.st_isu[i] = utf_isupper(i);
9715 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009716 /* The folded/upper-cased value is different between latin1 and
9717 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9718 * value for utf-8 to avoid this. */
9719 spelltab.st_fold[i] = (f < 256) ? f : i;
9720 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009721 }
9722 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009723 else
9724#endif
9725 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009726 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009727 for (i = 128; i < 256; ++i)
9728 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009729 if (MB_ISUPPER(i))
9730 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009731 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009732 spelltab.st_isu[i] = TRUE;
9733 spelltab.st_fold[i] = MB_TOLOWER(i);
9734 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009735 else if (MB_ISLOWER(i))
9736 {
9737 spelltab.st_isw[i] = TRUE;
9738 spelltab.st_upper[i] = MB_TOUPPER(i);
9739 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009740 }
9741 }
9742}
9743
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009744/*
9745 * Set the spell character tables from strings in the affix file.
9746 */
9747 static int
9748set_spell_chartab(fol, low, upp)
9749 char_u *fol;
9750 char_u *low;
9751 char_u *upp;
9752{
9753 /* We build the new tables here first, so that we can compare with the
9754 * previous one. */
9755 spelltab_T new_st;
9756 char_u *pf = fol, *pl = low, *pu = upp;
9757 int f, l, u;
9758
9759 clear_spell_chartab(&new_st);
9760
9761 while (*pf != NUL)
9762 {
9763 if (*pl == NUL || *pu == NUL)
9764 {
9765 EMSG(_(e_affform));
9766 return FAIL;
9767 }
9768#ifdef FEAT_MBYTE
9769 f = mb_ptr2char_adv(&pf);
9770 l = mb_ptr2char_adv(&pl);
9771 u = mb_ptr2char_adv(&pu);
9772#else
9773 f = *pf++;
9774 l = *pl++;
9775 u = *pu++;
9776#endif
9777 /* Every character that appears is a word character. */
9778 if (f < 256)
9779 new_st.st_isw[f] = TRUE;
9780 if (l < 256)
9781 new_st.st_isw[l] = TRUE;
9782 if (u < 256)
9783 new_st.st_isw[u] = TRUE;
9784
9785 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9786 * case-folding */
9787 if (l < 256 && l != f)
9788 {
9789 if (f >= 256)
9790 {
9791 EMSG(_(e_affrange));
9792 return FAIL;
9793 }
9794 new_st.st_fold[l] = f;
9795 }
9796
9797 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009798 * case-folding, it's upper case and the "UPP" is the upper case of
9799 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009800 if (u < 256 && u != f)
9801 {
9802 if (f >= 256)
9803 {
9804 EMSG(_(e_affrange));
9805 return FAIL;
9806 }
9807 new_st.st_fold[u] = f;
9808 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009809 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009810 }
9811 }
9812
9813 if (*pl != NUL || *pu != NUL)
9814 {
9815 EMSG(_(e_affform));
9816 return FAIL;
9817 }
9818
9819 return set_spell_finish(&new_st);
9820}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009821
9822/*
9823 * Set the spell character tables from strings in the .spl file.
9824 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009825 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009826set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009827 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009828 int cnt; /* length of "flags" */
9829 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009830{
9831 /* We build the new tables here first, so that we can compare with the
9832 * previous one. */
9833 spelltab_T new_st;
9834 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009835 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009836 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009837
9838 clear_spell_chartab(&new_st);
9839
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009840 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009841 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009842 if (i < cnt)
9843 {
9844 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9845 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9846 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009847
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009848 if (*p != NUL)
9849 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009850#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009851 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009852#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009853 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009854#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009855 new_st.st_fold[i + 128] = c;
9856 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9857 new_st.st_upper[c] = i + 128;
9858 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009859 }
9860
Bram Moolenaar5195e452005-08-19 20:32:47 +00009861 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009862}
9863
9864 static int
9865set_spell_finish(new_st)
9866 spelltab_T *new_st;
9867{
9868 int i;
9869
9870 if (did_set_spelltab)
9871 {
9872 /* check that it's the same table */
9873 for (i = 0; i < 256; ++i)
9874 {
9875 if (spelltab.st_isw[i] != new_st->st_isw[i]
9876 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009877 || spelltab.st_fold[i] != new_st->st_fold[i]
9878 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009879 {
9880 EMSG(_("E763: Word characters differ between spell files"));
9881 return FAIL;
9882 }
9883 }
9884 }
9885 else
9886 {
9887 /* copy the new spelltab into the one being used */
9888 spelltab = *new_st;
9889 did_set_spelltab = TRUE;
9890 }
9891
9892 return OK;
9893}
9894
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009895/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009896 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009897 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009898 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009899 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009900 */
9901 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009902spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009903 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009904 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009905{
Bram Moolenaarea408852005-06-25 22:49:46 +00009906#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009907 char_u *s;
9908 int l;
9909 int c;
9910
9911 if (has_mbyte)
9912 {
9913 l = MB_BYTE2LEN(*p);
9914 s = p;
9915 if (l == 1)
9916 {
9917 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009918 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009919 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009920 }
9921 else
9922 {
9923 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009924 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9925 : (wp->w_s->b_spell_ismw_mb != NULL
9926 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009927 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009928 }
9929
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009930 c = mb_ptr2char(s);
9931 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009932 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009933 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009934 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009935#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009936
Bram Moolenaar860cae12010-06-05 23:22:07 +02009937 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009938}
9939
9940/*
9941 * Return TRUE if "p" points to a word character.
9942 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9943 */
9944 static int
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009945spell_iswordp_nmw(p, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009946 char_u *p;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009947 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009948{
9949#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009950 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009951
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009952 if (has_mbyte)
9953 {
9954 c = mb_ptr2char(p);
9955 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009956 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009957 return spelltab.st_isw[c];
9958 }
9959#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009960 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009961}
9962
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009963#ifdef FEAT_MBYTE
9964/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009965 * Return TRUE if word class indicates a word character.
9966 * Only for characters above 255.
9967 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009968 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009969 */
9970 static int
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009971spell_mb_isword_class(cl, wp)
9972 int cl;
9973 win_T *wp;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009974{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009975 if (wp->w_s->b_cjk)
9976 /* East Asian characters are not considered word characters. */
9977 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009978 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9979}
9980
9981/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009982 * Return TRUE if "p" points to a word character.
9983 * Wide version of spell_iswordp().
9984 */
9985 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009986spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009987 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009988 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009989{
9990 int *s;
9991
Bram Moolenaar860cae12010-06-05 23:22:07 +02009992 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9993 : (wp->w_s->b_spell_ismw_mb != NULL
9994 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009995 s = p + 1;
9996 else
9997 s = p;
9998
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009999 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010000 {
10001 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010002 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010003 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010004 return spell_mb_isword_class(
10005 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010006 return 0;
10007 }
10008 return spelltab.st_isw[*s];
10009}
10010#endif
10011
Bram Moolenaarea408852005-06-25 22:49:46 +000010012/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010013 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010014 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010015 */
Bram Moolenaar5195e452005-08-19 20:32:47 +000010016 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010017write_spell_prefcond(fd, gap)
10018 FILE *fd;
10019 garray_T *gap;
10020{
10021 int i;
10022 char_u *p;
10023 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010024 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +000010025 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010026
Bram Moolenaar5195e452005-08-19 20:32:47 +000010027 if (fd != NULL)
10028 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
10029
10030 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010031
10032 for (i = 0; i < gap->ga_len; ++i)
10033 {
10034 /* <prefcond> : <condlen> <condstr> */
10035 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +000010036 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010037 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010038 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010039 if (fd != NULL)
10040 {
10041 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +000010042 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010043 }
10044 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010045 }
Bram Moolenaar5195e452005-08-19 20:32:47 +000010046 else if (fd != NULL)
10047 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010048 }
10049
Bram Moolenaar5195e452005-08-19 20:32:47 +000010050 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010051}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010052
10053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010054 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
10055 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010056 * When using a multi-byte 'encoding' the length may change!
10057 * Returns FAIL when something wrong.
10058 */
10059 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010060spell_casefold(str, len, buf, buflen)
10061 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010062 int len;
10063 char_u *buf;
10064 int buflen;
10065{
10066 int i;
10067
10068 if (len >= buflen)
10069 {
10070 buf[0] = NUL;
10071 return FAIL; /* result will not fit */
10072 }
10073
10074#ifdef FEAT_MBYTE
10075 if (has_mbyte)
10076 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010077 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010078 char_u *p;
10079 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010080
10081 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010082 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010083 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010084 if (outi + MB_MAXBYTES > buflen)
10085 {
10086 buf[outi] = NUL;
10087 return FAIL;
10088 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010089 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010090 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010091 }
10092 buf[outi] = NUL;
10093 }
10094 else
10095#endif
10096 {
10097 /* Be quick for non-multibyte encodings. */
10098 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010099 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010100 buf[i] = NUL;
10101 }
10102
10103 return OK;
10104}
10105
Bram Moolenaar4770d092006-01-12 23:22:24 +000010106/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010107#define SPS_BEST 1
10108#define SPS_FAST 2
10109#define SPS_DOUBLE 4
10110
Bram Moolenaar4770d092006-01-12 23:22:24 +000010111static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10112static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010113
10114/*
10115 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010116 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010117 */
10118 int
10119spell_check_sps()
10120{
10121 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010122 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010123 char_u buf[MAXPATHL];
10124 int f;
10125
10126 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010127 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010128
10129 for (p = p_sps; *p != NUL; )
10130 {
10131 copy_option_part(&p, buf, MAXPATHL, ",");
10132
10133 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010134 if (VIM_ISDIGIT(*buf))
10135 {
10136 s = buf;
10137 sps_limit = getdigits(&s);
10138 if (*s != NUL && !VIM_ISDIGIT(*s))
10139 f = -1;
10140 }
10141 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010142 f = SPS_BEST;
10143 else if (STRCMP(buf, "fast") == 0)
10144 f = SPS_FAST;
10145 else if (STRCMP(buf, "double") == 0)
10146 f = SPS_DOUBLE;
10147 else if (STRNCMP(buf, "expr:", 5) != 0
10148 && STRNCMP(buf, "file:", 5) != 0)
10149 f = -1;
10150
10151 if (f == -1 || (sps_flags != 0 && f != 0))
10152 {
10153 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010154 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010155 return FAIL;
10156 }
10157 if (f != 0)
10158 sps_flags = f;
10159 }
10160
10161 if (sps_flags == 0)
10162 sps_flags = SPS_BEST;
10163
10164 return OK;
10165}
10166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167/*
Bram Moolenaar134bf072013-09-25 18:54:24 +020010168 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010170 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010171 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 */
10173 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010174spell_suggest(count)
10175 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176{
10177 char_u *line;
10178 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 char_u wcopy[MAXWLEN + 2];
10180 char_u *p;
10181 int i;
10182 int c;
10183 suginfo_T sug;
10184 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010185 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010186 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010187 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010188 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010189 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010190 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010192 if (no_spell_checking(curwin))
10193 return;
10194
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010195 if (VIsual_active)
10196 {
10197 /* Use the Visually selected text as the bad word. But reject
10198 * a multi-line selection. */
10199 if (curwin->w_cursor.lnum != VIsual.lnum)
10200 {
10201 vim_beep();
10202 return;
10203 }
10204 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10205 if (badlen < 0)
10206 badlen = -badlen;
10207 else
10208 curwin->w_cursor.col = VIsual.col;
10209 ++badlen;
10210 end_visual_mode();
10211 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010212 /* Find the start of the badly spelled word. */
10213 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010214 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010216 /* No bad word or it starts after the cursor: use the word under the
10217 * cursor. */
10218 curwin->w_cursor = prev_cursor;
10219 line = ml_get_curline();
10220 p = line + curwin->w_cursor.col;
10221 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010222 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010223 mb_ptr_back(line, p);
10224 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010225 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010226 mb_ptr_adv(p);
10227
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010228 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010229 {
10230 beep_flush();
10231 return;
10232 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010233 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 }
10235
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010236 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010238 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010239 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010240
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010241 /* Make a copy of current line since autocommands may free the line. */
10242 line = vim_strsave(ml_get_curline());
10243 if (line == NULL)
10244 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010245
Bram Moolenaar5195e452005-08-19 20:32:47 +000010246 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10247 * 'spellsuggest', whatever is smaller. */
10248 if (sps_limit > (int)Rows - 2)
10249 limit = (int)Rows - 2;
10250 else
10251 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010252 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010253 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010254
10255 if (sug.su_ga.ga_len == 0)
10256 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010257 else if (count > 0)
10258 {
10259 if (count > sug.su_ga.ga_len)
10260 smsg((char_u *)_("Sorry, only %ld suggestions"),
10261 (long)sug.su_ga.ga_len);
10262 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010263 else
10264 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010265 vim_free(repl_from);
10266 repl_from = NULL;
10267 vim_free(repl_to);
10268 repl_to = NULL;
10269
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010270#ifdef FEAT_RIGHTLEFT
10271 /* When 'rightleft' is set the list is drawn right-left. */
10272 cmdmsg_rl = curwin->w_p_rl;
10273 if (cmdmsg_rl)
10274 msg_col = Columns - 1;
10275#endif
10276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 /* List the suggestions. */
10278 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010279 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010280 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10282 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010283#ifdef FEAT_RIGHTLEFT
10284 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10285 {
10286 /* And now the rabbit from the high hat: Avoid showing the
10287 * untranslated message rightleft. */
10288 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10289 sug.su_badlen, sug.su_badptr);
10290 }
10291#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 msg_puts(IObuff);
10293 msg_clr_eos();
10294 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 msg_scroll = TRUE;
10297 for (i = 0; i < sug.su_ga.ga_len; ++i)
10298 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010299 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300
10301 /* The suggested word may replace only part of the bad word, add
10302 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020010303 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010305 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 sug.su_badptr + stp->st_orglen,
10307 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010308 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10309#ifdef FEAT_RIGHTLEFT
10310 if (cmdmsg_rl)
10311 rl_mirror(IObuff);
10312#endif
10313 msg_puts(IObuff);
10314
10315 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010316 msg_puts(IObuff);
10317
10318 /* The word may replace more than "su_badlen". */
10319 if (sug.su_badlen < stp->st_orglen)
10320 {
10321 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10322 stp->st_orglen, sug.su_badptr);
10323 msg_puts(IObuff);
10324 }
10325
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010326 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010327 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010328 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010329 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010330 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010331 stp->st_salscore ? "s " : "",
10332 stp->st_score, stp->st_altscore);
10333 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010334 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010335 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010336#ifdef FEAT_RIGHTLEFT
10337 if (cmdmsg_rl)
10338 /* Mirror the numbers, but keep the leading space. */
10339 rl_mirror(IObuff + 1);
10340#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010341 msg_advance(30);
10342 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 msg_putchar('\n');
10345 }
10346
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010347#ifdef FEAT_RIGHTLEFT
10348 cmdmsg_rl = FALSE;
10349 msg_col = 0;
10350#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010351 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010352 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010353 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010354 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010355 lines_left = Rows; /* avoid more prompt */
10356 /* don't delay for 'smd' in normal_cmd() */
10357 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010358 }
10359
Bram Moolenaard12a1322005-08-21 22:08:24 +000010360 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10361 {
10362 /* Save the from and to text for :spellrepall. */
10363 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010364 if (sug.su_badlen > stp->st_orglen)
10365 {
10366 /* Replacing less than "su_badlen", append the remainder to
10367 * repl_to. */
10368 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10369 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10370 sug.su_badlen - stp->st_orglen,
10371 sug.su_badptr + stp->st_orglen);
10372 repl_to = vim_strsave(IObuff);
10373 }
10374 else
10375 {
10376 /* Replacing su_badlen or more, use the whole word. */
10377 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10378 repl_to = vim_strsave(stp->st_word);
10379 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010380
10381 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010382 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10383 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010384 if (p != NULL)
10385 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010386 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010387 mch_memmove(p, line, c);
10388 STRCPY(p + c, stp->st_word);
10389 STRCAT(p, sug.su_badptr + stp->st_orglen);
10390 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10391 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010392
10393 /* For redo we use a change-word command. */
10394 ResetRedobuff();
10395 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010396 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010397 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010398 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010399
10400 /* After this "p" may be invalid. */
10401 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010402 }
10403 }
10404 else
10405 curwin->w_cursor = prev_cursor;
10406
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010407 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010408skip:
10409 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010410}
10411
10412/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010413 * Check if the word at line "lnum" column "col" is required to start with a
10414 * capital. This uses 'spellcapcheck' of the current buffer.
10415 */
10416 static int
10417check_need_cap(lnum, col)
10418 linenr_T lnum;
10419 colnr_T col;
10420{
10421 int need_cap = FALSE;
10422 char_u *line;
10423 char_u *line_copy = NULL;
10424 char_u *p;
10425 colnr_T endcol;
10426 regmatch_T regmatch;
10427
Bram Moolenaar860cae12010-06-05 23:22:07 +020010428 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010429 return FALSE;
10430
10431 line = ml_get_curline();
10432 endcol = 0;
10433 if ((int)(skipwhite(line) - line) >= (int)col)
10434 {
10435 /* At start of line, check if previous line is empty or sentence
10436 * ends there. */
10437 if (lnum == 1)
10438 need_cap = TRUE;
10439 else
10440 {
10441 line = ml_get(lnum - 1);
10442 if (*skipwhite(line) == NUL)
10443 need_cap = TRUE;
10444 else
10445 {
10446 /* Append a space in place of the line break. */
10447 line_copy = concat_str(line, (char_u *)" ");
10448 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010449 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010450 }
10451 }
10452 }
10453 else
10454 endcol = col;
10455
10456 if (endcol > 0)
10457 {
10458 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010459 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010460 regmatch.rm_ic = FALSE;
10461 p = line + endcol;
10462 for (;;)
10463 {
10464 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010010465 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010466 break;
10467 if (vim_regexec(&regmatch, p, 0)
10468 && regmatch.endp[0] == line + endcol)
10469 {
10470 need_cap = TRUE;
10471 break;
10472 }
10473 }
10474 }
10475
10476 vim_free(line_copy);
10477
10478 return need_cap;
10479}
10480
10481
10482/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010483 * ":spellrepall"
10484 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010485 void
10486ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010487 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010488{
10489 pos_T pos = curwin->w_cursor;
10490 char_u *frompat;
10491 int addlen;
10492 char_u *line;
10493 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010494 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010495 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010496
10497 if (repl_from == NULL || repl_to == NULL)
10498 {
10499 EMSG(_("E752: No previous spell replacement"));
10500 return;
10501 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010502 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010503
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010504 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010505 if (frompat == NULL)
10506 return;
10507 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10508 p_ws = FALSE;
10509
Bram Moolenaar5195e452005-08-19 20:32:47 +000010510 sub_nsubs = 0;
10511 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010512 curwin->w_cursor.lnum = 0;
10513 while (!got_int)
10514 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010515 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010516 || u_save_cursor() == FAIL)
10517 break;
10518
10519 /* Only replace when the right word isn't there yet. This happens
10520 * when changing "etc" to "etc.". */
10521 line = ml_get_curline();
10522 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10523 repl_to, STRLEN(repl_to)) != 0)
10524 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010525 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010526 if (p == NULL)
10527 break;
10528 mch_memmove(p, line, curwin->w_cursor.col);
10529 STRCPY(p + curwin->w_cursor.col, repl_to);
10530 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10531 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10532 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010533
10534 if (curwin->w_cursor.lnum != prev_lnum)
10535 {
10536 ++sub_nlines;
10537 prev_lnum = curwin->w_cursor.lnum;
10538 }
10539 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010540 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010541 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010542 }
10543
10544 p_ws = save_ws;
10545 curwin->w_cursor = pos;
10546 vim_free(frompat);
10547
Bram Moolenaar5195e452005-08-19 20:32:47 +000010548 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010549 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010550 else
10551 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010552}
10553
10554/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010555 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10556 * a list of allocated strings.
10557 */
10558 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010559spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010560 garray_T *gap;
10561 char_u *word;
10562 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010563 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010564 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010565{
10566 suginfo_T sug;
10567 int i;
10568 suggest_T *stp;
10569 char_u *wcopy;
10570
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010571 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010572
10573 /* Make room in "gap". */
10574 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010575 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010576 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010577 for (i = 0; i < sug.su_ga.ga_len; ++i)
10578 {
10579 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010580
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010581 /* The suggested word may replace only part of "word", add the not
10582 * replaced part. */
10583 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010584 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010585 if (wcopy == NULL)
10586 break;
10587 STRCPY(wcopy, stp->st_word);
10588 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10589 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10590 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010591 }
10592
10593 spell_find_cleanup(&sug);
10594}
10595
10596/*
10597 * Find spell suggestions for the word at the start of "badptr".
10598 * Return the suggestions in "su->su_ga".
10599 * The maximum number of suggestions is "maxcount".
10600 * Note: does use info for the current window.
10601 * This is based on the mechanisms of Aspell, but completely reimplemented.
10602 */
10603 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010604spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010605 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010606 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010607 suginfo_T *su;
10608 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010609 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010610 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010611 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010612{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010613 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010614 char_u buf[MAXPATHL];
10615 char_u *p;
10616 int do_combine = FALSE;
10617 char_u *sps_copy;
10618#ifdef FEAT_EVAL
10619 static int expr_busy = FALSE;
10620#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010621 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010622 int i;
10623 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010624
10625 /*
10626 * Set the info in "*su".
10627 */
10628 vim_memset(su, 0, sizeof(suginfo_T));
10629 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10630 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010631 if (*badptr == NUL)
10632 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010633 hash_init(&su->su_banned);
10634
10635 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010636 if (badlen != 0)
10637 su->su_badlen = badlen;
10638 else
10639 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010640 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010641 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010642
10643 if (su->su_badlen >= MAXWLEN)
10644 su->su_badlen = MAXWLEN - 1; /* just in case */
10645 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10646 (void)spell_casefold(su->su_badptr, su->su_badlen,
10647 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010648 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010649 su->su_badflags = badword_captype(su->su_badptr,
10650 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010651 if (need_cap)
10652 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010653
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010654 /* Find the default language for sound folding. We simply use the first
10655 * one in 'spelllang' that supports sound folding. That's good for when
10656 * using multiple files for one language, it's not that bad when mixing
10657 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010658 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010659 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010660 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010661 if (lp->lp_sallang != NULL)
10662 {
10663 su->su_sallang = lp->lp_sallang;
10664 break;
10665 }
10666 }
10667
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010668 /* Soundfold the bad word with the default sound folding, so that we don't
10669 * have to do this many times. */
10670 if (su->su_sallang != NULL)
10671 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10672 su->su_sal_badword);
10673
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010674 /* If the word is not capitalised and spell_check() doesn't consider the
10675 * word to be bad then it might need to be capitalised. Add a suggestion
10676 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010677 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010678 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010679 {
10680 make_case_word(su->su_badword, buf, WF_ONECAP);
10681 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010682 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010683 }
10684
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010685 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010686 if (banbadword)
10687 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010688
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010689 /* Make a copy of 'spellsuggest', because the expression may change it. */
10690 sps_copy = vim_strsave(p_sps);
10691 if (sps_copy == NULL)
10692 return;
10693
10694 /* Loop over the items in 'spellsuggest'. */
10695 for (p = sps_copy; *p != NUL; )
10696 {
10697 copy_option_part(&p, buf, MAXPATHL, ",");
10698
10699 if (STRNCMP(buf, "expr:", 5) == 0)
10700 {
10701#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010702 /* Evaluate an expression. Skip this when called recursively,
10703 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010704 if (!expr_busy)
10705 {
10706 expr_busy = TRUE;
10707 spell_suggest_expr(su, buf + 5);
10708 expr_busy = FALSE;
10709 }
10710#endif
10711 }
10712 else if (STRNCMP(buf, "file:", 5) == 0)
10713 /* Use list of suggestions in a file. */
10714 spell_suggest_file(su, buf + 5);
10715 else
10716 {
10717 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010718 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010719 if (sps_flags & SPS_DOUBLE)
10720 do_combine = TRUE;
10721 }
10722 }
10723
10724 vim_free(sps_copy);
10725
10726 if (do_combine)
10727 /* Combine the two list of suggestions. This must be done last,
10728 * because sorting changes the order again. */
10729 score_combine(su);
10730}
10731
10732#ifdef FEAT_EVAL
10733/*
10734 * Find suggestions by evaluating expression "expr".
10735 */
10736 static void
10737spell_suggest_expr(su, expr)
10738 suginfo_T *su;
10739 char_u *expr;
10740{
10741 list_T *list;
10742 listitem_T *li;
10743 int score;
10744 char_u *p;
10745
10746 /* The work is split up in a few parts to avoid having to export
10747 * suginfo_T.
10748 * First evaluate the expression and get the resulting list. */
10749 list = eval_spell_expr(su->su_badword, expr);
10750 if (list != NULL)
10751 {
10752 /* Loop over the items in the list. */
10753 for (li = list->lv_first; li != NULL; li = li->li_next)
10754 if (li->li_tv.v_type == VAR_LIST)
10755 {
10756 /* Get the word and the score from the items. */
10757 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010758 if (score >= 0 && score <= su->su_maxscore)
10759 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10760 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010761 }
10762 list_unref(list);
10763 }
10764
Bram Moolenaar4770d092006-01-12 23:22:24 +000010765 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10766 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010767 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10768}
10769#endif
10770
10771/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010772 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010773 */
10774 static void
10775spell_suggest_file(su, fname)
10776 suginfo_T *su;
10777 char_u *fname;
10778{
10779 FILE *fd;
10780 char_u line[MAXWLEN * 2];
10781 char_u *p;
10782 int len;
10783 char_u cword[MAXWLEN];
10784
10785 /* Open the file. */
10786 fd = mch_fopen((char *)fname, "r");
10787 if (fd == NULL)
10788 {
10789 EMSG2(_(e_notopen), fname);
10790 return;
10791 }
10792
10793 /* Read it line by line. */
10794 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10795 {
10796 line_breakcheck();
10797
10798 p = vim_strchr(line, '/');
10799 if (p == NULL)
10800 continue; /* No Tab found, just skip the line. */
10801 *p++ = NUL;
10802 if (STRICMP(su->su_badword, line) == 0)
10803 {
10804 /* Match! Isolate the good word, until CR or NL. */
10805 for (len = 0; p[len] >= ' '; ++len)
10806 ;
10807 p[len] = NUL;
10808
10809 /* If the suggestion doesn't have specific case duplicate the case
10810 * of the bad word. */
10811 if (captype(p, NULL) == 0)
10812 {
10813 make_case_word(p, cword, su->su_badflags);
10814 p = cword;
10815 }
10816
10817 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010818 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010819 }
10820 }
10821
10822 fclose(fd);
10823
Bram Moolenaar4770d092006-01-12 23:22:24 +000010824 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10825 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010826 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10827}
10828
10829/*
10830 * Find suggestions for the internal method indicated by "sps_flags".
10831 */
10832 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010833spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010834 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010835 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010836{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010837 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010838 * Load the .sug file(s) that are available and not done yet.
10839 */
10840 suggest_load_files();
10841
10842 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010843 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010844 *
10845 * Set a maximum score to limit the combination of operations that is
10846 * tried.
10847 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010848 suggest_try_special(su);
10849
10850 /*
10851 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10852 * from the .aff file and inserting a space (split the word).
10853 */
10854 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010855
10856 /* For the resulting top-scorers compute the sound-a-like score. */
10857 if (sps_flags & SPS_DOUBLE)
10858 score_comp_sal(su);
10859
10860 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010861 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010862 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010863 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010864 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010865 if (sps_flags & SPS_BEST)
10866 /* Adjust the word score for the suggestions found so far for how
10867 * they sounds like. */
10868 rescore_suggestions(su);
10869
10870 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010871 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010872 * for the soundfold word, limits the changes that are being tried,
10873 * and "su_sfmaxscore" the rescored score, which is set by
10874 * cleanup_suggestions().
10875 * First find words with a small edit distance, because this is much
10876 * faster and often already finds the top-N suggestions. If we didn't
10877 * find many suggestions try again with a higher edit distance.
10878 * "sl_sounddone" is used to avoid doing the same word twice.
10879 */
10880 suggest_try_soundalike_prep();
10881 su->su_maxscore = SCORE_SFMAX1;
10882 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010883 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010884 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10885 {
10886 /* We didn't find enough matches, try again, allowing more
10887 * changes to the soundfold word. */
10888 su->su_maxscore = SCORE_SFMAX2;
10889 suggest_try_soundalike(su);
10890 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10891 {
10892 /* Still didn't find enough matches, try again, allowing even
10893 * more changes to the soundfold word. */
10894 su->su_maxscore = SCORE_SFMAX3;
10895 suggest_try_soundalike(su);
10896 }
10897 }
10898 su->su_maxscore = su->su_sfmaxscore;
10899 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010900 }
10901
Bram Moolenaar4770d092006-01-12 23:22:24 +000010902 /* When CTRL-C was hit while searching do show the results. Only clear
10903 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010904 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010905 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010906 {
10907 (void)vgetc();
10908 got_int = FALSE;
10909 }
10910
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010911 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010912 {
10913 if (sps_flags & SPS_BEST)
10914 /* Adjust the word score for how it sounds like. */
10915 rescore_suggestions(su);
10916
Bram Moolenaar4770d092006-01-12 23:22:24 +000010917 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10918 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010919 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010920 }
10921}
10922
10923/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010924 * Load the .sug files for languages that have one and weren't loaded yet.
10925 */
10926 static void
10927suggest_load_files()
10928{
10929 langp_T *lp;
10930 int lpi;
10931 slang_T *slang;
10932 char_u *dotp;
10933 FILE *fd;
10934 char_u buf[MAXWLEN];
10935 int i;
10936 time_t timestamp;
10937 int wcount;
10938 int wordnr;
10939 garray_T ga;
10940 int c;
10941
10942 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010943 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010944 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010945 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010946 slang = lp->lp_slang;
10947 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10948 {
10949 /* Change ".spl" to ".sug" and open the file. When the file isn't
10950 * found silently skip it. Do set "sl_sugloaded" so that we
10951 * don't try again and again. */
10952 slang->sl_sugloaded = TRUE;
10953
10954 dotp = vim_strrchr(slang->sl_fname, '.');
10955 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10956 continue;
10957 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010958 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010959 if (fd == NULL)
10960 goto nextone;
10961
10962 /*
10963 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10964 */
10965 for (i = 0; i < VIMSUGMAGICL; ++i)
10966 buf[i] = getc(fd); /* <fileID> */
10967 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10968 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010969 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010970 slang->sl_fname);
10971 goto nextone;
10972 }
10973 c = getc(fd); /* <versionnr> */
10974 if (c < VIMSUGVERSION)
10975 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010976 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010977 slang->sl_fname);
10978 goto nextone;
10979 }
10980 else if (c > VIMSUGVERSION)
10981 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010982 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010983 slang->sl_fname);
10984 goto nextone;
10985 }
10986
10987 /* Check the timestamp, it must be exactly the same as the one in
10988 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010989 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010990 if (timestamp != slang->sl_sugtime)
10991 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010992 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010993 slang->sl_fname);
10994 goto nextone;
10995 }
10996
10997 /*
10998 * <SUGWORDTREE>: <wordtree>
10999 * Read the trie with the soundfolded words.
11000 */
11001 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
11002 FALSE, 0) != 0)
11003 {
11004someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000011005 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011006 slang->sl_fname);
11007 slang_clear_sug(slang);
11008 goto nextone;
11009 }
11010
11011 /*
11012 * <SUGTABLE>: <sugwcount> <sugline> ...
11013 *
11014 * Read the table with word numbers. We use a file buffer for
11015 * this, because it's so much like a file with lines. Makes it
11016 * possible to swap the info and save on memory use.
11017 */
11018 slang->sl_sugbuf = open_spellbuf();
11019 if (slang->sl_sugbuf == NULL)
11020 goto someerror;
11021 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000011022 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011023 if (wcount < 0)
11024 goto someerror;
11025
11026 /* Read all the wordnr lists into the buffer, one NUL terminated
11027 * list per line. */
11028 ga_init2(&ga, 1, 100);
11029 for (wordnr = 0; wordnr < wcount; ++wordnr)
11030 {
11031 ga.ga_len = 0;
11032 for (;;)
11033 {
11034 c = getc(fd); /* <sugline> */
11035 if (c < 0 || ga_grow(&ga, 1) == FAIL)
11036 goto someerror;
11037 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
11038 if (c == NUL)
11039 break;
11040 }
11041 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
11042 ga.ga_data, ga.ga_len, TRUE) == FAIL)
11043 goto someerror;
11044 }
11045 ga_clear(&ga);
11046
11047 /*
11048 * Need to put word counts in the word tries, so that we can find
11049 * a word by its number.
11050 */
11051 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
11052 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
11053
11054nextone:
11055 if (fd != NULL)
11056 fclose(fd);
11057 STRCPY(dotp, ".spl");
11058 }
11059 }
11060}
11061
11062
11063/*
11064 * Fill in the wordcount fields for a trie.
11065 * Returns the total number of words.
11066 */
11067 static void
11068tree_count_words(byts, idxs)
11069 char_u *byts;
11070 idx_T *idxs;
11071{
11072 int depth;
11073 idx_T arridx[MAXWLEN];
11074 int curi[MAXWLEN];
11075 int c;
11076 idx_T n;
11077 int wordcount[MAXWLEN];
11078
11079 arridx[0] = 0;
11080 curi[0] = 1;
11081 wordcount[0] = 0;
11082 depth = 0;
11083 while (depth >= 0 && !got_int)
11084 {
11085 if (curi[depth] > byts[arridx[depth]])
11086 {
11087 /* Done all bytes at this node, go up one level. */
11088 idxs[arridx[depth]] = wordcount[depth];
11089 if (depth > 0)
11090 wordcount[depth - 1] += wordcount[depth];
11091
11092 --depth;
11093 fast_breakcheck();
11094 }
11095 else
11096 {
11097 /* Do one more byte at this node. */
11098 n = arridx[depth] + curi[depth];
11099 ++curi[depth];
11100
11101 c = byts[n];
11102 if (c == 0)
11103 {
11104 /* End of word, count it. */
11105 ++wordcount[depth];
11106
11107 /* Skip over any other NUL bytes (same word with different
11108 * flags). */
11109 while (byts[n + 1] == 0)
11110 {
11111 ++n;
11112 ++curi[depth];
11113 }
11114 }
11115 else
11116 {
11117 /* Normal char, go one level deeper to count the words. */
11118 ++depth;
11119 arridx[depth] = idxs[n];
11120 curi[depth] = 1;
11121 wordcount[depth] = 0;
11122 }
11123 }
11124 }
11125}
11126
11127/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011128 * Free the info put in "*su" by spell_find_suggest().
11129 */
11130 static void
11131spell_find_cleanup(su)
11132 suginfo_T *su;
11133{
11134 int i;
11135
11136 /* Free the suggestions. */
11137 for (i = 0; i < su->su_ga.ga_len; ++i)
11138 vim_free(SUG(su->su_ga, i).st_word);
11139 ga_clear(&su->su_ga);
11140 for (i = 0; i < su->su_sga.ga_len; ++i)
11141 vim_free(SUG(su->su_sga, i).st_word);
11142 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143
11144 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011145 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146}
11147
11148/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011149 * Make a copy of "word", with the first letter upper or lower cased, to
11150 * "wcopy[MAXWLEN]". "word" must not be empty.
11151 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 */
11153 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011154onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 char_u *wcopy;
11157 int upper; /* TRUE: first letter made upper case */
11158{
11159 char_u *p;
11160 int c;
11161 int l;
11162
11163 p = word;
11164#ifdef FEAT_MBYTE
11165 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011166 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167 else
11168#endif
11169 c = *p++;
11170 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011171 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011172 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011173 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174#ifdef FEAT_MBYTE
11175 if (has_mbyte)
11176 l = mb_char2bytes(c, wcopy);
11177 else
11178#endif
11179 {
11180 l = 1;
11181 wcopy[0] = c;
11182 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011183 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184}
11185
11186/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011187 * Make a copy of "word" with all the letters upper cased into
11188 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 */
11190 static void
11191allcap_copy(word, wcopy)
11192 char_u *word;
11193 char_u *wcopy;
11194{
11195 char_u *s;
11196 char_u *d;
11197 int c;
11198
11199 d = wcopy;
11200 for (s = word; *s != NUL; )
11201 {
11202#ifdef FEAT_MBYTE
11203 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011204 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 else
11206#endif
11207 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011208
11209#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +020011210 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +000011211 * would cause weird errors in other 8-bit encodings. */
11212 if (enc_latin1like && c == 0xdf)
11213 {
11214 c = 'S';
11215 if (d - wcopy >= MAXWLEN - 1)
11216 break;
11217 *d++ = c;
11218 }
11219 else
11220#endif
11221 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222
11223#ifdef FEAT_MBYTE
11224 if (has_mbyte)
11225 {
11226 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11227 break;
11228 d += mb_char2bytes(c, d);
11229 }
11230 else
11231#endif
11232 {
11233 if (d - wcopy >= MAXWLEN - 1)
11234 break;
11235 *d++ = c;
11236 }
11237 }
11238 *d = NUL;
11239}
11240
11241/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011242 * Try finding suggestions by recognizing specific situations.
11243 */
11244 static void
11245suggest_try_special(su)
11246 suginfo_T *su;
11247{
11248 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011249 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011250 int c;
11251 char_u word[MAXWLEN];
11252
11253 /*
11254 * Recognize a word that is repeated: "the the".
11255 */
11256 p = skiptowhite(su->su_fbadword);
11257 len = p - su->su_fbadword;
11258 p = skipwhite(p);
11259 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11260 {
11261 /* Include badflags: if the badword is onecap or allcap
11262 * use that for the goodword too: "The the" -> "The". */
11263 c = su->su_fbadword[len];
11264 su->su_fbadword[len] = NUL;
11265 make_case_word(su->su_fbadword, word, su->su_badflags);
11266 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011267
11268 /* Give a soundalike score of 0, compute the score as if deleting one
11269 * character. */
11270 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011271 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011272 }
11273}
11274
11275/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 * Try finding suggestions by adding/removing/swapping letters.
11277 */
11278 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011279suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011280 suginfo_T *su;
11281{
11282 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011283 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011285 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011286 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287
11288 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011289 * to find matches (esp. REP items). Append some more text, changing
11290 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011292 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011293 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011294 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011295
Bram Moolenaar860cae12010-06-05 23:22:07 +020011296 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011298 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011299
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011300 /* If reloading a spell file fails it's still in the list but
11301 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011302 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011303 continue;
11304
Bram Moolenaar4770d092006-01-12 23:22:24 +000011305 /* Try it for this language. Will add possible suggestions. */
11306 suggest_trie_walk(su, lp, fword, FALSE);
11307 }
11308}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309
Bram Moolenaar4770d092006-01-12 23:22:24 +000011310/* Check the maximum score, if we go over it we won't try this change. */
11311#define TRY_DEEPER(su, stack, depth, add) \
11312 (stack[depth].ts_score + (add) < su->su_maxscore)
11313
11314/*
11315 * Try finding suggestions by adding/removing/swapping letters.
11316 *
11317 * This uses a state machine. At each node in the tree we try various
11318 * operations. When trying if an operation works "depth" is increased and the
11319 * stack[] is used to store info. This allows combinations, thus insert one
11320 * character, replace one and delete another. The number of changes is
11321 * limited by su->su_maxscore.
11322 *
11323 * After implementing this I noticed an article by Kemal Oflazer that
11324 * describes something similar: "Error-tolerant Finite State Recognition with
11325 * Applications to Morphological Analysis and Spelling Correction" (1996).
11326 * The implementation in the article is simplified and requires a stack of
11327 * unknown depth. The implementation here only needs a stack depth equal to
11328 * the length of the word.
11329 *
11330 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11331 * The mechanism is the same, but we find a match with a sound-folded word
11332 * that comes from one or more original words. Each of these words may be
11333 * added, this is done by add_sound_suggest().
11334 * Don't use:
11335 * the prefix tree or the keep-case tree
11336 * "su->su_badlen"
11337 * anything to do with upper and lower case
11338 * anything to do with word or non-word characters ("spell_iswordp()")
11339 * banned words
11340 * word flags (rare, region, compounding)
11341 * word splitting for now
11342 * "similar_chars()"
11343 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11344 */
11345 static void
11346suggest_trie_walk(su, lp, fword, soundfold)
11347 suginfo_T *su;
11348 langp_T *lp;
11349 char_u *fword;
11350 int soundfold;
11351{
11352 char_u tword[MAXWLEN]; /* good word collected so far */
11353 trystate_T stack[MAXWLEN];
11354 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011355 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011356 * words and split word. NUL terminated
11357 * when going deeper but not when coming
11358 * back. */
11359 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11360 trystate_T *sp;
11361 int newscore;
11362 int score;
11363 char_u *byts, *fbyts, *pbyts;
11364 idx_T *idxs, *fidxs, *pidxs;
11365 int depth;
11366 int c, c2, c3;
11367 int n = 0;
11368 int flags;
11369 garray_T *gap;
11370 idx_T arridx;
11371 int len;
11372 char_u *p;
11373 fromto_T *ftp;
11374 int fl = 0, tl;
11375 int repextra = 0; /* extra bytes in fword[] from REP item */
11376 slang_T *slang = lp->lp_slang;
11377 int fword_ends;
11378 int goodword_ends;
11379#ifdef DEBUG_TRIEWALK
11380 /* Stores the name of the change made at each level. */
11381 char_u changename[MAXWLEN][80];
11382#endif
11383 int breakcheckcount = 1000;
11384 int compound_ok;
11385
11386 /*
11387 * Go through the whole case-fold tree, try changes at each node.
11388 * "tword[]" contains the word collected from nodes in the tree.
11389 * "fword[]" the word we are trying to match with (initially the bad
11390 * word).
11391 */
11392 depth = 0;
11393 sp = &stack[0];
11394 vim_memset(sp, 0, sizeof(trystate_T));
11395 sp->ts_curi = 1;
11396
11397 if (soundfold)
11398 {
11399 /* Going through the soundfold tree. */
11400 byts = fbyts = slang->sl_sbyts;
11401 idxs = fidxs = slang->sl_sidxs;
11402 pbyts = NULL;
11403 pidxs = NULL;
11404 sp->ts_prefixdepth = PFD_NOPREFIX;
11405 sp->ts_state = STATE_START;
11406 }
11407 else
11408 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011409 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011410 * When there are postponed prefixes we need to use these first. At
11411 * the end of the prefix we continue in the case-fold tree.
11412 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011413 fbyts = slang->sl_fbyts;
11414 fidxs = slang->sl_fidxs;
11415 pbyts = slang->sl_pbyts;
11416 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011417 if (pbyts != NULL)
11418 {
11419 byts = pbyts;
11420 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011421 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011422 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11423 }
11424 else
11425 {
11426 byts = fbyts;
11427 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011428 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011429 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011430 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011431 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011432
Bram Moolenaar4770d092006-01-12 23:22:24 +000011433 /*
11434 * Loop to find all suggestions. At each round we either:
11435 * - For the current state try one operation, advance "ts_curi",
11436 * increase "depth".
11437 * - When a state is done go to the next, set "ts_state".
11438 * - When all states are tried decrease "depth".
11439 */
11440 while (depth >= 0 && !got_int)
11441 {
11442 sp = &stack[depth];
11443 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011444 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011445 case STATE_START:
11446 case STATE_NOPREFIX:
11447 /*
11448 * Start of node: Deal with NUL bytes, which means
11449 * tword[] may end here.
11450 */
11451 arridx = sp->ts_arridx; /* current node in the tree */
11452 len = byts[arridx]; /* bytes in this node */
11453 arridx += sp->ts_curi; /* index of current byte */
11454
11455 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011456 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011457 /* Skip over the NUL bytes, we use them later. */
11458 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11459 ;
11460 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461
Bram Moolenaar4770d092006-01-12 23:22:24 +000011462 /* Always past NUL bytes now. */
11463 n = (int)sp->ts_state;
11464 sp->ts_state = STATE_ENDNUL;
11465 sp->ts_save_badflags = su->su_badflags;
11466
11467 /* At end of a prefix or at start of prefixtree: check for
11468 * following word. */
11469 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011470 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011471 /* Set su->su_badflags to the caps type at this position.
11472 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011473#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011474 if (has_mbyte)
11475 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11476 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011477#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011478 n = sp->ts_fidx;
11479 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11480 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011481 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011482#ifdef DEBUG_TRIEWALK
11483 sprintf(changename[depth], "prefix");
11484#endif
11485 go_deeper(stack, depth, 0);
11486 ++depth;
11487 sp = &stack[depth];
11488 sp->ts_prefixdepth = depth - 1;
11489 byts = fbyts;
11490 idxs = fidxs;
11491 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011492
Bram Moolenaar4770d092006-01-12 23:22:24 +000011493 /* Move the prefix to preword[] with the right case
11494 * and make find_keepcap_word() works. */
11495 tword[sp->ts_twordlen] = NUL;
11496 make_case_word(tword + sp->ts_splitoff,
11497 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011498 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011499 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011500 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011501 break;
11502 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011503
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 if (sp->ts_curi > len || byts[arridx] != 0)
11505 {
11506 /* Past bytes in node and/or past NUL bytes. */
11507 sp->ts_state = STATE_ENDNUL;
11508 sp->ts_save_badflags = su->su_badflags;
11509 break;
11510 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511
Bram Moolenaar4770d092006-01-12 23:22:24 +000011512 /*
11513 * End of word in tree.
11514 */
11515 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011516
Bram Moolenaar4770d092006-01-12 23:22:24 +000011517 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011518
11519 /* Skip words with the NOSUGGEST flag. */
11520 if (flags & WF_NOSUGGEST)
11521 break;
11522
Bram Moolenaar4770d092006-01-12 23:22:24 +000011523 fword_ends = (fword[sp->ts_fidx] == NUL
11524 || (soundfold
11525 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011526 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011527 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528
Bram Moolenaar4770d092006-01-12 23:22:24 +000011529 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011530 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011531 {
11532 /* There was a prefix before the word. Check that the prefix
11533 * can be used with this word. */
11534 /* Count the length of the NULs in the prefix. If there are
11535 * none this must be the first try without a prefix. */
11536 n = stack[sp->ts_prefixdepth].ts_arridx;
11537 len = pbyts[n++];
11538 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11539 ;
11540 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011541 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011542 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011543 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011544 if (c == 0)
11545 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011546
Bram Moolenaar4770d092006-01-12 23:22:24 +000011547 /* Use the WF_RARE flag for a rare prefix. */
11548 if (c & WF_RAREPFX)
11549 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011550
Bram Moolenaar4770d092006-01-12 23:22:24 +000011551 /* Tricky: when checking for both prefix and compounding
11552 * we run into the prefix flag first.
11553 * Remember that it's OK, so that we accept the prefix
11554 * when arriving at a compound flag. */
11555 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011556 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011557 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011558
Bram Moolenaar4770d092006-01-12 23:22:24 +000011559 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11560 * appending another compound word below. */
11561 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011562 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 goodword_ends = FALSE;
11564 else
11565 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011566
Bram Moolenaar4770d092006-01-12 23:22:24 +000011567 p = NULL;
11568 compound_ok = TRUE;
11569 if (sp->ts_complen > sp->ts_compsplit)
11570 {
11571 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011572 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011573 /* There was a word before this word. When there was no
11574 * change in this word (it was correct) add the first word
11575 * as a suggestion. If this word was corrected too, we
11576 * need to check if a correct word follows. */
11577 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011578 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011579 && STRNCMP(fword + sp->ts_splitfidx,
11580 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011581 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011582 {
11583 preword[sp->ts_prewordlen] = NUL;
11584 newscore = score_wordcount_adj(slang, sp->ts_score,
11585 preword + sp->ts_prewordlen,
11586 sp->ts_prewordlen > 0);
11587 /* Add the suggestion if the score isn't too bad. */
11588 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011589 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011590 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 newscore, 0, FALSE,
11592 lp->lp_sallang, FALSE);
11593 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011594 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011595 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011596 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011597 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011598 /* There was a compound word before this word. If this
11599 * word does not support compounding then give up
11600 * (splitting is tried for the word without compound
11601 * flag). */
11602 if (((unsigned)flags >> 24) == 0
11603 || sp->ts_twordlen - sp->ts_splitoff
11604 < slang->sl_compminlen)
11605 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011606#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011607 /* For multi-byte chars check character length against
11608 * COMPOUNDMIN. */
11609 if (has_mbyte
11610 && slang->sl_compminlen > 0
11611 && mb_charlen(tword + sp->ts_splitoff)
11612 < slang->sl_compminlen)
11613 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011614#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011615
Bram Moolenaar4770d092006-01-12 23:22:24 +000011616 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11617 compflags[sp->ts_complen + 1] = NUL;
11618 vim_strncpy(preword + sp->ts_prewordlen,
11619 tword + sp->ts_splitoff,
11620 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011621
11622 /* Verify CHECKCOMPOUNDPATTERN rules. */
11623 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11624 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011625 compound_ok = FALSE;
11626
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011627 if (compound_ok)
11628 {
11629 p = preword;
11630 while (*skiptowhite(p) != NUL)
11631 p = skipwhite(skiptowhite(p));
11632 if (fword_ends && !can_compound(slang, p,
11633 compflags + sp->ts_compsplit))
11634 /* Compound is not allowed. But it may still be
11635 * possible if we add another (short) word. */
11636 compound_ok = FALSE;
11637 }
11638
Bram Moolenaar4770d092006-01-12 23:22:24 +000011639 /* Get pointer to last char of previous word. */
11640 p = preword + sp->ts_prewordlen;
11641 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011642 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011643 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644
Bram Moolenaar4770d092006-01-12 23:22:24 +000011645 /*
11646 * Form the word with proper case in preword.
11647 * If there is a word from a previous split, append.
11648 * For the soundfold tree don't change the case, simply append.
11649 */
11650 if (soundfold)
11651 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11652 else if (flags & WF_KEEPCAP)
11653 /* Must find the word in the keep-case tree. */
11654 find_keepcap_word(slang, tword + sp->ts_splitoff,
11655 preword + sp->ts_prewordlen);
11656 else
11657 {
11658 /* Include badflags: If the badword is onecap or allcap
11659 * use that for the goodword too. But if the badword is
11660 * allcap and it's only one char long use onecap. */
11661 c = su->su_badflags;
11662 if ((c & WF_ALLCAP)
11663#ifdef FEAT_MBYTE
11664 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11665#else
11666 && su->su_badlen == 1
11667#endif
11668 )
11669 c = WF_ONECAP;
11670 c |= flags;
11671
11672 /* When appending a compound word after a word character don't
11673 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011674 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011675 c &= ~WF_ONECAP;
11676 make_case_word(tword + sp->ts_splitoff,
11677 preword + sp->ts_prewordlen, c);
11678 }
11679
11680 if (!soundfold)
11681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 /* Don't use a banned word. It may appear again as a good
11683 * word, thus remember it. */
11684 if (flags & WF_BANNED)
11685 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011686 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 break;
11688 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011689 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011690 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11691 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011692 {
11693 if (slang->sl_compprog == NULL)
11694 break;
11695 /* the word so far was banned but we may try compounding */
11696 goodword_ends = FALSE;
11697 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699
Bram Moolenaar4770d092006-01-12 23:22:24 +000011700 newscore = 0;
11701 if (!soundfold) /* soundfold words don't have flags */
11702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011704 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 newscore += SCORE_REGION;
11706 if (flags & WF_RARE)
11707 newscore += SCORE_RARE;
11708
Bram Moolenaar0c405862005-06-22 22:26:26 +000011709 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011710 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011711 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713
Bram Moolenaar4770d092006-01-12 23:22:24 +000011714 /* TODO: how about splitting in the soundfold tree? */
11715 if (fword_ends
11716 && goodword_ends
11717 && sp->ts_fidx >= sp->ts_fidxtry
11718 && compound_ok)
11719 {
11720 /* The badword also ends: add suggestions. */
11721#ifdef DEBUG_TRIEWALK
11722 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011723 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011724 int j;
11725
11726 /* print the stack of changes that brought us here */
11727 smsg("------ %s -------", fword);
11728 for (j = 0; j < depth; ++j)
11729 smsg("%s", changename[j]);
11730 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011731#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011732 if (soundfold)
11733 {
11734 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011735 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011736 add_sound_suggest(su, preword, sp->ts_score, lp);
11737 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011738 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011739 {
11740 /* Give a penalty when changing non-word char to word
11741 * char, e.g., "thes," -> "these". */
11742 p = fword + sp->ts_fidx;
11743 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011744 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011745 {
11746 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011747 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011748 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011749 newscore += SCORE_NONWORD;
11750 }
11751
Bram Moolenaar4770d092006-01-12 23:22:24 +000011752 /* Give a bonus to words seen before. */
11753 score = score_wordcount_adj(slang,
11754 sp->ts_score + newscore,
11755 preword + sp->ts_prewordlen,
11756 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011757
Bram Moolenaar4770d092006-01-12 23:22:24 +000011758 /* Add the suggestion if the score isn't too bad. */
11759 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011760 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011761 add_suggestion(su, &su->su_ga, preword,
11762 sp->ts_fidx - repextra,
11763 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011764
11765 if (su->su_badflags & WF_MIXCAP)
11766 {
11767 /* We really don't know if the word should be
11768 * upper or lower case, add both. */
11769 c = captype(preword, NULL);
11770 if (c == 0 || c == WF_ALLCAP)
11771 {
11772 make_case_word(tword + sp->ts_splitoff,
11773 preword + sp->ts_prewordlen,
11774 c == 0 ? WF_ALLCAP : 0);
11775
11776 add_suggestion(su, &su->su_ga, preword,
11777 sp->ts_fidx - repextra,
11778 score + SCORE_ICASE, 0, FALSE,
11779 lp->lp_sallang, FALSE);
11780 }
11781 }
11782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011784 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011785
Bram Moolenaar4770d092006-01-12 23:22:24 +000011786 /*
11787 * Try word split and/or compounding.
11788 */
11789 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011790#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011791 /* Don't split halfway a character. */
11792 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011793#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011794 )
11795 {
11796 int try_compound;
11797 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011798
Bram Moolenaar4770d092006-01-12 23:22:24 +000011799 /* If past the end of the bad word don't try a split.
11800 * Otherwise try changing the next word. E.g., find
11801 * suggestions for "the the" where the second "the" is
11802 * different. It's done like a split.
11803 * TODO: word split for soundfold words */
11804 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11805 && !soundfold;
11806
11807 /* Get here in several situations:
11808 * 1. The word in the tree ends:
11809 * If the word allows compounding try that. Otherwise try
11810 * a split by inserting a space. For both check that a
11811 * valid words starts at fword[sp->ts_fidx].
11812 * For NOBREAK do like compounding to be able to check if
11813 * the next word is valid.
11814 * 2. The badword does end, but it was due to a change (e.g.,
11815 * a swap). No need to split, but do check that the
11816 * following word is valid.
11817 * 3. The badword and the word in the tree end. It may still
11818 * be possible to compound another (short) word.
11819 */
11820 try_compound = FALSE;
11821 if (!soundfold
11822 && slang->sl_compprog != NULL
11823 && ((unsigned)flags >> 24) != 0
11824 && sp->ts_twordlen - sp->ts_splitoff
11825 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011826#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011827 && (!has_mbyte
11828 || slang->sl_compminlen == 0
11829 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011830 >= slang->sl_compminlen)
11831#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011832 && (slang->sl_compsylmax < MAXWLEN
11833 || sp->ts_complen + 1 - sp->ts_compsplit
11834 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011835 && (can_be_compound(sp, slang,
11836 compflags, ((unsigned)flags >> 24))))
11837
Bram Moolenaar4770d092006-01-12 23:22:24 +000011838 {
11839 try_compound = TRUE;
11840 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11841 compflags[sp->ts_complen + 1] = NUL;
11842 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011843
Bram Moolenaar4770d092006-01-12 23:22:24 +000011844 /* For NOBREAK we never try splitting, it won't make any word
11845 * valid. */
11846 if (slang->sl_nobreak)
11847 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011848
Bram Moolenaar4770d092006-01-12 23:22:24 +000011849 /* If we could add a compound word, and it's also possible to
11850 * split at this point, do the split first and set
11851 * TSF_DIDSPLIT to avoid doing it again. */
11852 else if (!fword_ends
11853 && try_compound
11854 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11855 {
11856 try_compound = FALSE;
11857 sp->ts_flags |= TSF_DIDSPLIT;
11858 --sp->ts_curi; /* do the same NUL again */
11859 compflags[sp->ts_complen] = NUL;
11860 }
11861 else
11862 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011863
Bram Moolenaar4770d092006-01-12 23:22:24 +000011864 if (try_split || try_compound)
11865 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011866 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011867 {
11868 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011869 * words so far are valid for compounding. If there
11870 * is only one word it must not have the NEEDCOMPOUND
11871 * flag. */
11872 if (sp->ts_complen == sp->ts_compsplit
11873 && (flags & WF_NEEDCOMP))
11874 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011875 p = preword;
11876 while (*skiptowhite(p) != NUL)
11877 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011878 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011879 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011880 compflags + sp->ts_compsplit))
11881 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011882
11883 if (slang->sl_nosplitsugs)
11884 newscore += SCORE_SPLIT_NO;
11885 else
11886 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011887
11888 /* Give a bonus to words seen before. */
11889 newscore = score_wordcount_adj(slang, newscore,
11890 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011891 }
11892
Bram Moolenaar4770d092006-01-12 23:22:24 +000011893 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011894 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011895 go_deeper(stack, depth, newscore);
11896#ifdef DEBUG_TRIEWALK
11897 if (!try_compound && !fword_ends)
11898 sprintf(changename[depth], "%.*s-%s: split",
11899 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11900 else
11901 sprintf(changename[depth], "%.*s-%s: compound",
11902 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11903#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011905 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011906 sp->ts_state = STATE_SPLITUNDO;
11907
11908 ++depth;
11909 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011911 /* Append a space to preword when splitting. */
11912 if (!try_compound && !fword_ends)
11913 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011914 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011915 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011916 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011917
11918 /* If the badword has a non-word character at this
11919 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011920 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011921 * character when the word ends. But only when the
11922 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011923 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +010011924 + sp->ts_fidx,
11925 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011926 || fword_ends)
11927 && fword[sp->ts_fidx] != NUL
11928 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011929 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011930 int l;
11931
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011932#ifdef FEAT_MBYTE
11933 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011934 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011935 else
11936#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011937 l = 1;
11938 if (fword_ends)
11939 {
11940 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011941 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011942 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011943 sp->ts_prewordlen += l;
11944 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011945 }
11946 else
11947 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11948 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011949 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011950
Bram Moolenaard12a1322005-08-21 22:08:24 +000011951 /* When compounding include compound flag in
11952 * compflags[] (already set above). When splitting we
11953 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011954 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011955 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011956 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011957 sp->ts_compsplit = sp->ts_complen;
11958 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011959
Bram Moolenaar53805d12005-08-01 07:08:33 +000011960 /* set su->su_badflags to the caps type at this
11961 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011962#ifdef FEAT_MBYTE
11963 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011964 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011965 else
11966#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011967 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011968 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011969 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011972 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011973
11974 /* If there are postponed prefixes, try these too. */
11975 if (pbyts != NULL)
11976 {
11977 byts = pbyts;
11978 idxs = pidxs;
11979 sp->ts_prefixdepth = PFD_PREFIXTREE;
11980 sp->ts_state = STATE_NOPREFIX;
11981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011982 }
11983 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011984 }
11985 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986
Bram Moolenaar4770d092006-01-12 23:22:24 +000011987 case STATE_SPLITUNDO:
11988 /* Undo the changes done for word split or compound word. */
11989 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990
Bram Moolenaar4770d092006-01-12 23:22:24 +000011991 /* Continue looking for NUL bytes. */
11992 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011993
Bram Moolenaar4770d092006-01-12 23:22:24 +000011994 /* In case we went into the prefix tree. */
11995 byts = fbyts;
11996 idxs = fidxs;
11997 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011998
Bram Moolenaar4770d092006-01-12 23:22:24 +000011999 case STATE_ENDNUL:
12000 /* Past the NUL bytes in the node. */
12001 su->su_badflags = sp->ts_save_badflags;
12002 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000012003#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012004 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000012005#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012006 )
12007 {
12008 /* The badword ends, can't use STATE_PLAIN. */
12009 sp->ts_state = STATE_DEL;
12010 break;
12011 }
12012 sp->ts_state = STATE_PLAIN;
12013 /*FALLTHROUGH*/
12014
12015 case STATE_PLAIN:
12016 /*
12017 * Go over all possible bytes at this node, add each to tword[]
12018 * and use child node. "ts_curi" is the index.
12019 */
12020 arridx = sp->ts_arridx;
12021 if (sp->ts_curi > byts[arridx])
12022 {
12023 /* Done all bytes at this node, do next state. When still at
12024 * already changed bytes skip the other tricks. */
12025 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012028 sp->ts_state = STATE_FINAL;
12029 }
12030 else
12031 {
12032 arridx += sp->ts_curi++;
12033 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034
Bram Moolenaar4770d092006-01-12 23:22:24 +000012035 /* Normal byte, go one level deeper. If it's not equal to the
12036 * byte in the bad word adjust the score. But don't even try
12037 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +010012038 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +000012039 * delete + substitute. */
12040 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000012041#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012042 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012043#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012044 )
12045 newscore = 0;
12046 else
12047 newscore = SCORE_SUBST;
12048 if ((newscore == 0
12049 || (sp->ts_fidx >= sp->ts_fidxtry
12050 && ((sp->ts_flags & TSF_DIDDEL) == 0
12051 || c != fword[sp->ts_delidx])))
12052 && TRY_DEEPER(su, stack, depth, newscore))
12053 {
12054 go_deeper(stack, depth, newscore);
12055#ifdef DEBUG_TRIEWALK
12056 if (newscore > 0)
12057 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12058 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12059 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012061 sprintf(changename[depth], "%.*s-%s: accept %c",
12062 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12063 fword[sp->ts_fidx]);
12064#endif
12065 ++depth;
12066 sp = &stack[depth];
12067 ++sp->ts_fidx;
12068 tword[sp->ts_twordlen++] = c;
12069 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012070#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012071 if (newscore == SCORE_SUBST)
12072 sp->ts_isdiff = DIFF_YES;
12073 if (has_mbyte)
12074 {
12075 /* Multi-byte characters are a bit complicated to
12076 * handle: They differ when any of the bytes differ
12077 * and then their length may also differ. */
12078 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012079 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012080 /* First byte. */
12081 sp->ts_tcharidx = 0;
12082 sp->ts_tcharlen = MB_BYTE2LEN(c);
12083 sp->ts_fcharstart = sp->ts_fidx - 1;
12084 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012085 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012086 }
12087 else if (sp->ts_isdiff == DIFF_INSERT)
12088 /* When inserting trail bytes don't advance in the
12089 * bad word. */
12090 --sp->ts_fidx;
12091 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12092 {
12093 /* Last byte of character. */
12094 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012095 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012096 /* Correct ts_fidx for the byte length of the
12097 * character (we didn't check that before). */
12098 sp->ts_fidx = sp->ts_fcharstart
12099 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012100 fword[sp->ts_fcharstart]);
12101
Bram Moolenaar4770d092006-01-12 23:22:24 +000012102 /* For changing a composing character adjust
12103 * the score from SCORE_SUBST to
12104 * SCORE_SUBCOMP. */
12105 if (enc_utf8
12106 && utf_iscomposing(
12107 mb_ptr2char(tword
12108 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012109 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012110 && utf_iscomposing(
12111 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012112 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012113 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012114 SCORE_SUBST - SCORE_SUBCOMP;
12115
Bram Moolenaar4770d092006-01-12 23:22:24 +000012116 /* For a similar character adjust score from
12117 * SCORE_SUBST to SCORE_SIMILAR. */
12118 else if (!soundfold
12119 && slang->sl_has_map
12120 && similar_chars(slang,
12121 mb_ptr2char(tword
12122 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012123 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012124 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012125 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012126 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012127 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012128 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012129 else if (sp->ts_isdiff == DIFF_INSERT
12130 && sp->ts_twordlen > sp->ts_tcharlen)
12131 {
12132 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12133 c = mb_ptr2char(p);
12134 if (enc_utf8 && utf_iscomposing(c))
12135 {
12136 /* Inserting a composing char doesn't
12137 * count that much. */
12138 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12139 }
12140 else
12141 {
12142 /* If the previous character was the same,
12143 * thus doubling a character, give a bonus
12144 * to the score. Also for the soundfold
12145 * tree (might seem illogical but does
12146 * give better scores). */
12147 mb_ptr_back(tword, p);
12148 if (c == mb_ptr2char(p))
12149 sp->ts_score -= SCORE_INS
12150 - SCORE_INSDUP;
12151 }
12152 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153
Bram Moolenaar4770d092006-01-12 23:22:24 +000012154 /* Starting a new char, reset the length. */
12155 sp->ts_tcharlen = 0;
12156 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012157 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012158 else
12159#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012160 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012161 /* If we found a similar char adjust the score.
12162 * We do this after calling go_deeper() because
12163 * it's slow. */
12164 if (newscore != 0
12165 && !soundfold
12166 && slang->sl_has_map
12167 && similar_chars(slang,
12168 c, fword[sp->ts_fidx - 1]))
12169 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012170 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012171 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012172 }
12173 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174
Bram Moolenaar4770d092006-01-12 23:22:24 +000012175 case STATE_DEL:
12176#ifdef FEAT_MBYTE
12177 /* When past the first byte of a multi-byte char don't try
12178 * delete/insert/swap a character. */
12179 if (has_mbyte && sp->ts_tcharlen > 0)
12180 {
12181 sp->ts_state = STATE_FINAL;
12182 break;
12183 }
12184#endif
12185 /*
12186 * Try skipping one character in the bad word (delete it).
12187 */
12188 sp->ts_state = STATE_INS_PREP;
12189 sp->ts_curi = 1;
12190 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12191 /* Deleting a vowel at the start of a word counts less, see
12192 * soundalike_score(). */
12193 newscore = 2 * SCORE_DEL / 3;
12194 else
12195 newscore = SCORE_DEL;
12196 if (fword[sp->ts_fidx] != NUL
12197 && TRY_DEEPER(su, stack, depth, newscore))
12198 {
12199 go_deeper(stack, depth, newscore);
12200#ifdef DEBUG_TRIEWALK
12201 sprintf(changename[depth], "%.*s-%s: delete %c",
12202 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12203 fword[sp->ts_fidx]);
12204#endif
12205 ++depth;
12206
12207 /* Remember what character we deleted, so that we can avoid
12208 * inserting it again. */
12209 stack[depth].ts_flags |= TSF_DIDDEL;
12210 stack[depth].ts_delidx = sp->ts_fidx;
12211
12212 /* Advance over the character in fword[]. Give a bonus to the
12213 * score if the same character is following "nn" -> "n". It's
12214 * a bit illogical for soundfold tree but it does give better
12215 * results. */
12216#ifdef FEAT_MBYTE
12217 if (has_mbyte)
12218 {
12219 c = mb_ptr2char(fword + sp->ts_fidx);
12220 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12221 if (enc_utf8 && utf_iscomposing(c))
12222 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12223 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12224 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12225 }
12226 else
12227#endif
12228 {
12229 ++stack[depth].ts_fidx;
12230 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12231 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12232 }
12233 break;
12234 }
12235 /*FALLTHROUGH*/
12236
12237 case STATE_INS_PREP:
12238 if (sp->ts_flags & TSF_DIDDEL)
12239 {
12240 /* If we just deleted a byte then inserting won't make sense,
12241 * a substitute is always cheaper. */
12242 sp->ts_state = STATE_SWAP;
12243 break;
12244 }
12245
12246 /* skip over NUL bytes */
12247 n = sp->ts_arridx;
12248 for (;;)
12249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012250 if (sp->ts_curi > byts[n])
12251 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012252 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012254 break;
12255 }
12256 if (byts[n + sp->ts_curi] != NUL)
12257 {
12258 /* Found a byte to insert. */
12259 sp->ts_state = STATE_INS;
12260 break;
12261 }
12262 ++sp->ts_curi;
12263 }
12264 break;
12265
12266 /*FALLTHROUGH*/
12267
12268 case STATE_INS:
12269 /* Insert one byte. Repeat this for each possible byte at this
12270 * node. */
12271 n = sp->ts_arridx;
12272 if (sp->ts_curi > byts[n])
12273 {
12274 /* Done all bytes at this node, go to next state. */
12275 sp->ts_state = STATE_SWAP;
12276 break;
12277 }
12278
12279 /* Do one more byte at this node, but:
12280 * - Skip NUL bytes.
12281 * - Skip the byte if it's equal to the byte in the word,
12282 * accepting that byte is always better.
12283 */
12284 n += sp->ts_curi++;
12285 c = byts[n];
12286 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12287 /* Inserting a vowel at the start of a word counts less,
12288 * see soundalike_score(). */
12289 newscore = 2 * SCORE_INS / 3;
12290 else
12291 newscore = SCORE_INS;
12292 if (c != fword[sp->ts_fidx]
12293 && TRY_DEEPER(su, stack, depth, newscore))
12294 {
12295 go_deeper(stack, depth, newscore);
12296#ifdef DEBUG_TRIEWALK
12297 sprintf(changename[depth], "%.*s-%s: insert %c",
12298 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12299 c);
12300#endif
12301 ++depth;
12302 sp = &stack[depth];
12303 tword[sp->ts_twordlen++] = c;
12304 sp->ts_arridx = idxs[n];
12305#ifdef FEAT_MBYTE
12306 if (has_mbyte)
12307 {
12308 fl = MB_BYTE2LEN(c);
12309 if (fl > 1)
12310 {
12311 /* There are following bytes for the same character.
12312 * We must find all bytes before trying
12313 * delete/insert/swap/etc. */
12314 sp->ts_tcharlen = fl;
12315 sp->ts_tcharidx = 1;
12316 sp->ts_isdiff = DIFF_INSERT;
12317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012318 }
12319 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012320 fl = 1;
12321 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012322#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012323 {
12324 /* If the previous character was the same, thus doubling a
12325 * character, give a bonus to the score. Also for
12326 * soundfold words (illogical but does give a better
12327 * score). */
12328 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012329 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012330 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012331 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012332 }
12333 break;
12334
12335 case STATE_SWAP:
12336 /*
12337 * Swap two bytes in the bad word: "12" -> "21".
12338 * We change "fword" here, it's changed back afterwards at
12339 * STATE_UNSWAP.
12340 */
12341 p = fword + sp->ts_fidx;
12342 c = *p;
12343 if (c == NUL)
12344 {
12345 /* End of word, can't swap or replace. */
12346 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012347 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012349
Bram Moolenaar4770d092006-01-12 23:22:24 +000012350 /* Don't swap if the first character is not a word character.
12351 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012352 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012353 {
12354 sp->ts_state = STATE_REP_INI;
12355 break;
12356 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012357
Bram Moolenaar4770d092006-01-12 23:22:24 +000012358#ifdef FEAT_MBYTE
12359 if (has_mbyte)
12360 {
12361 n = mb_cptr2len(p);
12362 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012363 if (p[n] == NUL)
12364 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012365 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012366 c2 = c; /* don't swap non-word char */
12367 else
12368 c2 = mb_ptr2char(p + n);
12369 }
12370 else
12371#endif
12372 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012373 if (p[1] == NUL)
12374 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012375 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012376 c2 = c; /* don't swap non-word char */
12377 else
12378 c2 = p[1];
12379 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012380
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012381 /* When the second character is NUL we can't swap. */
12382 if (c2 == NUL)
12383 {
12384 sp->ts_state = STATE_REP_INI;
12385 break;
12386 }
12387
Bram Moolenaar4770d092006-01-12 23:22:24 +000012388 /* When characters are identical, swap won't do anything.
12389 * Also get here if the second char is not a word character. */
12390 if (c == c2)
12391 {
12392 sp->ts_state = STATE_SWAP3;
12393 break;
12394 }
12395 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12396 {
12397 go_deeper(stack, depth, SCORE_SWAP);
12398#ifdef DEBUG_TRIEWALK
12399 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12400 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12401 c, c2);
12402#endif
12403 sp->ts_state = STATE_UNSWAP;
12404 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012405#ifdef FEAT_MBYTE
12406 if (has_mbyte)
12407 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012408 fl = mb_char2len(c2);
12409 mch_memmove(p, p + n, fl);
12410 mb_char2bytes(c, p + fl);
12411 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012412 }
12413 else
12414#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012415 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012416 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012417 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012418 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012419 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012420 }
12421 else
12422 /* If this swap doesn't work then SWAP3 won't either. */
12423 sp->ts_state = STATE_REP_INI;
12424 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012425
Bram Moolenaar4770d092006-01-12 23:22:24 +000012426 case STATE_UNSWAP:
12427 /* Undo the STATE_SWAP swap: "21" -> "12". */
12428 p = fword + sp->ts_fidx;
12429#ifdef FEAT_MBYTE
12430 if (has_mbyte)
12431 {
12432 n = MB_BYTE2LEN(*p);
12433 c = mb_ptr2char(p + n);
12434 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12435 mb_char2bytes(c, p);
12436 }
12437 else
12438#endif
12439 {
12440 c = *p;
12441 *p = p[1];
12442 p[1] = c;
12443 }
12444 /*FALLTHROUGH*/
12445
12446 case STATE_SWAP3:
12447 /* Swap two bytes, skipping one: "123" -> "321". We change
12448 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12449 p = fword + sp->ts_fidx;
12450#ifdef FEAT_MBYTE
12451 if (has_mbyte)
12452 {
12453 n = mb_cptr2len(p);
12454 c = mb_ptr2char(p);
12455 fl = mb_cptr2len(p + n);
12456 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012457 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012458 c3 = c; /* don't swap non-word char */
12459 else
12460 c3 = mb_ptr2char(p + n + fl);
12461 }
12462 else
12463#endif
12464 {
12465 c = *p;
12466 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012467 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012468 c3 = c; /* don't swap non-word char */
12469 else
12470 c3 = p[2];
12471 }
12472
12473 /* When characters are identical: "121" then SWAP3 result is
12474 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12475 * same as SWAP on next char: "112". Thus skip all swapping.
12476 * Also skip when c3 is NUL.
12477 * Also get here when the third character is not a word character.
12478 * Second character may any char: "a.b" -> "b.a" */
12479 if (c == c3 || c3 == NUL)
12480 {
12481 sp->ts_state = STATE_REP_INI;
12482 break;
12483 }
12484 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12485 {
12486 go_deeper(stack, depth, SCORE_SWAP3);
12487#ifdef DEBUG_TRIEWALK
12488 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12489 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12490 c, c3);
12491#endif
12492 sp->ts_state = STATE_UNSWAP3;
12493 ++depth;
12494#ifdef FEAT_MBYTE
12495 if (has_mbyte)
12496 {
12497 tl = mb_char2len(c3);
12498 mch_memmove(p, p + n + fl, tl);
12499 mb_char2bytes(c2, p + tl);
12500 mb_char2bytes(c, p + fl + tl);
12501 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12502 }
12503 else
12504#endif
12505 {
12506 p[0] = p[2];
12507 p[2] = c;
12508 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12509 }
12510 }
12511 else
12512 sp->ts_state = STATE_REP_INI;
12513 break;
12514
12515 case STATE_UNSWAP3:
12516 /* Undo STATE_SWAP3: "321" -> "123" */
12517 p = fword + sp->ts_fidx;
12518#ifdef FEAT_MBYTE
12519 if (has_mbyte)
12520 {
12521 n = MB_BYTE2LEN(*p);
12522 c2 = mb_ptr2char(p + n);
12523 fl = MB_BYTE2LEN(p[n]);
12524 c = mb_ptr2char(p + n + fl);
12525 tl = MB_BYTE2LEN(p[n + fl]);
12526 mch_memmove(p + fl + tl, p, n);
12527 mb_char2bytes(c, p);
12528 mb_char2bytes(c2, p + tl);
12529 p = p + tl;
12530 }
12531 else
12532#endif
12533 {
12534 c = *p;
12535 *p = p[2];
12536 p[2] = c;
12537 ++p;
12538 }
12539
Bram Moolenaar860cae12010-06-05 23:22:07 +020012540 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012541 {
12542 /* Middle char is not a word char, skip the rotate. First and
12543 * third char were already checked at swap and swap3. */
12544 sp->ts_state = STATE_REP_INI;
12545 break;
12546 }
12547
12548 /* Rotate three characters left: "123" -> "231". We change
12549 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12550 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12551 {
12552 go_deeper(stack, depth, SCORE_SWAP3);
12553#ifdef DEBUG_TRIEWALK
12554 p = fword + sp->ts_fidx;
12555 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12556 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12557 p[0], p[1], p[2]);
12558#endif
12559 sp->ts_state = STATE_UNROT3L;
12560 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012561 p = fword + sp->ts_fidx;
12562#ifdef FEAT_MBYTE
12563 if (has_mbyte)
12564 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012565 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012566 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012567 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012568 fl += mb_cptr2len(p + n + fl);
12569 mch_memmove(p, p + n, fl);
12570 mb_char2bytes(c, p + fl);
12571 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012572 }
12573 else
12574#endif
12575 {
12576 c = *p;
12577 *p = p[1];
12578 p[1] = p[2];
12579 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012580 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012581 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012582 }
12583 else
12584 sp->ts_state = STATE_REP_INI;
12585 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012586
Bram Moolenaar4770d092006-01-12 23:22:24 +000012587 case STATE_UNROT3L:
12588 /* Undo ROT3L: "231" -> "123" */
12589 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012590#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012591 if (has_mbyte)
12592 {
12593 n = MB_BYTE2LEN(*p);
12594 n += MB_BYTE2LEN(p[n]);
12595 c = mb_ptr2char(p + n);
12596 tl = MB_BYTE2LEN(p[n]);
12597 mch_memmove(p + tl, p, n);
12598 mb_char2bytes(c, p);
12599 }
12600 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012601#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012602 {
12603 c = p[2];
12604 p[2] = p[1];
12605 p[1] = *p;
12606 *p = c;
12607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608
Bram Moolenaar4770d092006-01-12 23:22:24 +000012609 /* Rotate three bytes right: "123" -> "312". We change "fword"
12610 * here, it's changed back afterwards at STATE_UNROT3R. */
12611 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12612 {
12613 go_deeper(stack, depth, SCORE_SWAP3);
12614#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012616 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12617 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12618 p[0], p[1], p[2]);
12619#endif
12620 sp->ts_state = STATE_UNROT3R;
12621 ++depth;
12622 p = fword + sp->ts_fidx;
12623#ifdef FEAT_MBYTE
12624 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012625 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012626 n = mb_cptr2len(p);
12627 n += mb_cptr2len(p + n);
12628 c = mb_ptr2char(p + n);
12629 tl = mb_cptr2len(p + n);
12630 mch_memmove(p + tl, p, n);
12631 mb_char2bytes(c, p);
12632 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012633 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012634 else
12635#endif
12636 {
12637 c = p[2];
12638 p[2] = p[1];
12639 p[1] = *p;
12640 *p = c;
12641 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12642 }
12643 }
12644 else
12645 sp->ts_state = STATE_REP_INI;
12646 break;
12647
12648 case STATE_UNROT3R:
12649 /* Undo ROT3R: "312" -> "123" */
12650 p = fword + sp->ts_fidx;
12651#ifdef FEAT_MBYTE
12652 if (has_mbyte)
12653 {
12654 c = mb_ptr2char(p);
12655 tl = MB_BYTE2LEN(*p);
12656 n = MB_BYTE2LEN(p[tl]);
12657 n += MB_BYTE2LEN(p[tl + n]);
12658 mch_memmove(p, p + tl, n);
12659 mb_char2bytes(c, p + n);
12660 }
12661 else
12662#endif
12663 {
12664 c = *p;
12665 *p = p[1];
12666 p[1] = p[2];
12667 p[2] = c;
12668 }
12669 /*FALLTHROUGH*/
12670
12671 case STATE_REP_INI:
12672 /* Check if matching with REP items from the .aff file would work.
12673 * Quickly skip if:
12674 * - there are no REP items and we are not in the soundfold trie
12675 * - the score is going to be too high anyway
12676 * - already applied a REP item or swapped here */
12677 if ((lp->lp_replang == NULL && !soundfold)
12678 || sp->ts_score + SCORE_REP >= su->su_maxscore
12679 || sp->ts_fidx < sp->ts_fidxtry)
12680 {
12681 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012684
Bram Moolenaar4770d092006-01-12 23:22:24 +000012685 /* Use the first byte to quickly find the first entry that may
12686 * match. If the index is -1 there is none. */
12687 if (soundfold)
12688 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12689 else
12690 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691
Bram Moolenaar4770d092006-01-12 23:22:24 +000012692 if (sp->ts_curi < 0)
12693 {
12694 sp->ts_state = STATE_FINAL;
12695 break;
12696 }
12697
12698 sp->ts_state = STATE_REP;
12699 /*FALLTHROUGH*/
12700
12701 case STATE_REP:
12702 /* Try matching with REP items from the .aff file. For each match
12703 * replace the characters and check if the resulting word is
12704 * valid. */
12705 p = fword + sp->ts_fidx;
12706
12707 if (soundfold)
12708 gap = &slang->sl_repsal;
12709 else
12710 gap = &lp->lp_replang->sl_rep;
12711 while (sp->ts_curi < gap->ga_len)
12712 {
12713 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12714 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012715 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012716 /* past possible matching entries */
12717 sp->ts_curi = gap->ga_len;
12718 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012719 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012720 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12721 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12722 {
12723 go_deeper(stack, depth, SCORE_REP);
12724#ifdef DEBUG_TRIEWALK
12725 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12726 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12727 ftp->ft_from, ftp->ft_to);
12728#endif
12729 /* Need to undo this afterwards. */
12730 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012731
Bram Moolenaar4770d092006-01-12 23:22:24 +000012732 /* Change the "from" to the "to" string. */
12733 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012734 fl = (int)STRLEN(ftp->ft_from);
12735 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012736 if (fl != tl)
12737 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012738 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012739 repextra += tl - fl;
12740 }
12741 mch_memmove(p, ftp->ft_to, tl);
12742 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12743#ifdef FEAT_MBYTE
12744 stack[depth].ts_tcharlen = 0;
12745#endif
12746 break;
12747 }
12748 }
12749
12750 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12751 /* No (more) matches. */
12752 sp->ts_state = STATE_FINAL;
12753
12754 break;
12755
12756 case STATE_REP_UNDO:
12757 /* Undo a REP replacement and continue with the next one. */
12758 if (soundfold)
12759 gap = &slang->sl_repsal;
12760 else
12761 gap = &lp->lp_replang->sl_rep;
12762 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012763 fl = (int)STRLEN(ftp->ft_from);
12764 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012765 p = fword + sp->ts_fidx;
12766 if (fl != tl)
12767 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012768 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012769 repextra -= tl - fl;
12770 }
12771 mch_memmove(p, ftp->ft_from, fl);
12772 sp->ts_state = STATE_REP;
12773 break;
12774
12775 default:
12776 /* Did all possible states at this level, go up one level. */
12777 --depth;
12778
12779 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12780 {
12781 /* Continue in or go back to the prefix tree. */
12782 byts = pbyts;
12783 idxs = pidxs;
12784 }
12785
12786 /* Don't check for CTRL-C too often, it takes time. */
12787 if (--breakcheckcount == 0)
12788 {
12789 ui_breakcheck();
12790 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012792 }
12793 }
12794}
12795
Bram Moolenaar4770d092006-01-12 23:22:24 +000012796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012798 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012800 static void
12801go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012802 trystate_T *stack;
12803 int depth;
12804 int score_add;
12805{
Bram Moolenaarea424162005-06-16 21:51:00 +000012806 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012808 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012810 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012811}
12812
Bram Moolenaar53805d12005-08-01 07:08:33 +000012813#ifdef FEAT_MBYTE
12814/*
12815 * Case-folding may change the number of bytes: Count nr of chars in
12816 * fword[flen] and return the byte length of that many chars in "word".
12817 */
12818 static int
12819nofold_len(fword, flen, word)
12820 char_u *fword;
12821 int flen;
12822 char_u *word;
12823{
12824 char_u *p;
12825 int i = 0;
12826
12827 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12828 ++i;
12829 for (p = word; i > 0; mb_ptr_adv(p))
12830 --i;
12831 return (int)(p - word);
12832}
12833#endif
12834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012835/*
12836 * "fword" is a good word with case folded. Find the matching keep-case
12837 * words and put it in "kword".
12838 * Theoretically there could be several keep-case words that result in the
12839 * same case-folded word, but we only find one...
12840 */
12841 static void
12842find_keepcap_word(slang, fword, kword)
12843 slang_T *slang;
12844 char_u *fword;
12845 char_u *kword;
12846{
12847 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12848 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012849 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012850
12851 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012852 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012853 int round[MAXWLEN];
12854 int fwordidx[MAXWLEN];
12855 int uwordidx[MAXWLEN];
12856 int kwordlen[MAXWLEN];
12857
12858 int flen, ulen;
12859 int l;
12860 int len;
12861 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012862 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012863 char_u *p;
12864 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012865 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866
12867 if (byts == NULL)
12868 {
12869 /* array is empty: "cannot happen" */
12870 *kword = NUL;
12871 return;
12872 }
12873
12874 /* Make an all-cap version of "fword". */
12875 allcap_copy(fword, uword);
12876
12877 /*
12878 * Each character needs to be tried both case-folded and upper-case.
12879 * All this gets very complicated if we keep in mind that changing case
12880 * may change the byte length of a multi-byte character...
12881 */
12882 depth = 0;
12883 arridx[0] = 0;
12884 round[0] = 0;
12885 fwordidx[0] = 0;
12886 uwordidx[0] = 0;
12887 kwordlen[0] = 0;
12888 while (depth >= 0)
12889 {
12890 if (fword[fwordidx[depth]] == NUL)
12891 {
12892 /* We are at the end of "fword". If the tree allows a word to end
12893 * here we have found a match. */
12894 if (byts[arridx[depth] + 1] == 0)
12895 {
12896 kword[kwordlen[depth]] = NUL;
12897 return;
12898 }
12899
12900 /* kword is getting too long, continue one level up */
12901 --depth;
12902 }
12903 else if (++round[depth] > 2)
12904 {
12905 /* tried both fold-case and upper-case character, continue one
12906 * level up */
12907 --depth;
12908 }
12909 else
12910 {
12911 /*
12912 * round[depth] == 1: Try using the folded-case character.
12913 * round[depth] == 2: Try using the upper-case character.
12914 */
12915#ifdef FEAT_MBYTE
12916 if (has_mbyte)
12917 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012918 flen = mb_cptr2len(fword + fwordidx[depth]);
12919 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 }
12921 else
12922#endif
12923 ulen = flen = 1;
12924 if (round[depth] == 1)
12925 {
12926 p = fword + fwordidx[depth];
12927 l = flen;
12928 }
12929 else
12930 {
12931 p = uword + uwordidx[depth];
12932 l = ulen;
12933 }
12934
12935 for (tryidx = arridx[depth]; l > 0; --l)
12936 {
12937 /* Perform a binary search in the list of accepted bytes. */
12938 len = byts[tryidx++];
12939 c = *p++;
12940 lo = tryidx;
12941 hi = tryidx + len - 1;
12942 while (lo < hi)
12943 {
12944 m = (lo + hi) / 2;
12945 if (byts[m] > c)
12946 hi = m - 1;
12947 else if (byts[m] < c)
12948 lo = m + 1;
12949 else
12950 {
12951 lo = hi = m;
12952 break;
12953 }
12954 }
12955
12956 /* Stop if there is no matching byte. */
12957 if (hi < lo || byts[lo] != c)
12958 break;
12959
12960 /* Continue at the child (if there is one). */
12961 tryidx = idxs[lo];
12962 }
12963
12964 if (l == 0)
12965 {
12966 /*
12967 * Found the matching char. Copy it to "kword" and go a
12968 * level deeper.
12969 */
12970 if (round[depth] == 1)
12971 {
12972 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12973 flen);
12974 kwordlen[depth + 1] = kwordlen[depth] + flen;
12975 }
12976 else
12977 {
12978 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12979 ulen);
12980 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12981 }
12982 fwordidx[depth + 1] = fwordidx[depth] + flen;
12983 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12984
12985 ++depth;
12986 arridx[depth] = tryidx;
12987 round[depth] = 0;
12988 }
12989 }
12990 }
12991
12992 /* Didn't find it: "cannot happen". */
12993 *kword = NUL;
12994}
12995
12996/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012997 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12998 * su->su_sga.
12999 */
13000 static void
13001score_comp_sal(su)
13002 suginfo_T *su;
13003{
13004 langp_T *lp;
13005 char_u badsound[MAXWLEN];
13006 int i;
13007 suggest_T *stp;
13008 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013009 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013010 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013011
13012 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
13013 return;
13014
13015 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013016 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013017 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013018 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013019 if (lp->lp_slang->sl_sal.ga_len > 0)
13020 {
13021 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013022 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013023
13024 for (i = 0; i < su->su_ga.ga_len; ++i)
13025 {
13026 stp = &SUG(su->su_ga, i);
13027
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013028 /* Case-fold the suggested word, sound-fold it and compute the
13029 * sound-a-like score. */
13030 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013031 if (score < SCORE_MAXMAX)
13032 {
13033 /* Add the suggestion. */
13034 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13035 sstp->st_word = vim_strsave(stp->st_word);
13036 if (sstp->st_word != NULL)
13037 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013038 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013039 sstp->st_score = score;
13040 sstp->st_altscore = 0;
13041 sstp->st_orglen = stp->st_orglen;
13042 ++su->su_sga.ga_len;
13043 }
13044 }
13045 }
13046 break;
13047 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013048 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013049}
13050
13051/*
13052 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013053 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013054 */
13055 static void
13056score_combine(su)
13057 suginfo_T *su;
13058{
13059 int i;
13060 int j;
13061 garray_T ga;
13062 garray_T *gap;
13063 langp_T *lp;
13064 suggest_T *stp;
13065 char_u *p;
13066 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013067 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013068 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013069 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013070
13071 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013072 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013073 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013074 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013075 if (lp->lp_slang->sl_sal.ga_len > 0)
13076 {
13077 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013078 slang = lp->lp_slang;
13079 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013080
13081 for (i = 0; i < su->su_ga.ga_len; ++i)
13082 {
13083 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013084 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013085 if (stp->st_altscore == SCORE_MAXMAX)
13086 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13087 else
13088 stp->st_score = (stp->st_score * 3
13089 + stp->st_altscore) / 4;
13090 stp->st_salscore = FALSE;
13091 }
13092 break;
13093 }
13094 }
13095
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013096 if (slang == NULL) /* Using "double" without sound folding. */
13097 {
13098 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13099 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013100 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013101 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013102
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013103 /* Add the alternate score to su_sga. */
13104 for (i = 0; i < su->su_sga.ga_len; ++i)
13105 {
13106 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013107 stp->st_altscore = spell_edit_score(slang,
13108 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013109 if (stp->st_score == SCORE_MAXMAX)
13110 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13111 else
13112 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13113 stp->st_salscore = TRUE;
13114 }
13115
Bram Moolenaar4770d092006-01-12 23:22:24 +000013116 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13117 * for both lists. */
13118 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013119 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013120 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013121 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13122
13123 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13124 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13125 return;
13126
13127 stp = &SUG(ga, 0);
13128 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13129 {
13130 /* round 1: get a suggestion from su_ga
13131 * round 2: get a suggestion from su_sga */
13132 for (round = 1; round <= 2; ++round)
13133 {
13134 gap = round == 1 ? &su->su_ga : &su->su_sga;
13135 if (i < gap->ga_len)
13136 {
13137 /* Don't add a word if it's already there. */
13138 p = SUG(*gap, i).st_word;
13139 for (j = 0; j < ga.ga_len; ++j)
13140 if (STRCMP(stp[j].st_word, p) == 0)
13141 break;
13142 if (j == ga.ga_len)
13143 stp[ga.ga_len++] = SUG(*gap, i);
13144 else
13145 vim_free(p);
13146 }
13147 }
13148 }
13149
13150 ga_clear(&su->su_ga);
13151 ga_clear(&su->su_sga);
13152
13153 /* Truncate the list to the number of suggestions that will be displayed. */
13154 if (ga.ga_len > su->su_maxcount)
13155 {
13156 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13157 vim_free(stp[i].st_word);
13158 ga.ga_len = su->su_maxcount;
13159 }
13160
13161 su->su_ga = ga;
13162}
13163
13164/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013165 * For the goodword in "stp" compute the soundalike score compared to the
13166 * badword.
13167 */
13168 static int
13169stp_sal_score(stp, su, slang, badsound)
13170 suggest_T *stp;
13171 suginfo_T *su;
13172 slang_T *slang;
13173 char_u *badsound; /* sound-folded badword */
13174{
13175 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013176 char_u *pbad;
13177 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013178 char_u badsound2[MAXWLEN];
13179 char_u fword[MAXWLEN];
13180 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013181 char_u goodword[MAXWLEN];
13182 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013183
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013184 lendiff = (int)(su->su_badlen - stp->st_orglen);
13185 if (lendiff >= 0)
13186 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013187 else
13188 {
13189 /* soundfold the bad word with more characters following */
13190 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13191
13192 /* When joining two words the sound often changes a lot. E.g., "t he"
13193 * sounds like "t h" while "the" sounds like "@". Avoid that by
13194 * removing the space. Don't do it when the good word also contains a
13195 * space. */
13196 if (vim_iswhite(su->su_badptr[su->su_badlen])
13197 && *skiptowhite(stp->st_word) == NUL)
13198 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013199 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013200
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013201 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013202 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013203 }
13204
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013205 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013206 {
13207 /* Add part of the bad word to the good word, so that we soundfold
13208 * what replaces the bad word. */
13209 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013210 vim_strncpy(goodword + stp->st_wordlen,
13211 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013212 pgood = goodword;
13213 }
13214 else
13215 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013216
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013217 /* Sound-fold the word and compute the score for the difference. */
13218 spell_soundfold(slang, pgood, FALSE, goodsound);
13219
13220 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013221}
13222
Bram Moolenaar4770d092006-01-12 23:22:24 +000013223/* structure used to store soundfolded words that add_sound_suggest() has
13224 * handled already. */
13225typedef struct
13226{
13227 short sft_score; /* lowest score used */
13228 char_u sft_word[1]; /* soundfolded word, actually longer */
13229} sftword_T;
13230
13231static sftword_T dumsft;
13232#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13233#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13234
13235/*
13236 * Prepare for calling suggest_try_soundalike().
13237 */
13238 static void
13239suggest_try_soundalike_prep()
13240{
13241 langp_T *lp;
13242 int lpi;
13243 slang_T *slang;
13244
13245 /* Do this for all languages that support sound folding and for which a
13246 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013247 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013248 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013249 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013250 slang = lp->lp_slang;
13251 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13252 /* prepare the hashtable used by add_sound_suggest() */
13253 hash_init(&slang->sl_sounddone);
13254 }
13255}
13256
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013257/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013259 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013260 */
13261 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013262suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 suginfo_T *su;
13264{
13265 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013267 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013268 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013269
Bram Moolenaar4770d092006-01-12 23:22:24 +000013270 /* Do this for all languages that support sound folding and for which a
13271 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013272 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013273 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013274 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013275 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013276 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 {
13278 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013279 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280
Bram Moolenaar4770d092006-01-12 23:22:24 +000013281 /* try all kinds of inserts/deletes/swaps/etc. */
13282 /* TODO: also soundfold the next words, so that we can try joining
13283 * and splitting */
13284 suggest_trie_walk(su, lp, salword, TRUE);
13285 }
13286 }
13287}
13288
13289/*
13290 * Finish up after calling suggest_try_soundalike().
13291 */
13292 static void
13293suggest_try_soundalike_finish()
13294{
13295 langp_T *lp;
13296 int lpi;
13297 slang_T *slang;
13298 int todo;
13299 hashitem_T *hi;
13300
13301 /* Do this for all languages that support sound folding and for which a
13302 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013303 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013304 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013305 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013306 slang = lp->lp_slang;
13307 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13308 {
13309 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013310 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013311 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13312 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013314 vim_free(HI2SFT(hi));
13315 --todo;
13316 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013317
13318 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013319 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013320 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013321 }
13322 }
13323}
13324
13325/*
13326 * A match with a soundfolded word is found. Add the good word(s) that
13327 * produce this soundfolded word.
13328 */
13329 static void
13330add_sound_suggest(su, goodword, score, lp)
13331 suginfo_T *su;
13332 char_u *goodword;
13333 int score; /* soundfold score */
13334 langp_T *lp;
13335{
13336 slang_T *slang = lp->lp_slang; /* language for sound folding */
13337 int sfwordnr;
13338 char_u *nrline;
13339 int orgnr;
13340 char_u theword[MAXWLEN];
13341 int i;
13342 int wlen;
13343 char_u *byts;
13344 idx_T *idxs;
13345 int n;
13346 int wordcount;
13347 int wc;
13348 int goodscore;
13349 hash_T hash;
13350 hashitem_T *hi;
13351 sftword_T *sft;
13352 int bc, gc;
13353 int limit;
13354
13355 /*
13356 * It's very well possible that the same soundfold word is found several
13357 * times with different scores. Since the following is quite slow only do
13358 * the words that have a better score than before. Use a hashtable to
13359 * remember the words that have been done.
13360 */
13361 hash = hash_hash(goodword);
13362 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13363 if (HASHITEM_EMPTY(hi))
13364 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013365 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13366 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013367 if (sft != NULL)
13368 {
13369 sft->sft_score = score;
13370 STRCPY(sft->sft_word, goodword);
13371 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13372 }
13373 }
13374 else
13375 {
13376 sft = HI2SFT(hi);
13377 if (score >= sft->sft_score)
13378 return;
13379 sft->sft_score = score;
13380 }
13381
13382 /*
13383 * Find the word nr in the soundfold tree.
13384 */
13385 sfwordnr = soundfold_find(slang, goodword);
13386 if (sfwordnr < 0)
13387 {
13388 EMSG2(_(e_intern2), "add_sound_suggest()");
13389 return;
13390 }
13391
13392 /*
13393 * go over the list of good words that produce this soundfold word
13394 */
13395 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13396 orgnr = 0;
13397 while (*nrline != NUL)
13398 {
13399 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13400 * previous wordnr. */
13401 orgnr += bytes2offset(&nrline);
13402
13403 byts = slang->sl_fbyts;
13404 idxs = slang->sl_fidxs;
13405
13406 /* Lookup the word "orgnr" one of the two tries. */
13407 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013408 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013409 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013410 {
13411 i = 1;
13412 if (wordcount == orgnr && byts[n + 1] == NUL)
13413 break; /* found end of word */
13414
13415 if (byts[n + 1] == NUL)
13416 ++wordcount;
13417
13418 /* skip over the NUL bytes */
13419 for ( ; byts[n + i] == NUL; ++i)
13420 if (i > byts[n]) /* safety check */
13421 {
13422 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013423 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013424 goto badword;
13425 }
13426
13427 /* One of the siblings must have the word. */
13428 for ( ; i < byts[n]; ++i)
13429 {
13430 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13431 if (wordcount + wc > orgnr)
13432 break;
13433 wordcount += wc;
13434 }
13435
Bram Moolenaarace8d8e2013-11-21 17:42:31 +010013436 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +000013437 n = idxs[n + i];
13438 }
13439badword:
13440 theword[wlen] = NUL;
13441
13442 /* Go over the possible flags and regions. */
13443 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13444 {
13445 char_u cword[MAXWLEN];
13446 char_u *p;
13447 int flags = (int)idxs[n + i];
13448
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013449 /* Skip words with the NOSUGGEST flag */
13450 if (flags & WF_NOSUGGEST)
13451 continue;
13452
Bram Moolenaar4770d092006-01-12 23:22:24 +000013453 if (flags & WF_KEEPCAP)
13454 {
13455 /* Must find the word in the keep-case tree. */
13456 find_keepcap_word(slang, theword, cword);
13457 p = cword;
13458 }
13459 else
13460 {
13461 flags |= su->su_badflags;
13462 if ((flags & WF_CAPMASK) != 0)
13463 {
13464 /* Need to fix case according to "flags". */
13465 make_case_word(theword, cword, flags);
13466 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 }
13468 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013469 p = theword;
13470 }
13471
13472 /* Add the suggestion. */
13473 if (sps_flags & SPS_DOUBLE)
13474 {
13475 /* Add the suggestion if the score isn't too bad. */
13476 if (score <= su->su_maxscore)
13477 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13478 score, 0, FALSE, slang, FALSE);
13479 }
13480 else
13481 {
13482 /* Add a penalty for words in another region. */
13483 if ((flags & WF_REGION)
13484 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13485 goodscore = SCORE_REGION;
13486 else
13487 goodscore = 0;
13488
13489 /* Add a small penalty for changing the first letter from
13490 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020013491 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +000013492 * letter is the same, that has already been counted. */
13493 gc = PTR2CHAR(p);
13494 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013495 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013496 bc = PTR2CHAR(su->su_badword);
13497 if (!SPELL_ISUPPER(bc)
13498 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13499 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013500 }
13501
Bram Moolenaar4770d092006-01-12 23:22:24 +000013502 /* Compute the score for the good word. This only does letter
13503 * insert/delete/swap/replace. REP items are not considered,
13504 * which may make the score a bit higher.
13505 * Use a limit for the score to make it work faster. Use
13506 * MAXSCORE(), because RESCORE() will change the score.
13507 * If the limit is very high then the iterative method is
13508 * inefficient, using an array is quicker. */
13509 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13510 if (limit > SCORE_LIMITMAX)
13511 goodscore += spell_edit_score(slang, su->su_badword, p);
13512 else
13513 goodscore += spell_edit_score_limit(slang, su->su_badword,
13514 p, limit);
13515
13516 /* When going over the limit don't bother to do the rest. */
13517 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013518 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013519 /* Give a bonus to words seen before. */
13520 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521
Bram Moolenaar4770d092006-01-12 23:22:24 +000013522 /* Add the suggestion if the score isn't too bad. */
13523 goodscore = RESCORE(goodscore, score);
13524 if (goodscore <= su->su_sfmaxscore)
13525 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13526 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 }
13529 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013530 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 }
13532}
13533
13534/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013535 * Find word "word" in fold-case tree for "slang" and return the word number.
13536 */
13537 static int
13538soundfold_find(slang, word)
13539 slang_T *slang;
13540 char_u *word;
13541{
13542 idx_T arridx = 0;
13543 int len;
13544 int wlen = 0;
13545 int c;
13546 char_u *ptr = word;
13547 char_u *byts;
13548 idx_T *idxs;
13549 int wordnr = 0;
13550
13551 byts = slang->sl_sbyts;
13552 idxs = slang->sl_sidxs;
13553
13554 for (;;)
13555 {
13556 /* First byte is the number of possible bytes. */
13557 len = byts[arridx++];
13558
13559 /* If the first possible byte is a zero the word could end here.
13560 * If the word ends we found the word. If not skip the NUL bytes. */
13561 c = ptr[wlen];
13562 if (byts[arridx] == NUL)
13563 {
13564 if (c == NUL)
13565 break;
13566
13567 /* Skip over the zeros, there can be several. */
13568 while (len > 0 && byts[arridx] == NUL)
13569 {
13570 ++arridx;
13571 --len;
13572 }
13573 if (len == 0)
13574 return -1; /* no children, word should have ended here */
13575 ++wordnr;
13576 }
13577
13578 /* If the word ends we didn't find it. */
13579 if (c == NUL)
13580 return -1;
13581
13582 /* Perform a binary search in the list of accepted bytes. */
13583 if (c == TAB) /* <Tab> is handled like <Space> */
13584 c = ' ';
13585 while (byts[arridx] < c)
13586 {
13587 /* The word count is in the first idxs[] entry of the child. */
13588 wordnr += idxs[idxs[arridx]];
13589 ++arridx;
13590 if (--len == 0) /* end of the bytes, didn't find it */
13591 return -1;
13592 }
13593 if (byts[arridx] != c) /* didn't find the byte */
13594 return -1;
13595
13596 /* Continue at the child (if there is one). */
13597 arridx = idxs[arridx];
13598 ++wlen;
13599
13600 /* One space in the good word may stand for several spaces in the
13601 * checked word. */
13602 if (c == ' ')
13603 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13604 ++wlen;
13605 }
13606
13607 return wordnr;
13608}
13609
13610/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013611 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 */
13613 static void
13614make_case_word(fword, cword, flags)
13615 char_u *fword;
13616 char_u *cword;
13617 int flags;
13618{
13619 if (flags & WF_ALLCAP)
13620 /* Make it all upper-case */
13621 allcap_copy(fword, cword);
13622 else if (flags & WF_ONECAP)
13623 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013624 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625 else
13626 /* Use goodword as-is. */
13627 STRCPY(cword, fword);
13628}
13629
Bram Moolenaarea424162005-06-16 21:51:00 +000013630/*
13631 * Use map string "map" for languages "lp".
13632 */
13633 static void
13634set_map_str(lp, map)
13635 slang_T *lp;
13636 char_u *map;
13637{
13638 char_u *p;
13639 int headc = 0;
13640 int c;
13641 int i;
13642
13643 if (*map == NUL)
13644 {
13645 lp->sl_has_map = FALSE;
13646 return;
13647 }
13648 lp->sl_has_map = TRUE;
13649
Bram Moolenaar4770d092006-01-12 23:22:24 +000013650 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013651 for (i = 0; i < 256; ++i)
13652 lp->sl_map_array[i] = 0;
13653#ifdef FEAT_MBYTE
13654 hash_init(&lp->sl_map_hash);
13655#endif
13656
13657 /*
13658 * The similar characters are stored separated with slashes:
13659 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13660 * before the same slash. For characters above 255 sl_map_hash is used.
13661 */
13662 for (p = map; *p != NUL; )
13663 {
13664#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013665 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013666#else
13667 c = *p++;
13668#endif
13669 if (c == '/')
13670 headc = 0;
13671 else
13672 {
13673 if (headc == 0)
13674 headc = c;
13675
13676#ifdef FEAT_MBYTE
13677 /* Characters above 255 don't fit in sl_map_array[], put them in
13678 * the hash table. Each entry is the char, a NUL the headchar and
13679 * a NUL. */
13680 if (c >= 256)
13681 {
13682 int cl = mb_char2len(c);
13683 int headcl = mb_char2len(headc);
13684 char_u *b;
13685 hash_T hash;
13686 hashitem_T *hi;
13687
13688 b = alloc((unsigned)(cl + headcl + 2));
13689 if (b == NULL)
13690 return;
13691 mb_char2bytes(c, b);
13692 b[cl] = NUL;
13693 mb_char2bytes(headc, b + cl + 1);
13694 b[cl + 1 + headcl] = NUL;
13695 hash = hash_hash(b);
13696 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13697 if (HASHITEM_EMPTY(hi))
13698 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13699 else
13700 {
13701 /* This should have been checked when generating the .spl
13702 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013703 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013704 vim_free(b);
13705 }
13706 }
13707 else
13708#endif
13709 lp->sl_map_array[c] = headc;
13710 }
13711 }
13712}
13713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013714/*
13715 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13716 * lines in the .aff file.
13717 */
13718 static int
13719similar_chars(slang, c1, c2)
13720 slang_T *slang;
13721 int c1;
13722 int c2;
13723{
Bram Moolenaarea424162005-06-16 21:51:00 +000013724 int m1, m2;
13725#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +020013726 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +000013727 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013728
Bram Moolenaarea424162005-06-16 21:51:00 +000013729 if (c1 >= 256)
13730 {
13731 buf[mb_char2bytes(c1, buf)] = 0;
13732 hi = hash_find(&slang->sl_map_hash, buf);
13733 if (HASHITEM_EMPTY(hi))
13734 m1 = 0;
13735 else
13736 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13737 }
13738 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013739#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013740 m1 = slang->sl_map_array[c1];
13741 if (m1 == 0)
13742 return FALSE;
13743
13744
13745#ifdef FEAT_MBYTE
13746 if (c2 >= 256)
13747 {
13748 buf[mb_char2bytes(c2, buf)] = 0;
13749 hi = hash_find(&slang->sl_map_hash, buf);
13750 if (HASHITEM_EMPTY(hi))
13751 m2 = 0;
13752 else
13753 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13754 }
13755 else
13756#endif
13757 m2 = slang->sl_map_array[c2];
13758
13759 return m1 == m2;
13760}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013761
13762/*
13763 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013764 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 */
13766 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013767add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13768 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013770 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013772 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013773 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013774 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013775 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013776 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013777 int maxsf; /* su_maxscore applies to soundfold score,
13778 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013780 int goodlen; /* len of goodword changed */
13781 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013783 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013785 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013787 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13788 * "thee the" is added next to changing the first "the" the "thee". */
13789 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013790 pbad = su->su_badptr + badlenarg;
13791 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013792 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013793 goodlen = (int)(pgood - goodword);
13794 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013795 if (goodlen <= 0 || badlen <= 0)
13796 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013797 mb_ptr_back(goodword, pgood);
13798 mb_ptr_back(su->su_badptr, pbad);
13799#ifdef FEAT_MBYTE
13800 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013801 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013802 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13803 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013804 }
13805 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013806#endif
13807 if (*pgood != *pbad)
13808 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013809 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013810
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013811 if (badlen == 0 && goodlen == 0)
13812 /* goodword doesn't change anything; may happen for "the the" changing
13813 * the first "the" to itself. */
13814 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013815
Bram Moolenaar89d40322006-08-29 15:30:07 +000013816 if (gap->ga_len == 0)
13817 i = -1;
13818 else
13819 {
13820 /* Check if the word is already there. Also check the length that is
13821 * being replaced "thes," -> "these" is a different suggestion from
13822 * "thes" -> "these". */
13823 stp = &SUG(*gap, 0);
13824 for (i = gap->ga_len; --i >= 0; ++stp)
13825 if (stp->st_wordlen == goodlen
13826 && stp->st_orglen == badlen
13827 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013829 /*
13830 * Found it. Remember the word with the lowest score.
13831 */
13832 if (stp->st_slang == NULL)
13833 stp->st_slang = slang;
13834
13835 new_sug.st_score = score;
13836 new_sug.st_altscore = altscore;
13837 new_sug.st_had_bonus = had_bonus;
13838
13839 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013840 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013841 /* Only one of the two had the soundalike score computed.
13842 * Need to do that for the other one now, otherwise the
13843 * scores can't be compared. This happens because
13844 * suggest_try_change() doesn't compute the soundalike
13845 * word to keep it fast, while some special methods set
13846 * the soundalike score to zero. */
13847 if (had_bonus)
13848 rescore_one(su, stp);
13849 else
13850 {
13851 new_sug.st_word = stp->st_word;
13852 new_sug.st_wordlen = stp->st_wordlen;
13853 new_sug.st_slang = stp->st_slang;
13854 new_sug.st_orglen = badlen;
13855 rescore_one(su, &new_sug);
13856 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013857 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013858
Bram Moolenaar89d40322006-08-29 15:30:07 +000013859 if (stp->st_score > new_sug.st_score)
13860 {
13861 stp->st_score = new_sug.st_score;
13862 stp->st_altscore = new_sug.st_altscore;
13863 stp->st_had_bonus = new_sug.st_had_bonus;
13864 }
13865 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013866 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013868
Bram Moolenaar4770d092006-01-12 23:22:24 +000013869 if (i < 0 && ga_grow(gap, 1) == OK)
13870 {
13871 /* Add a suggestion. */
13872 stp = &SUG(*gap, gap->ga_len);
13873 stp->st_word = vim_strnsave(goodword, goodlen);
13874 if (stp->st_word != NULL)
13875 {
13876 stp->st_wordlen = goodlen;
13877 stp->st_score = score;
13878 stp->st_altscore = altscore;
13879 stp->st_had_bonus = had_bonus;
13880 stp->st_orglen = badlen;
13881 stp->st_slang = slang;
13882 ++gap->ga_len;
13883
13884 /* If we have too many suggestions now, sort the list and keep
13885 * the best suggestions. */
13886 if (gap->ga_len > SUG_MAX_COUNT(su))
13887 {
13888 if (maxsf)
13889 su->su_sfmaxscore = cleanup_suggestions(gap,
13890 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13891 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013892 su->su_maxscore = cleanup_suggestions(gap,
13893 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 }
13895 }
13896 }
13897}
13898
13899/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013900 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13901 * for split words, such as "the the". Remove these from the list here.
13902 */
13903 static void
13904check_suggestions(su, gap)
13905 suginfo_T *su;
13906 garray_T *gap; /* either su_ga or su_sga */
13907{
13908 suggest_T *stp;
13909 int i;
13910 char_u longword[MAXWLEN + 1];
13911 int len;
13912 hlf_T attr;
13913
13914 stp = &SUG(*gap, 0);
13915 for (i = gap->ga_len - 1; i >= 0; --i)
13916 {
13917 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020013918 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013919 len = stp[i].st_wordlen;
13920 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13921 MAXWLEN - len);
13922 attr = HLF_COUNT;
13923 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13924 if (attr != HLF_COUNT)
13925 {
13926 /* Remove this entry. */
13927 vim_free(stp[i].st_word);
13928 --gap->ga_len;
13929 if (i < gap->ga_len)
13930 mch_memmove(stp + i, stp + i + 1,
13931 sizeof(suggest_T) * (gap->ga_len - i));
13932 }
13933 }
13934}
13935
13936
13937/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 * Add a word to be banned.
13939 */
13940 static void
13941add_banned(su, word)
13942 suginfo_T *su;
13943 char_u *word;
13944{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013945 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013946 hash_T hash;
13947 hashitem_T *hi;
13948
Bram Moolenaar4770d092006-01-12 23:22:24 +000013949 hash = hash_hash(word);
13950 hi = hash_lookup(&su->su_banned, word, hash);
13951 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013953 s = vim_strsave(word);
13954 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 hash_add_item(&su->su_banned, hi, s, hash);
13956 }
13957}
13958
13959/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013960 * Recompute the score for all suggestions if sound-folding is possible. This
13961 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013962 */
13963 static void
13964rescore_suggestions(su)
13965 suginfo_T *su;
13966{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013967 int i;
13968
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013969 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013970 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013971 rescore_one(su, &SUG(su->su_ga, i));
13972}
13973
13974/*
13975 * Recompute the score for one suggestion if sound-folding is possible.
13976 */
13977 static void
13978rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013979 suginfo_T *su;
13980 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013981{
13982 slang_T *slang = stp->st_slang;
13983 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013984 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013985
13986 /* Only rescore suggestions that have no sal score yet and do have a
13987 * language. */
13988 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13989 {
13990 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013991 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013992 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013993 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013994 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013995 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013996 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013997
13998 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013999 if (stp->st_altscore == SCORE_MAXMAX)
14000 stp->st_altscore = SCORE_BIG;
14001 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
14002 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014003 }
14004}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014006static int
14007#ifdef __BORLANDC__
14008_RTLENTRYF
14009#endif
14010sug_compare __ARGS((const void *s1, const void *s2));
14011
14012/*
14013 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014014 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 */
14016 static int
14017#ifdef __BORLANDC__
14018_RTLENTRYF
14019#endif
14020sug_compare(s1, s2)
14021 const void *s1;
14022 const void *s2;
14023{
14024 suggest_T *p1 = (suggest_T *)s1;
14025 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014026 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014028 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000014029 {
14030 n = p1->st_altscore - p2->st_altscore;
14031 if (n == 0)
14032 n = STRICMP(p1->st_word, p2->st_word);
14033 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014034 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035}
14036
14037/*
14038 * Cleanup the suggestions:
14039 * - Sort on score.
14040 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014041 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014043 static int
14044cleanup_suggestions(gap, maxscore, keep)
14045 garray_T *gap;
14046 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014047 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014049 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014050 int i;
14051
14052 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014053 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054
14055 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014056 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014058 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014059 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014060 gap->ga_len = keep;
14061 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014063 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014064}
14065
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014066#if defined(FEAT_EVAL) || defined(PROTO)
14067/*
14068 * Soundfold a string, for soundfold().
14069 * Result is in allocated memory, NULL for an error.
14070 */
14071 char_u *
14072eval_soundfold(word)
14073 char_u *word;
14074{
14075 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014076 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014077 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014078
Bram Moolenaar860cae12010-06-05 23:22:07 +020014079 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014080 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014081 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014082 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014083 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014084 if (lp->lp_slang->sl_sal.ga_len > 0)
14085 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014086 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014087 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014088 return vim_strsave(sound);
14089 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014090 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014091
14092 /* No language with sound folding, return word as-is. */
14093 return vim_strsave(word);
14094}
14095#endif
14096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097/*
14098 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014099 *
14100 * There are many ways to turn a word into a sound-a-like representation. The
14101 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14102 * swedish name matching - survey and test of different algorithms" by Klas
14103 * Erikson.
14104 *
14105 * We support two methods:
14106 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14107 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 */
14109 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014110spell_soundfold(slang, inword, folded, res)
14111 slang_T *slang;
14112 char_u *inword;
14113 int folded; /* "inword" is already case-folded */
14114 char_u *res;
14115{
14116 char_u fword[MAXWLEN];
14117 char_u *word;
14118
14119 if (slang->sl_sofo)
14120 /* SOFOFROM and SOFOTO used */
14121 spell_soundfold_sofo(slang, inword, res);
14122 else
14123 {
14124 /* SAL items used. Requires the word to be case-folded. */
14125 if (folded)
14126 word = inword;
14127 else
14128 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014129 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014130 word = fword;
14131 }
14132
14133#ifdef FEAT_MBYTE
14134 if (has_mbyte)
14135 spell_soundfold_wsal(slang, word, res);
14136 else
14137#endif
14138 spell_soundfold_sal(slang, word, res);
14139 }
14140}
14141
14142/*
14143 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14144 * SOFOTO lines.
14145 */
14146 static void
14147spell_soundfold_sofo(slang, inword, res)
14148 slang_T *slang;
14149 char_u *inword;
14150 char_u *res;
14151{
14152 char_u *s;
14153 int ri = 0;
14154 int c;
14155
14156#ifdef FEAT_MBYTE
14157 if (has_mbyte)
14158 {
14159 int prevc = 0;
14160 int *ip;
14161
14162 /* The sl_sal_first[] table contains the translation for chars up to
14163 * 255, sl_sal the rest. */
14164 for (s = inword; *s != NUL; )
14165 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014166 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014167 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14168 c = ' ';
14169 else if (c < 256)
14170 c = slang->sl_sal_first[c];
14171 else
14172 {
14173 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14174 if (ip == NULL) /* empty list, can't match */
14175 c = NUL;
14176 else
14177 for (;;) /* find "c" in the list */
14178 {
14179 if (*ip == 0) /* not found */
14180 {
14181 c = NUL;
14182 break;
14183 }
14184 if (*ip == c) /* match! */
14185 {
14186 c = ip[1];
14187 break;
14188 }
14189 ip += 2;
14190 }
14191 }
14192
14193 if (c != NUL && c != prevc)
14194 {
14195 ri += mb_char2bytes(c, res + ri);
14196 if (ri + MB_MAXBYTES > MAXWLEN)
14197 break;
14198 prevc = c;
14199 }
14200 }
14201 }
14202 else
14203#endif
14204 {
14205 /* The sl_sal_first[] table contains the translation. */
14206 for (s = inword; (c = *s) != NUL; ++s)
14207 {
14208 if (vim_iswhite(c))
14209 c = ' ';
14210 else
14211 c = slang->sl_sal_first[c];
14212 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14213 res[ri++] = c;
14214 }
14215 }
14216
14217 res[ri] = NUL;
14218}
14219
14220 static void
14221spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 slang_T *slang;
14223 char_u *inword;
14224 char_u *res;
14225{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014228 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014230 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 int n, k = 0;
14234 int z0;
14235 int k0;
14236 int n0;
14237 int c;
14238 int pri;
14239 int p0 = -333;
14240 int c0;
14241
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014242 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014243 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 if (slang->sl_rem_accents)
14245 {
14246 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014247 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014249 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014250 {
14251 *t++ = ' ';
14252 s = skipwhite(s);
14253 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014254 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014256 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 *t++ = *s;
14258 ++s;
14259 }
14260 }
14261 *t = NUL;
14262 }
14263 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +020014264 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014266 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267
14268 /*
14269 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014270 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014272 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 while ((c = word[i]) != NUL)
14274 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014275 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276 n = slang->sl_sal_first[c];
14277 z0 = 0;
14278
14279 if (n >= 0)
14280 {
14281 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014282 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014284 /* Quickly skip entries that don't match the word. Most
14285 * entries are less then three chars, optimize for that. */
14286 k = smp[n].sm_leadlen;
14287 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014288 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014289 if (word[i + 1] != s[1])
14290 continue;
14291 if (k > 2)
14292 {
14293 for (j = 2; j < k; ++j)
14294 if (word[i + j] != s[j])
14295 break;
14296 if (j < k)
14297 continue;
14298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 }
14300
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014301 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014303 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014304 while (*pf != NUL && *pf != word[i + k])
14305 ++pf;
14306 if (*pf == NUL)
14307 continue;
14308 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014310 s = smp[n].sm_rules;
14311 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312
14313 p0 = *s;
14314 k0 = k;
14315 while (*s == '-' && k > 1)
14316 {
14317 k--;
14318 s++;
14319 }
14320 if (*s == '<')
14321 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014322 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 {
14324 /* determine priority */
14325 pri = *s - '0';
14326 s++;
14327 }
14328 if (*s == '^' && *(s + 1) == '^')
14329 s++;
14330
14331 if (*s == NUL
14332 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014333 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014334 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014336 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014338 && spell_iswordp(word + i - 1, curwin)
14339 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 {
14341 /* search for followup rules, if: */
14342 /* followup and k > 1 and NO '-' in searchstring */
14343 c0 = word[i + k - 1];
14344 n0 = slang->sl_sal_first[c0];
14345
14346 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014347 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 {
14349 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014350 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014352 /* Quickly skip entries that don't match the word.
14353 * */
14354 k0 = smp[n0].sm_leadlen;
14355 if (k0 > 1)
14356 {
14357 if (word[i + k] != s[1])
14358 continue;
14359 if (k0 > 2)
14360 {
14361 pf = word + i + k + 1;
14362 for (j = 2; j < k0; ++j)
14363 if (*pf++ != s[j])
14364 break;
14365 if (j < k0)
14366 continue;
14367 }
14368 }
14369 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014371 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014372 {
14373 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014374 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014375 while (*pf != NUL && *pf != word[i + k0])
14376 ++pf;
14377 if (*pf == NUL)
14378 continue;
14379 ++k0;
14380 }
14381
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014382 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014383 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384 while (*s == '-')
14385 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014386 /* "k0" gets NOT reduced because
14387 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 s++;
14389 }
14390 if (*s == '<')
14391 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014392 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014393 {
14394 p0 = *s - '0';
14395 s++;
14396 }
14397
14398 if (*s == NUL
14399 /* *s == '^' cuts */
14400 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014401 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014402 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014403 {
14404 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407
14408 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 /* rule fits; stop search */
14412 break;
14413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 }
14415
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014416 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 }
14419
14420 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014421 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014422 if (s == NULL)
14423 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014424 pf = smp[n].sm_rules;
14425 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 if (p0 == 1 && z == 0)
14427 {
14428 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014429 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14430 || res[reslen - 1] == *s))
14431 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014432 z0 = 1;
14433 z = 1;
14434 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014435 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014436 {
14437 word[i + k0] = *s;
14438 k0++;
14439 s++;
14440 }
14441 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014442 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014443
14444 /* new "actual letter" */
14445 c = word[i];
14446 }
14447 else
14448 {
14449 /* no '<' rule used */
14450 i += k - 1;
14451 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014452 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014454 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014455 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456 s++;
14457 }
14458 /* new "actual letter" */
14459 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014460 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 {
14462 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014463 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014464 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014465 i = 0;
14466 z0 = 1;
14467 }
14468 }
14469 break;
14470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 }
14472 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014473 else if (vim_iswhite(c))
14474 {
14475 c = ' ';
14476 k = 1;
14477 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014478
14479 if (z0 == 0)
14480 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014481 if (k && !p0 && reslen < MAXWLEN && c != NUL
14482 && (!slang->sl_collapse || reslen == 0
14483 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014485 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014486
14487 i++;
14488 z = 0;
14489 k = 0;
14490 }
14491 }
14492
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014493 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494}
14495
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014496#ifdef FEAT_MBYTE
14497/*
14498 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14499 * Multi-byte version of spell_soundfold().
14500 */
14501 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014502spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014503 slang_T *slang;
14504 char_u *inword;
14505 char_u *res;
14506{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014507 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014508 int word[MAXWLEN];
14509 int wres[MAXWLEN];
14510 int l;
14511 char_u *s;
14512 int *ws;
14513 char_u *t;
14514 int *pf;
14515 int i, j, z;
14516 int reslen;
14517 int n, k = 0;
14518 int z0;
14519 int k0;
14520 int n0;
14521 int c;
14522 int pri;
14523 int p0 = -333;
14524 int c0;
14525 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014526 int wordlen;
14527
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014528
14529 /*
14530 * Convert the multi-byte string to a wide-character string.
14531 * Remove accents, if wanted. We actually remove all non-word characters.
14532 * But keep white space.
14533 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014534 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014535 for (s = inword; *s != NUL; )
14536 {
14537 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014538 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014539 if (slang->sl_rem_accents)
14540 {
14541 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14542 {
14543 if (did_white)
14544 continue;
14545 c = ' ';
14546 did_white = TRUE;
14547 }
14548 else
14549 {
14550 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +010014551 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014552 continue;
14553 }
14554 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014555 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014556 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014557 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014558
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014559 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014560 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014561 * Converted from C++ to C. Added support for multi-byte chars.
14562 * Changed to keep spaces.
14563 */
14564 i = reslen = z = 0;
14565 while ((c = word[i]) != NUL)
14566 {
14567 /* Start with the first rule that has the character in the word. */
14568 n = slang->sl_sal_first[c & 0xff];
14569 z0 = 0;
14570
14571 if (n >= 0)
14572 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014573 /* Check all rules for the same index byte.
14574 * If c is 0x300 need extra check for the end of the array, as
14575 * (c & 0xff) is NUL. */
14576 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14577 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014578 {
14579 /* Quickly skip entries that don't match the word. Most
14580 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014581 if (c != ws[0])
14582 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014583 k = smp[n].sm_leadlen;
14584 if (k > 1)
14585 {
14586 if (word[i + 1] != ws[1])
14587 continue;
14588 if (k > 2)
14589 {
14590 for (j = 2; j < k; ++j)
14591 if (word[i + j] != ws[j])
14592 break;
14593 if (j < k)
14594 continue;
14595 }
14596 }
14597
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014598 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014599 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014600 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014601 while (*pf != NUL && *pf != word[i + k])
14602 ++pf;
14603 if (*pf == NUL)
14604 continue;
14605 ++k;
14606 }
14607 s = smp[n].sm_rules;
14608 pri = 5; /* default priority */
14609
14610 p0 = *s;
14611 k0 = k;
14612 while (*s == '-' && k > 1)
14613 {
14614 k--;
14615 s++;
14616 }
14617 if (*s == '<')
14618 s++;
14619 if (VIM_ISDIGIT(*s))
14620 {
14621 /* determine priority */
14622 pri = *s - '0';
14623 s++;
14624 }
14625 if (*s == '^' && *(s + 1) == '^')
14626 s++;
14627
14628 if (*s == NUL
14629 || (*s == '^'
14630 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014631 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014632 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014633 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014634 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014635 && spell_iswordp_w(word + i - 1, curwin)
14636 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014637 {
14638 /* search for followup rules, if: */
14639 /* followup and k > 1 and NO '-' in searchstring */
14640 c0 = word[i + k - 1];
14641 n0 = slang->sl_sal_first[c0 & 0xff];
14642
14643 if (slang->sl_followup && k > 1 && n0 >= 0
14644 && p0 != '-' && word[i + k] != NUL)
14645 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014646 /* Test follow-up rule for "word[i + k]"; loop over
14647 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014648 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14649 == (c0 & 0xff); ++n0)
14650 {
14651 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014652 */
14653 if (c0 != ws[0])
14654 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014655 k0 = smp[n0].sm_leadlen;
14656 if (k0 > 1)
14657 {
14658 if (word[i + k] != ws[1])
14659 continue;
14660 if (k0 > 2)
14661 {
14662 pf = word + i + k + 1;
14663 for (j = 2; j < k0; ++j)
14664 if (*pf++ != ws[j])
14665 break;
14666 if (j < k0)
14667 continue;
14668 }
14669 }
14670 k0 += k - 1;
14671
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014672 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014673 {
14674 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014675 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014676 while (*pf != NUL && *pf != word[i + k0])
14677 ++pf;
14678 if (*pf == NUL)
14679 continue;
14680 ++k0;
14681 }
14682
14683 p0 = 5;
14684 s = smp[n0].sm_rules;
14685 while (*s == '-')
14686 {
14687 /* "k0" gets NOT reduced because
14688 * "if (k0 == k)" */
14689 s++;
14690 }
14691 if (*s == '<')
14692 s++;
14693 if (VIM_ISDIGIT(*s))
14694 {
14695 p0 = *s - '0';
14696 s++;
14697 }
14698
14699 if (*s == NUL
14700 /* *s == '^' cuts */
14701 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014702 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014703 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014704 {
14705 if (k0 == k)
14706 /* this is just a piece of the string */
14707 continue;
14708
14709 if (p0 < pri)
14710 /* priority too low */
14711 continue;
14712 /* rule fits; stop search */
14713 break;
14714 }
14715 }
14716
14717 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14718 == (c0 & 0xff))
14719 continue;
14720 }
14721
14722 /* replace string */
14723 ws = smp[n].sm_to_w;
14724 s = smp[n].sm_rules;
14725 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14726 if (p0 == 1 && z == 0)
14727 {
14728 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014729 if (reslen > 0 && ws != NULL && *ws != NUL
14730 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014731 || wres[reslen - 1] == *ws))
14732 reslen--;
14733 z0 = 1;
14734 z = 1;
14735 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014736 if (ws != NULL)
14737 while (*ws != NUL && word[i + k0] != NUL)
14738 {
14739 word[i + k0] = *ws;
14740 k0++;
14741 ws++;
14742 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014743 if (k > k0)
14744 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014745 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014746
14747 /* new "actual letter" */
14748 c = word[i];
14749 }
14750 else
14751 {
14752 /* no '<' rule used */
14753 i += k - 1;
14754 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014755 if (ws != NULL)
14756 while (*ws != NUL && ws[1] != NUL
14757 && reslen < MAXWLEN)
14758 {
14759 if (reslen == 0 || wres[reslen - 1] != *ws)
14760 wres[reslen++] = *ws;
14761 ws++;
14762 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014763 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014764 if (ws == NULL)
14765 c = NUL;
14766 else
14767 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014768 if (strstr((char *)s, "^^") != NULL)
14769 {
14770 if (c != NUL)
14771 wres[reslen++] = c;
14772 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +020014773 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014774 i = 0;
14775 z0 = 1;
14776 }
14777 }
14778 break;
14779 }
14780 }
14781 }
14782 else if (vim_iswhite(c))
14783 {
14784 c = ' ';
14785 k = 1;
14786 }
14787
14788 if (z0 == 0)
14789 {
14790 if (k && !p0 && reslen < MAXWLEN && c != NUL
14791 && (!slang->sl_collapse || reslen == 0
14792 || wres[reslen - 1] != c))
14793 /* condense only double letters */
14794 wres[reslen++] = c;
14795
14796 i++;
14797 z = 0;
14798 k = 0;
14799 }
14800 }
14801
14802 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14803 l = 0;
14804 for (n = 0; n < reslen; ++n)
14805 {
14806 l += mb_char2bytes(wres[n], res + l);
14807 if (l + MB_MAXBYTES > MAXWLEN)
14808 break;
14809 }
14810 res[l] = NUL;
14811}
14812#endif
14813
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014814/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014815 * Compute a score for two sound-a-like words.
14816 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14817 * Instead of a generic loop we write out the code. That keeps it fast by
14818 * avoiding checks that will not be possible.
14819 */
14820 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014821soundalike_score(goodstart, badstart)
14822 char_u *goodstart; /* sound-folded good word */
14823 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014824{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014825 char_u *goodsound = goodstart;
14826 char_u *badsound = badstart;
14827 int goodlen;
14828 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014829 int n;
14830 char_u *pl, *ps;
14831 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014832 int score = 0;
14833
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014834 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014835 * counted so much, vowels halfway the word aren't counted at all. */
14836 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14837 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014838 if ((badsound[0] == NUL && goodsound[1] == NUL)
14839 || (goodsound[0] == NUL && badsound[1] == NUL))
14840 /* changing word with vowel to word without a sound */
14841 return SCORE_DEL;
14842 if (badsound[0] == NUL || goodsound[0] == NUL)
14843 /* more than two changes */
14844 return SCORE_MAXMAX;
14845
Bram Moolenaar4770d092006-01-12 23:22:24 +000014846 if (badsound[1] == goodsound[1]
14847 || (badsound[1] != NUL
14848 && goodsound[1] != NUL
14849 && badsound[2] == goodsound[2]))
14850 {
14851 /* handle like a substitute */
14852 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014853 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014854 {
14855 score = 2 * SCORE_DEL / 3;
14856 if (*badsound == '*')
14857 ++badsound;
14858 else
14859 ++goodsound;
14860 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014861 }
14862
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014863 goodlen = (int)STRLEN(goodsound);
14864 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014865
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014866 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014867 * changes. */
14868 n = goodlen - badlen;
14869 if (n < -2 || n > 2)
14870 return SCORE_MAXMAX;
14871
14872 if (n > 0)
14873 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014874 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014875 ps = badsound;
14876 }
14877 else
14878 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014879 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014880 ps = goodsound;
14881 }
14882
14883 /* Skip over the identical part. */
14884 while (*pl == *ps && *pl != NUL)
14885 {
14886 ++pl;
14887 ++ps;
14888 }
14889
14890 switch (n)
14891 {
14892 case -2:
14893 case 2:
14894 /*
14895 * Must delete two characters from "pl".
14896 */
14897 ++pl; /* first delete */
14898 while (*pl == *ps)
14899 {
14900 ++pl;
14901 ++ps;
14902 }
14903 /* strings must be equal after second delete */
14904 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014905 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014906
14907 /* Failed to compare. */
14908 break;
14909
14910 case -1:
14911 case 1:
14912 /*
14913 * Minimal one delete from "pl" required.
14914 */
14915
14916 /* 1: delete */
14917 pl2 = pl + 1;
14918 ps2 = ps;
14919 while (*pl2 == *ps2)
14920 {
14921 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014922 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014923 ++pl2;
14924 ++ps2;
14925 }
14926
14927 /* 2: delete then swap, then rest must be equal */
14928 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14929 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014930 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014931
14932 /* 3: delete then substitute, then the rest must be equal */
14933 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014934 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014935
14936 /* 4: first swap then delete */
14937 if (pl[0] == ps[1] && pl[1] == ps[0])
14938 {
14939 pl2 = pl + 2; /* swap, skip two chars */
14940 ps2 = ps + 2;
14941 while (*pl2 == *ps2)
14942 {
14943 ++pl2;
14944 ++ps2;
14945 }
14946 /* delete a char and then strings must be equal */
14947 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014948 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014949 }
14950
14951 /* 5: first substitute then delete */
14952 pl2 = pl + 1; /* substitute, skip one char */
14953 ps2 = ps + 1;
14954 while (*pl2 == *ps2)
14955 {
14956 ++pl2;
14957 ++ps2;
14958 }
14959 /* delete a char and then strings must be equal */
14960 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014961 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014962
14963 /* Failed to compare. */
14964 break;
14965
14966 case 0:
14967 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014968 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014969 * insert is only possible in combination with a delete.
14970 * 1: check if for identical strings
14971 */
14972 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014973 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014974
14975 /* 2: swap */
14976 if (pl[0] == ps[1] && pl[1] == ps[0])
14977 {
14978 pl2 = pl + 2; /* swap, skip two chars */
14979 ps2 = ps + 2;
14980 while (*pl2 == *ps2)
14981 {
14982 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014983 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014984 ++pl2;
14985 ++ps2;
14986 }
14987 /* 3: swap and swap again */
14988 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14989 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014990 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014991
14992 /* 4: swap and substitute */
14993 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014994 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014995 }
14996
14997 /* 5: substitute */
14998 pl2 = pl + 1;
14999 ps2 = ps + 1;
15000 while (*pl2 == *ps2)
15001 {
15002 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015003 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015004 ++pl2;
15005 ++ps2;
15006 }
15007
15008 /* 6: substitute and swap */
15009 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
15010 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015011 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015012
15013 /* 7: substitute and substitute */
15014 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015015 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015016
15017 /* 8: insert then delete */
15018 pl2 = pl;
15019 ps2 = ps + 1;
15020 while (*pl2 == *ps2)
15021 {
15022 ++pl2;
15023 ++ps2;
15024 }
15025 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015026 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015027
15028 /* 9: delete then insert */
15029 pl2 = pl + 1;
15030 ps2 = ps;
15031 while (*pl2 == *ps2)
15032 {
15033 ++pl2;
15034 ++ps2;
15035 }
15036 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015037 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015038
15039 /* Failed to compare. */
15040 break;
15041 }
15042
15043 return SCORE_MAXMAX;
15044}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046/*
15047 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015048 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015049 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015050 * The algorithm is described by Du and Chang, 1992.
15051 * The implementation of the algorithm comes from Aspell editdist.cpp,
15052 * edit_distance(). It has been converted from C++ to C and modified to
15053 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054 */
15055 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000015056spell_edit_score(slang, badword, goodword)
15057 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015058 char_u *badword;
15059 char_u *goodword;
15060{
15061 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015062 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 int j, i;
15064 int t;
15065 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015066 int pbc, pgc;
15067#ifdef FEAT_MBYTE
15068 char_u *p;
15069 int wbadword[MAXWLEN];
15070 int wgoodword[MAXWLEN];
15071
15072 if (has_mbyte)
15073 {
15074 /* Get the characters from the multi-byte strings and put them in an
15075 * int array for easy access. */
15076 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015077 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015078 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015079 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015080 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015081 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015082 }
15083 else
15084#endif
15085 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015086 badlen = (int)STRLEN(badword) + 1;
15087 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089
15090 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15091#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15093 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015094 if (cnt == NULL)
15095 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096
15097 CNT(0, 0) = 0;
15098 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015099 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015100
15101 for (i = 1; i <= badlen; ++i)
15102 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015103 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015104 for (j = 1; j <= goodlen; ++j)
15105 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015106#ifdef FEAT_MBYTE
15107 if (has_mbyte)
15108 {
15109 bc = wbadword[i - 1];
15110 gc = wgoodword[j - 1];
15111 }
15112 else
15113#endif
15114 {
15115 bc = badword[i - 1];
15116 gc = goodword[j - 1];
15117 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015118 if (bc == gc)
15119 CNT(i, j) = CNT(i - 1, j - 1);
15120 else
15121 {
15122 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015123 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015124 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15125 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015126 {
15127 /* For a similar character use SCORE_SIMILAR. */
15128 if (slang != NULL
15129 && slang->sl_has_map
15130 && similar_chars(slang, gc, bc))
15131 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15132 else
15133 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15134 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015136 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015137 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015138#ifdef FEAT_MBYTE
15139 if (has_mbyte)
15140 {
15141 pbc = wbadword[i - 2];
15142 pgc = wgoodword[j - 2];
15143 }
15144 else
15145#endif
15146 {
15147 pbc = badword[i - 2];
15148 pgc = goodword[j - 2];
15149 }
15150 if (bc == pgc && pbc == gc)
15151 {
15152 t = SCORE_SWAP + CNT(i - 2, j - 2);
15153 if (t < CNT(i, j))
15154 CNT(i, j) = t;
15155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015156 }
15157 t = SCORE_DEL + CNT(i - 1, j);
15158 if (t < CNT(i, j))
15159 CNT(i, j) = t;
15160 t = SCORE_INS + CNT(i, j - 1);
15161 if (t < CNT(i, j))
15162 CNT(i, j) = t;
15163 }
15164 }
15165 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015166
15167 i = CNT(badlen - 1, goodlen - 1);
15168 vim_free(cnt);
15169 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015171
Bram Moolenaar4770d092006-01-12 23:22:24 +000015172typedef struct
15173{
15174 int badi;
15175 int goodi;
15176 int score;
15177} limitscore_T;
15178
15179/*
15180 * Like spell_edit_score(), but with a limit on the score to make it faster.
15181 * May return SCORE_MAXMAX when the score is higher than "limit".
15182 *
15183 * This uses a stack for the edits still to be tried.
15184 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15185 * for multi-byte characters.
15186 */
15187 static int
15188spell_edit_score_limit(slang, badword, goodword, limit)
15189 slang_T *slang;
15190 char_u *badword;
15191 char_u *goodword;
15192 int limit;
15193{
15194 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15195 int stackidx;
15196 int bi, gi;
15197 int bi2, gi2;
15198 int bc, gc;
15199 int score;
15200 int score_off;
15201 int minscore;
15202 int round;
15203
15204#ifdef FEAT_MBYTE
15205 /* Multi-byte characters require a bit more work, use a different function
15206 * to avoid testing "has_mbyte" quite often. */
15207 if (has_mbyte)
15208 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15209#endif
15210
15211 /*
15212 * The idea is to go from start to end over the words. So long as
15213 * characters are equal just continue, this always gives the lowest score.
15214 * When there is a difference try several alternatives. Each alternative
15215 * increases "score" for the edit distance. Some of the alternatives are
15216 * pushed unto a stack and tried later, some are tried right away. At the
15217 * end of the word the score for one alternative is known. The lowest
15218 * possible score is stored in "minscore".
15219 */
15220 stackidx = 0;
15221 bi = 0;
15222 gi = 0;
15223 score = 0;
15224 minscore = limit + 1;
15225
15226 for (;;)
15227 {
15228 /* Skip over an equal part, score remains the same. */
15229 for (;;)
15230 {
15231 bc = badword[bi];
15232 gc = goodword[gi];
15233 if (bc != gc) /* stop at a char that's different */
15234 break;
15235 if (bc == NUL) /* both words end */
15236 {
15237 if (score < minscore)
15238 minscore = score;
15239 goto pop; /* do next alternative */
15240 }
15241 ++bi;
15242 ++gi;
15243 }
15244
15245 if (gc == NUL) /* goodword ends, delete badword chars */
15246 {
15247 do
15248 {
15249 if ((score += SCORE_DEL) >= minscore)
15250 goto pop; /* do next alternative */
15251 } while (badword[++bi] != NUL);
15252 minscore = score;
15253 }
15254 else if (bc == NUL) /* badword ends, insert badword chars */
15255 {
15256 do
15257 {
15258 if ((score += SCORE_INS) >= minscore)
15259 goto pop; /* do next alternative */
15260 } while (goodword[++gi] != NUL);
15261 minscore = score;
15262 }
15263 else /* both words continue */
15264 {
15265 /* If not close to the limit, perform a change. Only try changes
15266 * that may lead to a lower score than "minscore".
15267 * round 0: try deleting a char from badword
15268 * round 1: try inserting a char in badword */
15269 for (round = 0; round <= 1; ++round)
15270 {
15271 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15272 if (score_off < minscore)
15273 {
15274 if (score_off + SCORE_EDIT_MIN >= minscore)
15275 {
15276 /* Near the limit, rest of the words must match. We
15277 * can check that right now, no need to push an item
15278 * onto the stack. */
15279 bi2 = bi + 1 - round;
15280 gi2 = gi + round;
15281 while (goodword[gi2] == badword[bi2])
15282 {
15283 if (goodword[gi2] == NUL)
15284 {
15285 minscore = score_off;
15286 break;
15287 }
15288 ++bi2;
15289 ++gi2;
15290 }
15291 }
15292 else
15293 {
15294 /* try deleting/inserting a character later */
15295 stack[stackidx].badi = bi + 1 - round;
15296 stack[stackidx].goodi = gi + round;
15297 stack[stackidx].score = score_off;
15298 ++stackidx;
15299 }
15300 }
15301 }
15302
15303 if (score + SCORE_SWAP < minscore)
15304 {
15305 /* If swapping two characters makes a match then the
15306 * substitution is more expensive, thus there is no need to
15307 * try both. */
15308 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15309 {
15310 /* Swap two characters, that is: skip them. */
15311 gi += 2;
15312 bi += 2;
15313 score += SCORE_SWAP;
15314 continue;
15315 }
15316 }
15317
15318 /* Substitute one character for another which is the same
15319 * thing as deleting a character from both goodword and badword.
15320 * Use a better score when there is only a case difference. */
15321 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15322 score += SCORE_ICASE;
15323 else
15324 {
15325 /* For a similar character use SCORE_SIMILAR. */
15326 if (slang != NULL
15327 && slang->sl_has_map
15328 && similar_chars(slang, gc, bc))
15329 score += SCORE_SIMILAR;
15330 else
15331 score += SCORE_SUBST;
15332 }
15333
15334 if (score < minscore)
15335 {
15336 /* Do the substitution. */
15337 ++gi;
15338 ++bi;
15339 continue;
15340 }
15341 }
15342pop:
15343 /*
15344 * Get here to try the next alternative, pop it from the stack.
15345 */
15346 if (stackidx == 0) /* stack is empty, finished */
15347 break;
15348
15349 /* pop an item from the stack */
15350 --stackidx;
15351 gi = stack[stackidx].goodi;
15352 bi = stack[stackidx].badi;
15353 score = stack[stackidx].score;
15354 }
15355
15356 /* When the score goes over "limit" it may actually be much higher.
15357 * Return a very large number to avoid going below the limit when giving a
15358 * bonus. */
15359 if (minscore > limit)
15360 return SCORE_MAXMAX;
15361 return minscore;
15362}
15363
15364#ifdef FEAT_MBYTE
15365/*
15366 * Multi-byte version of spell_edit_score_limit().
15367 * Keep it in sync with the above!
15368 */
15369 static int
15370spell_edit_score_limit_w(slang, badword, goodword, limit)
15371 slang_T *slang;
15372 char_u *badword;
15373 char_u *goodword;
15374 int limit;
15375{
15376 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15377 int stackidx;
15378 int bi, gi;
15379 int bi2, gi2;
15380 int bc, gc;
15381 int score;
15382 int score_off;
15383 int minscore;
15384 int round;
15385 char_u *p;
15386 int wbadword[MAXWLEN];
15387 int wgoodword[MAXWLEN];
15388
15389 /* Get the characters from the multi-byte strings and put them in an
15390 * int array for easy access. */
15391 bi = 0;
15392 for (p = badword; *p != NUL; )
15393 wbadword[bi++] = mb_cptr2char_adv(&p);
15394 wbadword[bi++] = 0;
15395 gi = 0;
15396 for (p = goodword; *p != NUL; )
15397 wgoodword[gi++] = mb_cptr2char_adv(&p);
15398 wgoodword[gi++] = 0;
15399
15400 /*
15401 * The idea is to go from start to end over the words. So long as
15402 * characters are equal just continue, this always gives the lowest score.
15403 * When there is a difference try several alternatives. Each alternative
15404 * increases "score" for the edit distance. Some of the alternatives are
15405 * pushed unto a stack and tried later, some are tried right away. At the
15406 * end of the word the score for one alternative is known. The lowest
15407 * possible score is stored in "minscore".
15408 */
15409 stackidx = 0;
15410 bi = 0;
15411 gi = 0;
15412 score = 0;
15413 minscore = limit + 1;
15414
15415 for (;;)
15416 {
15417 /* Skip over an equal part, score remains the same. */
15418 for (;;)
15419 {
15420 bc = wbadword[bi];
15421 gc = wgoodword[gi];
15422
15423 if (bc != gc) /* stop at a char that's different */
15424 break;
15425 if (bc == NUL) /* both words end */
15426 {
15427 if (score < minscore)
15428 minscore = score;
15429 goto pop; /* do next alternative */
15430 }
15431 ++bi;
15432 ++gi;
15433 }
15434
15435 if (gc == NUL) /* goodword ends, delete badword chars */
15436 {
15437 do
15438 {
15439 if ((score += SCORE_DEL) >= minscore)
15440 goto pop; /* do next alternative */
15441 } while (wbadword[++bi] != NUL);
15442 minscore = score;
15443 }
15444 else if (bc == NUL) /* badword ends, insert badword chars */
15445 {
15446 do
15447 {
15448 if ((score += SCORE_INS) >= minscore)
15449 goto pop; /* do next alternative */
15450 } while (wgoodword[++gi] != NUL);
15451 minscore = score;
15452 }
15453 else /* both words continue */
15454 {
15455 /* If not close to the limit, perform a change. Only try changes
15456 * that may lead to a lower score than "minscore".
15457 * round 0: try deleting a char from badword
15458 * round 1: try inserting a char in badword */
15459 for (round = 0; round <= 1; ++round)
15460 {
15461 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15462 if (score_off < minscore)
15463 {
15464 if (score_off + SCORE_EDIT_MIN >= minscore)
15465 {
15466 /* Near the limit, rest of the words must match. We
15467 * can check that right now, no need to push an item
15468 * onto the stack. */
15469 bi2 = bi + 1 - round;
15470 gi2 = gi + round;
15471 while (wgoodword[gi2] == wbadword[bi2])
15472 {
15473 if (wgoodword[gi2] == NUL)
15474 {
15475 minscore = score_off;
15476 break;
15477 }
15478 ++bi2;
15479 ++gi2;
15480 }
15481 }
15482 else
15483 {
15484 /* try deleting a character from badword later */
15485 stack[stackidx].badi = bi + 1 - round;
15486 stack[stackidx].goodi = gi + round;
15487 stack[stackidx].score = score_off;
15488 ++stackidx;
15489 }
15490 }
15491 }
15492
15493 if (score + SCORE_SWAP < minscore)
15494 {
15495 /* If swapping two characters makes a match then the
15496 * substitution is more expensive, thus there is no need to
15497 * try both. */
15498 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15499 {
15500 /* Swap two characters, that is: skip them. */
15501 gi += 2;
15502 bi += 2;
15503 score += SCORE_SWAP;
15504 continue;
15505 }
15506 }
15507
15508 /* Substitute one character for another which is the same
15509 * thing as deleting a character from both goodword and badword.
15510 * Use a better score when there is only a case difference. */
15511 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15512 score += SCORE_ICASE;
15513 else
15514 {
15515 /* For a similar character use SCORE_SIMILAR. */
15516 if (slang != NULL
15517 && slang->sl_has_map
15518 && similar_chars(slang, gc, bc))
15519 score += SCORE_SIMILAR;
15520 else
15521 score += SCORE_SUBST;
15522 }
15523
15524 if (score < minscore)
15525 {
15526 /* Do the substitution. */
15527 ++gi;
15528 ++bi;
15529 continue;
15530 }
15531 }
15532pop:
15533 /*
15534 * Get here to try the next alternative, pop it from the stack.
15535 */
15536 if (stackidx == 0) /* stack is empty, finished */
15537 break;
15538
15539 /* pop an item from the stack */
15540 --stackidx;
15541 gi = stack[stackidx].goodi;
15542 bi = stack[stackidx].badi;
15543 score = stack[stackidx].score;
15544 }
15545
15546 /* When the score goes over "limit" it may actually be much higher.
15547 * Return a very large number to avoid going below the limit when giving a
15548 * bonus. */
15549 if (minscore > limit)
15550 return SCORE_MAXMAX;
15551 return minscore;
15552}
15553#endif
15554
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015555/*
15556 * ":spellinfo"
15557 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015558 void
15559ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015560 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015561{
15562 int lpi;
15563 langp_T *lp;
15564 char_u *p;
15565
15566 if (no_spell_checking(curwin))
15567 return;
15568
15569 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015570 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015571 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015572 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015573 msg_puts((char_u *)"file: ");
15574 msg_puts(lp->lp_slang->sl_fname);
15575 msg_putchar('\n');
15576 p = lp->lp_slang->sl_info;
15577 if (p != NULL)
15578 {
15579 msg_puts(p);
15580 msg_putchar('\n');
15581 }
15582 }
15583 msg_end();
15584}
15585
Bram Moolenaar4770d092006-01-12 23:22:24 +000015586#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15587#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015588#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015589#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15590#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015591
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015592/*
15593 * ":spelldump"
15594 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015595 void
15596ex_spelldump(eap)
15597 exarg_T *eap;
15598{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015599 char_u *spl;
15600 long dummy;
15601
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015602 if (no_spell_checking(curwin))
15603 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015604 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015605
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015606 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015607 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +020015608
15609 /* enable spelling locally in the new window */
15610 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
15611 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
15612 vim_free(spl);
15613
Bram Moolenaar860cae12010-06-05 23:22:07 +020015614 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015615 return;
15616
Bram Moolenaar860cae12010-06-05 23:22:07 +020015617 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015618
15619 /* Delete the empty line that we started with. */
15620 if (curbuf->b_ml.ml_line_count > 1)
15621 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15622
15623 redraw_later(NOT_VALID);
15624}
15625
15626/*
15627 * Go through all possible words and:
15628 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15629 * "ic" and "dir" are not used.
15630 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15631 */
15632 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015633spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015634 char_u *pat; /* leading part of the word */
15635 int ic; /* ignore case */
15636 int *dir; /* direction for adding matches */
15637 int dumpflags_arg; /* DUMPFLAG_* */
15638{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015639 langp_T *lp;
15640 slang_T *slang;
15641 idx_T arridx[MAXWLEN];
15642 int curi[MAXWLEN];
15643 char_u word[MAXWLEN];
15644 int c;
15645 char_u *byts;
15646 idx_T *idxs;
15647 linenr_T lnum = 0;
15648 int round;
15649 int depth;
15650 int n;
15651 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015652 char_u *region_names = NULL; /* region names being used */
15653 int do_region = TRUE; /* dump region names and numbers */
15654 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015655 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015656 int dumpflags = dumpflags_arg;
15657 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015658
Bram Moolenaard0131a82006-03-04 21:46:13 +000015659 /* When ignoring case or when the pattern starts with capital pass this on
15660 * to dump_word(). */
15661 if (pat != NULL)
15662 {
15663 if (ic)
15664 dumpflags |= DUMPFLAG_ICASE;
15665 else
15666 {
15667 n = captype(pat, NULL);
15668 if (n == WF_ONECAP)
15669 dumpflags |= DUMPFLAG_ONECAP;
15670 else if (n == WF_ALLCAP
15671#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015672 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015673#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015674 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015675#endif
15676 )
15677 dumpflags |= DUMPFLAG_ALLCAP;
15678 }
15679 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015680
Bram Moolenaar7887d882005-07-01 22:33:52 +000015681 /* Find out if we can support regions: All languages must support the same
15682 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015683 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015684 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015685 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015686 p = lp->lp_slang->sl_regions;
15687 if (p[0] != 0)
15688 {
15689 if (region_names == NULL) /* first language with regions */
15690 region_names = p;
15691 else if (STRCMP(region_names, p) != 0)
15692 {
15693 do_region = FALSE; /* region names are different */
15694 break;
15695 }
15696 }
15697 }
15698
15699 if (do_region && region_names != NULL)
15700 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015701 if (pat == NULL)
15702 {
15703 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15704 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15705 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015706 }
15707 else
15708 do_region = FALSE;
15709
15710 /*
15711 * Loop over all files loaded for the entries in 'spelllang'.
15712 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015713 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015714 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015715 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015716 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015717 if (slang->sl_fbyts == NULL) /* reloading failed */
15718 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015719
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015720 if (pat == NULL)
15721 {
15722 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15723 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15724 }
15725
15726 /* When matching with a pattern and there are no prefixes only use
15727 * parts of the tree that match "pat". */
15728 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015729 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015730 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015731 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015732
15733 /* round 1: case-folded tree
15734 * round 2: keep-case tree */
15735 for (round = 1; round <= 2; ++round)
15736 {
15737 if (round == 1)
15738 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015739 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015740 byts = slang->sl_fbyts;
15741 idxs = slang->sl_fidxs;
15742 }
15743 else
15744 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015745 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015746 byts = slang->sl_kbyts;
15747 idxs = slang->sl_kidxs;
15748 }
15749 if (byts == NULL)
15750 continue; /* array is empty */
15751
15752 depth = 0;
15753 arridx[0] = 0;
15754 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015755 while (depth >= 0 && !got_int
15756 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015757 {
15758 if (curi[depth] > byts[arridx[depth]])
15759 {
15760 /* Done all bytes at this node, go up one level. */
15761 --depth;
15762 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015763 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015764 }
15765 else
15766 {
15767 /* Do one more byte at this node. */
15768 n = arridx[depth] + curi[depth];
15769 ++curi[depth];
15770 c = byts[n];
15771 if (c == 0)
15772 {
15773 /* End of word, deal with the word.
15774 * Don't use keep-case words in the fold-case tree,
15775 * they will appear in the keep-case tree.
15776 * Only use the word when the region matches. */
15777 flags = (int)idxs[n];
15778 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015779 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015780 && (do_region
15781 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015782 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015783 & lp->lp_region) != 0))
15784 {
15785 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015786 if (!do_region)
15787 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015788
15789 /* Dump the basic word if there is no prefix or
15790 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015791 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015792 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015793 {
15794 dump_word(slang, word, pat, dir,
15795 dumpflags, flags, lnum);
15796 if (pat == NULL)
15797 ++lnum;
15798 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015799
15800 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015801 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015802 lnum = dump_prefixes(slang, word, pat, dir,
15803 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015804 }
15805 }
15806 else
15807 {
15808 /* Normal char, go one level deeper. */
15809 word[depth++] = c;
15810 arridx[depth] = idxs[n];
15811 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015812
15813 /* Check if this characters matches with the pattern.
15814 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015815 * Always ignore case here, dump_word() will check
15816 * proper case later. This isn't exactly right when
15817 * length changes for multi-byte characters with
15818 * ignore case... */
15819 if (depth <= patlen
15820 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015821 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015822 }
15823 }
15824 }
15825 }
15826 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015827}
15828
15829/*
15830 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015831 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015832 */
15833 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015834dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015835 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015836 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015837 char_u *pat;
15838 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015839 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015840 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015841 linenr_T lnum;
15842{
15843 int keepcap = FALSE;
15844 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015845 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015846 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015847 char_u badword[MAXWLEN + 10];
15848 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015849 int flags = wordflags;
15850
15851 if (dumpflags & DUMPFLAG_ONECAP)
15852 flags |= WF_ONECAP;
15853 if (dumpflags & DUMPFLAG_ALLCAP)
15854 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015855
Bram Moolenaar4770d092006-01-12 23:22:24 +000015856 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015857 {
15858 /* Need to fix case according to "flags". */
15859 make_case_word(word, cword, flags);
15860 p = cword;
15861 }
15862 else
15863 {
15864 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015865 if ((dumpflags & DUMPFLAG_KEEPCASE)
15866 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015867 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015868 keepcap = TRUE;
15869 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015870 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015871
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015872 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015873 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015874 /* Add flags and regions after a slash. */
15875 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015876 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015877 STRCPY(badword, p);
15878 STRCAT(badword, "/");
15879 if (keepcap)
15880 STRCAT(badword, "=");
15881 if (flags & WF_BANNED)
15882 STRCAT(badword, "!");
15883 else if (flags & WF_RARE)
15884 STRCAT(badword, "?");
15885 if (flags & WF_REGION)
15886 for (i = 0; i < 7; ++i)
15887 if (flags & (0x10000 << i))
15888 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15889 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015890 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015891
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015892 if (dumpflags & DUMPFLAG_COUNT)
15893 {
15894 hashitem_T *hi;
15895
15896 /* Include the word count for ":spelldump!". */
15897 hi = hash_find(&slang->sl_wordcount, tw);
15898 if (!HASHITEM_EMPTY(hi))
15899 {
15900 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15901 tw, HI2WC(hi)->wc_count);
15902 p = IObuff;
15903 }
15904 }
15905
15906 ml_append(lnum, p, (colnr_T)0, FALSE);
15907 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015908 else if (((dumpflags & DUMPFLAG_ICASE)
15909 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15910 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015911 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015912 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015913 /* if dir was BACKWARD then honor it just once */
15914 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015915}
15916
15917/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015918 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15919 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015920 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015921 * Return the updated line number.
15922 */
15923 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015924dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015925 slang_T *slang;
15926 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015927 char_u *pat;
15928 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015929 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015930 int flags; /* flags with prefix ID */
15931 linenr_T startlnum;
15932{
15933 idx_T arridx[MAXWLEN];
15934 int curi[MAXWLEN];
15935 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015936 char_u word_up[MAXWLEN];
15937 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015938 int c;
15939 char_u *byts;
15940 idx_T *idxs;
15941 linenr_T lnum = startlnum;
15942 int depth;
15943 int n;
15944 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015945 int i;
15946
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015947 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015948 * upper-case letter in word_up[]. */
15949 c = PTR2CHAR(word);
15950 if (SPELL_TOUPPER(c) != c)
15951 {
15952 onecap_copy(word, word_up, TRUE);
15953 has_word_up = TRUE;
15954 }
15955
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015956 byts = slang->sl_pbyts;
15957 idxs = slang->sl_pidxs;
15958 if (byts != NULL) /* array not is empty */
15959 {
15960 /*
15961 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015962 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015963 */
15964 depth = 0;
15965 arridx[0] = 0;
15966 curi[0] = 1;
15967 while (depth >= 0 && !got_int)
15968 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015969 n = arridx[depth];
15970 len = byts[n];
15971 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015972 {
15973 /* Done all bytes at this node, go up one level. */
15974 --depth;
15975 line_breakcheck();
15976 }
15977 else
15978 {
15979 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015980 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015981 ++curi[depth];
15982 c = byts[n];
15983 if (c == 0)
15984 {
15985 /* End of prefix, find out how many IDs there are. */
15986 for (i = 1; i < len; ++i)
15987 if (byts[n + i] != 0)
15988 break;
15989 curi[depth] += i - 1;
15990
Bram Moolenaar53805d12005-08-01 07:08:33 +000015991 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15992 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015993 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015994 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015995 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015996 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015997 : flags, lnum);
15998 if (lnum != 0)
15999 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016000 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000016001
16002 /* Check for prefix that matches the word when the
16003 * first letter is upper-case, but only if the prefix has
16004 * a condition. */
16005 if (has_word_up)
16006 {
16007 c = valid_word_prefix(i, n, flags, word_up, slang,
16008 TRUE);
16009 if (c != 0)
16010 {
16011 vim_strncpy(prefix + depth, word_up,
16012 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016013 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000016014 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000016015 : flags, lnum);
16016 if (lnum != 0)
16017 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000016018 }
16019 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000016020 }
16021 else
16022 {
16023 /* Normal char, go one level deeper. */
16024 prefix[depth++] = c;
16025 arridx[depth] = idxs[n];
16026 curi[depth] = 1;
16027 }
16028 }
16029 }
16030 }
16031
16032 return lnum;
16033}
16034
Bram Moolenaar95529562005-08-25 21:21:38 +000016035/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016036 * Move "p" to the end of word "start".
16037 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000016038 */
16039 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020016040spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000016041 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020016042 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000016043{
16044 char_u *p = start;
16045
Bram Moolenaar860cae12010-06-05 23:22:07 +020016046 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000016047 mb_ptr_adv(p);
16048 return p;
16049}
16050
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016051#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016052/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016053 * For Insert mode completion CTRL-X s:
16054 * Find start of the word in front of column "startcol".
16055 * We don't check if it is badly spelled, with completion we can only change
16056 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016057 * Returns the column number of the word.
16058 */
16059 int
16060spell_word_start(startcol)
16061 int startcol;
16062{
16063 char_u *line;
16064 char_u *p;
16065 int col = 0;
16066
Bram Moolenaar95529562005-08-25 21:21:38 +000016067 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016068 return startcol;
16069
16070 /* Find a word character before "startcol". */
16071 line = ml_get_curline();
16072 for (p = line + startcol; p > line; )
16073 {
16074 mb_ptr_back(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +010016075 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016076 break;
16077 }
16078
16079 /* Go back to start of the word. */
16080 while (p > line)
16081 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016082 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016083 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016084 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016085 break;
16086 col = 0;
16087 }
16088
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016089 return col;
16090}
16091
16092/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016093 * Need to check for 'spellcapcheck' now, the word is removed before
16094 * expand_spelling() is called. Therefore the ugly global variable.
16095 */
16096static int spell_expand_need_cap;
16097
16098 void
16099spell_expand_check_cap(col)
16100 colnr_T col;
16101{
16102 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16103}
16104
16105/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016106 * Get list of spelling suggestions.
16107 * Used for Insert mode completion CTRL-X ?.
16108 * Returns the number of matches. The matches are in "matchp[]", array of
16109 * allocated strings.
16110 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016111 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016112expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016113 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016114 char_u *pat;
16115 char_u ***matchp;
16116{
16117 garray_T ga;
16118
Bram Moolenaar4770d092006-01-12 23:22:24 +000016119 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016120 *matchp = ga.ga_data;
16121 return ga.ga_len;
16122}
16123#endif
16124
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016125#endif /* FEAT_SPELL */