blob: 3645dd3c45b8fdb6ca72338e2fc4921cdf2db5af [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000080 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#include "vim.h"
307
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000308#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000309
Bram Moolenaar4770d092006-01-12 23:22:24 +0000310#ifndef UNIX /* it's in os_unix.h for Unix */
311# include <time.h> /* for time_t */
312#endif
313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000314#define MAXWLEN 250 /* Assume max. word len is this many bytes.
315 Some places assume a word length fits in a
316 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000317
Bram Moolenaare52325c2005-08-22 22:54:29 +0000318/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000319 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000320#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000321typedef int idx_T;
322#else
323typedef long idx_T;
324#endif
325
Bram Moolenaar56f78042010-12-08 17:09:32 +0100326#ifdef VMS
327# define SPL_FNAME_TMPL "%s_%s.spl"
328# define SPL_FNAME_ADD "_add."
329# define SPL_FNAME_ASCII "_ascii."
330#else
331# define SPL_FNAME_TMPL "%s.%s.spl"
332# define SPL_FNAME_ADD ".add."
333# define SPL_FNAME_ASCII ".ascii."
334#endif
335
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000336/* Flags used for a word. Only the lowest byte can be used, the region byte
337 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000338#define WF_REGION 0x01 /* region byte follows */
339#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
340#define WF_ALLCAP 0x04 /* word must be all capitals */
341#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000342#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000343#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000344#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000345#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000347/* for <flags2>, shifted up one byte to be used in wn_flags */
348#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000349#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000350#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000351#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000352#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
353#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000354
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000355/* only used for su_badflags */
356#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
357
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000358#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000359
Bram Moolenaar53805d12005-08-01 07:08:33 +0000360/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000361#define WFP_RARE 0x01 /* rare prefix */
362#define WFP_NC 0x02 /* prefix is not combining */
363#define WFP_UP 0x04 /* to-upper prefix */
364#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
365#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000366
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000367/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
368 * byte) and prefcondnr (two bytes). */
369#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
370#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
371#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
372#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
373 * COMPOUNDPERMITFLAG */
374#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
375 * COMPOUNDFORBIDFLAG */
376
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000377
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000378/* flags for <compoptions> */
379#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
380#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
381#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
382#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
383
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000384/* Special byte values for <byte>. Some are only used in the tree for
385 * postponed prefixes, some only in the other trees. This is a bit messy... */
386#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000387 * postponed prefix: no <pflags> */
388#define BY_INDEX 1 /* child is shared, index follows */
389#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
390 * postponed prefix: <pflags> follows */
391#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
392 * follow; never used in prefix tree */
393#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000394
Bram Moolenaar4770d092006-01-12 23:22:24 +0000395/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
396 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000397 * One replacement: from "ft_from" to "ft_to". */
398typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000399{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400 char_u *ft_from;
401 char_u *ft_to;
402} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000403
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000404/* Info from "SAL" entries in ".aff" file used in sl_sal.
405 * The info is split for quick processing by spell_soundfold().
406 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
407typedef struct salitem_S
408{
409 char_u *sm_lead; /* leading letters */
410 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000411 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000412 char_u *sm_rules; /* rules like ^, $, priority */
413 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000414#ifdef FEAT_MBYTE
415 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000416 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000417 int *sm_to_w; /* wide character copy of "sm_to" */
418#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000419} salitem_T;
420
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000421#ifdef FEAT_MBYTE
422typedef int salfirst_T;
423#else
424typedef short salfirst_T;
425#endif
426
Bram Moolenaar5195e452005-08-19 20:32:47 +0000427/* Values for SP_*ERROR are negative, positive values are used by
428 * read_cnt_string(). */
429#define SP_TRUNCERROR -1 /* spell file truncated error */
430#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000431#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000432
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000433/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000434 * Structure used to store words and other info for one language, loaded from
435 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000436 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
437 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
438 *
439 * The "byts" array stores the possible bytes in each tree node, preceded by
440 * the number of possible bytes, sorted on byte value:
441 * <len> <byte1> <byte2> ...
442 * The "idxs" array stores the index of the child node corresponding to the
443 * byte in "byts".
444 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000445 * the flags, region mask and affixID for the word. There may be several
446 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000447 */
448typedef struct slang_S slang_T;
449struct slang_S
450{
451 slang_T *sl_next; /* next language */
452 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000453 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000454 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000455
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000458 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000459 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000460 char_u *sl_pbyts; /* prefix tree word bytes */
461 idx_T *sl_pidxs; /* prefix tree word indexes */
462
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000463 char_u *sl_info; /* infotext string or NULL */
464
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000465 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000466
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000467 char_u *sl_midword; /* MIDWORD string or NULL */
468
Bram Moolenaar4770d092006-01-12 23:22:24 +0000469 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
470
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000471 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000472 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000473 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000474 int sl_compoptions; /* COMP_* flags */
475 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000476 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000477 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000478 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000480 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000481 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000482 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
483 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000484
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000485 int sl_prefixcnt; /* number of items in "sl_prefprog" */
486 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488 garray_T sl_rep; /* list of fromto_T entries from REP lines */
489 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
490 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000492 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493 there is none */
494 int sl_followup; /* SAL followup */
495 int sl_collapse; /* SAL collapse_result */
496 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000497 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
498 * "sl_sal_first" maps chars, when has_mbyte
499 * "sl_sal" is a list of wide char lists. */
500 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
501 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000502 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000503
504 /* Info from the .sug file. Loaded on demand. */
505 time_t sl_sugtime; /* timestamp for .sug file */
506 char_u *sl_sbyts; /* soundfolded word bytes */
507 idx_T *sl_sidxs; /* soundfolded word indexes */
508 buf_T *sl_sugbuf; /* buffer with word number table */
509 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
510 load */
511
Bram Moolenaarea424162005-06-16 21:51:00 +0000512 int sl_has_map; /* TRUE if there is a MAP line */
513#ifdef FEAT_MBYTE
514 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
515 int sl_map_array[256]; /* MAP for first 256 chars */
516#else
517 char_u sl_map_array[256]; /* MAP for first 256 chars */
518#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000519 hashtab_T sl_sounddone; /* table with soundfolded words that have
520 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000521};
522
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000523/* First language that is loaded, start of the linked list of loaded
524 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525static slang_T *first_lang = NULL;
526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527/* Flags used in .spl file for soundsalike flags. */
528#define SAL_F0LLOWUP 1
529#define SAL_COLLAPSE 2
530#define SAL_REM_ACCENTS 4
531
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000532/*
533 * Structure used in "b_langp", filled from 'spelllang'.
534 */
535typedef struct langp_S
536{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000537 slang_T *lp_slang; /* info for this language */
538 slang_T *lp_sallang; /* language used for sound folding or NULL */
539 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000540 int lp_region; /* bitmask for region or REGION_ALL */
541} langp_T;
542
543#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
544
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000545#define REGION_ALL 0xff /* word valid in all regions */
546
Bram Moolenaar5195e452005-08-19 20:32:47 +0000547#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
548#define VIMSPELLMAGICL 8
549#define VIMSPELLVERSION 50
550
Bram Moolenaar4770d092006-01-12 23:22:24 +0000551#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
552#define VIMSUGMAGICL 6
553#define VIMSUGVERSION 1
554
Bram Moolenaar5195e452005-08-19 20:32:47 +0000555/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
556#define SN_REGION 0 /* <regionname> section */
557#define SN_CHARFLAGS 1 /* charflags section */
558#define SN_MIDWORD 2 /* <midword> section */
559#define SN_PREFCOND 3 /* <prefcond> section */
560#define SN_REP 4 /* REP items section */
561#define SN_SAL 5 /* SAL items section */
562#define SN_SOFO 6 /* soundfolding section */
563#define SN_MAP 7 /* MAP items section */
564#define SN_COMPOUND 8 /* compound words section */
565#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000566#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000567#define SN_SUGFILE 11 /* timestamp for .sug file */
568#define SN_REPSAL 12 /* REPSAL items section */
569#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000570#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000571#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000572#define SN_END 255 /* end of sections */
573
574#define SNF_REQUIRED 1 /* <sectionflags>: required section */
575
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576/* Result values. Lower number is accepted over higher one. */
577#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000578#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000579#define SP_RARE 1
580#define SP_LOCAL 2
581#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000582
Bram Moolenaar7887d882005-07-01 22:33:52 +0000583/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000584static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000585
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586typedef struct wordcount_S
587{
588 short_u wc_count; /* nr of times word was seen */
589 char_u wc_word[1]; /* word, actually longer */
590} wordcount_T;
591
592static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000593#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000594#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
595#define MAXWORDCOUNT 0xffff
596
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000597/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598 * Information used when looking for suggestions.
599 */
600typedef struct suginfo_S
601{
602 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000605 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u *su_badptr; /* start of bad word in line */
608 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000609 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
611 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000612 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000613 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000614 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615} suginfo_T;
616
617/* One word suggestion. Used in "si_ga". */
618typedef struct suggest_S
619{
620 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000621 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622 int st_orglen; /* length of replaced text */
623 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624 int st_altscore; /* used when st_score compares equal */
625 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000626 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000627 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628} suggest_T;
629
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000630#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000631
Bram Moolenaar4770d092006-01-12 23:22:24 +0000632/* TRUE if a word appears in the list of banned words. */
633#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
634
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000635/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000636 * what is displayed, because when rescore_suggestions() is called the score
637 * may change and wrong suggestions may be removed later. */
638#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639
640/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
641 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000642#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643
644/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000645#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000646#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000648#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000650#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000651#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000652#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653#define SCORE_SUBST 93 /* substitute a character */
654#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000658#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000659#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000660#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000661#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000662#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000664#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000665#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
666 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000667
Bram Moolenaar4770d092006-01-12 23:22:24 +0000668#define SCORE_COMMON1 30 /* subtracted for words seen before */
669#define SCORE_COMMON2 40 /* subtracted for words often seen */
670#define SCORE_COMMON3 50 /* subtracted for words very often seen */
671#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
672#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
673
674/* When trying changed soundfold words it becomes slow when trying more than
675 * two changes. With less then two changes it's slightly faster but we miss a
676 * few good suggestions. In rare cases we need to try three of four changes.
677 */
678#define SCORE_SFMAX1 200 /* maximum score for first try */
679#define SCORE_SFMAX2 300 /* maximum score for second try */
680#define SCORE_SFMAX3 400 /* maximum score for third try */
681
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000683#define SCORE_MAXMAX 999999 /* accept any score */
684#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
685
686/* for spell_edit_score_limit() we need to know the minimum value of
687 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
688#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689
690/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691 * Structure to store info for word matching.
692 */
693typedef struct matchinf_S
694{
695 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000696
697 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000698 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000699 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000701 char_u *mi_cend; /* char after what was used for
702 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000703
704 /* case-folded text */
705 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000706 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000707
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 /* for when checking word after a prefix */
709 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000710 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000711 int mi_prefcnt; /* number of entries at mi_prefarridx */
712 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000713#ifdef FEAT_MBYTE
714 int mi_cprefixlen; /* byte length of prefix in original
715 case */
716#else
717# define mi_cprefixlen mi_prefixlen /* it's the same value */
718#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000719
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000720 /* for when checking a compound word */
721 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000722 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
723 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000724 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000725
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000726 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000727 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000728 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200729 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000730
731 /* for NOBREAK */
732 int mi_result2; /* "mi_resul" without following word */
733 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000734} matchinf_T;
735
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000736/*
737 * The tables used for recognizing word characters according to spelling.
738 * These are only used for the first 256 characters of 'encoding'.
739 */
740typedef struct spelltab_S
741{
742 char_u st_isw[256]; /* flags: is word char */
743 char_u st_isu[256]; /* flags: is uppercase char */
744 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000746} spelltab_T;
747
748static spelltab_T spelltab;
749static int did_set_spelltab;
750
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000751#define CF_WORD 0x01
752#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000753
754static void clear_spell_chartab __ARGS((spelltab_T *sp));
755static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200756static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000757static int spell_iswordp_nmw __ARGS((char_u *p));
758#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000759static int spell_mb_isword_class __ARGS((int cl));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000761#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000762static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000763
764/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000766 */
767typedef enum
768{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_START = 0, /* At start of node check for NUL bytes (goodword
770 * ends); if badword ends there is a match, otherwise
771 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000772 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000773 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
775 STATE_PLAIN, /* Use each byte of the node. */
776 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000777 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000778 STATE_INS, /* Insert a byte in the bad word. */
779 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNSWAP, /* Undo swap two characters. */
781 STATE_SWAP3, /* Swap two characters over three. */
782 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000783 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000784 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000785 STATE_REP_INI, /* Prepare for using REP items. */
786 STATE_REP, /* Use matching REP items from the .aff file. */
787 STATE_REP_UNDO, /* Undo a REP item replacement. */
788 STATE_FINAL /* End of this node. */
789} state_T;
790
791/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000792 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 */
794typedef struct trystate_S
795{
Bram Moolenaarea424162005-06-16 21:51:00 +0000796 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000797 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000798 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000799 short ts_curi; /* index in list of child nodes */
800 char_u ts_fidx; /* index in fword[], case-folded bad word */
801 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
802 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000803 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000804 * PFD_PREFIXTREE or PFD_NOPREFIX */
805 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000806#ifdef FEAT_MBYTE
807 char_u ts_tcharlen; /* number of bytes in tword character */
808 char_u ts_tcharidx; /* current byte index in tword character */
809 char_u ts_isdiff; /* DIFF_ values */
810 char_u ts_fcharstart; /* index in fword where badword char started */
811#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000812 char_u ts_prewordlen; /* length of word in "preword[]" */
813 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000814 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000815 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000816 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000817 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000818 char_u ts_delidx; /* index in fword for char that was deleted,
819 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000820} trystate_T;
821
Bram Moolenaarea424162005-06-16 21:51:00 +0000822/* values for ts_isdiff */
823#define DIFF_NONE 0 /* no different byte (yet) */
824#define DIFF_YES 1 /* different byte found */
825#define DIFF_INSERT 2 /* inserting character */
826
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827/* values for ts_flags */
828#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
829#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000830#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000831
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000833#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000834#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000835#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000836
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000837/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000838#define FIND_FOLDWORD 0 /* find word case-folded */
839#define FIND_KEEPWORD 1 /* find keep-case word */
840#define FIND_PREFIX 2 /* find word after prefix */
841#define FIND_COMPOUND 3 /* find case-folded compound word */
842#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000843
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000844static slang_T *slang_alloc __ARGS((char_u *lang));
845static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000846static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000847static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000849static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000850static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000851static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
852static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000853static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000854static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000855static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000856static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000857static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000858static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000859static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000860static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000861static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000862static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000863static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000864static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
865static int read_charflags_section __ARGS((FILE *fd));
866static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000868static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000869static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
870static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
871static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000872static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
873static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000874static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000875static int init_syl_tab __ARGS((slang_T *slang));
876static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000877static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
878static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000879#ifdef FEAT_MBYTE
880static int *mb_str2wide __ARGS((char_u *s));
881#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000882static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200883static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
884static void clear_midword __ARGS((win_T *buf));
885static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000886static int find_region __ARGS((char_u *rp, char_u *region));
887static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000888static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000889static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000890static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000891static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000892static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000893static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000894static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000895#ifdef FEAT_EVAL
896static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
897#endif
898static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000899static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
900static void suggest_load_files __ARGS((void));
901static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000903static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000904static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000905static void suggest_try_special __ARGS((suginfo_T *su));
906static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000907static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
908static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000909#ifdef FEAT_MBYTE
910static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
911#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000912static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913static void score_comp_sal __ARGS((suginfo_T *su));
914static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000915static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000917static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static void suggest_try_soundalike_finish __ARGS((void));
919static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
920static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000922static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000923static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000924static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
925static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000926static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000927static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000928static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000929static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000930static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
931static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
932static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000934static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000935#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000937static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
938static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#ifdef FEAT_MBYTE
940static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
941#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000942static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
943static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000944static buf_T *open_spellbuf __ARGS((void));
945static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000946
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000947/*
948 * Use our own character-case definitions, because the current locale may
949 * differ from what the .spl file uses.
950 * These must not be called with negative number!
951 */
952#ifndef FEAT_MBYTE
953/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000954# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
955# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000956# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
957#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000958# if defined(HAVE_WCHAR_H)
959# include <wchar.h> /* for towupper() and towlower() */
960# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000961/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
962 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
963 * the "w" library function for characters above 255 if available. */
964# ifdef HAVE_TOWLOWER
965# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000966 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000967# else
968# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000969 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000970# endif
971
972# ifdef HAVE_TOWUPPER
973# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000974 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000975# else
976# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000977 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000978# endif
979
980# ifdef HAVE_ISWUPPER
981# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
982 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
983# else
984# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000985 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000986# endif
987#endif
988
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000989
990static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000991static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000992static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000993static char *e_affname = N_("Affix name too long in %s line %d: %s");
994static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
995static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000996static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000998/* Remember what "z?" replaced. */
999static char_u *repl_from = NULL;
1000static char_u *repl_to = NULL;
1001
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001002/*
1003 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001004 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001005 * "*attrp" is set to the highlight index for a badly spelled word. For a
1006 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001007 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001009 * "capcol" is used to check for a Capitalised word after the end of a
1010 * sentence. If it's zero then perform the check. Return the column where to
1011 * check next, or -1 when no sentence end was found. If it's NULL then don't
1012 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001013 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 * Returns the length of the word in bytes, also when it's OK, so that the
1015 * caller can skip over the word.
1016 */
1017 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001018spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001020 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001021 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001023 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001024{
1025 matchinf_T mi; /* Most things are put in "mi" so that it can
1026 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001027 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001028 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001029 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001030 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001031 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001032
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001033 /* A word never starts at a space or a control character. Return quickly
1034 * then, skipping over the character. */
1035 if (*ptr <= ' ')
1036 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001037
1038 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001039 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001040 return 1;
1041
Bram Moolenaar5195e452005-08-19 20:32:47 +00001042 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001043
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001044 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001045 * 0X99FF. But always do check spelling to find "3GPP" and "11
1046 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001049 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1050 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001051 else
1052 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001053 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055
Bram Moolenaar0c405862005-06-22 22:26:26 +00001056 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001057 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001058 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001059 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001061 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001062 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001063 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001064 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001065
Bram Moolenaar860cae12010-06-05 23:22:07 +02001066 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001067 {
1068 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001069 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001071 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001073 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001074 if (capcol != NULL)
1075 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001076
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001077 /* We always use the characters up to the next non-word character,
1078 * also for bad words. */
1079 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001081 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001082 mi.mi_capflags = 0;
1083 mi.mi_cend = NULL;
1084 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085
Bram Moolenaar5195e452005-08-19 20:32:47 +00001086 /* case-fold the word with one non-word character, so that we can check
1087 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001088 if (*mi.mi_fend != NUL)
1089 mb_ptr_adv(mi.mi_fend);
1090
1091 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1092 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001093 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /* The word is bad unless we recognize it. */
1096 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001097 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001098
1099 /*
1100 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001101 * We check them all, because a word may be matched longer in another
1102 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001103 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001104 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001105 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001106 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001107
1108 /* If reloading fails the language is still in the list but everything
1109 * has been cleared. */
1110 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1111 continue;
1112
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001113 /* Check for a matching word in case-folded words. */
1114 find_word(&mi, FIND_FOLDWORD);
1115
1116 /* Check for a matching word in keep-case words. */
1117 find_word(&mi, FIND_KEEPWORD);
1118
1119 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001120 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001121
1122 /* For a NOBREAK language, may want to use a word without a following
1123 * word as a backup. */
1124 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1125 && mi.mi_result2 != SP_BAD)
1126 {
1127 mi.mi_result = mi.mi_result2;
1128 mi.mi_end = mi.mi_end2;
1129 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001130
1131 /* Count the word in the first language where it's found to be OK. */
1132 if (count_word && mi.mi_result == SP_OK)
1133 {
1134 count_common_word(mi.mi_lp->lp_slang, ptr,
1135 (int)(mi.mi_end - ptr), 1);
1136 count_word = FALSE;
1137 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001138 }
1139
1140 if (mi.mi_result != SP_OK)
1141 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001142 /* If we found a number skip over it. Allows for "42nd". Do flag
1143 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001144 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001145 {
1146 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1147 return nrlen;
1148 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001149
1150 /* When we are at a non-word character there is no error, just
1151 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001152 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001153 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001154 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001155 {
1156 regmatch_T regmatch;
1157
1158 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001159 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001160 regmatch.rm_ic = FALSE;
1161 if (vim_regexec(&regmatch, ptr, 0))
1162 *capcol = (int)(regmatch.endp[0] - ptr);
1163 }
1164
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001165#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001166 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001167 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001168#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001169 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001170 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001171 else if (mi.mi_end == ptr)
1172 /* Always include at least one character. Required for when there
1173 * is a mixup in "midword". */
1174 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001175 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001176 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001177 {
1178 char_u *p, *fp;
1179 int save_result = mi.mi_result;
1180
1181 /* First language in 'spelllang' is NOBREAK. Find first position
1182 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001183 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001184 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001186 p = mi.mi_word;
1187 fp = mi.mi_fword;
1188 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001189 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001190 mb_ptr_adv(p);
1191 mb_ptr_adv(fp);
1192 if (p >= mi.mi_end)
1193 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001194 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001195 find_word(&mi, FIND_COMPOUND);
1196 if (mi.mi_result != SP_BAD)
1197 {
1198 mi.mi_end = p;
1199 break;
1200 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001201 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001202 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001203 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001204 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001205
1206 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001207 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001208 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001209 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001210 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001211 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001212 }
1213
Bram Moolenaar5195e452005-08-19 20:32:47 +00001214 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1215 {
1216 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001217 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001218 return wrongcaplen;
1219 }
1220
Bram Moolenaar51485f02005-06-04 21:55:20 +00001221 return (int)(mi.mi_end - ptr);
1222}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001223
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224/*
1225 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001226 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1227 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1228 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1229 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230 *
1231 * For a match mip->mi_result is updated.
1232 */
1233 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001234find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001236 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001238 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001240 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001241 int endidxcnt = 0;
1242 int len;
1243 int wlen = 0;
1244 int flen;
1245 int c;
1246 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001247 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001248#ifdef FEAT_MBYTE
1249 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001250#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001251 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001252 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001253 slang_T *slang = mip->mi_lp->lp_slang;
1254 unsigned flags;
1255 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001256 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001258 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001259 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001260
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001261 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001262 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001263 /* Check for word with matching case in keep-case tree. */
1264 ptr = mip->mi_word;
1265 flen = 9999; /* no case folding, always enough bytes */
1266 byts = slang->sl_kbyts;
1267 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001268
1269 if (mode == FIND_KEEPCOMPOUND)
1270 /* Skip over the previously found word(s). */
1271 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001272 }
1273 else
1274 {
1275 /* Check for case-folded in case-folded tree. */
1276 ptr = mip->mi_fword;
1277 flen = mip->mi_fwordlen; /* available case-folded bytes */
1278 byts = slang->sl_fbyts;
1279 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001280
1281 if (mode == FIND_PREFIX)
1282 {
1283 /* Skip over the prefix. */
1284 wlen = mip->mi_prefixlen;
1285 flen -= mip->mi_prefixlen;
1286 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001287 else if (mode == FIND_COMPOUND)
1288 {
1289 /* Skip over the previously found word(s). */
1290 wlen = mip->mi_compoff;
1291 flen -= mip->mi_compoff;
1292 }
1293
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294 }
1295
Bram Moolenaar51485f02005-06-04 21:55:20 +00001296 if (byts == NULL)
1297 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001298
Bram Moolenaar51485f02005-06-04 21:55:20 +00001299 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001300 * Repeat advancing in the tree until:
1301 * - there is a byte that doesn't match,
1302 * - we reach the end of the tree,
1303 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001304 */
1305 for (;;)
1306 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001307 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001308 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001309
1310 len = byts[arridx++];
1311
1312 /* If the first possible byte is a zero the word could end here.
1313 * Remember this index, we first check for the longest word. */
1314 if (byts[arridx] == 0)
1315 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001316 if (endidxcnt == MAXWLEN)
1317 {
1318 /* Must be a corrupted spell file. */
1319 EMSG(_(e_format));
1320 return;
1321 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001322 endlen[endidxcnt] = wlen;
1323 endidx[endidxcnt++] = arridx++;
1324 --len;
1325
1326 /* Skip over the zeros, there can be several flag/region
1327 * combinations. */
1328 while (len > 0 && byts[arridx] == 0)
1329 {
1330 ++arridx;
1331 --len;
1332 }
1333 if (len == 0)
1334 break; /* no children, word must end here */
1335 }
1336
1337 /* Stop looking at end of the line. */
1338 if (ptr[wlen] == NUL)
1339 break;
1340
1341 /* Perform a binary search in the list of accepted bytes. */
1342 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001343 if (c == TAB) /* <Tab> is handled like <Space> */
1344 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001345 lo = arridx;
1346 hi = arridx + len - 1;
1347 while (lo < hi)
1348 {
1349 m = (lo + hi) / 2;
1350 if (byts[m] > c)
1351 hi = m - 1;
1352 else if (byts[m] < c)
1353 lo = m + 1;
1354 else
1355 {
1356 lo = hi = m;
1357 break;
1358 }
1359 }
1360
1361 /* Stop if there is no matching byte. */
1362 if (hi < lo || byts[lo] != c)
1363 break;
1364
1365 /* Continue at the child (if there is one). */
1366 arridx = idxs[lo];
1367 ++wlen;
1368 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001369
1370 /* One space in the good word may stand for several spaces in the
1371 * checked word. */
1372 if (c == ' ')
1373 {
1374 for (;;)
1375 {
1376 if (flen <= 0 && *mip->mi_fend != NUL)
1377 flen = fold_more(mip);
1378 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1379 break;
1380 ++wlen;
1381 --flen;
1382 }
1383 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001384 }
1385
1386 /*
1387 * Verify that one of the possible endings is valid. Try the longest
1388 * first.
1389 */
1390 while (endidxcnt > 0)
1391 {
1392 --endidxcnt;
1393 arridx = endidx[endidxcnt];
1394 wlen = endlen[endidxcnt];
1395
1396#ifdef FEAT_MBYTE
1397 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1398 continue; /* not at first byte of character */
1399#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001400 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001401 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001402 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001403 continue; /* next char is a word character */
1404 word_ends = FALSE;
1405 }
1406 else
1407 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001408 /* The prefix flag is before compound flags. Once a valid prefix flag
1409 * has been found we try compound flags. */
1410 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411
1412#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 {
1415 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001416 * when folding case. This can be slow, take a shortcut when the
1417 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001419 if (STRNCMP(ptr, p, wlen) != 0)
1420 {
1421 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1422 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001423 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001425 }
1426#endif
1427
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001428 /* Check flags and region. For FIND_PREFIX check the condition and
1429 * prefix ID.
1430 * Repeat this if there are more flags/region alternatives until there
1431 * is a match. */
1432 res = SP_BAD;
1433 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1434 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001435 {
1436 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001437
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001438 /* For the fold-case tree check that the case of the checked word
1439 * matches with what the word in the tree requires.
1440 * For keep-case tree the case is always right. For prefixes we
1441 * don't bother to check. */
1442 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001443 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 if (mip->mi_cend != mip->mi_word + wlen)
1445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001446 /* mi_capflags was set for a different word length, need
1447 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001449 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001450 }
1451
Bram Moolenaar0c405862005-06-22 22:26:26 +00001452 if (mip->mi_capflags == WF_KEEPCAP
1453 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001454 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001455 }
1456
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001457 /* When mode is FIND_PREFIX the word must support the prefix:
1458 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001459 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001460 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001461 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001463 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001464 mip->mi_word + mip->mi_cprefixlen, slang,
1465 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001466 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001467 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001468
1469 /* Use the WF_RARE flag for a rare prefix. */
1470 if (c & WF_RAREPFX)
1471 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001472 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473 }
1474
Bram Moolenaar78622822005-08-23 21:00:13 +00001475 if (slang->sl_nobreak)
1476 {
1477 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1478 && (flags & WF_BANNED) == 0)
1479 {
1480 /* NOBREAK: found a valid following word. That's all we
1481 * need to know, so return. */
1482 mip->mi_result = SP_OK;
1483 break;
1484 }
1485 }
1486
1487 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1488 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001490 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001491 * COMPOUNDMIN reject it quickly.
1492 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 * that's too short... Myspell compatibility requires this
1494 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001495 if (((unsigned)flags >> 24) == 0
1496 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001497 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001498#ifdef FEAT_MBYTE
1499 /* For multi-byte chars check character length against
1500 * COMPOUNDMIN. */
1501 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001502 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001503 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1504 wlen - mip->mi_compoff) < slang->sl_compminlen)
1505 continue;
1506#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001507
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001508 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001509 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001510 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1511 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001512 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001513 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001514
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001515 /* Don't allow compounding on a side where an affix was added,
1516 * unless COMPOUNDPERMITFLAG was used. */
1517 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1518 continue;
1519 if (!word_ends && (flags & WF_NOCOMPAFT))
1520 continue;
1521
Bram Moolenaard12a1322005-08-21 22:08:24 +00001522 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001523 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001524 ? slang->sl_compstartflags
1525 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001526 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001527 continue;
1528
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001529 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1530 * discard the compound word. */
1531 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1532 continue;
1533
Bram Moolenaare52325c2005-08-22 22:54:29 +00001534 if (mode == FIND_COMPOUND)
1535 {
1536 int capflags;
1537
1538 /* Need to check the caps type of the appended compound
1539 * word. */
1540#ifdef FEAT_MBYTE
1541 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1542 mip->mi_compoff) != 0)
1543 {
1544 /* case folding may have changed the length */
1545 p = mip->mi_word;
1546 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1547 mb_ptr_adv(p);
1548 }
1549 else
1550#endif
1551 p = mip->mi_word + mip->mi_compoff;
1552 capflags = captype(p, mip->mi_word + wlen);
1553 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1554 && (flags & WF_FIXCAP) != 0))
1555 continue;
1556
1557 if (capflags != WF_ALLCAP)
1558 {
1559 /* When the character before the word is a word
1560 * character we do not accept a Onecap word. We do
1561 * accept a no-caps word, even when the dictionary
1562 * word specifies ONECAP. */
1563 mb_ptr_back(mip->mi_word, p);
1564 if (spell_iswordp_nmw(p)
1565 ? capflags == WF_ONECAP
1566 : (flags & WF_ONECAP) != 0
1567 && capflags != WF_ONECAP)
1568 continue;
1569 }
1570 }
1571
Bram Moolenaar5195e452005-08-19 20:32:47 +00001572 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001573 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001574 * the number of syllables must not be too large. */
1575 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1576 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1577 if (word_ends)
1578 {
1579 char_u fword[MAXWLEN];
1580
1581 if (slang->sl_compsylmax < MAXWLEN)
1582 {
1583 /* "fword" is only needed for checking syllables. */
1584 if (ptr == mip->mi_word)
1585 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1586 else
1587 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1588 }
1589 if (!can_compound(slang, fword, mip->mi_compflags))
1590 continue;
1591 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001592 else if (slang->sl_comprules != NULL
1593 && !match_compoundrule(slang, mip->mi_compflags))
1594 /* The compound flags collected so far do not match any
1595 * COMPOUNDRULE, discard the compounded word. */
1596 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001597 }
1598
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001599 /* Check NEEDCOMPOUND: can't use word without compounding. */
1600 else if (flags & WF_NEEDCOMP)
1601 continue;
1602
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 nobreak_result = SP_OK;
1604
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001605 if (!word_ends)
1606 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001607 int save_result = mip->mi_result;
1608 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001609 langp_T *save_lp = mip->mi_lp;
1610 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001611
1612 /* Check that a valid word follows. If there is one and we
1613 * are compounding, it will set "mi_result", thus we are
1614 * always finished here. For NOBREAK we only check that a
1615 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001616 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001617 if (slang->sl_nobreak)
1618 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001619
1620 /* Find following word in case-folded tree. */
1621 mip->mi_compoff = endlen[endidxcnt];
1622#ifdef FEAT_MBYTE
1623 if (has_mbyte && mode == FIND_KEEPWORD)
1624 {
1625 /* Compute byte length in case-folded word from "wlen":
1626 * byte length in keep-case word. Length may change when
1627 * folding case. This can be slow, take a shortcut when
1628 * the case-folded word is equal to the keep-case word. */
1629 p = mip->mi_fword;
1630 if (STRNCMP(ptr, p, wlen) != 0)
1631 {
1632 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1633 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001634 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001635 }
1636 }
1637#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001638 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001639 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001640 if (flags & WF_COMPROOT)
1641 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001642
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001643 /* For NOBREAK we need to try all NOBREAK languages, at least
1644 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001645 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001646 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001647 if (slang->sl_nobreak)
1648 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001649 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001650 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1651 || !mip->mi_lp->lp_slang->sl_nobreak)
1652 continue;
1653 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001654
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655 find_word(mip, FIND_COMPOUND);
1656
1657 /* When NOBREAK any word that matches is OK. Otherwise we
1658 * need to find the longest match, thus try with keep-case
1659 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001660 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1661 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001662 /* Find following word in keep-case tree. */
1663 mip->mi_compoff = wlen;
1664 find_word(mip, FIND_KEEPCOMPOUND);
1665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001666#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1667 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1668 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001669 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1670 {
1671 /* Check for following word with prefix. */
1672 mip->mi_compoff = c;
1673 find_prefix(mip, FIND_COMPOUND);
1674 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001675#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001676 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001677
1678 if (!slang->sl_nobreak)
1679 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001680 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001681 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001682 if (flags & WF_COMPROOT)
1683 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001684 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001685
Bram Moolenaar78622822005-08-23 21:00:13 +00001686 if (slang->sl_nobreak)
1687 {
1688 nobreak_result = mip->mi_result;
1689 mip->mi_result = save_result;
1690 mip->mi_end = save_end;
1691 }
1692 else
1693 {
1694 if (mip->mi_result == SP_OK)
1695 break;
1696 continue;
1697 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001698 }
1699
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001700 if (flags & WF_BANNED)
1701 res = SP_BANNED;
1702 else if (flags & WF_REGION)
1703 {
1704 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001705 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001706 res = SP_OK;
1707 else
1708 res = SP_LOCAL;
1709 }
1710 else if (flags & WF_RARE)
1711 res = SP_RARE;
1712 else
1713 res = SP_OK;
1714
Bram Moolenaar78622822005-08-23 21:00:13 +00001715 /* Always use the longest match and the best result. For NOBREAK
1716 * we separately keep the longest match without a following good
1717 * word as a fall-back. */
1718 if (nobreak_result == SP_BAD)
1719 {
1720 if (mip->mi_result2 > res)
1721 {
1722 mip->mi_result2 = res;
1723 mip->mi_end2 = mip->mi_word + wlen;
1724 }
1725 else if (mip->mi_result2 == res
1726 && mip->mi_end2 < mip->mi_word + wlen)
1727 mip->mi_end2 = mip->mi_word + wlen;
1728 }
1729 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001730 {
1731 mip->mi_result = res;
1732 mip->mi_end = mip->mi_word + wlen;
1733 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001734 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001735 mip->mi_end = mip->mi_word + wlen;
1736
Bram Moolenaar78622822005-08-23 21:00:13 +00001737 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001738 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001739 }
1740
Bram Moolenaar78622822005-08-23 21:00:13 +00001741 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001742 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001743 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001744}
1745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001747 * Return TRUE if there is a match between the word ptr[wlen] and
1748 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1749 * word.
1750 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1751 * end of ptr[wlen] and the second part matches after it.
1752 */
1753 static int
1754match_checkcompoundpattern(ptr, wlen, gap)
1755 char_u *ptr;
1756 int wlen;
1757 garray_T *gap; /* &sl_comppat */
1758{
1759 int i;
1760 char_u *p;
1761 int len;
1762
1763 for (i = 0; i + 1 < gap->ga_len; i += 2)
1764 {
1765 p = ((char_u **)gap->ga_data)[i + 1];
1766 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1767 {
1768 /* Second part matches at start of following compound word, now
1769 * check if first part matches at end of previous word. */
1770 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001771 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001772 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1773 return TRUE;
1774 }
1775 }
1776 return FALSE;
1777}
1778
1779/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001780 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1781 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001782 */
1783 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001784can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001785 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001786 char_u *word;
1787 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001788{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001789 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001790#ifdef FEAT_MBYTE
1791 char_u uflags[MAXWLEN * 2];
1792 int i;
1793#endif
1794 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001795
1796 if (slang->sl_compprog == NULL)
1797 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001798#ifdef FEAT_MBYTE
1799 if (enc_utf8)
1800 {
1801 /* Need to convert the single byte flags to utf8 characters. */
1802 p = uflags;
1803 for (i = 0; flags[i] != NUL; ++i)
1804 p += mb_char2bytes(flags[i], p);
1805 *p = NUL;
1806 p = uflags;
1807 }
1808 else
1809#endif
1810 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001811 regmatch.regprog = slang->sl_compprog;
1812 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001813 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001814 return FALSE;
1815
Bram Moolenaare52325c2005-08-22 22:54:29 +00001816 /* Count the number of syllables. This may be slow, do it last. If there
1817 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001818 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001819 if (slang->sl_compsylmax < MAXWLEN
1820 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001821 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001822 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001823}
1824
1825/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001826 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1827 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1828 * lines if they don't contain wildcards.
1829 */
1830 static int
1831can_be_compound(sp, slang, compflags, flag)
1832 trystate_T *sp;
1833 slang_T *slang;
1834 char_u *compflags;
1835 int flag;
1836{
1837 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1838 * then it can't possibly compound. */
1839 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1840 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1841 return FALSE;
1842
1843 /* If there are no wildcards, we can check if the flags collected so far
1844 * possibly can form a match with COMPOUNDRULE patterns. This only
1845 * makes sense when we have two or more words. */
1846 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1847 {
1848 int v;
1849
1850 compflags[sp->ts_complen] = flag;
1851 compflags[sp->ts_complen + 1] = NUL;
1852 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1853 compflags[sp->ts_complen] = NUL;
1854 return v;
1855 }
1856
1857 return TRUE;
1858}
1859
1860
1861/*
1862 * Return TRUE if the compound flags in compflags[] match the start of any
1863 * compound rule. This is used to stop trying a compound if the flags
1864 * collected so far can't possibly match any compound rule.
1865 * Caller must check that slang->sl_comprules is not NULL.
1866 */
1867 static int
1868match_compoundrule(slang, compflags)
1869 slang_T *slang;
1870 char_u *compflags;
1871{
1872 char_u *p;
1873 int i;
1874 int c;
1875
1876 /* loop over all the COMPOUNDRULE entries */
1877 for (p = slang->sl_comprules; *p != NUL; ++p)
1878 {
1879 /* loop over the flags in the compound word we have made, match
1880 * them against the current rule entry */
1881 for (i = 0; ; ++i)
1882 {
1883 c = compflags[i];
1884 if (c == NUL)
1885 /* found a rule that matches for the flags we have so far */
1886 return TRUE;
1887 if (*p == '/' || *p == NUL)
1888 break; /* end of rule, it's too short */
1889 if (*p == '[')
1890 {
1891 int match = FALSE;
1892
1893 /* compare against all the flags in [] */
1894 ++p;
1895 while (*p != ']' && *p != NUL)
1896 if (*p++ == c)
1897 match = TRUE;
1898 if (!match)
1899 break; /* none matches */
1900 }
1901 else if (*p != c)
1902 break; /* flag of word doesn't match flag in pattern */
1903 ++p;
1904 }
1905
1906 /* Skip to the next "/", where the next pattern starts. */
1907 p = vim_strchr(p, '/');
1908 if (p == NULL)
1909 break;
1910 }
1911
1912 /* Checked all the rules and none of them match the flags, so there
1913 * can't possibly be a compound starting with these flags. */
1914 return FALSE;
1915}
1916
1917/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001918 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1919 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001920 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001921 */
1922 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001923valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001924 int totprefcnt; /* nr of prefix IDs */
1925 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001926 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001927 char_u *word;
1928 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001929 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001930{
1931 int prefcnt;
1932 int pidx;
1933 regprog_T *rp;
1934 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001935 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001936
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001937 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001938 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1939 {
1940 pidx = slang->sl_pidxs[arridx + prefcnt];
1941
1942 /* Check the prefix ID. */
1943 if (prefid != (pidx & 0xff))
1944 continue;
1945
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001946 /* Check if the prefix doesn't combine and the word already has a
1947 * suffix. */
1948 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1949 continue;
1950
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001951 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001952 * stored in the two bytes above the prefix ID byte. */
1953 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001954 if (rp != NULL)
1955 {
1956 regmatch.regprog = rp;
1957 regmatch.rm_ic = FALSE;
1958 if (!vim_regexec(&regmatch, word, 0))
1959 continue;
1960 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001961 else if (cond_req)
1962 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001963
Bram Moolenaar53805d12005-08-01 07:08:33 +00001964 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001965 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001966 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001967 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001968}
1969
1970/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971 * Check if the word at "mip->mi_word" has a matching prefix.
1972 * If it does, then check the following word.
1973 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001974 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1975 * prefix in a compound word.
1976 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 * For a match mip->mi_result is updated.
1978 */
1979 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001980find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001981 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001982 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001983{
1984 idx_T arridx = 0;
1985 int len;
1986 int wlen = 0;
1987 int flen;
1988 int c;
1989 char_u *ptr;
1990 idx_T lo, hi, m;
1991 slang_T *slang = mip->mi_lp->lp_slang;
1992 char_u *byts;
1993 idx_T *idxs;
1994
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001995 byts = slang->sl_pbyts;
1996 if (byts == NULL)
1997 return; /* array is empty */
1998
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001999 /* We use the case-folded word here, since prefixes are always
2000 * case-folded. */
2001 ptr = mip->mi_fword;
2002 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002003 if (mode == FIND_COMPOUND)
2004 {
2005 /* Skip over the previously found word(s). */
2006 ptr += mip->mi_compoff;
2007 flen -= mip->mi_compoff;
2008 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002009 idxs = slang->sl_pidxs;
2010
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002011 /*
2012 * Repeat advancing in the tree until:
2013 * - there is a byte that doesn't match,
2014 * - we reach the end of the tree,
2015 * - or we reach the end of the line.
2016 */
2017 for (;;)
2018 {
2019 if (flen == 0 && *mip->mi_fend != NUL)
2020 flen = fold_more(mip);
2021
2022 len = byts[arridx++];
2023
2024 /* If the first possible byte is a zero the prefix could end here.
2025 * Check if the following word matches and supports the prefix. */
2026 if (byts[arridx] == 0)
2027 {
2028 /* There can be several prefixes with different conditions. We
2029 * try them all, since we don't know which one will give the
2030 * longest match. The word is the same each time, pass the list
2031 * of possible prefixes to find_word(). */
2032 mip->mi_prefarridx = arridx;
2033 mip->mi_prefcnt = len;
2034 while (len > 0 && byts[arridx] == 0)
2035 {
2036 ++arridx;
2037 --len;
2038 }
2039 mip->mi_prefcnt -= len;
2040
2041 /* Find the word that comes after the prefix. */
2042 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002043 if (mode == FIND_COMPOUND)
2044 /* Skip over the previously found word(s). */
2045 mip->mi_prefixlen += mip->mi_compoff;
2046
Bram Moolenaar53805d12005-08-01 07:08:33 +00002047#ifdef FEAT_MBYTE
2048 if (has_mbyte)
2049 {
2050 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002051 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2052 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002053 }
2054 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002055 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002056#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002057 find_word(mip, FIND_PREFIX);
2058
2059
2060 if (len == 0)
2061 break; /* no children, word must end here */
2062 }
2063
2064 /* Stop looking at end of the line. */
2065 if (ptr[wlen] == NUL)
2066 break;
2067
2068 /* Perform a binary search in the list of accepted bytes. */
2069 c = ptr[wlen];
2070 lo = arridx;
2071 hi = arridx + len - 1;
2072 while (lo < hi)
2073 {
2074 m = (lo + hi) / 2;
2075 if (byts[m] > c)
2076 hi = m - 1;
2077 else if (byts[m] < c)
2078 lo = m + 1;
2079 else
2080 {
2081 lo = hi = m;
2082 break;
2083 }
2084 }
2085
2086 /* Stop if there is no matching byte. */
2087 if (hi < lo || byts[lo] != c)
2088 break;
2089
2090 /* Continue at the child (if there is one). */
2091 arridx = idxs[lo];
2092 ++wlen;
2093 --flen;
2094 }
2095}
2096
2097/*
2098 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002099 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002100 * Return the length of the folded chars in bytes.
2101 */
2102 static int
2103fold_more(mip)
2104 matchinf_T *mip;
2105{
2106 int flen;
2107 char_u *p;
2108
2109 p = mip->mi_fend;
2110 do
2111 {
2112 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002113 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002114
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002115 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002116 if (*mip->mi_fend != NUL)
2117 mb_ptr_adv(mip->mi_fend);
2118
2119 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2120 mip->mi_fword + mip->mi_fwordlen,
2121 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002122 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002123 mip->mi_fwordlen += flen;
2124 return flen;
2125}
2126
2127/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 * Check case flags for a word. Return TRUE if the word has the requested
2129 * case.
2130 */
2131 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002132spell_valid_case(wordflags, treeflags)
2133 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 int treeflags; /* flags for the word in the spell tree */
2135{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002136 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002138 && ((treeflags & WF_ONECAP) == 0
2139 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140}
2141
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002142/*
2143 * Return TRUE if spell checking is not enabled.
2144 */
2145 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002146no_spell_checking(wp)
2147 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002148{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002149 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2150 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002151 {
2152 EMSG(_("E756: Spell checking is not enabled"));
2153 return TRUE;
2154 }
2155 return FALSE;
2156}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002158/*
2159 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002160 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2161 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002162 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2163 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002164 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 */
2166 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002167spell_move_to(wp, dir, allwords, curline, attrp)
2168 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002170 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002172 hlf_T *attrp; /* return: attributes of bad word or NULL
2173 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002174{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002175 linenr_T lnum;
2176 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002177 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002178 char_u *line;
2179 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002180 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002181 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002183# ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002184 int has_syntax = syntax_present(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002185# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002186 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002187 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002188 char_u *buf = NULL;
2189 int buflen = 0;
2190 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002191 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002192 int found_one = FALSE;
2193 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194
Bram Moolenaar95529562005-08-25 21:21:38 +00002195 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002197
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002198 /*
2199 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002200 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002201 *
2202 * When searching backwards, we continue in the line to find the last
2203 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002204 *
2205 * We concatenate the start of the next line, so that wrapped words work
2206 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2207 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002208 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002209 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002210 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002211
2212 while (!got_int)
2213 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002214 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002216 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002217 if (buflen < len + MAXWLEN + 2)
2218 {
2219 vim_free(buf);
2220 buflen = len + MAXWLEN + 2;
2221 buf = alloc(buflen);
2222 if (buf == NULL)
2223 break;
2224 }
2225
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002226 /* In first line check first word for Capital. */
2227 if (lnum == 1)
2228 capcol = 0;
2229
2230 /* For checking first word with a capital skip white space. */
2231 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002232 capcol = (int)(skipwhite(line) - line);
2233 else if (curline && wp == curwin)
2234 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002235 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002236 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002237 if (check_need_cap(lnum, col))
2238 capcol = col;
2239
2240 /* Need to get the line again, may have looked at the previous
2241 * one. */
2242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2243 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002244
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 /* Copy the line into "buf" and append the start of the next line if
2246 * possible. */
2247 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002248 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002249 spell_cat_line(buf + STRLEN(buf),
2250 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251
2252 p = buf + skip;
2253 endp = buf + len;
2254 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002255 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002256 /* When searching backward don't search after the cursor. Unless
2257 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002258 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002259 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002260 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002261 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002262 break;
2263
2264 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002266 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002268 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002270 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002271 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002273 /* When searching forward only accept a bad word after
2274 * the cursor. */
2275 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002276 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002277 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002278 && (wrapped
2279 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002280 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002281 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002283# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002284 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002285 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002286 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002287 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002288 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002289 if (!can_spell)
2290 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002291 }
2292 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002293#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002294 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002295
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 if (can_spell)
2297 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002298 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002299 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002300 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002301#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002302 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002303#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002304 if (dir == FORWARD)
2305 {
2306 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002307 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002308 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002309 if (attrp != NULL)
2310 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002311 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002312 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002313 else if (curline)
2314 /* Insert mode completion: put cursor after
2315 * the bad word. */
2316 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002317 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002318 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002320 else
2321 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 }
2324
Bram Moolenaar51485f02005-06-04 21:55:20 +00002325 /* advance to character after the word */
2326 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002327 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 }
2329
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 if (dir == BACKWARD && found_pos.lnum != 0)
2331 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002332 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002333 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002335 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002336 }
2337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002339 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002341 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002342 if (dir == BACKWARD)
2343 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002344 /* If we are back at the starting line and searched it again there
2345 * is no match, give up. */
2346 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002347 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002348
2349 if (lnum > 1)
2350 --lnum;
2351 else if (!p_ws)
2352 break; /* at first line and 'nowrapscan' */
2353 else
2354 {
2355 /* Wrap around to the end of the buffer. May search the
2356 * starting line again and accept the last match. */
2357 lnum = wp->w_buffer->b_ml.ml_line_count;
2358 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002359 if (!shortmess(SHM_SEARCH))
2360 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002361 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002362 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002363 }
2364 else
2365 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002366 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2367 ++lnum;
2368 else if (!p_ws)
2369 break; /* at first line and 'nowrapscan' */
2370 else
2371 {
2372 /* Wrap around to the start of the buffer. May search the
2373 * starting line again and accept the first match. */
2374 lnum = 1;
2375 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376 if (!shortmess(SHM_SEARCH))
2377 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002378 }
2379
2380 /* If we are back at the starting line and there is no match then
2381 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002382 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002383 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002384
2385 /* Skip the characters at the start of the next line that were
2386 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002387 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002389 else
2390 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002392 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002393 --capcol;
2394
2395 /* But after empty line check first word in next line */
2396 if (*skipwhite(line) == NUL)
2397 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399
2400 line_breakcheck();
2401 }
2402
Bram Moolenaar0c405862005-06-22 22:26:26 +00002403 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002404 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002405}
2406
2407/*
2408 * For spell checking: concatenate the start of the following line "line" into
2409 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002410 * Keep the blanks at the start of the next line, this is used in win_line()
2411 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002412 */
2413 void
2414spell_cat_line(buf, line, maxlen)
2415 char_u *buf;
2416 char_u *line;
2417 int maxlen;
2418{
2419 char_u *p;
2420 int n;
2421
2422 p = skipwhite(line);
2423 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2424 p = skipwhite(p + 1);
2425
2426 if (*p != NUL)
2427 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002428 /* Only worth concatenating if there is something else than spaces to
2429 * concatenate. */
2430 n = (int)(p - line) + 1;
2431 if (n < maxlen - 1)
2432 {
2433 vim_memset(buf, ' ', n);
2434 vim_strncpy(buf + n, p, maxlen - 1 - n);
2435 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002437}
2438
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002439/*
2440 * Structure used for the cookie argument of do_in_runtimepath().
2441 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002442typedef struct spelload_S
2443{
2444 char_u sl_lang[MAXWLEN + 1]; /* language name */
2445 slang_T *sl_slang; /* resulting slang_T struct */
2446 int sl_nobreak; /* NOBREAK language found */
2447} spelload_T;
2448
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002450 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002453 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454spell_load_lang(lang)
2455 char_u *lang;
2456{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002457 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002459 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002460#ifdef FEAT_AUTOCMD
2461 int round;
2462#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463
Bram Moolenaarb765d632005-06-07 21:00:02 +00002464 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002465 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002466 STRCPY(sl.sl_lang, lang);
2467 sl.sl_slang = NULL;
2468 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002470#ifdef FEAT_AUTOCMD
2471 /* We may retry when no spell file is found for the language, an
2472 * autocommand may load it then. */
2473 for (round = 1; round <= 2; ++round)
2474#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002475 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002476 /*
2477 * Find the first spell file for "lang" in 'runtimepath' and load it.
2478 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002479 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002480#ifdef VMS
2481 "spell/%s_%s.spl",
2482#else
2483 "spell/%s.%s.spl",
2484#endif
2485 lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002486 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002487
2488 if (r == FAIL && *sl.sl_lang != NUL)
2489 {
2490 /* Try loading the ASCII version. */
2491 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002492#ifdef VMS
2493 "spell/%s_ascii.spl",
2494#else
2495 "spell/%s.ascii.spl",
2496#endif
2497 lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002498 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2499
2500#ifdef FEAT_AUTOCMD
2501 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2502 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2503 curbuf->b_fname, FALSE, curbuf))
2504 continue;
2505 break;
2506#endif
2507 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002508#ifdef FEAT_AUTOCMD
2509 break;
2510#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 }
2512
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002513 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002514 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01002515 smsg((char_u *)
2516#ifdef VMS
2517 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2518#else
2519 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2520#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002522 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002523 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002524 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002525 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002526 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002527 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002528 }
2529}
2530
2531/*
2532 * Return the encoding used for spell checking: Use 'encoding', except that we
2533 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2534 */
2535 static char_u *
2536spell_enc()
2537{
2538
2539#ifdef FEAT_MBYTE
2540 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2541 return p_enc;
2542#endif
2543 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544}
2545
2546/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002547 * Get the name of the .spl file for the internal wordlist into
2548 * "fname[MAXPATHL]".
2549 */
2550 static void
2551int_wordlist_spl(fname)
2552 char_u *fname;
2553{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002554 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002555 int_wordlist, spell_enc());
2556}
2557
2558/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002560 * Caller must fill "sl_next".
2561 */
2562 static slang_T *
2563slang_alloc(lang)
2564 char_u *lang;
2565{
2566 slang_T *lp;
2567
Bram Moolenaar51485f02005-06-04 21:55:20 +00002568 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 if (lp != NULL)
2570 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002571 if (lang != NULL)
2572 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002575 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002576 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002577 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002579
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 return lp;
2581}
2582
2583/*
2584 * Free the contents of an slang_T and the structure itself.
2585 */
2586 static void
2587slang_free(lp)
2588 slang_T *lp;
2589{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 vim_free(lp->sl_fname);
2592 slang_clear(lp);
2593 vim_free(lp);
2594}
2595
2596/*
2597 * Clear an slang_T so that the file can be reloaded.
2598 */
2599 static void
2600slang_clear(lp)
2601 slang_T *lp;
2602{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002603 garray_T *gap;
2604 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002605 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002606 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002607 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608
Bram Moolenaar51485f02005-06-04 21:55:20 +00002609 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002610 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002611 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002613 vim_free(lp->sl_pbyts);
2614 lp->sl_pbyts = NULL;
2615
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002617 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002620 vim_free(lp->sl_pidxs);
2621 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002622
Bram Moolenaar4770d092006-01-12 23:22:24 +00002623 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002624 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002625 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2626 while (gap->ga_len > 0)
2627 {
2628 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2629 vim_free(ftp->ft_from);
2630 vim_free(ftp->ft_to);
2631 }
2632 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002634
2635 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002636 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002637 {
2638 /* "ga_len" is set to 1 without adding an item for latin1 */
2639 if (gap->ga_data != NULL)
2640 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2641 for (i = 0; i < gap->ga_len; ++i)
2642 vim_free(((int **)gap->ga_data)[i]);
2643 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002644 else
2645 /* SAL items: free salitem_T items */
2646 while (gap->ga_len > 0)
2647 {
2648 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2649 vim_free(smp->sm_lead);
2650 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2651 vim_free(smp->sm_to);
2652#ifdef FEAT_MBYTE
2653 vim_free(smp->sm_lead_w);
2654 vim_free(smp->sm_oneof_w);
2655 vim_free(smp->sm_to_w);
2656#endif
2657 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002658 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002659
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002660 for (i = 0; i < lp->sl_prefixcnt; ++i)
2661 vim_free(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 Moolenaar5195e452005-08-19 20:32:47 +00002672 vim_free(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 Moolenaar706cdeb2007-05-06 21:55:31 +00004231 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4232 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004233 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004234 if (spl_copy == NULL)
4235 goto theend;
4236
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004237 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004238 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004239 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004240 /* Get one language name. */
4241 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004242 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004243 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004244
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004245 /* If the name ends in ".spl" use it as the name of the spell file.
4246 * If there is a region name let "region" point to it and remove it
4247 * from the name. */
4248 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4249 {
4250 filename = TRUE;
4251
Bram Moolenaarb6356332005-07-18 21:40:44 +00004252 /* Locate a region and remove it from the file name. */
4253 p = vim_strchr(gettail(lang), '_');
4254 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4255 && !ASCII_ISALPHA(p[3]))
4256 {
4257 vim_strncpy(region_cp, p + 1, 2);
4258 mch_memmove(p, p + 3, len - (p - lang) - 2);
4259 len -= 3;
4260 region = region_cp;
4261 }
4262 else
4263 dont_use_region = TRUE;
4264
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004265 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004266 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4267 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004268 break;
4269 }
4270 else
4271 {
4272 filename = FALSE;
4273 if (len > 3 && lang[len - 3] == '_')
4274 {
4275 region = lang + len - 2;
4276 len -= 3;
4277 lang[len] = NUL;
4278 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004279 else
4280 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004281
4282 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004283 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4284 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004285 break;
4286 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004287
Bram Moolenaarb6356332005-07-18 21:40:44 +00004288 if (region != NULL)
4289 {
4290 /* If the region differs from what was used before then don't
4291 * use it for 'spellfile'. */
4292 if (use_region != NULL && STRCMP(region, use_region) != 0)
4293 dont_use_region = TRUE;
4294 use_region = region;
4295 }
4296
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004297 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004299 {
4300 if (filename)
4301 (void)spell_load_file(lang, lang, NULL, FALSE);
4302 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004303 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004304 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004305#ifdef FEAT_AUTOCMD
4306 /* SpellFileMissing autocommands may do anything, including
4307 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004308 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004309 {
4310 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4311 goto theend;
4312 }
4313#endif
4314 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004315 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004316
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004317 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004318 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004319 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004320 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4321 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4322 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004323 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004324 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004325 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004326 {
4327 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004328 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004329 if (c == REGION_ALL)
4330 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004331 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004332 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004333 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004334 /* This addition file is for other regions. */
4335 region_mask = 0;
4336 }
4337 else
4338 /* This is probably an error. Give a warning and
4339 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004340 smsg((char_u *)
4341 _("Warning: region %s not supported"),
4342 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004343 }
4344 else
4345 region_mask = 1 << c;
4346 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004347
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004348 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004349 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004350 if (ga_grow(&ga, 1) == FAIL)
4351 {
4352 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004353 ret_msg = e_outofmem;
4354 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004355 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004357 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4358 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004359 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004360 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004361 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004362 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364 }
4365
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004366 /* round 0: load int_wordlist, if possible.
4367 * round 1: load first name in 'spellfile'.
4368 * round 2: load second name in 'spellfile.
4369 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004370 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004371 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004373 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004374 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004375 /* Internal wordlist, if there is one. */
4376 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004377 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004378 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004379 }
4380 else
4381 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004382 /* One entry in 'spellfile'. */
4383 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4384 STRCAT(spf_name, ".spl");
4385
4386 /* If it was already found above then skip it. */
4387 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004388 {
4389 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4390 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004391 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004392 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004393 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004394 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004395 }
4396
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004397 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004398 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4399 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004401 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004402 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004403 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004404 * region name, the region is ignored otherwise. for int_wordlist
4405 * use an arbitrary name. */
4406 if (round == 0)
4407 STRCPY(lang, "internal wordlist");
4408 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004409 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004410 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004411 p = vim_strchr(lang, '.');
4412 if (p != NULL)
4413 *p = NUL; /* truncate at ".encoding.add" */
4414 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004415 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004416
4417 /* If one of the languages has NOBREAK we assume the addition
4418 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004419 if (slang != NULL && nobreak)
4420 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004422 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004424 region_mask = REGION_ALL;
4425 if (use_region != NULL && !dont_use_region)
4426 {
4427 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004428 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004429 if (c != REGION_ALL)
4430 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004432 /* This spell file is for other regions. */
4433 region_mask = 0;
4434 }
4435
4436 if (region_mask != 0)
4437 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004438 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4439 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4440 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004441 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4442 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004443 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 }
4446 }
4447
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004448 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004449 ga_clear(&wp->w_s->b_langp);
4450 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004451
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004452 /* For each language figure out what language to use for sound folding and
4453 * REP items. If the language doesn't support it itself use another one
4454 * with the same name. E.g. for "en-math" use "en". */
4455 for (i = 0; i < ga.ga_len; ++i)
4456 {
4457 lp = LANGP_ENTRY(ga, i);
4458
4459 /* sound folding */
4460 if (lp->lp_slang->sl_sal.ga_len > 0)
4461 /* language does sound folding itself */
4462 lp->lp_sallang = lp->lp_slang;
4463 else
4464 /* find first similar language that does sound folding */
4465 for (j = 0; j < ga.ga_len; ++j)
4466 {
4467 lp2 = LANGP_ENTRY(ga, j);
4468 if (lp2->lp_slang->sl_sal.ga_len > 0
4469 && STRNCMP(lp->lp_slang->sl_name,
4470 lp2->lp_slang->sl_name, 2) == 0)
4471 {
4472 lp->lp_sallang = lp2->lp_slang;
4473 break;
4474 }
4475 }
4476
4477 /* REP items */
4478 if (lp->lp_slang->sl_rep.ga_len > 0)
4479 /* language has REP items itself */
4480 lp->lp_replang = lp->lp_slang;
4481 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004482 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004483 for (j = 0; j < ga.ga_len; ++j)
4484 {
4485 lp2 = LANGP_ENTRY(ga, j);
4486 if (lp2->lp_slang->sl_rep.ga_len > 0
4487 && STRNCMP(lp->lp_slang->sl_name,
4488 lp2->lp_slang->sl_name, 2) == 0)
4489 {
4490 lp->lp_replang = lp2->lp_slang;
4491 break;
4492 }
4493 }
4494 }
4495
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004496theend:
4497 vim_free(spl_copy);
4498 recursive = FALSE;
4499 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004500}
4501
4502/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004503 * Clear the midword characters for buffer "buf".
4504 */
4505 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004506clear_midword(wp)
4507 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004508{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004509 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004510#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004511 vim_free(wp->w_s->b_spell_ismw_mb);
4512 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004513#endif
4514}
4515
4516/*
4517 * Use the "sl_midword" field of language "lp" for buffer "buf".
4518 * They add up to any currently used midword characters.
4519 */
4520 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004521use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004522 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004523 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004524{
4525 char_u *p;
4526
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004527 if (lp->sl_midword == NULL) /* there aren't any */
4528 return;
4529
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004530 for (p = lp->sl_midword; *p != NUL; )
4531#ifdef FEAT_MBYTE
4532 if (has_mbyte)
4533 {
4534 int c, l, n;
4535 char_u *bp;
4536
4537 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004538 l = (*mb_ptr2len)(p);
4539 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004540 wp->w_s->b_spell_ismw[c] = TRUE;
4541 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004542 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004543 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004544 else
4545 {
4546 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004547 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4548 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004549 if (bp != NULL)
4550 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004551 vim_free(wp->w_s->b_spell_ismw_mb);
4552 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004553 vim_strncpy(bp + n, p, l);
4554 }
4555 }
4556 p += l;
4557 }
4558 else
4559#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004560 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004561}
4562
4563/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004564 * Find the region "region[2]" in "rp" (points to "sl_regions").
4565 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004566 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004567 */
4568 static int
4569find_region(rp, region)
4570 char_u *rp;
4571 char_u *region;
4572{
4573 int i;
4574
4575 for (i = 0; ; i += 2)
4576 {
4577 if (rp[i] == NUL)
4578 return REGION_ALL;
4579 if (rp[i] == region[0] && rp[i + 1] == region[1])
4580 break;
4581 }
4582 return i / 2;
4583}
4584
4585/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004587 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004588 * Word WF_ONECAP
4589 * W WORD WF_ALLCAP
4590 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 */
4592 static int
4593captype(word, end)
4594 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004596{
4597 char_u *p;
4598 int c;
4599 int firstcap;
4600 int allcap;
4601 int past_second = FALSE; /* past second word char */
4602
4603 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004604 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004605 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 return 0; /* only non-word characters, illegal word */
4607#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004608 if (has_mbyte)
4609 c = mb_ptr2char_adv(&p);
4610 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004611#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004612 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004613 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004614
4615 /*
4616 * Need to check all letters to find a word with mixed upper/lower.
4617 * But a word with an upper char only at start is a ONECAP.
4618 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004620 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004621 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004622 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004623 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624 {
4625 /* UUl -> KEEPCAP */
4626 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004627 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004628 allcap = FALSE;
4629 }
4630 else if (!allcap)
4631 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004632 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004633 past_second = TRUE;
4634 }
4635
4636 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004637 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004638 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004639 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004640 return 0;
4641}
4642
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004643/*
4644 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4645 * capital. So that make_case_word() can turn WOrd into Word.
4646 * Add ALLCAP for "WOrD".
4647 */
4648 static int
4649badword_captype(word, end)
4650 char_u *word;
4651 char_u *end;
4652{
4653 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004654 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004655 int l, u;
4656 int first;
4657 char_u *p;
4658
4659 if (flags & WF_KEEPCAP)
4660 {
4661 /* Count the number of UPPER and lower case letters. */
4662 l = u = 0;
4663 first = FALSE;
4664 for (p = word; p < end; mb_ptr_adv(p))
4665 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004666 c = PTR2CHAR(p);
4667 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004668 {
4669 ++u;
4670 if (p == word)
4671 first = TRUE;
4672 }
4673 else
4674 ++l;
4675 }
4676
4677 /* If there are more UPPER than lower case letters suggest an
4678 * ALLCAP word. Otherwise, if the first letter is UPPER then
4679 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4680 * require three upper case letters. */
4681 if (u > l && u > 2)
4682 flags |= WF_ALLCAP;
4683 else if (first)
4684 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004685
4686 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4687 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004688 }
4689 return flags;
4690}
4691
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004692# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4693/*
4694 * Free all languages.
4695 */
4696 void
4697spell_free_all()
4698{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004699 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004700 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004701 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004702
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004703 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004704 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004705 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004706
4707 while (first_lang != NULL)
4708 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004709 slang = first_lang;
4710 first_lang = slang->sl_next;
4711 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004712 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004713
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004714 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004715 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004716 /* Delete the internal wordlist and its .spl file */
4717 mch_remove(int_wordlist);
4718 int_wordlist_spl(fname);
4719 mch_remove(fname);
4720 vim_free(int_wordlist);
4721 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004722 }
4723
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004724 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004725
4726 vim_free(repl_to);
4727 repl_to = NULL;
4728 vim_free(repl_from);
4729 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004730}
4731# endif
4732
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733# if defined(FEAT_MBYTE) || defined(PROTO)
4734/*
4735 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004736 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004737 */
4738 void
4739spell_reload()
4740{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004741 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004742
Bram Moolenaarea408852005-06-25 22:49:46 +00004743 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004744 init_spell_chartab();
4745
4746 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004747 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004748
4749 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004750 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004751 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004752 /* Only load the wordlists when 'spelllang' is set and there is a
4753 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004754 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004755 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004756 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004757 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004758 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004759# ifdef FEAT_WINDOWS
4760 break;
4761# endif
4762 }
4763 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004764 }
4765}
4766# endif
4767
Bram Moolenaarb765d632005-06-07 21:00:02 +00004768/*
4769 * Reload the spell file "fname" if it's loaded.
4770 */
4771 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004772spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004773 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004775{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004776 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004779 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004780 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004781 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004782 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004783 slang_clear(slang);
4784 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004785 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004786 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004787 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004789 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791
4792 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004793 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004795 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004796}
4797
4798
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799/*
4800 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004801 */
4802
Bram Moolenaar51485f02005-06-04 21:55:20 +00004803#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004804 and .dic file. */
4805/*
4806 * Main structure to store the contents of a ".aff" file.
4807 */
4808typedef struct afffile_S
4809{
4810 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004811 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004812 unsigned af_rare; /* RARE ID for rare word */
4813 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004814 unsigned af_bad; /* BAD ID for banned word */
4815 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004816 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004817 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004818 unsigned af_comproot; /* COMPOUNDROOT ID */
4819 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4820 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004822 int af_pfxpostpone; /* postpone prefixes without chop string and
4823 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004824 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4825 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004826 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004827} afffile_T;
4828
Bram Moolenaar6de68532005-08-24 22:08:48 +00004829#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004830#define AFT_LONG 1 /* flags are two characters */
4831#define AFT_CAPLONG 2 /* flags are one or two characters */
4832#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004833
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004834typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004835/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4836struct affentry_S
4837{
4838 affentry_T *ae_next; /* next affix with same name/number */
4839 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4840 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004841 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842 char_u *ae_cond; /* condition (NULL for ".") */
4843 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004844 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4845 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004846};
4847
Bram Moolenaar6de68532005-08-24 22:08:48 +00004848#ifdef FEAT_MBYTE
4849# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4850#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004851# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004852#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004853
Bram Moolenaar51485f02005-06-04 21:55:20 +00004854/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4855typedef struct affheader_S
4856{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004857 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4858 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4859 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004860 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004861 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004862 affentry_T *ah_first; /* first affix entry */
4863} affheader_T;
4864
4865#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4866
Bram Moolenaar6de68532005-08-24 22:08:48 +00004867/* Flag used in compound items. */
4868typedef struct compitem_S
4869{
4870 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4871 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4872 int ci_newID; /* affix ID after renumbering. */
4873} compitem_T;
4874
4875#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4876
Bram Moolenaar51485f02005-06-04 21:55:20 +00004877/*
4878 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004879 * the need to keep track of each allocated thing, everything is freed all at
4880 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004881 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4882 * "sb_data" is correct for systems where pointers must be aligned on
4883 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004884 */
4885#define SBLOCKSIZE 16000 /* size of sb_data */
4886typedef struct sblock_S sblock_T;
4887struct sblock_S
4888{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004889 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004890 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004891 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004892};
4893
4894/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004895 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897typedef struct wordnode_S wordnode_T;
4898struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004899{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004900 union /* shared to save space */
4901 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004902 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004903 int index; /* index in written nodes (valid after first
4904 round) */
4905 } wn_u1;
4906 union /* shared to save space */
4907 {
4908 wordnode_T *next; /* next node with same hash key */
4909 wordnode_T *wnode; /* parent node that will write this node */
4910 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004911 wordnode_T *wn_child; /* child (next byte in word) */
4912 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4913 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004914 int wn_refs; /* Nr. of references to this node. Only
4915 relevant for first node in a list of
4916 siblings, in following siblings it is
4917 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004918 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004919
4920 /* Info for when "wn_byte" is NUL.
4921 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4922 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4923 * "wn_region" the LSW of the wordnr. */
4924 char_u wn_affixID; /* supported/required prefix ID or 0 */
4925 short_u wn_flags; /* WF_ flags */
4926 short wn_region; /* region mask */
4927
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004928#ifdef SPELL_PRINTTREE
4929 int wn_nr; /* sequence nr for printing */
4930#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004931};
4932
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004933#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4934
Bram Moolenaar51485f02005-06-04 21:55:20 +00004935#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004936
Bram Moolenaar51485f02005-06-04 21:55:20 +00004937/*
4938 * Info used while reading the spell files.
4939 */
4940typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004941{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004942 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004943 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004944
Bram Moolenaar51485f02005-06-04 21:55:20 +00004945 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004946 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004947
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004948 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004949
Bram Moolenaar4770d092006-01-12 23:22:24 +00004950 long si_sugtree; /* creating the soundfolding trie */
4951
Bram Moolenaar51485f02005-06-04 21:55:20 +00004952 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004953 long si_blocks_cnt; /* memory blocks allocated */
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01004954 int si_did_emsg; /* TRUE when ran out of memory */
4955
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004956 long si_compress_cnt; /* words to add before lowering
4957 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004958 wordnode_T *si_first_free; /* List of nodes that have been freed during
4959 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004960 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004961#ifdef SPELL_PRINTTREE
4962 int si_wordnode_nr; /* sequence nr for nodes */
4963#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004964 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004965
Bram Moolenaar51485f02005-06-04 21:55:20 +00004966 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004967 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004968 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004969 int si_region; /* region mask */
4970 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004971 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004972 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004973 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004974 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004975 int si_region_count; /* number of regions supported (1 when there
4976 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004977 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004978 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004979
4980 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004981 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004982 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004983 char_u *si_sofofr; /* SOFOFROM text */
4984 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004985 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004986 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004987 int si_followup; /* soundsalike: ? */
4988 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004989 hashtab_T si_commonwords; /* hashtable for common words */
4990 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004991 int si_rem_accents; /* soundsalike: remove accents */
4992 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004993 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004994 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004995 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004996 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004997 int si_compoptions; /* COMP_ flags */
4998 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4999 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005000 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00005001 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005002 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005003 garray_T si_prefcond; /* table with conditions for postponed
5004 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005005 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005006 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005007} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005008
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005009static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005010static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005011static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005012static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005013static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5014static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5015static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005016static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005017static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5018static void aff_check_number __ARGS((int spinval, int affval, char *name));
5019static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005020static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005021static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5022static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00005023static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005024static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005025static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005026static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005027static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005028static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005029static 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 +00005030static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5031static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5032static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005033static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005034static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005035static 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 +00005036static 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 +00005037static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005038static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005039static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5040static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5041static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005042static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005043static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005044static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005045static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005046static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5047static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5048static int sug_maketable __ARGS((spellinfo_T *spin));
5049static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5050static int offset2bytes __ARGS((int nr, char_u *buf));
5051static int bytes2offset __ARGS((char_u **pp));
5052static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005053static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005054static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005055static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005056
Bram Moolenaar53805d12005-08-01 07:08:33 +00005057/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5058 * but it must be negative to indicate the prefix tree to tree_add_word().
5059 * Use a negative number with the lower 8 bits zero. */
5060#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005061
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005062/* flags for "condit" argument of store_aff_word() */
5063#define CONDIT_COMB 1 /* affix must combine */
5064#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5065#define CONDIT_SUF 4 /* add a suffix for matching flags */
5066#define CONDIT_AFF 8 /* word already has an affix */
5067
Bram Moolenaar5195e452005-08-19 20:32:47 +00005068/*
5069 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5070 */
5071static long compress_start = 30000; /* memory / SBLOCKSIZE */
5072static long compress_inc = 100; /* memory / SBLOCKSIZE */
5073static long compress_added = 500000; /* word count */
5074
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005075#ifdef SPELL_PRINTTREE
5076/*
5077 * For debugging the tree code: print the current tree in a (more or less)
5078 * readable format, so that we can see what happens when adding a word and/or
5079 * compressing the tree.
5080 * Based on code from Olaf Seibert.
5081 */
5082#define PRINTLINESIZE 1000
5083#define PRINTWIDTH 6
5084
5085#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5086 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5087
5088static char line1[PRINTLINESIZE];
5089static char line2[PRINTLINESIZE];
5090static char line3[PRINTLINESIZE];
5091
5092 static void
5093spell_clear_flags(wordnode_T *node)
5094{
5095 wordnode_T *np;
5096
5097 for (np = node; np != NULL; np = np->wn_sibling)
5098 {
5099 np->wn_u1.index = FALSE;
5100 spell_clear_flags(np->wn_child);
5101 }
5102}
5103
5104 static void
5105spell_print_node(wordnode_T *node, int depth)
5106{
5107 if (node->wn_u1.index)
5108 {
5109 /* Done this node before, print the reference. */
5110 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5111 PRINTSOME(line2, depth, " ", 0, 0);
5112 PRINTSOME(line3, depth, " ", 0, 0);
5113 msg(line1);
5114 msg(line2);
5115 msg(line3);
5116 }
5117 else
5118 {
5119 node->wn_u1.index = TRUE;
5120
5121 if (node->wn_byte != NUL)
5122 {
5123 if (node->wn_child != NULL)
5124 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5125 else
5126 /* Cannot happen? */
5127 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5128 }
5129 else
5130 PRINTSOME(line1, depth, " $ ", 0, 0);
5131
5132 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5133
5134 if (node->wn_sibling != NULL)
5135 PRINTSOME(line3, depth, " | ", 0, 0);
5136 else
5137 PRINTSOME(line3, depth, " ", 0, 0);
5138
5139 if (node->wn_byte == NUL)
5140 {
5141 msg(line1);
5142 msg(line2);
5143 msg(line3);
5144 }
5145
5146 /* do the children */
5147 if (node->wn_byte != NUL && node->wn_child != NULL)
5148 spell_print_node(node->wn_child, depth + 1);
5149
5150 /* do the siblings */
5151 if (node->wn_sibling != NULL)
5152 {
5153 /* get rid of all parent details except | */
5154 STRCPY(line1, line3);
5155 STRCPY(line2, line3);
5156 spell_print_node(node->wn_sibling, depth);
5157 }
5158 }
5159}
5160
5161 static void
5162spell_print_tree(wordnode_T *root)
5163{
5164 if (root != NULL)
5165 {
5166 /* Clear the "wn_u1.index" fields, used to remember what has been
5167 * done. */
5168 spell_clear_flags(root);
5169
5170 /* Recursively print the tree. */
5171 spell_print_node(root, 0);
5172 }
5173}
5174#endif /* SPELL_PRINTTREE */
5175
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005176/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005177 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005178 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005179 */
5180 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005181spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005182 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005183 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005184{
5185 FILE *fd;
5186 afffile_T *aff;
5187 char_u rline[MAXLINELEN];
5188 char_u *line;
5189 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005190#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005191 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005192 int itemcnt;
5193 char_u *p;
5194 int lnum = 0;
5195 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005196 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005197 int aff_todo = 0;
5198 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005199 char_u *low = NULL;
5200 char_u *fol = NULL;
5201 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005202 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005203 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005204 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005205 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005207 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005208 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005209 int compminlen = 0; /* COMPOUNDMIN value */
5210 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005211 int compoptions = 0; /* COMP_ flags */
5212 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005213 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005214 concatenated */
5215 char_u *midword = NULL; /* MIDWORD value */
5216 char_u *syllable = NULL; /* SYLLABLE value */
5217 char_u *sofofrom = NULL; /* SOFOFROM value */
5218 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005219
Bram Moolenaar51485f02005-06-04 21:55:20 +00005220 /*
5221 * Open the file.
5222 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005223 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005224 if (fd == NULL)
5225 {
5226 EMSG2(_(e_notopen), fname);
5227 return NULL;
5228 }
5229
Bram Moolenaar4770d092006-01-12 23:22:24 +00005230 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5231 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 /* Only do REP lines when not done in another .aff file already. */
5234 do_rep = spin->si_rep.ga_len == 0;
5235
Bram Moolenaar4770d092006-01-12 23:22:24 +00005236 /* Only do REPSAL lines when not done in another .aff file already. */
5237 do_repsal = spin->si_repsal.ga_len == 0;
5238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239 /* Only do SAL lines when not done in another .aff file already. */
5240 do_sal = spin->si_sal.ga_len == 0;
5241
5242 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005243 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244
Bram Moolenaar51485f02005-06-04 21:55:20 +00005245 /*
5246 * Allocate and init the afffile_T structure.
5247 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005248 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005249 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005250 {
5251 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005252 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005253 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254 hash_init(&aff->af_pref);
5255 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005256 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005257
5258 /*
5259 * Read all the lines in the file one by one.
5260 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005261 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005262 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005263 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005264 ++lnum;
5265
5266 /* Skip comment lines. */
5267 if (*rline == '#')
5268 continue;
5269
5270 /* Convert from "SET" to 'encoding' when needed. */
5271 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005272#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005273 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005274 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005275 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005276 if (pc == NULL)
5277 {
5278 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5279 fname, lnum, rline);
5280 continue;
5281 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005282 line = pc;
5283 }
5284 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005285#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005286 {
5287 pc = NULL;
5288 line = rline;
5289 }
5290
5291 /* Split the line up in white separated items. Put a NUL after each
5292 * item. */
5293 itemcnt = 0;
5294 for (p = line; ; )
5295 {
5296 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5297 ++p;
5298 if (*p == NUL)
5299 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005300 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005301 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005302 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005303 /* A few items have arbitrary text argument, don't split them. */
5304 if (itemcnt == 2 && spell_info_item(items[0]))
5305 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5306 ++p;
5307 else
5308 while (*p > ' ') /* skip until white space or CR/NL */
5309 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005310 if (*p == NUL)
5311 break;
5312 *p++ = NUL;
5313 }
5314
5315 /* Handle non-empty lines. */
5316 if (itemcnt > 0)
5317 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005318 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005319 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005320#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005321 /* Setup for conversion from "ENC" to 'encoding'. */
5322 aff->af_enc = enc_canonize(items[1]);
5323 if (aff->af_enc != NULL && !spin->si_ascii
5324 && convert_setup(&spin->si_conv, aff->af_enc,
5325 p_enc) == FAIL)
5326 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5327 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005328 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005329#else
5330 smsg((char_u *)_("Conversion in %s not supported"), fname);
5331#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005332 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005333 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005334 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005335 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005336 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005337 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005338 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005339 aff->af_flagtype = AFT_NUM;
5340 else if (STRCMP(items[1], "caplong") == 0)
5341 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005342 else
5343 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5344 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005345 if (aff->af_rare != 0
5346 || aff->af_keepcase != 0
5347 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005348 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005349 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005350 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005351 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005352 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005353 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005354 || aff->af_suff.ht_used > 0
5355 || aff->af_pref.ht_used > 0)
5356 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5357 fname, lnum, items[1]);
5358 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005359 else if (spell_info_item(items[0]))
5360 {
5361 p = (char_u *)getroom(spin,
5362 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5363 + STRLEN(items[0])
5364 + STRLEN(items[1]) + 3, FALSE);
5365 if (p != NULL)
5366 {
5367 if (spin->si_info != NULL)
5368 {
5369 STRCPY(p, spin->si_info);
5370 STRCAT(p, "\n");
5371 }
5372 STRCAT(p, items[0]);
5373 STRCAT(p, " ");
5374 STRCAT(p, items[1]);
5375 spin->si_info = p;
5376 }
5377 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005378 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005379 && midword == NULL)
5380 {
5381 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005382 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005383 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005386 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005387 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005388 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5389 || is_aff_rule(items, itemcnt, "RARE", 2))
5390 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005391 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005392 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005393 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005394 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005395 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005396 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5397 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005398 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005399 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005400 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005401 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005402 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005403 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5404 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5405 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005406 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005407 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5408 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005409 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005410 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005411 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005412 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005413 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5414 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005415 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005416 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005417 && aff->af_circumfix == 0)
5418 {
5419 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5420 fname, lnum);
5421 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005422 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005423 && aff->af_nosuggest == 0)
5424 {
5425 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5426 fname, lnum);
5427 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005428 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5429 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005430 && aff->af_needcomp == 0)
5431 {
5432 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5433 fname, lnum);
5434 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005435 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005436 && aff->af_comproot == 0)
5437 {
5438 aff->af_comproot = 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, "COMPOUNDFORBIDFLAG", 2)
5442 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005443 {
5444 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5445 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005446 if (aff->af_pref.ht_used > 0)
5447 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5448 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005449 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005450 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5451 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005452 {
5453 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5454 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005455 if (aff->af_pref.ht_used > 0)
5456 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5457 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005458 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005459 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005460 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005461 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005462 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005463 * "Na" into "Na+", "1234" into "1234+". */
5464 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005465 if (p != NULL)
5466 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005467 STRCPY(p, items[1]);
5468 STRCAT(p, "+");
5469 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005470 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005471 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005472 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5473 {
5474 /* We don't use the count, but do check that it's a number and
5475 * not COMPOUNDRULE mistyped. */
5476 if (atoi((char *)items[1]) == 0)
5477 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5478 fname, lnum, items[1]);
5479 }
5480 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005481 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005482 /* Don't use the first rule if it is a number. */
5483 if (compflags != NULL || *skipdigits(items[1]) != NUL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005484 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005485 /* Concatenate this string to previously defined ones,
5486 * using a slash to separate them. */
5487 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005488 if (compflags != NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005489 l += (int)STRLEN(compflags) + 1;
5490 p = getroom(spin, l, FALSE);
5491 if (p != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005492 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01005493 if (compflags != NULL)
5494 {
5495 STRCPY(p, compflags);
5496 STRCAT(p, "/");
5497 }
5498 STRCAT(p, items[1]);
5499 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005500 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005501 }
5502 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005503 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005504 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005505 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005506 compmax = atoi((char *)items[1]);
5507 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005508 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005509 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005510 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005511 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005512 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005513 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005514 compminlen = atoi((char *)items[1]);
5515 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005516 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5517 fname, lnum, items[1]);
5518 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005519 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005520 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005521 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005522 compsylmax = atoi((char *)items[1]);
5523 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005524 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5525 fname, lnum, items[1]);
5526 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005527 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005528 {
5529 compoptions |= COMP_CHECKDUP;
5530 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005531 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005532 {
5533 compoptions |= COMP_CHECKREP;
5534 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005535 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005536 {
5537 compoptions |= COMP_CHECKCASE;
5538 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005539 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005540 {
5541 compoptions |= COMP_CHECKTRIPLE;
5542 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005543 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005544 {
5545 if (atoi((char *)items[1]) == 0)
5546 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5547 fname, lnum, items[1]);
5548 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005549 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005550 {
5551 garray_T *gap = &spin->si_comppat;
5552 int i;
5553
5554 /* Only add the couple if it isn't already there. */
5555 for (i = 0; i < gap->ga_len - 1; i += 2)
5556 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5557 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5558 items[2]) == 0)
5559 break;
5560 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5561 {
5562 ((char_u **)(gap->ga_data))[gap->ga_len++]
5563 = getroom_save(spin, items[1]);
5564 ((char_u **)(gap->ga_data))[gap->ga_len++]
5565 = getroom_save(spin, items[2]);
5566 }
5567 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005568 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005569 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005570 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005571 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005572 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005573 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005574 {
5575 spin->si_nobreak = TRUE;
5576 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005577 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005578 {
5579 spin->si_nosplitsugs = TRUE;
5580 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005581 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005582 {
5583 spin->si_nosugfile = TRUE;
5584 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005585 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005586 {
5587 aff->af_pfxpostpone = TRUE;
5588 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005589 else if ((STRCMP(items[0], "PFX") == 0
5590 || STRCMP(items[0], "SFX") == 0)
5591 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005592 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005593 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005594 int lasti = 4;
5595 char_u key[AH_KEY_LEN];
5596
5597 if (*items[0] == 'P')
5598 tp = &aff->af_pref;
5599 else
5600 tp = &aff->af_suff;
5601
5602 /* Myspell allows the same affix name to be used multiple
5603 * times. The affix files that do this have an undocumented
5604 * "S" flag on all but the last block, thus we check for that
5605 * and store it in ah_follows. */
5606 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5607 hi = hash_find(tp, key);
5608 if (!HASHITEM_EMPTY(hi))
5609 {
5610 cur_aff = HI2AH(hi);
5611 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5612 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5613 fname, lnum, items[1]);
5614 if (!cur_aff->ah_follows)
5615 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5616 fname, lnum, items[1]);
5617 }
5618 else
5619 {
5620 /* New affix letter. */
5621 cur_aff = (affheader_T *)getroom(spin,
5622 sizeof(affheader_T), TRUE);
5623 if (cur_aff == NULL)
5624 break;
5625 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5626 fname, lnum);
5627 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5628 break;
5629 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005630 || cur_aff->ah_flag == aff->af_rare
5631 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005632 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005633 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005634 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005635 || cur_aff->ah_flag == aff->af_needcomp
5636 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005637 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 +00005638 fname, lnum, items[1]);
5639 STRCPY(cur_aff->ah_key, items[1]);
5640 hash_add(tp, cur_aff->ah_key);
5641
5642 cur_aff->ah_combine = (*items[2] == 'Y');
5643 }
5644
5645 /* Check for the "S" flag, which apparently means that another
5646 * block with the same affix name is following. */
5647 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5648 {
5649 ++lasti;
5650 cur_aff->ah_follows = TRUE;
5651 }
5652 else
5653 cur_aff->ah_follows = FALSE;
5654
Bram Moolenaar8db73182005-06-17 21:51:16 +00005655 /* Myspell allows extra text after the item, but that might
5656 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005657 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005658 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005659
Bram Moolenaar95529562005-08-25 21:21:38 +00005660 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005661 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5662 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005663
Bram Moolenaar95529562005-08-25 21:21:38 +00005664 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005665 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005666 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005667 {
5668 /* Use a new number in the .spl file later, to be able
5669 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005670 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005671 cur_aff->ah_newID = ++spin->si_newprefID;
5672
5673 /* We only really use ah_newID if the prefix is
5674 * postponed. We know that only after handling all
5675 * the items. */
5676 did_postpone_prefix = FALSE;
5677 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005678 else
5679 /* Did use the ID in a previous block. */
5680 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005681 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005682
Bram Moolenaar51485f02005-06-04 21:55:20 +00005683 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005684 }
5685 else if ((STRCMP(items[0], "PFX") == 0
5686 || STRCMP(items[0], "SFX") == 0)
5687 && aff_todo > 0
5688 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005689 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005690 {
5691 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005692 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005693 int lasti = 5;
5694
Bram Moolenaar8db73182005-06-17 21:51:16 +00005695 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005696 * mean mistakes go unnoticed. Require a comment-starter.
5697 * Hunspell uses a "-" item. */
5698 if (itemcnt > lasti && *items[lasti] != '#'
5699 && (STRCMP(items[lasti], "-") != 0
5700 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005701 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005702
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703 /* New item for an affix letter. */
5704 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005705 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005706 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005707 if (aff_entry == NULL)
5708 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005709
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005710 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005711 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005712 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005713 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005714 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005715
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005716 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005717 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5718 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005719 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005720 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005721 aff_process_flags(aff, aff_entry);
5722 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005723 }
5724
Bram Moolenaar51485f02005-06-04 21:55:20 +00005725 /* Don't use an affix entry with non-ASCII characters when
5726 * "spin->si_ascii" is TRUE. */
5727 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005728 || has_non_ascii(aff_entry->ae_add)))
5729 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005730 aff_entry->ae_next = cur_aff->ah_first;
5731 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005732
5733 if (STRCMP(items[4], ".") != 0)
5734 {
5735 char_u buf[MAXLINELEN];
5736
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005737 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005738 if (*items[0] == 'P')
5739 sprintf((char *)buf, "^%s", items[4]);
5740 else
5741 sprintf((char *)buf, "%s$", items[4]);
5742 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005743 RE_MAGIC + RE_STRING + RE_STRICT);
5744 if (aff_entry->ae_prog == NULL)
5745 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5746 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005747 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005748
5749 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005750 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005751 * Can't be done for an affix with flags, ignoring
5752 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005753 if (*items[0] == 'P' && aff->af_pfxpostpone
5754 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005755 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005756 /* When the chop string is one lower-case letter and
5757 * the add string ends in the upper-case letter we set
5758 * the "upper" flag, clear "ae_chop" and remove the
5759 * letters from "ae_add". The condition must either
5760 * be empty or start with the same letter. */
5761 if (aff_entry->ae_chop != NULL
5762 && aff_entry->ae_add != NULL
5763#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005764 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005765 aff_entry->ae_chop)] == NUL
5766#else
5767 && aff_entry->ae_chop[1] == NUL
5768#endif
5769 )
5770 {
5771 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005772
Bram Moolenaar53805d12005-08-01 07:08:33 +00005773 c = PTR2CHAR(aff_entry->ae_chop);
5774 c_up = SPELL_TOUPPER(c);
5775 if (c_up != c
5776 && (aff_entry->ae_cond == NULL
5777 || PTR2CHAR(aff_entry->ae_cond) == c))
5778 {
5779 p = aff_entry->ae_add
5780 + STRLEN(aff_entry->ae_add);
5781 mb_ptr_back(aff_entry->ae_add, p);
5782 if (PTR2CHAR(p) == c_up)
5783 {
5784 upper = TRUE;
5785 aff_entry->ae_chop = NULL;
5786 *p = NUL;
5787
5788 /* The condition is matched with the
5789 * actual word, thus must check for the
5790 * upper-case letter. */
5791 if (aff_entry->ae_cond != NULL)
5792 {
5793 char_u buf[MAXLINELEN];
5794#ifdef FEAT_MBYTE
5795 if (has_mbyte)
5796 {
5797 onecap_copy(items[4], buf, TRUE);
5798 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005799 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005800 }
5801 else
5802#endif
5803 *aff_entry->ae_cond = c_up;
5804 if (aff_entry->ae_cond != NULL)
5805 {
5806 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005807 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005808 vim_free(aff_entry->ae_prog);
5809 aff_entry->ae_prog = vim_regcomp(
5810 buf, RE_MAGIC + RE_STRING);
5811 }
5812 }
5813 }
5814 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005815 }
5816
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005817 if (aff_entry->ae_chop == NULL
5818 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005819 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005820 int idx;
5821 char_u **pp;
5822 int n;
5823
Bram Moolenaar6de68532005-08-24 22:08:48 +00005824 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005825 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5826 --idx)
5827 {
5828 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5829 if (str_equal(p, aff_entry->ae_cond))
5830 break;
5831 }
5832 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5833 {
5834 /* Not found, add a new condition. */
5835 idx = spin->si_prefcond.ga_len++;
5836 pp = ((char_u **)spin->si_prefcond.ga_data)
5837 + idx;
5838 if (aff_entry->ae_cond == NULL)
5839 *pp = NULL;
5840 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005841 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005842 aff_entry->ae_cond);
5843 }
5844
5845 /* Add the prefix to the prefix tree. */
5846 if (aff_entry->ae_add == NULL)
5847 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005848 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005849 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005850
Bram Moolenaar53805d12005-08-01 07:08:33 +00005851 /* PFX_FLAGS is a negative number, so that
5852 * tree_add_word() knows this is the prefix tree. */
5853 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005854 if (!cur_aff->ah_combine)
5855 n |= WFP_NC;
5856 if (upper)
5857 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005858 if (aff_entry->ae_comppermit)
5859 n |= WFP_COMPPERMIT;
5860 if (aff_entry->ae_compforbid)
5861 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005862 tree_add_word(spin, p, spin->si_prefroot, n,
5863 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005864 did_postpone_prefix = TRUE;
5865 }
5866
5867 /* Didn't actually use ah_newID, backup si_newprefID. */
5868 if (aff_todo == 0 && !did_postpone_prefix)
5869 {
5870 --spin->si_newprefID;
5871 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005872 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005873 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005874 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005875 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005876 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005877 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005878 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005879 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005880 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005881 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005882 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005883 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005884 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005885 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005886 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005887 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005888 else if (is_aff_rule(items, itemcnt, "REP", 2)
5889 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005890 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005891 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005892 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005893 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005894 fname, lnum);
5895 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005896 else if ((STRCMP(items[0], "REP") == 0
5897 || STRCMP(items[0], "REPSAL") == 0)
5898 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005899 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005900 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005901 /* Myspell ignores extra arguments, we require it starts with
5902 * # to detect mistakes. */
5903 if (itemcnt > 3 && items[3][0] != '#')
5904 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005905 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005906 {
5907 /* Replace underscore with space (can't include a space
5908 * directly). */
5909 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5910 if (*p == '_')
5911 *p = ' ';
5912 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5913 if (*p == '_')
5914 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005915 add_fromto(spin, items[0][3] == 'S'
5916 ? &spin->si_repsal
5917 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005918 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005919 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005920 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005921 {
5922 /* MAP item or count */
5923 if (!found_map)
5924 {
5925 /* First line contains the count. */
5926 found_map = TRUE;
5927 if (!isdigit(*items[1]))
5928 smsg((char_u *)_("Expected MAP count in %s line %d"),
5929 fname, lnum);
5930 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005931 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005932 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005933 int c;
5934
5935 /* Check that every character appears only once. */
5936 for (p = items[1]; *p != NUL; )
5937 {
5938#ifdef FEAT_MBYTE
5939 c = mb_ptr2char_adv(&p);
5940#else
5941 c = *p++;
5942#endif
5943 if ((spin->si_map.ga_len > 0
5944 && vim_strchr(spin->si_map.ga_data, c)
5945 != NULL)
5946 || vim_strchr(p, c) != NULL)
5947 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5948 fname, lnum);
5949 }
5950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005951 /* We simply concatenate all the MAP strings, separated by
5952 * slashes. */
5953 ga_concat(&spin->si_map, items[1]);
5954 ga_append(&spin->si_map, '/');
5955 }
5956 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005957 /* Accept "SAL from to" and "SAL from to #comment". */
5958 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005959 {
5960 if (do_sal)
5961 {
5962 /* SAL item (sounds-a-like)
5963 * Either one of the known keys or a from-to pair. */
5964 if (STRCMP(items[1], "followup") == 0)
5965 spin->si_followup = sal_to_bool(items[2]);
5966 else if (STRCMP(items[1], "collapse_result") == 0)
5967 spin->si_collapse = sal_to_bool(items[2]);
5968 else if (STRCMP(items[1], "remove_accents") == 0)
5969 spin->si_rem_accents = sal_to_bool(items[2]);
5970 else
5971 /* when "to" is "_" it means empty */
5972 add_fromto(spin, &spin->si_sal, items[1],
5973 STRCMP(items[2], "_") == 0 ? (char_u *)""
5974 : items[2]);
5975 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005976 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005977 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005978 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005979 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005980 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005981 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005982 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005983 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005984 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005985 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005986 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005987 else if (STRCMP(items[0], "COMMON") == 0)
5988 {
5989 int i;
5990
5991 for (i = 1; i < itemcnt; ++i)
5992 {
5993 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5994 items[i])))
5995 {
5996 p = vim_strsave(items[i]);
5997 if (p == NULL)
5998 break;
5999 hash_add(&spin->si_commonwords, p);
6000 }
6001 }
6002 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006003 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006004 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006005 fname, lnum, items[0]);
6006 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006007 }
6008
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006009 if (fol != NULL || low != NULL || upp != NULL)
6010 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006011 if (spin->si_clear_chartab)
6012 {
6013 /* Clear the char type tables, don't want to use any of the
6014 * currently used spell properties. */
6015 init_spell_chartab();
6016 spin->si_clear_chartab = FALSE;
6017 }
6018
Bram Moolenaar3982c542005-06-08 21:56:31 +00006019 /*
6020 * Don't write a word table for an ASCII file, so that we don't check
6021 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006022 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006023 * mb_get_class(), the list of chars in the file will be incomplete.
6024 */
6025 if (!spin->si_ascii
6026#ifdef FEAT_MBYTE
6027 && !enc_utf8
6028#endif
6029 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006030 {
6031 if (fol == NULL || low == NULL || upp == NULL)
6032 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6033 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006034 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006035 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006036
6037 vim_free(fol);
6038 vim_free(low);
6039 vim_free(upp);
6040 }
6041
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006042 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006043 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006044 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006045 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006046 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006047 }
6048
Bram Moolenaar6de68532005-08-24 22:08:48 +00006049 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006050 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006051 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6052 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006053 }
6054
Bram Moolenaar6de68532005-08-24 22:08:48 +00006055 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006056 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006057 if (syllable == NULL)
6058 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6059 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6060 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006061 }
6062
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006063 if (compoptions != 0)
6064 {
6065 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6066 spin->si_compoptions |= compoptions;
6067 }
6068
Bram Moolenaar6de68532005-08-24 22:08:48 +00006069 if (compflags != NULL)
6070 process_compflags(spin, aff, compflags);
6071
6072 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006073 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006074 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006075 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006076 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006077 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006078 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006079 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006080 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006081 }
6082
Bram Moolenaar6de68532005-08-24 22:08:48 +00006083 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006084 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006085 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6086 spin->si_syllable = syllable;
6087 }
6088
6089 if (sofofrom != NULL || sofoto != NULL)
6090 {
6091 if (sofofrom == NULL || sofoto == NULL)
6092 smsg((char_u *)_("Missing SOFO%s line in %s"),
6093 sofofrom == NULL ? "FROM" : "TO", fname);
6094 else if (spin->si_sal.ga_len > 0)
6095 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006096 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006097 {
6098 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6099 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6100 spin->si_sofofr = sofofrom;
6101 spin->si_sofoto = sofoto;
6102 }
6103 }
6104
6105 if (midword != NULL)
6106 {
6107 aff_check_string(spin->si_midword, midword, "MIDWORD");
6108 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006109 }
6110
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006111 vim_free(pc);
6112 fclose(fd);
6113 return aff;
6114}
6115
6116/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006117 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6118 * a comment is following after item "mincount".
6119 */
6120 static int
6121is_aff_rule(items, itemcnt, rulename, mincount)
6122 char_u **items;
6123 int itemcnt;
6124 char *rulename;
6125 int mincount;
6126{
6127 return (STRCMP(items[0], rulename) == 0
6128 && (itemcnt == mincount
6129 || (itemcnt > mincount && items[mincount][0] == '#')));
6130}
6131
6132/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006133 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6134 * ae_flags to ae_comppermit and ae_compforbid.
6135 */
6136 static void
6137aff_process_flags(affile, entry)
6138 afffile_T *affile;
6139 affentry_T *entry;
6140{
6141 char_u *p;
6142 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006143 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006144
6145 if (entry->ae_flags != NULL
6146 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6147 {
6148 for (p = entry->ae_flags; *p != NUL; )
6149 {
6150 prevp = p;
6151 flag = get_affitem(affile->af_flagtype, &p);
6152 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6153 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006154 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006155 p = prevp;
6156 if (flag == affile->af_comppermit)
6157 entry->ae_comppermit = TRUE;
6158 else
6159 entry->ae_compforbid = TRUE;
6160 }
6161 if (affile->af_flagtype == AFT_NUM && *p == ',')
6162 ++p;
6163 }
6164 if (*entry->ae_flags == NUL)
6165 entry->ae_flags = NULL; /* nothing left */
6166 }
6167}
6168
6169/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006170 * Return TRUE if "s" is the name of an info item in the affix file.
6171 */
6172 static int
6173spell_info_item(s)
6174 char_u *s;
6175{
6176 return STRCMP(s, "NAME") == 0
6177 || STRCMP(s, "HOME") == 0
6178 || STRCMP(s, "VERSION") == 0
6179 || STRCMP(s, "AUTHOR") == 0
6180 || STRCMP(s, "EMAIL") == 0
6181 || STRCMP(s, "COPYRIGHT") == 0;
6182}
6183
6184/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006185 * Turn an affix flag name into a number, according to the FLAG type.
6186 * returns zero for failure.
6187 */
6188 static unsigned
6189affitem2flag(flagtype, item, fname, lnum)
6190 int flagtype;
6191 char_u *item;
6192 char_u *fname;
6193 int lnum;
6194{
6195 unsigned res;
6196 char_u *p = item;
6197
6198 res = get_affitem(flagtype, &p);
6199 if (res == 0)
6200 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006201 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006202 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6203 fname, lnum, item);
6204 else
6205 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6206 fname, lnum, item);
6207 }
6208 if (*p != NUL)
6209 {
6210 smsg((char_u *)_(e_affname), fname, lnum, item);
6211 return 0;
6212 }
6213
6214 return res;
6215}
6216
6217/*
6218 * Get one affix name from "*pp" and advance the pointer.
6219 * Returns zero for an error, still advances the pointer then.
6220 */
6221 static unsigned
6222get_affitem(flagtype, pp)
6223 int flagtype;
6224 char_u **pp;
6225{
6226 int res;
6227
Bram Moolenaar95529562005-08-25 21:21:38 +00006228 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006229 {
6230 if (!VIM_ISDIGIT(**pp))
6231 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006232 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006233 return 0;
6234 }
6235 res = getdigits(pp);
6236 }
6237 else
6238 {
6239#ifdef FEAT_MBYTE
6240 res = mb_ptr2char_adv(pp);
6241#else
6242 res = *(*pp)++;
6243#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006244 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006245 && res >= 'A' && res <= 'Z'))
6246 {
6247 if (**pp == NUL)
6248 return 0;
6249#ifdef FEAT_MBYTE
6250 res = mb_ptr2char_adv(pp) + (res << 16);
6251#else
6252 res = *(*pp)++ + (res << 16);
6253#endif
6254 }
6255 }
6256 return res;
6257}
6258
6259/*
6260 * Process the "compflags" string used in an affix file and append it to
6261 * spin->si_compflags.
6262 * The processing involves changing the affix names to ID numbers, so that
6263 * they fit in one byte.
6264 */
6265 static void
6266process_compflags(spin, aff, compflags)
6267 spellinfo_T *spin;
6268 afffile_T *aff;
6269 char_u *compflags;
6270{
6271 char_u *p;
6272 char_u *prevp;
6273 unsigned flag;
6274 compitem_T *ci;
6275 int id;
6276 int len;
6277 char_u *tp;
6278 char_u key[AH_KEY_LEN];
6279 hashitem_T *hi;
6280
6281 /* Make room for the old and the new compflags, concatenated with a / in
6282 * between. Processing it makes it shorter, but we don't know by how
6283 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006284 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006285 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006286 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006287 p = getroom(spin, len, FALSE);
6288 if (p == NULL)
6289 return;
6290 if (spin->si_compflags != NULL)
6291 {
6292 STRCPY(p, spin->si_compflags);
6293 STRCAT(p, "/");
6294 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006295 spin->si_compflags = p;
6296 tp = p + STRLEN(p);
6297
6298 for (p = compflags; *p != NUL; )
6299 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006300 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006301 /* Copy non-flag characters directly. */
6302 *tp++ = *p++;
6303 else
6304 {
6305 /* First get the flag number, also checks validity. */
6306 prevp = p;
6307 flag = get_affitem(aff->af_flagtype, &p);
6308 if (flag != 0)
6309 {
6310 /* Find the flag in the hashtable. If it was used before, use
6311 * the existing ID. Otherwise add a new entry. */
6312 vim_strncpy(key, prevp, p - prevp);
6313 hi = hash_find(&aff->af_comp, key);
6314 if (!HASHITEM_EMPTY(hi))
6315 id = HI2CI(hi)->ci_newID;
6316 else
6317 {
6318 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6319 if (ci == NULL)
6320 break;
6321 STRCPY(ci->ci_key, key);
6322 ci->ci_flag = flag;
6323 /* Avoid using a flag ID that has a special meaning in a
6324 * regexp (also inside []). */
6325 do
6326 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006327 check_renumber(spin);
6328 id = spin->si_newcompID--;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01006329 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006330 ci->ci_newID = id;
6331 hash_add(&aff->af_comp, ci->ci_key);
6332 }
6333 *tp++ = id;
6334 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006335 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006336 ++p;
6337 }
6338 }
6339
6340 *tp = NUL;
6341}
6342
6343/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006344 * Check that the new IDs for postponed affixes and compounding don't overrun
6345 * each other. We have almost 255 available, but start at 0-127 to avoid
6346 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6347 * When that is used up an error message is given.
6348 */
6349 static void
6350check_renumber(spin)
6351 spellinfo_T *spin;
6352{
6353 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6354 {
6355 spin->si_newprefID = 127;
6356 spin->si_newcompID = 255;
6357 }
6358}
6359
6360/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006361 * Return TRUE if flag "flag" appears in affix list "afflist".
6362 */
6363 static int
6364flag_in_afflist(flagtype, afflist, flag)
6365 int flagtype;
6366 char_u *afflist;
6367 unsigned flag;
6368{
6369 char_u *p;
6370 unsigned n;
6371
6372 switch (flagtype)
6373 {
6374 case AFT_CHAR:
6375 return vim_strchr(afflist, flag) != NULL;
6376
Bram Moolenaar95529562005-08-25 21:21:38 +00006377 case AFT_CAPLONG:
6378 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006379 for (p = afflist; *p != NUL; )
6380 {
6381#ifdef FEAT_MBYTE
6382 n = mb_ptr2char_adv(&p);
6383#else
6384 n = *p++;
6385#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006386 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006387 && *p != NUL)
6388#ifdef FEAT_MBYTE
6389 n = mb_ptr2char_adv(&p) + (n << 16);
6390#else
6391 n = *p++ + (n << 16);
6392#endif
6393 if (n == flag)
6394 return TRUE;
6395 }
6396 break;
6397
Bram Moolenaar95529562005-08-25 21:21:38 +00006398 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006399 for (p = afflist; *p != NUL; )
6400 {
6401 n = getdigits(&p);
6402 if (n == flag)
6403 return TRUE;
6404 if (*p != NUL) /* skip over comma */
6405 ++p;
6406 }
6407 break;
6408 }
6409 return FALSE;
6410}
6411
6412/*
6413 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6414 */
6415 static void
6416aff_check_number(spinval, affval, name)
6417 int spinval;
6418 int affval;
6419 char *name;
6420{
6421 if (spinval != 0 && spinval != affval)
6422 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6423}
6424
6425/*
6426 * Give a warning when "spinval" and "affval" strings are set and not the same.
6427 */
6428 static void
6429aff_check_string(spinval, affval, name)
6430 char_u *spinval;
6431 char_u *affval;
6432 char *name;
6433{
6434 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6435 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6436}
6437
6438/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006439 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6440 * NULL as equal.
6441 */
6442 static int
6443str_equal(s1, s2)
6444 char_u *s1;
6445 char_u *s2;
6446{
6447 if (s1 == NULL || s2 == NULL)
6448 return s1 == s2;
6449 return STRCMP(s1, s2) == 0;
6450}
6451
6452/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006453 * Add a from-to item to "gap". Used for REP and SAL items.
6454 * They are stored case-folded.
6455 */
6456 static void
6457add_fromto(spin, gap, from, to)
6458 spellinfo_T *spin;
6459 garray_T *gap;
6460 char_u *from;
6461 char_u *to;
6462{
6463 fromto_T *ftp;
6464 char_u word[MAXWLEN];
6465
6466 if (ga_grow(gap, 1) == OK)
6467 {
6468 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006469 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006470 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006471 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006472 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006473 ++gap->ga_len;
6474 }
6475}
6476
6477/*
6478 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6479 */
6480 static int
6481sal_to_bool(s)
6482 char_u *s;
6483{
6484 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6485}
6486
6487/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006488 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6489 * When "s" is NULL FALSE is returned.
6490 */
6491 static int
6492has_non_ascii(s)
6493 char_u *s;
6494{
6495 char_u *p;
6496
6497 if (s != NULL)
6498 for (p = s; *p != NUL; ++p)
6499 if (*p >= 128)
6500 return TRUE;
6501 return FALSE;
6502}
6503
6504/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006505 * Free the structure filled by spell_read_aff().
6506 */
6507 static void
6508spell_free_aff(aff)
6509 afffile_T *aff;
6510{
6511 hashtab_T *ht;
6512 hashitem_T *hi;
6513 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006514 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006515 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516
6517 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006519 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6521 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006522 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006523 for (hi = ht->ht_array; todo > 0; ++hi)
6524 {
6525 if (!HASHITEM_EMPTY(hi))
6526 {
6527 --todo;
6528 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006529 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6530 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006531 }
6532 }
6533 if (ht == &aff->af_suff)
6534 break;
6535 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006537 hash_clear(&aff->af_pref);
6538 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006539 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006540}
6541
6542/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006543 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006544 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006545 */
6546 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006547spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006548 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006549 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006550 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006551{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006552 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006553 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006554 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006555 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006556 char_u store_afflist[MAXWLEN];
6557 int pfxlen;
6558 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006559 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006560 char_u *pc;
6561 char_u *w;
6562 int l;
6563 hash_T hash;
6564 hashitem_T *hi;
6565 FILE *fd;
6566 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006567 int non_ascii = 0;
6568 int retval = OK;
6569 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006570 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006571 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006572
Bram Moolenaar51485f02005-06-04 21:55:20 +00006573 /*
6574 * Open the file.
6575 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006576 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006577 if (fd == NULL)
6578 {
6579 EMSG2(_(e_notopen), fname);
6580 return FAIL;
6581 }
6582
Bram Moolenaar51485f02005-06-04 21:55:20 +00006583 /* The hashtable is only used to detect duplicated words. */
6584 hash_init(&ht);
6585
Bram Moolenaar4770d092006-01-12 23:22:24 +00006586 vim_snprintf((char *)IObuff, IOSIZE,
6587 _("Reading dictionary file %s ..."), fname);
6588 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006589
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006590 /* start with a message for the first line */
6591 spin->si_msg_count = 999999;
6592
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006593 /* Read and ignore the first line: word count. */
6594 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006595 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006596 EMSG2(_("E760: No word count in %s"), fname);
6597
6598 /*
6599 * Read all the lines in the file one by one.
6600 * The words are converted to 'encoding' here, before being added to
6601 * the hashtable.
6602 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006603 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006604 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006605 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006606 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006607 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006608 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006609
Bram Moolenaar51485f02005-06-04 21:55:20 +00006610 /* Remove CR, LF and white space from the end. White space halfway
6611 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006612 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006613 while (l > 0 && line[l - 1] <= ' ')
6614 --l;
6615 if (l == 0)
6616 continue; /* empty line */
6617 line[l] = NUL;
6618
Bram Moolenaarb765d632005-06-07 21:00:02 +00006619#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006620 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006621 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006622 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006624 if (pc == NULL)
6625 {
6626 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6627 fname, lnum, line);
6628 continue;
6629 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006630 w = pc;
6631 }
6632 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006633#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006634 {
6635 pc = NULL;
6636 w = line;
6637 }
6638
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006639 /* Truncate the word at the "/", set "afflist" to what follows.
6640 * Replace "\/" by "/" and "\\" by "\". */
6641 afflist = NULL;
6642 for (p = w; *p != NUL; mb_ptr_adv(p))
6643 {
6644 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006645 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006646 else if (*p == '/')
6647 {
6648 *p = NUL;
6649 afflist = p + 1;
6650 break;
6651 }
6652 }
6653
6654 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6655 if (spin->si_ascii && has_non_ascii(w))
6656 {
6657 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006658 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006659 continue;
6660 }
6661
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006662 /* This takes time, print a message every 10000 words. */
6663 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006664 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006665 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006666 vim_snprintf((char *)message, sizeof(message),
6667 _("line %6d, word %6d - %s"),
6668 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6669 msg_start();
6670 msg_puts_long_attr(message, 0);
6671 msg_clr_eos();
6672 msg_didout = FALSE;
6673 msg_col = 0;
6674 out_flush();
6675 }
6676
Bram Moolenaar51485f02005-06-04 21:55:20 +00006677 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006678 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006679 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006680 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006681 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006682 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006683 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006684 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006685
Bram Moolenaar51485f02005-06-04 21:55:20 +00006686 hash = hash_hash(dw);
6687 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006688 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006689 {
6690 if (p_verbose > 0)
6691 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006692 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006693 else if (duplicate == 0)
6694 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6695 fname, lnum, dw);
6696 ++duplicate;
6697 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006698 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006699 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006701 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006702 store_afflist[0] = NUL;
6703 pfxlen = 0;
6704 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006705 if (afflist != NULL)
6706 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006707 /* Extract flags from the affix list. */
6708 flags |= get_affix_flags(affile, afflist);
6709
Bram Moolenaar6de68532005-08-24 22:08:48 +00006710 if (affile->af_needaffix != 0 && flag_in_afflist(
6711 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006712 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006713
6714 if (affile->af_pfxpostpone)
6715 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006716 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006717
Bram Moolenaar5195e452005-08-19 20:32:47 +00006718 if (spin->si_compflags != NULL)
6719 /* Need to store the list of compound flags with the word.
6720 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006721 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006722 }
6723
Bram Moolenaar51485f02005-06-04 21:55:20 +00006724 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006725 if (store_word(spin, dw, flags, spin->si_region,
6726 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006727 retval = FAIL;
6728
6729 if (afflist != NULL)
6730 {
6731 /* Find all matching suffixes and add the resulting words.
6732 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006733 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006734 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006735 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006736 retval = FAIL;
6737
6738 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006739 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006740 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006741 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006742 retval = FAIL;
6743 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006744
6745 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006746 }
6747
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006748 if (duplicate > 0)
6749 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006750 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006751 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6752 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006753 hash_clear(&ht);
6754
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006755 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006756 return retval;
6757}
6758
6759/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006760 * Check for affix flags in "afflist" that are turned into word flags.
6761 * Return WF_ flags.
6762 */
6763 static int
6764get_affix_flags(affile, afflist)
6765 afffile_T *affile;
6766 char_u *afflist;
6767{
6768 int flags = 0;
6769
6770 if (affile->af_keepcase != 0 && flag_in_afflist(
6771 affile->af_flagtype, afflist, affile->af_keepcase))
6772 flags |= WF_KEEPCAP | WF_FIXCAP;
6773 if (affile->af_rare != 0 && flag_in_afflist(
6774 affile->af_flagtype, afflist, affile->af_rare))
6775 flags |= WF_RARE;
6776 if (affile->af_bad != 0 && flag_in_afflist(
6777 affile->af_flagtype, afflist, affile->af_bad))
6778 flags |= WF_BANNED;
6779 if (affile->af_needcomp != 0 && flag_in_afflist(
6780 affile->af_flagtype, afflist, affile->af_needcomp))
6781 flags |= WF_NEEDCOMP;
6782 if (affile->af_comproot != 0 && flag_in_afflist(
6783 affile->af_flagtype, afflist, affile->af_comproot))
6784 flags |= WF_COMPROOT;
6785 if (affile->af_nosuggest != 0 && flag_in_afflist(
6786 affile->af_flagtype, afflist, affile->af_nosuggest))
6787 flags |= WF_NOSUGGEST;
6788 return flags;
6789}
6790
6791/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006792 * Get the list of prefix IDs from the affix list "afflist".
6793 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006794 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6795 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006796 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006797 static int
6798get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006799 afffile_T *affile;
6800 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006801 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006802{
6803 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006804 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006805 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006806 int id;
6807 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006808 hashitem_T *hi;
6809
Bram Moolenaar6de68532005-08-24 22:08:48 +00006810 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006811 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006812 prevp = p;
6813 if (get_affitem(affile->af_flagtype, &p) != 0)
6814 {
6815 /* A flag is a postponed prefix flag if it appears in "af_pref"
6816 * and it's ID is not zero. */
6817 vim_strncpy(key, prevp, p - prevp);
6818 hi = hash_find(&affile->af_pref, key);
6819 if (!HASHITEM_EMPTY(hi))
6820 {
6821 id = HI2AH(hi)->ah_newID;
6822 if (id != 0)
6823 store_afflist[cnt++] = id;
6824 }
6825 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006826 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006827 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006828 }
6829
Bram Moolenaar5195e452005-08-19 20:32:47 +00006830 store_afflist[cnt] = NUL;
6831 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006832}
6833
6834/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list of compound IDs from the affix list "afflist" that are used
6836 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006837 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006838 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006839 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006840get_compflags(affile, afflist, store_afflist)
6841 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006842 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006843 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006844{
6845 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006846 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006847 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006848 char_u key[AH_KEY_LEN];
6849 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006850
Bram Moolenaar6de68532005-08-24 22:08:48 +00006851 for (p = afflist; *p != NUL; )
6852 {
6853 prevp = p;
6854 if (get_affitem(affile->af_flagtype, &p) != 0)
6855 {
6856 /* A flag is a compound flag if it appears in "af_comp". */
6857 vim_strncpy(key, prevp, p - prevp);
6858 hi = hash_find(&affile->af_comp, key);
6859 if (!HASHITEM_EMPTY(hi))
6860 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6861 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006862 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006863 ++p;
6864 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006865
Bram Moolenaar5195e452005-08-19 20:32:47 +00006866 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006867}
6868
6869/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006870 * Apply affixes to a word and store the resulting words.
6871 * "ht" is the hashtable with affentry_T that need to be applied, either
6872 * prefixes or suffixes.
6873 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6874 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006875 *
6876 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006877 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006878 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006879store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006880 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006881 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006882 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006883 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006884 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006885 hashtab_T *ht;
6886 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006887 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006888 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006889 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006890 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6891 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006892{
6893 int todo;
6894 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006895 affheader_T *ah;
6896 affentry_T *ae;
6897 regmatch_T regmatch;
6898 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006899 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006900 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006901 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006902 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006903 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006904 int use_pfxlen;
6905 int need_affix;
6906 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006907 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006908 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006909 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006910
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006911 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006912 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006913 {
6914 if (!HASHITEM_EMPTY(hi))
6915 {
6916 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006917 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006918
Bram Moolenaar51485f02005-06-04 21:55:20 +00006919 /* Check that the affix combines, if required, and that the word
6920 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006921 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6922 && flag_in_afflist(affile->af_flagtype, afflist,
6923 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006924 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006925 /* Loop over all affix entries with this name. */
6926 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006927 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006928 /* Check the condition. It's not logical to match case
6929 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006930 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006931 * Another requirement from Myspell is that the chop
6932 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006933 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006934 * prefixes with a chop string and/or flags.
6935 * When a previously added affix had CIRCUMFIX this one
6936 * must have it too, if it had not then this one must not
6937 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006938 regmatch.regprog = ae->ae_prog;
6939 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006940 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006941 || ae->ae_chop != NULL
6942 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006943 && (ae->ae_chop == NULL
6944 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006945 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006946 || vim_regexec(&regmatch, word, (colnr_T)0))
6947 && (((condit & CONDIT_CFIX) == 0)
6948 == ((condit & CONDIT_AFF) == 0
6949 || ae->ae_flags == NULL
6950 || !flag_in_afflist(affile->af_flagtype,
6951 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006952 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006953 /* Match. Remove the chop and add the affix. */
6954 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006955 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006956 /* prefix: chop/add at the start of the word */
6957 if (ae->ae_add == NULL)
6958 *newword = NUL;
6959 else
6960 STRCPY(newword, ae->ae_add);
6961 p = word;
6962 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006963 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006964 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006965#ifdef FEAT_MBYTE
6966 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006967 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006968 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006969 for ( ; i > 0; --i)
6970 mb_ptr_adv(p);
6971 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006972 else
6973#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006974 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006975 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006976 STRCAT(newword, p);
6977 }
6978 else
6979 {
6980 /* suffix: chop/add at the end of the word */
6981 STRCPY(newword, word);
6982 if (ae->ae_chop != NULL)
6983 {
6984 /* Remove chop string. */
6985 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006986 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006987 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006988 mb_ptr_back(newword, p);
6989 *p = NUL;
6990 }
6991 if (ae->ae_add != NULL)
6992 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006993 }
6994
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006995 use_flags = flags;
6996 use_pfxlist = pfxlist;
6997 use_pfxlen = pfxlen;
6998 need_affix = FALSE;
6999 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
7000 if (ae->ae_flags != NULL)
7001 {
7002 /* Extract flags from the affix list. */
7003 use_flags |= get_affix_flags(affile, ae->ae_flags);
7004
7005 if (affile->af_needaffix != 0 && flag_in_afflist(
7006 affile->af_flagtype, ae->ae_flags,
7007 affile->af_needaffix))
7008 need_affix = TRUE;
7009
7010 /* When there is a CIRCUMFIX flag the other affix
7011 * must also have it and we don't add the word
7012 * with one affix. */
7013 if (affile->af_circumfix != 0 && flag_in_afflist(
7014 affile->af_flagtype, ae->ae_flags,
7015 affile->af_circumfix))
7016 {
7017 use_condit |= CONDIT_CFIX;
7018 if ((condit & CONDIT_CFIX) == 0)
7019 need_affix = TRUE;
7020 }
7021
7022 if (affile->af_pfxpostpone
7023 || spin->si_compflags != NULL)
7024 {
7025 if (affile->af_pfxpostpone)
7026 /* Get prefix IDS from the affix list. */
7027 use_pfxlen = get_pfxlist(affile,
7028 ae->ae_flags, store_afflist);
7029 else
7030 use_pfxlen = 0;
7031 use_pfxlist = store_afflist;
7032
7033 /* Combine the prefix IDs. Avoid adding the
7034 * same ID twice. */
7035 for (i = 0; i < pfxlen; ++i)
7036 {
7037 for (j = 0; j < use_pfxlen; ++j)
7038 if (pfxlist[i] == use_pfxlist[j])
7039 break;
7040 if (j == use_pfxlen)
7041 use_pfxlist[use_pfxlen++] = pfxlist[i];
7042 }
7043
7044 if (spin->si_compflags != NULL)
7045 /* Get compound IDS from the affix list. */
7046 get_compflags(affile, ae->ae_flags,
7047 use_pfxlist + use_pfxlen);
7048
7049 /* Combine the list of compound flags.
7050 * Concatenate them to the prefix IDs list.
7051 * Avoid adding the same ID twice. */
7052 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7053 {
7054 for (j = use_pfxlen;
7055 use_pfxlist[j] != NUL; ++j)
7056 if (pfxlist[i] == use_pfxlist[j])
7057 break;
7058 if (use_pfxlist[j] == NUL)
7059 {
7060 use_pfxlist[j++] = pfxlist[i];
7061 use_pfxlist[j] = NUL;
7062 }
7063 }
7064 }
7065 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007066
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007067 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007068 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007069 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007070 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007071 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007072 use_pfxlist = pfx_pfxlist;
7073 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007074
7075 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007076 if (spin->si_prefroot != NULL
7077 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007078 {
7079 /* ... add a flag to indicate an affix was used. */
7080 use_flags |= WF_HAS_AFF;
7081
7082 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007083 * affixes is not allowed. But do use the
7084 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007085 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007086 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007087 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007088
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007089 /* When compounding is supported and there is no
7090 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7091 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007092 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007093 {
7094 if (xht != NULL)
7095 use_flags |= WF_NOCOMPAFT;
7096 else
7097 use_flags |= WF_NOCOMPBEF;
7098 }
7099
Bram Moolenaar51485f02005-06-04 21:55:20 +00007100 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007101 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007102 spin->si_region, use_pfxlist,
7103 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007104 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007105
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007106 /* When added a prefix or a first suffix and the affix
7107 * has flags may add a(nother) suffix. RECURSIVE! */
7108 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7109 if (store_aff_word(spin, newword, ae->ae_flags,
7110 affile, &affile->af_suff, xht,
7111 use_condit & (xht == NULL
7112 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007113 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007114 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007115
7116 /* When added a suffix and combining is allowed also
7117 * try adding a prefix additionally. Both for the
7118 * word flags and for the affix flags. RECURSIVE! */
7119 if (xht != NULL && ah->ah_combine)
7120 {
7121 if (store_aff_word(spin, newword,
7122 afflist, affile,
7123 xht, NULL, use_condit,
7124 use_flags, use_pfxlist,
7125 pfxlen) == FAIL
7126 || (ae->ae_flags != NULL
7127 && store_aff_word(spin, newword,
7128 ae->ae_flags, affile,
7129 xht, NULL, use_condit,
7130 use_flags, use_pfxlist,
7131 pfxlen) == FAIL))
7132 retval = FAIL;
7133 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007134 }
7135 }
7136 }
7137 }
7138 }
7139
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007140 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007141}
7142
7143/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007144 * Read a file with a list of words.
7145 */
7146 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007147spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007148 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007149 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007150{
7151 FILE *fd;
7152 long lnum = 0;
7153 char_u rline[MAXLINELEN];
7154 char_u *line;
7155 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007156 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007157 int l;
7158 int retval = OK;
7159 int did_word = FALSE;
7160 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007161 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007162 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007163
7164 /*
7165 * Open the file.
7166 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007167 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007168 if (fd == NULL)
7169 {
7170 EMSG2(_(e_notopen), fname);
7171 return FAIL;
7172 }
7173
Bram Moolenaar4770d092006-01-12 23:22:24 +00007174 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7175 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007176
7177 /*
7178 * Read all the lines in the file one by one.
7179 */
7180 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7181 {
7182 line_breakcheck();
7183 ++lnum;
7184
7185 /* Skip comment lines. */
7186 if (*rline == '#')
7187 continue;
7188
7189 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007190 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007191 while (l > 0 && rline[l - 1] <= ' ')
7192 --l;
7193 if (l == 0)
7194 continue; /* empty or blank line */
7195 rline[l] = NUL;
7196
Bram Moolenaar9c102382006-05-03 21:26:49 +00007197 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007199#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007200 if (spin->si_conv.vc_type != CONV_NONE)
7201 {
7202 pc = string_convert(&spin->si_conv, rline, NULL);
7203 if (pc == NULL)
7204 {
7205 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7206 fname, lnum, rline);
7207 continue;
7208 }
7209 line = pc;
7210 }
7211 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007212#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007213 {
7214 pc = NULL;
7215 line = rline;
7216 }
7217
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007218 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007219 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007220 ++line;
7221 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007222 {
7223 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007224 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7225 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007226 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007227 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7228 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007229 else
7230 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007231#ifdef FEAT_MBYTE
7232 char_u *enc;
7233
Bram Moolenaar51485f02005-06-04 21:55:20 +00007234 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007235 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007236 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007237 if (enc != NULL && !spin->si_ascii
7238 && convert_setup(&spin->si_conv, enc,
7239 p_enc) == FAIL)
7240 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007241 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007242 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007243 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007244#else
7245 smsg((char_u *)_("Conversion in %s not supported"), fname);
7246#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007247 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007248 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007249 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007250
Bram Moolenaar3982c542005-06-08 21:56:31 +00007251 if (STRNCMP(line, "regions=", 8) == 0)
7252 {
7253 if (spin->si_region_count > 1)
7254 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7255 fname, lnum, line);
7256 else
7257 {
7258 line += 8;
7259 if (STRLEN(line) > 16)
7260 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7261 fname, lnum, line);
7262 else
7263 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007264 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007265 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007266
7267 /* Adjust the mask for a word valid in all regions. */
7268 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007269 }
7270 }
7271 continue;
7272 }
7273
Bram Moolenaar7887d882005-07-01 22:33:52 +00007274 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7275 fname, lnum, line - 1);
7276 continue;
7277 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007278
Bram Moolenaar7887d882005-07-01 22:33:52 +00007279 flags = 0;
7280 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007281
Bram Moolenaar7887d882005-07-01 22:33:52 +00007282 /* Check for flags and region after a slash. */
7283 p = vim_strchr(line, '/');
7284 if (p != NULL)
7285 {
7286 *p++ = NUL;
7287 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007288 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007289 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007290 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007291 else if (*p == '!') /* Bad, bad, wicked word. */
7292 flags |= WF_BANNED;
7293 else if (*p == '?') /* Rare word. */
7294 flags |= WF_RARE;
7295 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007296 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007297 if ((flags & WF_REGION) == 0) /* first one */
7298 regionmask = 0;
7299 flags |= WF_REGION;
7300
7301 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007302 if (l > spin->si_region_count)
7303 {
7304 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007305 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007306 break;
7307 }
7308 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007309 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007310 else
7311 {
7312 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7313 fname, lnum, p);
7314 break;
7315 }
7316 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007317 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007318 }
7319
7320 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7321 if (spin->si_ascii && has_non_ascii(line))
7322 {
7323 ++non_ascii;
7324 continue;
7325 }
7326
7327 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007328 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007329 {
7330 retval = FAIL;
7331 break;
7332 }
7333 did_word = TRUE;
7334 }
7335
7336 vim_free(pc);
7337 fclose(fd);
7338
Bram Moolenaar4770d092006-01-12 23:22:24 +00007339 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007340 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007341 vim_snprintf((char *)IObuff, IOSIZE,
7342 _("Ignored %d words with non-ASCII characters"), non_ascii);
7343 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007344 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007345
Bram Moolenaar51485f02005-06-04 21:55:20 +00007346 return retval;
7347}
7348
7349/*
7350 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007351 * This avoids calling free() for every little struct we use (and keeping
7352 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007353 * The memory is cleared to all zeros.
7354 * Returns NULL when out of memory.
7355 */
7356 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007357getroom(spin, len, align)
7358 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007359 size_t len; /* length needed */
7360 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007361{
7362 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007363 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007364
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007365 if (align && bl != NULL)
7366 /* Round size up for alignment. On some systems structures need to be
7367 * aligned to the size of a pointer (e.g., SPARC). */
7368 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7369 & ~(sizeof(char *) - 1);
7370
Bram Moolenaar51485f02005-06-04 21:55:20 +00007371 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7372 {
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007373 if (len >= SBLOCKSIZE)
7374 bl = NULL;
7375 else
7376 /* Allocate a block of memory. It is not freed until much later. */
7377 bl = (sblock_T *)alloc_clear(
7378 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007379 if (bl == NULL)
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007380 {
7381 if (!spin->si_did_emsg)
7382 {
7383 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7384 spin->si_did_emsg = TRUE;
7385 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007386 return NULL;
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007387 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007388 bl->sb_next = spin->si_blocks;
7389 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007390 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007391 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007392 }
7393
7394 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007395 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007396
7397 return p;
7398}
7399
7400/*
7401 * Make a copy of a string into memory allocated with getroom().
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007402 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007403 */
7404 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007405getroom_save(spin, s)
7406 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007407 char_u *s;
7408{
7409 char_u *sc;
7410
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007411 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007412 if (sc != NULL)
7413 STRCPY(sc, s);
7414 return sc;
7415}
7416
7417
7418/*
7419 * Free the list of allocated sblock_T.
7420 */
7421 static void
7422free_blocks(bl)
7423 sblock_T *bl;
7424{
7425 sblock_T *next;
7426
7427 while (bl != NULL)
7428 {
7429 next = bl->sb_next;
7430 vim_free(bl);
7431 bl = next;
7432 }
7433}
7434
7435/*
7436 * Allocate the root of a word tree.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007437 * Returns NULL when out of memory.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007438 */
7439 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007440wordtree_alloc(spin)
7441 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007442{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007443 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007444}
7445
7446/*
7447 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007448 * Always store it in the case-folded tree. For a keep-case word this is
7449 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7450 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007451 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007452 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7453 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007454 */
7455 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007456store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007457 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007458 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007459 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007460 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007461 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007462 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007463{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007464 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007465 int ct = captype(word, word + len);
7466 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007467 int res = OK;
7468 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007470 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007471 for (p = pfxlist; res == OK; ++p)
7472 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007473 if (!need_affix || (p != NULL && *p != NUL))
7474 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007475 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007476 if (p == NULL || *p == NUL)
7477 break;
7478 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007479 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007480
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007481 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007482 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007483 for (p = pfxlist; res == OK; ++p)
7484 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007485 if (!need_affix || (p != NULL && *p != NUL))
7486 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007487 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007488 if (p == NULL || *p == NUL)
7489 break;
7490 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007491 ++spin->si_keepwcount;
7492 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007493 return res;
7494}
7495
7496/*
7497 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007498 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007499 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007500 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007501 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007502 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007503tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007504 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007505 char_u *word;
7506 wordnode_T *root;
7507 int flags;
7508 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007509 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007510{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007511 wordnode_T *node = root;
7512 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007513 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007514 wordnode_T **prev = NULL;
7515 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007516
Bram Moolenaar51485f02005-06-04 21:55:20 +00007517 /* Add each byte of the word to the tree, including the NUL at the end. */
7518 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007519 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007520 /* When there is more than one reference to this node we need to make
7521 * a copy, so that we can modify it. Copy the whole list of siblings
7522 * (we don't optimize for a partly shared list of siblings). */
7523 if (node != NULL && node->wn_refs > 1)
7524 {
7525 --node->wn_refs;
7526 copyprev = prev;
7527 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7528 {
7529 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007530 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007531 if (np == NULL)
7532 return FAIL;
7533 np->wn_child = copyp->wn_child;
7534 if (np->wn_child != NULL)
7535 ++np->wn_child->wn_refs; /* child gets extra ref */
7536 np->wn_byte = copyp->wn_byte;
7537 if (np->wn_byte == NUL)
7538 {
7539 np->wn_flags = copyp->wn_flags;
7540 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007541 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007542 }
7543
7544 /* Link the new node in the list, there will be one ref. */
7545 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007546 if (copyprev != NULL)
7547 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007548 copyprev = &np->wn_sibling;
7549
7550 /* Let "node" point to the head of the copied list. */
7551 if (copyp == node)
7552 node = np;
7553 }
7554 }
7555
Bram Moolenaar51485f02005-06-04 21:55:20 +00007556 /* Look for the sibling that has the same character. They are sorted
7557 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007558 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007559 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007560 while (node != NULL
7561 && (node->wn_byte < word[i]
7562 || (node->wn_byte == NUL
7563 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007564 ? node->wn_affixID < (unsigned)affixID
7565 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007566 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007567 && (spin->si_sugtree
7568 ? (node->wn_region & 0xffff) < region
7569 : node->wn_affixID
7570 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007571 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007572 prev = &node->wn_sibling;
7573 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007574 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007575 if (node == NULL
7576 || node->wn_byte != word[i]
7577 || (word[i] == NUL
7578 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007579 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007580 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007581 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007582 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007583 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007584 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007585 if (np == NULL)
7586 return FAIL;
7587 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007588
7589 /* If "node" is NULL this is a new child or the end of the sibling
7590 * list: ref count is one. Otherwise use ref count of sibling and
7591 * make ref count of sibling one (matters when inserting in front
7592 * of the list of siblings). */
7593 if (node == NULL)
7594 np->wn_refs = 1;
7595 else
7596 {
7597 np->wn_refs = node->wn_refs;
7598 node->wn_refs = 1;
7599 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007600 if (prev != NULL)
7601 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007602 np->wn_sibling = node;
7603 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007604 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007605
Bram Moolenaar51485f02005-06-04 21:55:20 +00007606 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007607 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007608 node->wn_flags = flags;
7609 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007610 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007611 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007612 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007613 prev = &node->wn_child;
7614 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007615 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007616#ifdef SPELL_PRINTTREE
7617 smsg("Added \"%s\"", word);
7618 spell_print_tree(root->wn_sibling);
7619#endif
7620
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007621 /* count nr of words added since last message */
7622 ++spin->si_msg_count;
7623
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007624 if (spin->si_compress_cnt > 1)
7625 {
7626 if (--spin->si_compress_cnt == 1)
7627 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007628 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007629 }
7630
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007631 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007632 * When we have allocated lots of memory we need to compress the word tree
7633 * to free up some room. But compression is slow, and we might actually
7634 * need that room, thus only compress in the following situations:
7635 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007636 * "compress_start" blocks.
7637 * 2. When compressed before and used "compress_inc" blocks before
7638 * adding "compress_added" words (si_compress_cnt > 1).
7639 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007640 * (si_compress_cnt == 1) and the number of free nodes drops below the
7641 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007642 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007643#ifndef SPELL_PRINTTREE
7644 if (spin->si_compress_cnt == 1
7645 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007646 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007647#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007648 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007649 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007650 * when the freed up room has been used and another "compress_inc"
7651 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007652 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007653 spin->si_blocks_cnt -= compress_inc;
7654 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007655
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007656 if (spin->si_verbose)
7657 {
7658 msg_start();
7659 msg_puts((char_u *)_(msg_compressing));
7660 msg_clr_eos();
7661 msg_didout = FALSE;
7662 msg_col = 0;
7663 out_flush();
7664 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007665
7666 /* Compress both trees. Either they both have many nodes, which makes
7667 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007668 * compression goes fast. But when filling the souldfold word tree
7669 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007670 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007671 if (affixID >= 0)
7672 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007673 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007674
7675 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007676}
7677
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007678/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007679 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7680 * Sets "sps_flags".
7681 */
7682 int
7683spell_check_msm()
7684{
7685 char_u *p = p_msm;
7686 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007687 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007688 long added = 0;
7689
7690 if (!VIM_ISDIGIT(*p))
7691 return FAIL;
7692 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7693 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7694 if (*p != ',')
7695 return FAIL;
7696 ++p;
7697 if (!VIM_ISDIGIT(*p))
7698 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007699 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007700 if (*p != ',')
7701 return FAIL;
7702 ++p;
7703 if (!VIM_ISDIGIT(*p))
7704 return FAIL;
7705 added = getdigits(&p) * 1024;
7706 if (*p != NUL)
7707 return FAIL;
7708
Bram Moolenaar89d40322006-08-29 15:30:07 +00007709 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007710 return FAIL;
7711
7712 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007713 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007714 compress_added = added;
7715 return OK;
7716}
7717
7718
7719/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007720 * Get a wordnode_T, either from the list of previously freed nodes or
7721 * allocate a new one.
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007722 * Returns NULL when out of memory.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007723 */
7724 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007725get_wordnode(spin)
7726 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007727{
7728 wordnode_T *n;
7729
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007730 if (spin->si_first_free == NULL)
7731 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007732 else
7733 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007734 n = spin->si_first_free;
7735 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007736 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007737 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007738 }
7739#ifdef SPELL_PRINTTREE
Bram Moolenaarc98d5ee2011-02-01 13:59:48 +01007740 if (n != NULL)
7741 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007742#endif
7743 return n;
7744}
7745
7746/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007747 * Decrement the reference count on a node (which is the head of a list of
7748 * siblings). If the reference count becomes zero free the node and its
7749 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007750 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007751 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007752 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007753deref_wordnode(spin, node)
7754 spellinfo_T *spin;
7755 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007756{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007757 wordnode_T *np;
7758 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007759
7760 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007761 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007762 for (np = node; np != NULL; np = np->wn_sibling)
7763 {
7764 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007765 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007766 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007767 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007768 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007769 ++cnt; /* length field */
7770 }
7771 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007772}
7773
7774/*
7775 * Free a wordnode_T for re-use later.
7776 * Only the "wn_child" field becomes invalid.
7777 */
7778 static void
7779free_wordnode(spin, n)
7780 spellinfo_T *spin;
7781 wordnode_T *n;
7782{
7783 n->wn_child = spin->si_first_free;
7784 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007785 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007786}
7787
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007788/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007789 * Compress a tree: find tails that are identical and can be shared.
7790 */
7791 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007792wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007793 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007794 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007795{
7796 hashtab_T ht;
7797 int n;
7798 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007799 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007800
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007801 /* Skip the root itself, it's not actually used. The first sibling is the
7802 * start of the tree. */
7803 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007804 {
7805 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007806 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007807
7808#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007809 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007810#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007811 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007812 if (tot > 1000000)
7813 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007814 else if (tot == 0)
7815 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007816 else
7817 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007818 vim_snprintf((char *)IObuff, IOSIZE,
7819 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7820 n, tot, tot - n, perc);
7821 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007822 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007823#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007824 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007825#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007826 hash_clear(&ht);
7827 }
7828}
7829
7830/*
7831 * Compress a node, its siblings and its children, depth first.
7832 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007833 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007834 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007835node_compress(spin, node, ht, tot)
7836 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007837 wordnode_T *node;
7838 hashtab_T *ht;
7839 int *tot; /* total count of nodes before compressing,
7840 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007841{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007842 wordnode_T *np;
7843 wordnode_T *tp;
7844 wordnode_T *child;
7845 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007846 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007847 int len = 0;
7848 unsigned nr, n;
7849 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007850
Bram Moolenaar51485f02005-06-04 21:55:20 +00007851 /*
7852 * Go through the list of siblings. Compress each child and then try
7853 * finding an identical child to replace it.
7854 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007855 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007856 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007857 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007858 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007859 ++len;
7860 if ((child = np->wn_child) != NULL)
7861 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007862 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007863 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007864
7865 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007866 hash = hash_hash(child->wn_u1.hashkey);
7867 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007868 if (!HASHITEM_EMPTY(hi))
7869 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007870 /* There are children we encountered before with a hash value
7871 * identical to the current child. Now check if there is one
7872 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007873 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007874 if (node_equal(child, tp))
7875 {
7876 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007877 * current one. This means the current child and all
7878 * its siblings is unlinked from the tree. */
7879 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007880 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007881 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007882 break;
7883 }
7884 if (tp == NULL)
7885 {
7886 /* No other child with this hash value equals the child of
7887 * the node, add it to the linked list after the first
7888 * item. */
7889 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007890 child->wn_u2.next = tp->wn_u2.next;
7891 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007892 }
7893 }
7894 else
7895 /* No other child has this hash value, add it to the
7896 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007897 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007898 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007899 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007900 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007901
7902 /*
7903 * Make a hash key for the node and its siblings, so that we can quickly
7904 * find a lookalike node. This must be done after compressing the sibling
7905 * list, otherwise the hash key would become invalid by the compression.
7906 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007907 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007908 nr = 0;
7909 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007910 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007911 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007912 /* end node: use wn_flags, wn_region and wn_affixID */
7913 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007914 else
7915 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007916 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007917 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007918 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007919
7920 /* Avoid NUL bytes, it terminates the hash key. */
7921 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007922 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007923 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007924 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007925 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007926 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007927 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007928 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7929 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007930
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007931 /* Check for CTRL-C pressed now and then. */
7932 fast_breakcheck();
7933
Bram Moolenaar51485f02005-06-04 21:55:20 +00007934 return compressed;
7935}
7936
7937/*
7938 * Return TRUE when two nodes have identical siblings and children.
7939 */
7940 static int
7941node_equal(n1, n2)
7942 wordnode_T *n1;
7943 wordnode_T *n2;
7944{
7945 wordnode_T *p1;
7946 wordnode_T *p2;
7947
7948 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7949 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7950 if (p1->wn_byte != p2->wn_byte
7951 || (p1->wn_byte == NUL
7952 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007953 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007954 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007955 : (p1->wn_child != p2->wn_child)))
7956 break;
7957
7958 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007959}
7960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007961static int
7962#ifdef __BORLANDC__
7963_RTLENTRYF
7964#endif
7965rep_compare __ARGS((const void *s1, const void *s2));
7966
7967/*
7968 * Function given to qsort() to sort the REP items on "from" string.
7969 */
7970 static int
7971#ifdef __BORLANDC__
7972_RTLENTRYF
7973#endif
7974rep_compare(s1, s2)
7975 const void *s1;
7976 const void *s2;
7977{
7978 fromto_T *p1 = (fromto_T *)s1;
7979 fromto_T *p2 = (fromto_T *)s2;
7980
7981 return STRCMP(p1->ft_from, p2->ft_from);
7982}
7983
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007984/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007985 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007986 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007987 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007988 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007989write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007990 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007991 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007992{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007993 FILE *fd;
7994 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007995 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007996 wordnode_T *tree;
7997 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007998 int i;
7999 int l;
8000 garray_T *gap;
8001 fromto_T *ftp;
8002 char_u *p;
8003 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008004 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008005 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008006 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008007
Bram Moolenaarb765d632005-06-07 21:00:02 +00008008 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00008009 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008010 {
8011 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008012 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008013 }
8014
Bram Moolenaar5195e452005-08-19 20:32:47 +00008015 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008016 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008017 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008018 if (fwv != (size_t)1)
8019 /* Catch first write error, don't try writing more. */
8020 goto theend;
8021
Bram Moolenaar5195e452005-08-19 20:32:47 +00008022 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008023
Bram Moolenaar5195e452005-08-19 20:32:47 +00008024 /*
8025 * <SECTIONS>: <section> ... <sectionend>
8026 */
8027
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008028 /* SN_INFO: <infotext> */
8029 if (spin->si_info != NULL)
8030 {
8031 putc(SN_INFO, fd); /* <sectionID> */
8032 putc(0, fd); /* <sectionflags> */
8033
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008034 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008035 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008036 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008037 }
8038
Bram Moolenaar5195e452005-08-19 20:32:47 +00008039 /* SN_REGION: <regionname> ...
8040 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008041 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008042 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008043 putc(SN_REGION, fd); /* <sectionID> */
8044 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8045 l = spin->si_region_count * 2;
8046 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008047 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008048 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008049 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008050 }
8051 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008052 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008053
Bram Moolenaar5195e452005-08-19 20:32:47 +00008054 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8055 *
8056 * The table with character flags and the table for case folding.
8057 * This makes sure the same characters are recognized as word characters
8058 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008059 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008060 * 'encoding'.
8061 * Also skip this for an .add.spl file, the main spell file must contain
8062 * the table (avoids that it conflicts). File is shorter too.
8063 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008064 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008065 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008066 char_u folchars[128 * 8];
8067 int flags;
8068
Bram Moolenaard12a1322005-08-21 22:08:24 +00008069 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008070 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8071
8072 /* Form the <folchars> string first, we need to know its length. */
8073 l = 0;
8074 for (i = 128; i < 256; ++i)
8075 {
8076#ifdef FEAT_MBYTE
8077 if (has_mbyte)
8078 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8079 else
8080#endif
8081 folchars[l++] = spelltab.st_fold[i];
8082 }
8083 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8084
8085 fputc(128, fd); /* <charflagslen> */
8086 for (i = 128; i < 256; ++i)
8087 {
8088 flags = 0;
8089 if (spelltab.st_isw[i])
8090 flags |= CF_WORD;
8091 if (spelltab.st_isu[i])
8092 flags |= CF_UPPER;
8093 fputc(flags, fd); /* <charflags> */
8094 }
8095
8096 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008097 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008098 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008099
Bram Moolenaar5195e452005-08-19 20:32:47 +00008100 /* SN_MIDWORD: <midword> */
8101 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008102 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008103 putc(SN_MIDWORD, fd); /* <sectionID> */
8104 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008106 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008107 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008108 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8109 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008110 }
8111
Bram Moolenaar5195e452005-08-19 20:32:47 +00008112 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8113 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008114 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008115 putc(SN_PREFCOND, fd); /* <sectionID> */
8116 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8117
8118 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8119 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8120
8121 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008122 }
8123
Bram Moolenaar5195e452005-08-19 20:32:47 +00008124 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008125 * SN_SAL: <salflags> <salcount> <sal> ...
8126 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008127
Bram Moolenaar5195e452005-08-19 20:32:47 +00008128 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008129 * round 2: SN_SAL section (unless SN_SOFO is used)
8130 * round 3: SN_REPSAL section */
8131 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008132 {
8133 if (round == 1)
8134 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008135 else if (round == 2)
8136 {
8137 /* Don't write SN_SAL when using a SN_SOFO section */
8138 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8139 continue;
8140 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008142 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008143 gap = &spin->si_repsal;
8144
8145 /* Don't write the section if there are no items. */
8146 if (gap->ga_len == 0)
8147 continue;
8148
8149 /* Sort the REP/REPSAL items. */
8150 if (round != 2)
8151 qsort(gap->ga_data, (size_t)gap->ga_len,
8152 sizeof(fromto_T), rep_compare);
8153
8154 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8155 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008156
Bram Moolenaar5195e452005-08-19 20:32:47 +00008157 /* This is for making suggestions, section is not required. */
8158 putc(0, fd); /* <sectionflags> */
8159
8160 /* Compute the length of what follows. */
8161 l = 2; /* count <repcount> or <salcount> */
8162 for (i = 0; i < gap->ga_len; ++i)
8163 {
8164 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008165 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8166 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008167 }
8168 if (round == 2)
8169 ++l; /* count <salflags> */
8170 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8171
8172 if (round == 2)
8173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 i = 0;
8175 if (spin->si_followup)
8176 i |= SAL_F0LLOWUP;
8177 if (spin->si_collapse)
8178 i |= SAL_COLLAPSE;
8179 if (spin->si_rem_accents)
8180 i |= SAL_REM_ACCENTS;
8181 putc(i, fd); /* <salflags> */
8182 }
8183
8184 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8185 for (i = 0; i < gap->ga_len; ++i)
8186 {
8187 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8188 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8189 ftp = &((fromto_T *)gap->ga_data)[i];
8190 for (rr = 1; rr <= 2; ++rr)
8191 {
8192 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008193 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008194 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008195 if (l > 0)
8196 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008197 }
8198 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200 }
8201
Bram Moolenaar5195e452005-08-19 20:32:47 +00008202 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8203 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008204 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8205 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008206 putc(SN_SOFO, fd); /* <sectionID> */
8207 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008208
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008209 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008210 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8211 /* <sectionlen> */
8212
8213 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008214 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008215
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008216 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008217 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008218 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008219 }
8220
Bram Moolenaar4770d092006-01-12 23:22:24 +00008221 /* SN_WORDS: <word> ...
8222 * This is for making suggestions, section is not required. */
8223 if (spin->si_commonwords.ht_used > 0)
8224 {
8225 putc(SN_WORDS, fd); /* <sectionID> */
8226 putc(0, fd); /* <sectionflags> */
8227
8228 /* round 1: count the bytes
8229 * round 2: write the bytes */
8230 for (round = 1; round <= 2; ++round)
8231 {
8232 int todo;
8233 int len = 0;
8234 hashitem_T *hi;
8235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008236 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008237 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8238 if (!HASHITEM_EMPTY(hi))
8239 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008240 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008241 len += l;
8242 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008243 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008244 --todo;
8245 }
8246 if (round == 1)
8247 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8248 }
8249 }
8250
Bram Moolenaar5195e452005-08-19 20:32:47 +00008251 /* SN_MAP: <mapstr>
8252 * This is for making suggestions, section is not required. */
8253 if (spin->si_map.ga_len > 0)
8254 {
8255 putc(SN_MAP, fd); /* <sectionID> */
8256 putc(0, fd); /* <sectionflags> */
8257 l = spin->si_map.ga_len;
8258 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008259 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008260 /* <mapstr> */
8261 }
8262
Bram Moolenaar4770d092006-01-12 23:22:24 +00008263 /* SN_SUGFILE: <timestamp>
8264 * This is used to notify that a .sug file may be available and at the
8265 * same time allows for checking that a .sug file that is found matches
8266 * with this .spl file. That's because the word numbers must be exactly
8267 * right. */
8268 if (!spin->si_nosugfile
8269 && (spin->si_sal.ga_len > 0
8270 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8271 {
8272 putc(SN_SUGFILE, fd); /* <sectionID> */
8273 putc(0, fd); /* <sectionflags> */
8274 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8275
8276 /* Set si_sugtime and write it to the file. */
8277 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008278 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008279 }
8280
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008281 /* SN_NOSPLITSUGS: nothing
8282 * This is used to notify that no suggestions with word splits are to be
8283 * made. */
8284 if (spin->si_nosplitsugs)
8285 {
8286 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8287 putc(0, fd); /* <sectionflags> */
8288 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8289 }
8290
Bram Moolenaar5195e452005-08-19 20:32:47 +00008291 /* SN_COMPOUND: compound info.
8292 * We don't mark it required, when not supported all compound words will
8293 * be bad words. */
8294 if (spin->si_compflags != NULL)
8295 {
8296 putc(SN_COMPOUND, fd); /* <sectionID> */
8297 putc(0, fd); /* <sectionflags> */
8298
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008299 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008300 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008301 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008302 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8303
Bram Moolenaar5195e452005-08-19 20:32:47 +00008304 putc(spin->si_compmax, fd); /* <compmax> */
8305 putc(spin->si_compminlen, fd); /* <compminlen> */
8306 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008307 putc(0, fd); /* for Vim 7.0b compatibility */
8308 putc(spin->si_compoptions, fd); /* <compoptions> */
8309 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8310 /* <comppatcount> */
8311 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8312 {
8313 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008314 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008315 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8316 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008317 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008318 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008319 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008320 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008321 }
8322
Bram Moolenaar78622822005-08-23 21:00:13 +00008323 /* SN_NOBREAK: NOBREAK flag */
8324 if (spin->si_nobreak)
8325 {
8326 putc(SN_NOBREAK, fd); /* <sectionID> */
8327 putc(0, fd); /* <sectionflags> */
8328
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008329 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008330 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8331 }
8332
Bram Moolenaar5195e452005-08-19 20:32:47 +00008333 /* SN_SYLLABLE: syllable info.
8334 * We don't mark it required, when not supported syllables will not be
8335 * counted. */
8336 if (spin->si_syllable != NULL)
8337 {
8338 putc(SN_SYLLABLE, fd); /* <sectionID> */
8339 putc(0, fd); /* <sectionflags> */
8340
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008341 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008342 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008343 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8344 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008345 }
8346
8347 /* end of <SECTIONS> */
8348 putc(SN_END, fd); /* <sectionend> */
8349
Bram Moolenaar50cde822005-06-05 21:54:54 +00008350
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008351 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008352 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008353 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008354 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008355 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008356 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008357 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008358 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008359 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008360 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008361 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008362 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008363
Bram Moolenaar0c405862005-06-22 22:26:26 +00008364 /* Clear the index and wnode fields in the tree. */
8365 clear_node(tree);
8366
Bram Moolenaar51485f02005-06-04 21:55:20 +00008367 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008368 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008369 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008370 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008371
Bram Moolenaar51485f02005-06-04 21:55:20 +00008372 /* number of nodes in 4 bytes */
8373 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008374 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008375
Bram Moolenaar51485f02005-06-04 21:55:20 +00008376 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008377 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008378 }
8379
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008380 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008381 if (putc(0, fd) == EOF)
8382 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008383theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008384 if (fclose(fd) == EOF)
8385 retval = FAIL;
8386
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008387 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008388 retval = FAIL;
8389 if (retval == FAIL)
8390 EMSG(_(e_write));
8391
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008392 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008393}
8394
8395/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008396 * Clear the index and wnode fields of "node", it siblings and its
8397 * children. This is needed because they are a union with other items to save
8398 * space.
8399 */
8400 static void
8401clear_node(node)
8402 wordnode_T *node;
8403{
8404 wordnode_T *np;
8405
8406 if (node != NULL)
8407 for (np = node; np != NULL; np = np->wn_sibling)
8408 {
8409 np->wn_u1.index = 0;
8410 np->wn_u2.wnode = NULL;
8411
8412 if (np->wn_byte != NUL)
8413 clear_node(np->wn_child);
8414 }
8415}
8416
8417
8418/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008419 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008420 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008421 * This first writes the list of possible bytes (siblings). Then for each
8422 * byte recursively write the children.
8423 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008424 * NOTE: The code here must match the code in read_tree_node(), since
8425 * assumptions are made about the indexes (so that we don't have to write them
8426 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008427 *
8428 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008429 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008430 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008431put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008432 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008433 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008434 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008435 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008436 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008437{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008438 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008439 int siblingcount = 0;
8440 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008441 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008442
Bram Moolenaar51485f02005-06-04 21:55:20 +00008443 /* If "node" is zero the tree is empty. */
8444 if (node == NULL)
8445 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008446
Bram Moolenaar51485f02005-06-04 21:55:20 +00008447 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008448 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008449
8450 /* Count the number of siblings. */
8451 for (np = node; np != NULL; np = np->wn_sibling)
8452 ++siblingcount;
8453
8454 /* Write the sibling count. */
8455 if (fd != NULL)
8456 putc(siblingcount, fd); /* <siblingcount> */
8457
8458 /* Write each sibling byte and optionally extra info. */
8459 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008460 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008461 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008462 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008463 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008464 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008465 /* For a NUL byte (end of word) write the flags etc. */
8466 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008467 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008468 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008469 * associated condition nr (stored in wn_region). The
8470 * byte value is misused to store the "rare" and "not
8471 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008472 if (np->wn_flags == (short_u)PFX_FLAGS)
8473 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008474 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008475 {
8476 putc(BY_FLAGS, fd); /* <byte> */
8477 putc(np->wn_flags, fd); /* <pflags> */
8478 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008479 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008480 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008481 }
8482 else
8483 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008484 /* For word trees we write the flag/region items. */
8485 flags = np->wn_flags;
8486 if (regionmask != 0 && np->wn_region != regionmask)
8487 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008488 if (np->wn_affixID != 0)
8489 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008490 if (flags == 0)
8491 {
8492 /* word without flags or region */
8493 putc(BY_NOFLAGS, fd); /* <byte> */
8494 }
8495 else
8496 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008497 if (np->wn_flags >= 0x100)
8498 {
8499 putc(BY_FLAGS2, fd); /* <byte> */
8500 putc(flags, fd); /* <flags> */
8501 putc((unsigned)flags >> 8, fd); /* <flags2> */
8502 }
8503 else
8504 {
8505 putc(BY_FLAGS, fd); /* <byte> */
8506 putc(flags, fd); /* <flags> */
8507 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008508 if (flags & WF_REGION)
8509 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008510 if (flags & WF_AFX)
8511 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008512 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008513 }
8514 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008515 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008516 else
8517 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008518 if (np->wn_child->wn_u1.index != 0
8519 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008520 {
8521 /* The child is written elsewhere, write the reference. */
8522 if (fd != NULL)
8523 {
8524 putc(BY_INDEX, fd); /* <byte> */
8525 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008526 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008527 }
8528 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008529 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008530 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008531 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008532
Bram Moolenaar51485f02005-06-04 21:55:20 +00008533 if (fd != NULL)
8534 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8535 {
8536 EMSG(_(e_write));
8537 return 0;
8538 }
8539 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008540 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008541
8542 /* Space used in the array when reading: one for each sibling and one for
8543 * the count. */
8544 newindex += siblingcount + 1;
8545
8546 /* Recursively dump the children of each sibling. */
8547 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008548 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8549 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008550 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008551
8552 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008553}
8554
8555
8556/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008557 * ":mkspell [-ascii] outfile infile ..."
8558 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008559 */
8560 void
8561ex_mkspell(eap)
8562 exarg_T *eap;
8563{
8564 int fcount;
8565 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008566 char_u *arg = eap->arg;
8567 int ascii = FALSE;
8568
8569 if (STRNCMP(arg, "-ascii", 6) == 0)
8570 {
8571 ascii = TRUE;
8572 arg = skipwhite(arg + 6);
8573 }
8574
8575 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8576 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008578 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008579 FreeWild(fcount, fnames);
8580 }
8581}
8582
8583/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008584 * Create the .sug file.
8585 * Uses the soundfold info in "spin".
8586 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8587 */
8588 static void
8589spell_make_sugfile(spin, wfname)
8590 spellinfo_T *spin;
8591 char_u *wfname;
8592{
8593 char_u fname[MAXPATHL];
8594 int len;
8595 slang_T *slang;
8596 int free_slang = FALSE;
8597
8598 /*
8599 * Read back the .spl file that was written. This fills the required
8600 * info for soundfolding. This also uses less memory than the
8601 * pointer-linked version of the trie. And it avoids having two versions
8602 * of the code for the soundfolding stuff.
8603 * It might have been done already by spell_reload_one().
8604 */
8605 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8606 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8607 break;
8608 if (slang == NULL)
8609 {
8610 spell_message(spin, (char_u *)_("Reading back spell file..."));
8611 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8612 if (slang == NULL)
8613 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008614 free_slang = TRUE;
8615 }
8616
8617 /*
8618 * Clear the info in "spin" that is used.
8619 */
8620 spin->si_blocks = NULL;
8621 spin->si_blocks_cnt = 0;
8622 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8623 spin->si_free_count = 0;
8624 spin->si_first_free = NULL;
8625 spin->si_foldwcount = 0;
8626
8627 /*
8628 * Go through the trie of good words, soundfold each word and add it to
8629 * the soundfold trie.
8630 */
8631 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8632 if (sug_filltree(spin, slang) == FAIL)
8633 goto theend;
8634
8635 /*
8636 * Create the table which links each soundfold word with a list of the
8637 * good words it may come from. Creates buffer "spin->si_spellbuf".
8638 * This also removes the wordnr from the NUL byte entries to make
8639 * compression possible.
8640 */
8641 if (sug_maketable(spin) == FAIL)
8642 goto theend;
8643
8644 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8645 (long)spin->si_spellbuf->b_ml.ml_line_count);
8646
8647 /*
8648 * Compress the soundfold trie.
8649 */
8650 spell_message(spin, (char_u *)_(msg_compressing));
8651 wordtree_compress(spin, spin->si_foldroot);
8652
8653 /*
8654 * Write the .sug file.
8655 * Make the file name by changing ".spl" to ".sug".
8656 */
8657 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008658 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008659 fname[len - 2] = 'u';
8660 fname[len - 1] = 'g';
8661 sug_write(spin, fname);
8662
8663theend:
8664 if (free_slang)
8665 slang_free(slang);
8666 free_blocks(spin->si_blocks);
8667 close_spellbuf(spin->si_spellbuf);
8668}
8669
8670/*
8671 * Build the soundfold trie for language "slang".
8672 */
8673 static int
8674sug_filltree(spin, slang)
8675 spellinfo_T *spin;
8676 slang_T *slang;
8677{
8678 char_u *byts;
8679 idx_T *idxs;
8680 int depth;
8681 idx_T arridx[MAXWLEN];
8682 int curi[MAXWLEN];
8683 char_u tword[MAXWLEN];
8684 char_u tsalword[MAXWLEN];
8685 int c;
8686 idx_T n;
8687 unsigned words_done = 0;
8688 int wordcount[MAXWLEN];
8689
8690 /* We use si_foldroot for the souldfolded trie. */
8691 spin->si_foldroot = wordtree_alloc(spin);
8692 if (spin->si_foldroot == NULL)
8693 return FAIL;
8694
8695 /* let tree_add_word() know we're adding to the soundfolded tree */
8696 spin->si_sugtree = TRUE;
8697
8698 /*
8699 * Go through the whole case-folded tree, soundfold each word and put it
8700 * in the trie.
8701 */
8702 byts = slang->sl_fbyts;
8703 idxs = slang->sl_fidxs;
8704
8705 arridx[0] = 0;
8706 curi[0] = 1;
8707 wordcount[0] = 0;
8708
8709 depth = 0;
8710 while (depth >= 0 && !got_int)
8711 {
8712 if (curi[depth] > byts[arridx[depth]])
8713 {
8714 /* Done all bytes at this node, go up one level. */
8715 idxs[arridx[depth]] = wordcount[depth];
8716 if (depth > 0)
8717 wordcount[depth - 1] += wordcount[depth];
8718
8719 --depth;
8720 line_breakcheck();
8721 }
8722 else
8723 {
8724
8725 /* Do one more byte at this node. */
8726 n = arridx[depth] + curi[depth];
8727 ++curi[depth];
8728
8729 c = byts[n];
8730 if (c == 0)
8731 {
8732 /* Sound-fold the word. */
8733 tword[depth] = NUL;
8734 spell_soundfold(slang, tword, TRUE, tsalword);
8735
8736 /* We use the "flags" field for the MSB of the wordnr,
8737 * "region" for the LSB of the wordnr. */
8738 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8739 words_done >> 16, words_done & 0xffff,
8740 0) == FAIL)
8741 return FAIL;
8742
8743 ++words_done;
8744 ++wordcount[depth];
8745
8746 /* Reset the block count each time to avoid compression
8747 * kicking in. */
8748 spin->si_blocks_cnt = 0;
8749
8750 /* Skip over any other NUL bytes (same word with different
8751 * flags). */
8752 while (byts[n + 1] == 0)
8753 {
8754 ++n;
8755 ++curi[depth];
8756 }
8757 }
8758 else
8759 {
8760 /* Normal char, go one level deeper. */
8761 tword[depth++] = c;
8762 arridx[depth] = idxs[n];
8763 curi[depth] = 1;
8764 wordcount[depth] = 0;
8765 }
8766 }
8767 }
8768
8769 smsg((char_u *)_("Total number of words: %d"), words_done);
8770
8771 return OK;
8772}
8773
8774/*
8775 * Make the table that links each word in the soundfold trie to the words it
8776 * can be produced from.
8777 * This is not unlike lines in a file, thus use a memfile to be able to access
8778 * the table efficiently.
8779 * Returns FAIL when out of memory.
8780 */
8781 static int
8782sug_maketable(spin)
8783 spellinfo_T *spin;
8784{
8785 garray_T ga;
8786 int res = OK;
8787
8788 /* Allocate a buffer, open a memline for it and create the swap file
8789 * (uses a temp file, not a .swp file). */
8790 spin->si_spellbuf = open_spellbuf();
8791 if (spin->si_spellbuf == NULL)
8792 return FAIL;
8793
8794 /* Use a buffer to store the line info, avoids allocating many small
8795 * pieces of memory. */
8796 ga_init2(&ga, 1, 100);
8797
8798 /* recursively go through the tree */
8799 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8800 res = FAIL;
8801
8802 ga_clear(&ga);
8803 return res;
8804}
8805
8806/*
8807 * Fill the table for one node and its children.
8808 * Returns the wordnr at the start of the node.
8809 * Returns -1 when out of memory.
8810 */
8811 static int
8812sug_filltable(spin, node, startwordnr, gap)
8813 spellinfo_T *spin;
8814 wordnode_T *node;
8815 int startwordnr;
8816 garray_T *gap; /* place to store line of numbers */
8817{
8818 wordnode_T *p, *np;
8819 int wordnr = startwordnr;
8820 int nr;
8821 int prev_nr;
8822
8823 for (p = node; p != NULL; p = p->wn_sibling)
8824 {
8825 if (p->wn_byte == NUL)
8826 {
8827 gap->ga_len = 0;
8828 prev_nr = 0;
8829 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8830 {
8831 if (ga_grow(gap, 10) == FAIL)
8832 return -1;
8833
8834 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8835 /* Compute the offset from the previous nr and store the
8836 * offset in a way that it takes a minimum number of bytes.
8837 * It's a bit like utf-8, but without the need to mark
8838 * following bytes. */
8839 nr -= prev_nr;
8840 prev_nr += nr;
8841 gap->ga_len += offset2bytes(nr,
8842 (char_u *)gap->ga_data + gap->ga_len);
8843 }
8844
8845 /* add the NUL byte */
8846 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8847
8848 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8849 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8850 return -1;
8851 ++wordnr;
8852
8853 /* Remove extra NUL entries, we no longer need them. We don't
8854 * bother freeing the nodes, the won't be reused anyway. */
8855 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8856 p->wn_sibling = p->wn_sibling->wn_sibling;
8857
8858 /* Clear the flags on the remaining NUL node, so that compression
8859 * works a lot better. */
8860 p->wn_flags = 0;
8861 p->wn_region = 0;
8862 }
8863 else
8864 {
8865 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8866 if (wordnr == -1)
8867 return -1;
8868 }
8869 }
8870 return wordnr;
8871}
8872
8873/*
8874 * Convert an offset into a minimal number of bytes.
8875 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8876 * bytes.
8877 */
8878 static int
8879offset2bytes(nr, buf)
8880 int nr;
8881 char_u *buf;
8882{
8883 int rem;
8884 int b1, b2, b3, b4;
8885
8886 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8887 b1 = nr % 255 + 1;
8888 rem = nr / 255;
8889 b2 = rem % 255 + 1;
8890 rem = rem / 255;
8891 b3 = rem % 255 + 1;
8892 b4 = rem / 255 + 1;
8893
8894 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8895 {
8896 buf[0] = 0xe0 + b4;
8897 buf[1] = b3;
8898 buf[2] = b2;
8899 buf[3] = b1;
8900 return 4;
8901 }
8902 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8903 {
8904 buf[0] = 0xc0 + b3;
8905 buf[1] = b2;
8906 buf[2] = b1;
8907 return 3;
8908 }
8909 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8910 {
8911 buf[0] = 0x80 + b2;
8912 buf[1] = b1;
8913 return 2;
8914 }
8915 /* 1 byte */
8916 buf[0] = b1;
8917 return 1;
8918}
8919
8920/*
8921 * Opposite of offset2bytes().
8922 * "pp" points to the bytes and is advanced over it.
8923 * Returns the offset.
8924 */
8925 static int
8926bytes2offset(pp)
8927 char_u **pp;
8928{
8929 char_u *p = *pp;
8930 int nr;
8931 int c;
8932
8933 c = *p++;
8934 if ((c & 0x80) == 0x00) /* 1 byte */
8935 {
8936 nr = c - 1;
8937 }
8938 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8939 {
8940 nr = (c & 0x3f) - 1;
8941 nr = nr * 255 + (*p++ - 1);
8942 }
8943 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8944 {
8945 nr = (c & 0x1f) - 1;
8946 nr = nr * 255 + (*p++ - 1);
8947 nr = nr * 255 + (*p++ - 1);
8948 }
8949 else /* 4 bytes */
8950 {
8951 nr = (c & 0x0f) - 1;
8952 nr = nr * 255 + (*p++ - 1);
8953 nr = nr * 255 + (*p++ - 1);
8954 nr = nr * 255 + (*p++ - 1);
8955 }
8956
8957 *pp = p;
8958 return nr;
8959}
8960
8961/*
8962 * Write the .sug file in "fname".
8963 */
8964 static void
8965sug_write(spin, fname)
8966 spellinfo_T *spin;
8967 char_u *fname;
8968{
8969 FILE *fd;
8970 wordnode_T *tree;
8971 int nodecount;
8972 int wcount;
8973 char_u *line;
8974 linenr_T lnum;
8975 int len;
8976
8977 /* Create the file. Note that an existing file is silently overwritten! */
8978 fd = mch_fopen((char *)fname, "w");
8979 if (fd == NULL)
8980 {
8981 EMSG2(_(e_notopen), fname);
8982 return;
8983 }
8984
8985 vim_snprintf((char *)IObuff, IOSIZE,
8986 _("Writing suggestion file %s ..."), fname);
8987 spell_message(spin, IObuff);
8988
8989 /*
8990 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8991 */
8992 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8993 {
8994 EMSG(_(e_write));
8995 goto theend;
8996 }
8997 putc(VIMSUGVERSION, fd); /* <versionnr> */
8998
8999 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02009000 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009001
9002 /*
9003 * <SUGWORDTREE>
9004 */
9005 spin->si_memtot = 0;
9006 tree = spin->si_foldroot->wn_sibling;
9007
9008 /* Clear the index and wnode fields in the tree. */
9009 clear_node(tree);
9010
9011 /* Count the number of nodes. Needed to be able to allocate the
9012 * memory when reading the nodes. Also fills in index for shared
9013 * nodes. */
9014 nodecount = put_node(NULL, tree, 0, 0, FALSE);
9015
9016 /* number of nodes in 4 bytes */
9017 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9018 spin->si_memtot += nodecount + nodecount * sizeof(int);
9019
9020 /* Write the nodes. */
9021 (void)put_node(fd, tree, 0, 0, FALSE);
9022
9023 /*
9024 * <SUGTABLE>: <sugwcount> <sugline> ...
9025 */
9026 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9027 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9028
9029 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9030 {
9031 /* <sugline>: <sugnr> ... NUL */
9032 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009033 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009034 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9035 {
9036 EMSG(_(e_write));
9037 goto theend;
9038 }
9039 spin->si_memtot += len;
9040 }
9041
9042 /* Write another byte to check for errors. */
9043 if (putc(0, fd) == EOF)
9044 EMSG(_(e_write));
9045
9046 vim_snprintf((char *)IObuff, IOSIZE,
9047 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9048 spell_message(spin, IObuff);
9049
9050theend:
9051 /* close the file */
9052 fclose(fd);
9053}
9054
9055/*
9056 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9057 * list and only contains text lines. Can use a swapfile to reduce memory
9058 * use.
9059 * Most other fields are invalid! Esp. watch out for string options being
9060 * NULL and there is no undo info.
9061 * Returns NULL when out of memory.
9062 */
9063 static buf_T *
9064open_spellbuf()
9065{
9066 buf_T *buf;
9067
9068 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9069 if (buf != NULL)
9070 {
9071 buf->b_spell = TRUE;
9072 buf->b_p_swf = TRUE; /* may create a swap file */
9073 ml_open(buf);
9074 ml_open_file(buf); /* create swap file now */
9075 }
9076 return buf;
9077}
9078
9079/*
9080 * Close the buffer used for spell info.
9081 */
9082 static void
9083close_spellbuf(buf)
9084 buf_T *buf;
9085{
9086 if (buf != NULL)
9087 {
9088 ml_close(buf, TRUE);
9089 vim_free(buf);
9090 }
9091}
9092
9093
9094/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009095 * Create a Vim spell file from one or more word lists.
9096 * "fnames[0]" is the output file name.
9097 * "fnames[fcount - 1]" is the last input file name.
9098 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9099 * and ".spl" is appended to make the output file name.
9100 */
9101 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009102mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009103 int fcount;
9104 char_u **fnames;
9105 int ascii; /* -ascii argument given */
9106 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009107 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009108{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109 char_u fname[MAXPATHL];
9110 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009111 char_u **innames;
9112 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009113 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009114 int i;
9115 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009116 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009117 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009118 spellinfo_T spin;
9119
9120 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009122 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 spin.si_followup = TRUE;
9124 spin.si_rem_accents = TRUE;
9125 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009126 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9128 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009129 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009130 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009131 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009132 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009133
Bram Moolenaarb765d632005-06-07 21:00:02 +00009134 /* default: fnames[0] is output file, following are input files */
9135 innames = &fnames[1];
9136 incount = fcount - 1;
9137
9138 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009139 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009140 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009141 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9142 {
9143 /* For ":mkspell path/en.latin1.add" output file is
9144 * "path/en.latin1.add.spl". */
9145 innames = &fnames[0];
9146 incount = 1;
9147 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9148 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009149 else if (fcount == 1)
9150 {
9151 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9152 innames = &fnames[0];
9153 incount = 1;
Bram Moolenaar56f78042010-12-08 17:09:32 +01009154 vim_snprintf((char *)wfname, sizeof(wfname), SPL_FNAME_TMPL,
9155 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009156 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009157 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9158 {
9159 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009161 }
9162 else
9163 /* Name should be language, make the file name from it. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009164 vim_snprintf((char *)wfname, sizeof(wfname), SPL_FNAME_TMPL,
9165 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009166
9167 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009168 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169 spin.si_ascii = TRUE;
9170
9171 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009172 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009173 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009174 }
9175
Bram Moolenaarb765d632005-06-07 21:00:02 +00009176 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009177 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009178 else if (vim_strchr(gettail(wfname), '_') != NULL)
9179 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009180 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009181 EMSG(_("E754: Only up to 8 regions supported"));
9182 else
9183 {
9184 /* Check for overwriting before doing things that may take a lot of
9185 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009186 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009187 {
9188 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009189 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009190 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009191 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009192 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009193 EMSG2(_(e_isadir2), wfname);
9194 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009195 }
9196
9197 /*
9198 * Init the aff and dic pointers.
9199 * Get the region names if there are more than 2 arguments.
9200 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009201 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009202 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009203 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009204
Bram Moolenaar3982c542005-06-08 21:56:31 +00009205 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009206 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009207 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009208 if (STRLEN(gettail(innames[i])) < 5
9209 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009210 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009211 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9212 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009213 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009214 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9215 spin.si_region_name[i * 2 + 1] =
9216 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009217 }
9218 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009219 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009220
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009221 spin.si_foldroot = wordtree_alloc(&spin);
9222 spin.si_keeproot = wordtree_alloc(&spin);
9223 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009224 if (spin.si_foldroot == NULL
9225 || spin.si_keeproot == NULL
9226 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009227 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009228 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009229 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009230 }
9231
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009232 /* When not producing a .add.spl file clear the character table when
9233 * we encounter one in the .aff file. This means we dump the current
9234 * one in the .spl file if the .aff file doesn't define one. That's
9235 * better than guessing the contents, the table will match a
9236 * previously loaded spell file. */
9237 if (!spin.si_add)
9238 spin.si_clear_chartab = TRUE;
9239
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009240 /*
9241 * Read all the .aff and .dic files.
9242 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009243 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009244 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009245 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009246 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009247 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009248 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009249
Bram Moolenaarb765d632005-06-07 21:00:02 +00009250 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009251 if (mch_stat((char *)fname, &st) >= 0)
9252 {
9253 /* Read the .aff file. Will init "spin->si_conv" based on the
9254 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009255 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009256 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009257 error = TRUE;
9258 else
9259 {
9260 /* Read the .dic file and store the words in the trees. */
9261 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009262 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009263 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009264 error = TRUE;
9265 }
9266 }
9267 else
9268 {
9269 /* No .aff file, try reading the file as a word list. Store
9270 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009271 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009272 error = TRUE;
9273 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009274
Bram Moolenaarb765d632005-06-07 21:00:02 +00009275#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009276 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009277 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009278#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009279 }
9280
Bram Moolenaar78622822005-08-23 21:00:13 +00009281 if (spin.si_compflags != NULL && spin.si_nobreak)
9282 MSG(_("Warning: both compounding and NOBREAK specified"));
9283
Bram Moolenaar4770d092006-01-12 23:22:24 +00009284 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009285 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009286 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009287 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009288 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009289 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009290 wordtree_compress(&spin, spin.si_foldroot);
9291 wordtree_compress(&spin, spin.si_keeproot);
9292 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009293 }
9294
Bram Moolenaar4770d092006-01-12 23:22:24 +00009295 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009296 {
9297 /*
9298 * Write the info in the spell file.
9299 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009300 vim_snprintf((char *)IObuff, IOSIZE,
9301 _("Writing spell file %s ..."), wfname);
9302 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009303
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009304 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009305
Bram Moolenaar4770d092006-01-12 23:22:24 +00009306 spell_message(&spin, (char_u *)_("Done!"));
9307 vim_snprintf((char *)IObuff, IOSIZE,
9308 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9309 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009310
Bram Moolenaar4770d092006-01-12 23:22:24 +00009311 /*
9312 * If the file is loaded need to reload it.
9313 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009314 if (!error)
9315 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009316 }
9317
9318 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009320 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 ga_clear(&spin.si_sal);
9322 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009323 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009324 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009325 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009326
9327 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009328 for (i = 0; i < incount; ++i)
9329 if (afile[i] != NULL)
9330 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009331
9332 /* Free all the bits and pieces at once. */
9333 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009334
9335 /*
9336 * If there is soundfolding info and no NOSUGFILE item create the
9337 * .sug file with the soundfolded word trie.
9338 */
9339 if (spin.si_sugtime != 0 && !error && !got_int)
9340 spell_make_sugfile(&spin, wfname);
9341
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009342 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009343}
9344
Bram Moolenaar4770d092006-01-12 23:22:24 +00009345/*
9346 * Display a message for spell file processing when 'verbose' is set or using
9347 * ":mkspell". "str" can be IObuff.
9348 */
9349 static void
9350spell_message(spin, str)
9351 spellinfo_T *spin;
9352 char_u *str;
9353{
9354 if (spin->si_verbose || p_verbose > 2)
9355 {
9356 if (!spin->si_verbose)
9357 verbose_enter();
9358 MSG(str);
9359 out_flush();
9360 if (!spin->si_verbose)
9361 verbose_leave();
9362 }
9363}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009364
9365/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009366 * ":[count]spellgood {word}"
9367 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009368 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009369 */
9370 void
9371ex_spell(eap)
9372 exarg_T *eap;
9373{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009374 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009375 eap->forceit ? 0 : (int)eap->line2,
9376 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009377}
9378
9379/*
9380 * Add "word[len]" to 'spellfile' as a good or bad word.
9381 */
9382 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009383spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009384 char_u *word;
9385 int len;
9386 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009387 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009388 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009389 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009390{
Bram Moolenaara3917072006-09-14 08:48:14 +00009391 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009392 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009393 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009394 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009395 char_u fnamebuf[MAXPATHL];
9396 char_u line[MAXWLEN * 2];
9397 long fpos, fpos_next = 0;
9398 int i;
9399 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009400
Bram Moolenaar89d40322006-08-29 15:30:07 +00009401 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009402 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009403 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009404 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009405 int_wordlist = vim_tempname('s');
9406 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009407 return;
9408 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009409 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009410 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009411 else
9412 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009413 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009414 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009415 {
9416 init_spellfile();
9417 new_spf = TRUE;
9418 }
9419
Bram Moolenaar860cae12010-06-05 23:22:07 +02009420 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009421 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009422 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009423 return;
9424 }
9425
Bram Moolenaar860cae12010-06-05 23:22:07 +02009426 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009427 {
9428 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009429 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009430 break;
9431 if (*spf == NUL)
9432 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009433 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009434 return;
9435 }
9436 }
9437
Bram Moolenaarb765d632005-06-07 21:00:02 +00009438 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009439 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009440 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9441 buf = NULL;
9442 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009443 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009444 EMSG(_(e_bufloaded));
9445 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009446 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009447
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009448 fname = fnamebuf;
9449 }
9450
Bram Moolenaard0131a82006-03-04 21:46:13 +00009451 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009452 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009453 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009454 * since its flags sort before the one with WF_BANNED. */
9455 fd = mch_fopen((char *)fname, "r");
9456 if (fd != NULL)
9457 {
9458 while (!vim_fgets(line, MAXWLEN * 2, fd))
9459 {
9460 fpos = fpos_next;
9461 fpos_next = ftell(fd);
9462 if (STRNCMP(word, line, len) == 0
9463 && (line[len] == '/' || line[len] < ' '))
9464 {
9465 /* Found duplicate word. Remove it by writing a '#' at
9466 * the start of the line. Mixing reading and writing
9467 * doesn't work for all systems, close the file first. */
9468 fclose(fd);
9469 fd = mch_fopen((char *)fname, "r+");
9470 if (fd == NULL)
9471 break;
9472 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009473 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009474 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009475 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009476 {
9477 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009478 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009479 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009480 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009481 fseek(fd, fpos_next, SEEK_SET);
9482 }
9483 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009484 if (fd != NULL)
9485 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009486 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009487 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009488
9489 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009490 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009491 fd = mch_fopen((char *)fname, "a");
9492 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009493 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009494 char_u *p;
9495
Bram Moolenaard0131a82006-03-04 21:46:13 +00009496 /* We just initialized the 'spellfile' option and can't open the
9497 * file. We may need to create the "spell" directory first. We
9498 * already checked the runtime directory is writable in
9499 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009500 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009501 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009502 int c = *p;
9503
Bram Moolenaard0131a82006-03-04 21:46:13 +00009504 /* The directory doesn't exist. Try creating it and opening
9505 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009506 *p = NUL;
9507 vim_mkdir(fname, 0755);
9508 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009509 fd = mch_fopen((char *)fname, "a");
9510 }
9511 }
9512
9513 if (fd == NULL)
9514 EMSG2(_(e_notopen), fname);
9515 else
9516 {
9517 if (bad)
9518 fprintf(fd, "%.*s/!\n", len, word);
9519 else
9520 fprintf(fd, "%.*s\n", len, word);
9521 fclose(fd);
9522
9523 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9524 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009525 }
9526 }
9527
Bram Moolenaard0131a82006-03-04 21:46:13 +00009528 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009529 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009530 /* Update the .add.spl file. */
9531 mkspell(1, &fname, FALSE, TRUE, TRUE);
9532
9533 /* If the .add file is edited somewhere, reload it. */
9534 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009535 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009536
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009537 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009538 }
9539}
9540
9541/*
9542 * Initialize 'spellfile' for the current buffer.
9543 */
9544 static void
9545init_spellfile()
9546{
9547 char_u buf[MAXPATHL];
9548 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009549 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009550 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009551 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009552 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009553 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009554
Bram Moolenaar860cae12010-06-05 23:22:07 +02009555 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009556 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009557 /* Find the end of the language name. Exclude the region. If there
9558 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009559 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009560 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009561 if (vim_ispathsep(*lend))
9562 {
9563 aspath = TRUE;
9564 lstart = lend + 1;
9565 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009566
9567 /* Loop over all entries in 'runtimepath'. Use the first one where we
9568 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009569 rtp = p_rtp;
9570 while (*rtp != NUL)
9571 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009572 if (aspath)
9573 /* Use directory of an entry with path, e.g., for
9574 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009575 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9576 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009577 else
9578 /* Copy the path from 'runtimepath' to buf[]. */
9579 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009580 if (filewritable(buf) == 2)
9581 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009582 /* Use the first language name from 'spelllang' and the
9583 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009584 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009585 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9586 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009587 else
9588 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009589 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009590 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009591 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009592 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009593 vim_mkdir(buf, 0755);
9594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009595 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009596 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009597 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009598 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009599 l = (int)STRLEN(buf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009600 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009601 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9602 fname != NULL
9603 && strstr((char *)gettail(fname), ".ascii.") != NULL
9604 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009605 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9606 break;
9607 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009608 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009609 }
9610 }
9611}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009612
Bram Moolenaar51485f02005-06-04 21:55:20 +00009613
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009614/*
9615 * Init the chartab used for spelling for ASCII.
9616 * EBCDIC is not supported!
9617 */
9618 static void
9619clear_spell_chartab(sp)
9620 spelltab_T *sp;
9621{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009622 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009623
9624 /* Init everything to FALSE. */
9625 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9626 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9627 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009628 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009629 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009630 sp->st_upper[i] = i;
9631 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009632
9633 /* We include digits. A word shouldn't start with a digit, but handling
9634 * that is done separately. */
9635 for (i = '0'; i <= '9'; ++i)
9636 sp->st_isw[i] = TRUE;
9637 for (i = 'A'; i <= 'Z'; ++i)
9638 {
9639 sp->st_isw[i] = TRUE;
9640 sp->st_isu[i] = TRUE;
9641 sp->st_fold[i] = i + 0x20;
9642 }
9643 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009644 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009645 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009646 sp->st_upper[i] = i - 0x20;
9647 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009648}
9649
9650/*
9651 * Init the chartab used for spelling. Only depends on 'encoding'.
9652 * Called once while starting up and when 'encoding' changes.
9653 * The default is to use isalpha(), but the spell file should define the word
9654 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009655 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009656 */
9657 void
9658init_spell_chartab()
9659{
9660 int i;
9661
9662 did_set_spelltab = FALSE;
9663 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009664#ifdef FEAT_MBYTE
9665 if (enc_dbcs)
9666 {
9667 /* DBCS: assume double-wide characters are word characters. */
9668 for (i = 128; i <= 255; ++i)
9669 if (MB_BYTE2LEN(i) == 2)
9670 spelltab.st_isw[i] = TRUE;
9671 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009672 else if (enc_utf8)
9673 {
9674 for (i = 128; i < 256; ++i)
9675 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009676 int f = utf_fold(i);
9677 int u = utf_toupper(i);
9678
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009679 spelltab.st_isu[i] = utf_isupper(i);
9680 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009681 /* The folded/upper-cased value is different between latin1 and
9682 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9683 * value for utf-8 to avoid this. */
9684 spelltab.st_fold[i] = (f < 256) ? f : i;
9685 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009686 }
9687 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009688 else
9689#endif
9690 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009691 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009692 for (i = 128; i < 256; ++i)
9693 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009694 if (MB_ISUPPER(i))
9695 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009696 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009697 spelltab.st_isu[i] = TRUE;
9698 spelltab.st_fold[i] = MB_TOLOWER(i);
9699 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009700 else if (MB_ISLOWER(i))
9701 {
9702 spelltab.st_isw[i] = TRUE;
9703 spelltab.st_upper[i] = MB_TOUPPER(i);
9704 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009705 }
9706 }
9707}
9708
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009709/*
9710 * Set the spell character tables from strings in the affix file.
9711 */
9712 static int
9713set_spell_chartab(fol, low, upp)
9714 char_u *fol;
9715 char_u *low;
9716 char_u *upp;
9717{
9718 /* We build the new tables here first, so that we can compare with the
9719 * previous one. */
9720 spelltab_T new_st;
9721 char_u *pf = fol, *pl = low, *pu = upp;
9722 int f, l, u;
9723
9724 clear_spell_chartab(&new_st);
9725
9726 while (*pf != NUL)
9727 {
9728 if (*pl == NUL || *pu == NUL)
9729 {
9730 EMSG(_(e_affform));
9731 return FAIL;
9732 }
9733#ifdef FEAT_MBYTE
9734 f = mb_ptr2char_adv(&pf);
9735 l = mb_ptr2char_adv(&pl);
9736 u = mb_ptr2char_adv(&pu);
9737#else
9738 f = *pf++;
9739 l = *pl++;
9740 u = *pu++;
9741#endif
9742 /* Every character that appears is a word character. */
9743 if (f < 256)
9744 new_st.st_isw[f] = TRUE;
9745 if (l < 256)
9746 new_st.st_isw[l] = TRUE;
9747 if (u < 256)
9748 new_st.st_isw[u] = TRUE;
9749
9750 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9751 * case-folding */
9752 if (l < 256 && l != f)
9753 {
9754 if (f >= 256)
9755 {
9756 EMSG(_(e_affrange));
9757 return FAIL;
9758 }
9759 new_st.st_fold[l] = f;
9760 }
9761
9762 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009763 * case-folding, it's upper case and the "UPP" is the upper case of
9764 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009765 if (u < 256 && u != f)
9766 {
9767 if (f >= 256)
9768 {
9769 EMSG(_(e_affrange));
9770 return FAIL;
9771 }
9772 new_st.st_fold[u] = f;
9773 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009774 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009775 }
9776 }
9777
9778 if (*pl != NUL || *pu != NUL)
9779 {
9780 EMSG(_(e_affform));
9781 return FAIL;
9782 }
9783
9784 return set_spell_finish(&new_st);
9785}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009786
9787/*
9788 * Set the spell character tables from strings in the .spl file.
9789 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009790 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009791set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009792 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009793 int cnt; /* length of "flags" */
9794 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009795{
9796 /* We build the new tables here first, so that we can compare with the
9797 * previous one. */
9798 spelltab_T new_st;
9799 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009800 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009801 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009802
9803 clear_spell_chartab(&new_st);
9804
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009805 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009806 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009807 if (i < cnt)
9808 {
9809 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9810 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9811 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009812
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009813 if (*p != NUL)
9814 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009815#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009816 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009817#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009818 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009819#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009820 new_st.st_fold[i + 128] = c;
9821 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9822 new_st.st_upper[c] = i + 128;
9823 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009824 }
9825
Bram Moolenaar5195e452005-08-19 20:32:47 +00009826 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009827}
9828
9829 static int
9830set_spell_finish(new_st)
9831 spelltab_T *new_st;
9832{
9833 int i;
9834
9835 if (did_set_spelltab)
9836 {
9837 /* check that it's the same table */
9838 for (i = 0; i < 256; ++i)
9839 {
9840 if (spelltab.st_isw[i] != new_st->st_isw[i]
9841 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009842 || spelltab.st_fold[i] != new_st->st_fold[i]
9843 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009844 {
9845 EMSG(_("E763: Word characters differ between spell files"));
9846 return FAIL;
9847 }
9848 }
9849 }
9850 else
9851 {
9852 /* copy the new spelltab into the one being used */
9853 spelltab = *new_st;
9854 did_set_spelltab = TRUE;
9855 }
9856
9857 return OK;
9858}
9859
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009860/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009861 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009862 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009863 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009864 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009865 */
9866 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009867spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009868 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009869 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009870{
Bram Moolenaarea408852005-06-25 22:49:46 +00009871#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009872 char_u *s;
9873 int l;
9874 int c;
9875
9876 if (has_mbyte)
9877 {
9878 l = MB_BYTE2LEN(*p);
9879 s = p;
9880 if (l == 1)
9881 {
9882 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009883 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009884 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009885 }
9886 else
9887 {
9888 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009889 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9890 : (wp->w_s->b_spell_ismw_mb != NULL
9891 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009892 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009893 }
9894
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009895 c = mb_ptr2char(s);
9896 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009897 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009898 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009899 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009900#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009901
Bram Moolenaar860cae12010-06-05 23:22:07 +02009902 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009903}
9904
9905/*
9906 * Return TRUE if "p" points to a word character.
9907 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9908 */
9909 static int
9910spell_iswordp_nmw(p)
9911 char_u *p;
9912{
9913#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009914 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009915
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009916 if (has_mbyte)
9917 {
9918 c = mb_ptr2char(p);
9919 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009920 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009921 return spelltab.st_isw[c];
9922 }
9923#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009924 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009925}
9926
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009927#ifdef FEAT_MBYTE
9928/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009929 * Return TRUE if word class indicates a word character.
9930 * Only for characters above 255.
9931 * Unicode subscript and superscript are not considered word characters.
9932 */
9933 static int
9934spell_mb_isword_class(cl)
9935 int cl;
9936{
9937 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9938}
9939
9940/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009941 * Return TRUE if "p" points to a word character.
9942 * Wide version of spell_iswordp().
9943 */
9944 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009945spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009946 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009947 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009948{
9949 int *s;
9950
Bram Moolenaar860cae12010-06-05 23:22:07 +02009951 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9952 : (wp->w_s->b_spell_ismw_mb != NULL
9953 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009954 s = p + 1;
9955 else
9956 s = p;
9957
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009958 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009959 {
9960 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009961 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009962 if (enc_dbcs)
9963 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9964 return 0;
9965 }
9966 return spelltab.st_isw[*s];
9967}
9968#endif
9969
Bram Moolenaarea408852005-06-25 22:49:46 +00009970/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009971 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009972 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009973 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009974 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009975write_spell_prefcond(fd, gap)
9976 FILE *fd;
9977 garray_T *gap;
9978{
9979 int i;
9980 char_u *p;
9981 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009982 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009983 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009984
Bram Moolenaar5195e452005-08-19 20:32:47 +00009985 if (fd != NULL)
9986 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9987
9988 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009989
9990 for (i = 0; i < gap->ga_len; ++i)
9991 {
9992 /* <prefcond> : <condlen> <condstr> */
9993 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009994 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009995 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009996 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009997 if (fd != NULL)
9998 {
9999 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +000010000 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010001 }
10002 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +000010003 }
Bram Moolenaar5195e452005-08-19 20:32:47 +000010004 else if (fd != NULL)
10005 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010006 }
10007
Bram Moolenaar5195e452005-08-19 20:32:47 +000010008 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010009}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010010
10011/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010012 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
10013 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010014 * When using a multi-byte 'encoding' the length may change!
10015 * Returns FAIL when something wrong.
10016 */
10017 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010018spell_casefold(str, len, buf, buflen)
10019 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010020 int len;
10021 char_u *buf;
10022 int buflen;
10023{
10024 int i;
10025
10026 if (len >= buflen)
10027 {
10028 buf[0] = NUL;
10029 return FAIL; /* result will not fit */
10030 }
10031
10032#ifdef FEAT_MBYTE
10033 if (has_mbyte)
10034 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010035 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010036 char_u *p;
10037 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010038
10039 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010040 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010041 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010042 if (outi + MB_MAXBYTES > buflen)
10043 {
10044 buf[outi] = NUL;
10045 return FAIL;
10046 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010047 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010048 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010049 }
10050 buf[outi] = NUL;
10051 }
10052 else
10053#endif
10054 {
10055 /* Be quick for non-multibyte encodings. */
10056 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010057 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010058 buf[i] = NUL;
10059 }
10060
10061 return OK;
10062}
10063
Bram Moolenaar4770d092006-01-12 23:22:24 +000010064/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010065#define SPS_BEST 1
10066#define SPS_FAST 2
10067#define SPS_DOUBLE 4
10068
Bram Moolenaar4770d092006-01-12 23:22:24 +000010069static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10070static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010071
10072/*
10073 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010074 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010075 */
10076 int
10077spell_check_sps()
10078{
10079 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010080 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010081 char_u buf[MAXPATHL];
10082 int f;
10083
10084 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010085 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010086
10087 for (p = p_sps; *p != NUL; )
10088 {
10089 copy_option_part(&p, buf, MAXPATHL, ",");
10090
10091 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010092 if (VIM_ISDIGIT(*buf))
10093 {
10094 s = buf;
10095 sps_limit = getdigits(&s);
10096 if (*s != NUL && !VIM_ISDIGIT(*s))
10097 f = -1;
10098 }
10099 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010100 f = SPS_BEST;
10101 else if (STRCMP(buf, "fast") == 0)
10102 f = SPS_FAST;
10103 else if (STRCMP(buf, "double") == 0)
10104 f = SPS_DOUBLE;
10105 else if (STRNCMP(buf, "expr:", 5) != 0
10106 && STRNCMP(buf, "file:", 5) != 0)
10107 f = -1;
10108
10109 if (f == -1 || (sps_flags != 0 && f != 0))
10110 {
10111 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010112 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010113 return FAIL;
10114 }
10115 if (f != 0)
10116 sps_flags = f;
10117 }
10118
10119 if (sps_flags == 0)
10120 sps_flags = SPS_BEST;
10121
10122 return OK;
10123}
10124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125/*
10126 * "z?": Find badly spelled word under or after the cursor.
10127 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010128 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010129 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 */
10131 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010132spell_suggest(count)
10133 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134{
10135 char_u *line;
10136 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 char_u wcopy[MAXWLEN + 2];
10138 char_u *p;
10139 int i;
10140 int c;
10141 suginfo_T sug;
10142 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010143 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010144 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010145 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010146 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010147 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010148 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010149
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010150 if (no_spell_checking(curwin))
10151 return;
10152
10153#ifdef FEAT_VISUAL
10154 if (VIsual_active)
10155 {
10156 /* Use the Visually selected text as the bad word. But reject
10157 * a multi-line selection. */
10158 if (curwin->w_cursor.lnum != VIsual.lnum)
10159 {
10160 vim_beep();
10161 return;
10162 }
10163 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10164 if (badlen < 0)
10165 badlen = -badlen;
10166 else
10167 curwin->w_cursor.col = VIsual.col;
10168 ++badlen;
10169 end_visual_mode();
10170 }
10171 else
10172#endif
10173 /* Find the start of the badly spelled word. */
10174 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010175 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010177 /* No bad word or it starts after the cursor: use the word under the
10178 * cursor. */
10179 curwin->w_cursor = prev_cursor;
10180 line = ml_get_curline();
10181 p = line + curwin->w_cursor.col;
10182 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010183 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010184 mb_ptr_back(line, p);
10185 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010186 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010187 mb_ptr_adv(p);
10188
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010189 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010190 {
10191 beep_flush();
10192 return;
10193 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010194 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 }
10196
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010197 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010199 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010200 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010201
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010202 /* Make a copy of current line since autocommands may free the line. */
10203 line = vim_strsave(ml_get_curline());
10204 if (line == NULL)
10205 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010206
Bram Moolenaar5195e452005-08-19 20:32:47 +000010207 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10208 * 'spellsuggest', whatever is smaller. */
10209 if (sps_limit > (int)Rows - 2)
10210 limit = (int)Rows - 2;
10211 else
10212 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010213 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010214 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215
10216 if (sug.su_ga.ga_len == 0)
10217 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010218 else if (count > 0)
10219 {
10220 if (count > sug.su_ga.ga_len)
10221 smsg((char_u *)_("Sorry, only %ld suggestions"),
10222 (long)sug.su_ga.ga_len);
10223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 else
10225 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010226 vim_free(repl_from);
10227 repl_from = NULL;
10228 vim_free(repl_to);
10229 repl_to = NULL;
10230
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010231#ifdef FEAT_RIGHTLEFT
10232 /* When 'rightleft' is set the list is drawn right-left. */
10233 cmdmsg_rl = curwin->w_p_rl;
10234 if (cmdmsg_rl)
10235 msg_col = Columns - 1;
10236#endif
10237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 /* List the suggestions. */
10239 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010240 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010241 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10243 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010244#ifdef FEAT_RIGHTLEFT
10245 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10246 {
10247 /* And now the rabbit from the high hat: Avoid showing the
10248 * untranslated message rightleft. */
10249 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10250 sug.su_badlen, sug.su_badptr);
10251 }
10252#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 msg_puts(IObuff);
10254 msg_clr_eos();
10255 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 msg_scroll = TRUE;
10258 for (i = 0; i < sug.su_ga.ga_len; ++i)
10259 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010260 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261
10262 /* The suggested word may replace only part of the bad word, add
10263 * the not replaced part. */
10264 STRCPY(wcopy, stp->st_word);
10265 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010266 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 sug.su_badptr + stp->st_orglen,
10268 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010269 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10270#ifdef FEAT_RIGHTLEFT
10271 if (cmdmsg_rl)
10272 rl_mirror(IObuff);
10273#endif
10274 msg_puts(IObuff);
10275
10276 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010277 msg_puts(IObuff);
10278
10279 /* The word may replace more than "su_badlen". */
10280 if (sug.su_badlen < stp->st_orglen)
10281 {
10282 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10283 stp->st_orglen, sug.su_badptr);
10284 msg_puts(IObuff);
10285 }
10286
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010287 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010288 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010289 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010290 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010291 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010292 stp->st_salscore ? "s " : "",
10293 stp->st_score, stp->st_altscore);
10294 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010295 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010296 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010297#ifdef FEAT_RIGHTLEFT
10298 if (cmdmsg_rl)
10299 /* Mirror the numbers, but keep the leading space. */
10300 rl_mirror(IObuff + 1);
10301#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010302 msg_advance(30);
10303 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 msg_putchar('\n');
10306 }
10307
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010308#ifdef FEAT_RIGHTLEFT
10309 cmdmsg_rl = FALSE;
10310 msg_col = 0;
10311#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010313 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010314 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010315 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010316 lines_left = Rows; /* avoid more prompt */
10317 /* don't delay for 'smd' in normal_cmd() */
10318 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010319 }
10320
Bram Moolenaard12a1322005-08-21 22:08:24 +000010321 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10322 {
10323 /* Save the from and to text for :spellrepall. */
10324 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010325 if (sug.su_badlen > stp->st_orglen)
10326 {
10327 /* Replacing less than "su_badlen", append the remainder to
10328 * repl_to. */
10329 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10330 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10331 sug.su_badlen - stp->st_orglen,
10332 sug.su_badptr + stp->st_orglen);
10333 repl_to = vim_strsave(IObuff);
10334 }
10335 else
10336 {
10337 /* Replacing su_badlen or more, use the whole word. */
10338 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10339 repl_to = vim_strsave(stp->st_word);
10340 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010341
10342 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010343 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10344 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010345 if (p != NULL)
10346 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010347 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010348 mch_memmove(p, line, c);
10349 STRCPY(p + c, stp->st_word);
10350 STRCAT(p, sug.su_badptr + stp->st_orglen);
10351 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10352 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010353
10354 /* For redo we use a change-word command. */
10355 ResetRedobuff();
10356 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010357 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010358 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010359 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010360
10361 /* After this "p" may be invalid. */
10362 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010363 }
10364 }
10365 else
10366 curwin->w_cursor = prev_cursor;
10367
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010368 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010369skip:
10370 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010371}
10372
10373/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010374 * Check if the word at line "lnum" column "col" is required to start with a
10375 * capital. This uses 'spellcapcheck' of the current buffer.
10376 */
10377 static int
10378check_need_cap(lnum, col)
10379 linenr_T lnum;
10380 colnr_T col;
10381{
10382 int need_cap = FALSE;
10383 char_u *line;
10384 char_u *line_copy = NULL;
10385 char_u *p;
10386 colnr_T endcol;
10387 regmatch_T regmatch;
10388
Bram Moolenaar860cae12010-06-05 23:22:07 +020010389 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010390 return FALSE;
10391
10392 line = ml_get_curline();
10393 endcol = 0;
10394 if ((int)(skipwhite(line) - line) >= (int)col)
10395 {
10396 /* At start of line, check if previous line is empty or sentence
10397 * ends there. */
10398 if (lnum == 1)
10399 need_cap = TRUE;
10400 else
10401 {
10402 line = ml_get(lnum - 1);
10403 if (*skipwhite(line) == NUL)
10404 need_cap = TRUE;
10405 else
10406 {
10407 /* Append a space in place of the line break. */
10408 line_copy = concat_str(line, (char_u *)" ");
10409 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010410 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010411 }
10412 }
10413 }
10414 else
10415 endcol = col;
10416
10417 if (endcol > 0)
10418 {
10419 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010420 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010421 regmatch.rm_ic = FALSE;
10422 p = line + endcol;
10423 for (;;)
10424 {
10425 mb_ptr_back(line, p);
10426 if (p == line || spell_iswordp_nmw(p))
10427 break;
10428 if (vim_regexec(&regmatch, p, 0)
10429 && regmatch.endp[0] == line + endcol)
10430 {
10431 need_cap = TRUE;
10432 break;
10433 }
10434 }
10435 }
10436
10437 vim_free(line_copy);
10438
10439 return need_cap;
10440}
10441
10442
10443/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010444 * ":spellrepall"
10445 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010446 void
10447ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010448 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010449{
10450 pos_T pos = curwin->w_cursor;
10451 char_u *frompat;
10452 int addlen;
10453 char_u *line;
10454 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010455 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010456 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010457
10458 if (repl_from == NULL || repl_to == NULL)
10459 {
10460 EMSG(_("E752: No previous spell replacement"));
10461 return;
10462 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010463 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010464
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010465 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010466 if (frompat == NULL)
10467 return;
10468 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10469 p_ws = FALSE;
10470
Bram Moolenaar5195e452005-08-19 20:32:47 +000010471 sub_nsubs = 0;
10472 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010473 curwin->w_cursor.lnum = 0;
10474 while (!got_int)
10475 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010476 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010477 || u_save_cursor() == FAIL)
10478 break;
10479
10480 /* Only replace when the right word isn't there yet. This happens
10481 * when changing "etc" to "etc.". */
10482 line = ml_get_curline();
10483 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10484 repl_to, STRLEN(repl_to)) != 0)
10485 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010486 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010487 if (p == NULL)
10488 break;
10489 mch_memmove(p, line, curwin->w_cursor.col);
10490 STRCPY(p + curwin->w_cursor.col, repl_to);
10491 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10492 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10493 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010494
10495 if (curwin->w_cursor.lnum != prev_lnum)
10496 {
10497 ++sub_nlines;
10498 prev_lnum = curwin->w_cursor.lnum;
10499 }
10500 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010501 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010502 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010503 }
10504
10505 p_ws = save_ws;
10506 curwin->w_cursor = pos;
10507 vim_free(frompat);
10508
Bram Moolenaar5195e452005-08-19 20:32:47 +000010509 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010510 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010511 else
10512 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010513}
10514
10515/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010516 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10517 * a list of allocated strings.
10518 */
10519 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010520spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010521 garray_T *gap;
10522 char_u *word;
10523 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010524 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010525 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010526{
10527 suginfo_T sug;
10528 int i;
10529 suggest_T *stp;
10530 char_u *wcopy;
10531
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010532 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010533
10534 /* Make room in "gap". */
10535 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010536 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010537 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010538 for (i = 0; i < sug.su_ga.ga_len; ++i)
10539 {
10540 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010541
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010542 /* The suggested word may replace only part of "word", add the not
10543 * replaced part. */
10544 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010545 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010546 if (wcopy == NULL)
10547 break;
10548 STRCPY(wcopy, stp->st_word);
10549 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10550 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10551 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010552 }
10553
10554 spell_find_cleanup(&sug);
10555}
10556
10557/*
10558 * Find spell suggestions for the word at the start of "badptr".
10559 * Return the suggestions in "su->su_ga".
10560 * The maximum number of suggestions is "maxcount".
10561 * Note: does use info for the current window.
10562 * This is based on the mechanisms of Aspell, but completely reimplemented.
10563 */
10564 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010565spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010566 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010567 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010568 suginfo_T *su;
10569 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010570 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010571 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010572 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010573{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010574 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010575 char_u buf[MAXPATHL];
10576 char_u *p;
10577 int do_combine = FALSE;
10578 char_u *sps_copy;
10579#ifdef FEAT_EVAL
10580 static int expr_busy = FALSE;
10581#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010582 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010583 int i;
10584 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010585
10586 /*
10587 * Set the info in "*su".
10588 */
10589 vim_memset(su, 0, sizeof(suginfo_T));
10590 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10591 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010592 if (*badptr == NUL)
10593 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010594 hash_init(&su->su_banned);
10595
10596 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010597 if (badlen != 0)
10598 su->su_badlen = badlen;
10599 else
10600 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010601 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010602 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010603
10604 if (su->su_badlen >= MAXWLEN)
10605 su->su_badlen = MAXWLEN - 1; /* just in case */
10606 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10607 (void)spell_casefold(su->su_badptr, su->su_badlen,
10608 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010609 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010610 su->su_badflags = badword_captype(su->su_badptr,
10611 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010612 if (need_cap)
10613 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010614
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010615 /* Find the default language for sound folding. We simply use the first
10616 * one in 'spelllang' that supports sound folding. That's good for when
10617 * using multiple files for one language, it's not that bad when mixing
10618 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010619 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010620 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010621 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010622 if (lp->lp_sallang != NULL)
10623 {
10624 su->su_sallang = lp->lp_sallang;
10625 break;
10626 }
10627 }
10628
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010629 /* Soundfold the bad word with the default sound folding, so that we don't
10630 * have to do this many times. */
10631 if (su->su_sallang != NULL)
10632 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10633 su->su_sal_badword);
10634
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010635 /* If the word is not capitalised and spell_check() doesn't consider the
10636 * word to be bad then it might need to be capitalised. Add a suggestion
10637 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010638 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010639 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010640 {
10641 make_case_word(su->su_badword, buf, WF_ONECAP);
10642 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010643 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010644 }
10645
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010646 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010647 if (banbadword)
10648 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010649
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010650 /* Make a copy of 'spellsuggest', because the expression may change it. */
10651 sps_copy = vim_strsave(p_sps);
10652 if (sps_copy == NULL)
10653 return;
10654
10655 /* Loop over the items in 'spellsuggest'. */
10656 for (p = sps_copy; *p != NUL; )
10657 {
10658 copy_option_part(&p, buf, MAXPATHL, ",");
10659
10660 if (STRNCMP(buf, "expr:", 5) == 0)
10661 {
10662#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010663 /* Evaluate an expression. Skip this when called recursively,
10664 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010665 if (!expr_busy)
10666 {
10667 expr_busy = TRUE;
10668 spell_suggest_expr(su, buf + 5);
10669 expr_busy = FALSE;
10670 }
10671#endif
10672 }
10673 else if (STRNCMP(buf, "file:", 5) == 0)
10674 /* Use list of suggestions in a file. */
10675 spell_suggest_file(su, buf + 5);
10676 else
10677 {
10678 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010679 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010680 if (sps_flags & SPS_DOUBLE)
10681 do_combine = TRUE;
10682 }
10683 }
10684
10685 vim_free(sps_copy);
10686
10687 if (do_combine)
10688 /* Combine the two list of suggestions. This must be done last,
10689 * because sorting changes the order again. */
10690 score_combine(su);
10691}
10692
10693#ifdef FEAT_EVAL
10694/*
10695 * Find suggestions by evaluating expression "expr".
10696 */
10697 static void
10698spell_suggest_expr(su, expr)
10699 suginfo_T *su;
10700 char_u *expr;
10701{
10702 list_T *list;
10703 listitem_T *li;
10704 int score;
10705 char_u *p;
10706
10707 /* The work is split up in a few parts to avoid having to export
10708 * suginfo_T.
10709 * First evaluate the expression and get the resulting list. */
10710 list = eval_spell_expr(su->su_badword, expr);
10711 if (list != NULL)
10712 {
10713 /* Loop over the items in the list. */
10714 for (li = list->lv_first; li != NULL; li = li->li_next)
10715 if (li->li_tv.v_type == VAR_LIST)
10716 {
10717 /* Get the word and the score from the items. */
10718 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010719 if (score >= 0 && score <= su->su_maxscore)
10720 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10721 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010722 }
10723 list_unref(list);
10724 }
10725
Bram Moolenaar4770d092006-01-12 23:22:24 +000010726 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10727 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010728 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10729}
10730#endif
10731
10732/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010733 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010734 */
10735 static void
10736spell_suggest_file(su, fname)
10737 suginfo_T *su;
10738 char_u *fname;
10739{
10740 FILE *fd;
10741 char_u line[MAXWLEN * 2];
10742 char_u *p;
10743 int len;
10744 char_u cword[MAXWLEN];
10745
10746 /* Open the file. */
10747 fd = mch_fopen((char *)fname, "r");
10748 if (fd == NULL)
10749 {
10750 EMSG2(_(e_notopen), fname);
10751 return;
10752 }
10753
10754 /* Read it line by line. */
10755 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10756 {
10757 line_breakcheck();
10758
10759 p = vim_strchr(line, '/');
10760 if (p == NULL)
10761 continue; /* No Tab found, just skip the line. */
10762 *p++ = NUL;
10763 if (STRICMP(su->su_badword, line) == 0)
10764 {
10765 /* Match! Isolate the good word, until CR or NL. */
10766 for (len = 0; p[len] >= ' '; ++len)
10767 ;
10768 p[len] = NUL;
10769
10770 /* If the suggestion doesn't have specific case duplicate the case
10771 * of the bad word. */
10772 if (captype(p, NULL) == 0)
10773 {
10774 make_case_word(p, cword, su->su_badflags);
10775 p = cword;
10776 }
10777
10778 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010779 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010780 }
10781 }
10782
10783 fclose(fd);
10784
Bram Moolenaar4770d092006-01-12 23:22:24 +000010785 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10786 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010787 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10788}
10789
10790/*
10791 * Find suggestions for the internal method indicated by "sps_flags".
10792 */
10793 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010794spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010795 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010796 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010797{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010798 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010799 * Load the .sug file(s) that are available and not done yet.
10800 */
10801 suggest_load_files();
10802
10803 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010804 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010805 *
10806 * Set a maximum score to limit the combination of operations that is
10807 * tried.
10808 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010809 suggest_try_special(su);
10810
10811 /*
10812 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10813 * from the .aff file and inserting a space (split the word).
10814 */
10815 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010816
10817 /* For the resulting top-scorers compute the sound-a-like score. */
10818 if (sps_flags & SPS_DOUBLE)
10819 score_comp_sal(su);
10820
10821 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010822 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010823 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010824 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010825 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010826 if (sps_flags & SPS_BEST)
10827 /* Adjust the word score for the suggestions found so far for how
10828 * they sounds like. */
10829 rescore_suggestions(su);
10830
10831 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010832 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010833 * for the soundfold word, limits the changes that are being tried,
10834 * and "su_sfmaxscore" the rescored score, which is set by
10835 * cleanup_suggestions().
10836 * First find words with a small edit distance, because this is much
10837 * faster and often already finds the top-N suggestions. If we didn't
10838 * find many suggestions try again with a higher edit distance.
10839 * "sl_sounddone" is used to avoid doing the same word twice.
10840 */
10841 suggest_try_soundalike_prep();
10842 su->su_maxscore = SCORE_SFMAX1;
10843 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010844 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010845 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10846 {
10847 /* We didn't find enough matches, try again, allowing more
10848 * changes to the soundfold word. */
10849 su->su_maxscore = SCORE_SFMAX2;
10850 suggest_try_soundalike(su);
10851 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10852 {
10853 /* Still didn't find enough matches, try again, allowing even
10854 * more changes to the soundfold word. */
10855 su->su_maxscore = SCORE_SFMAX3;
10856 suggest_try_soundalike(su);
10857 }
10858 }
10859 su->su_maxscore = su->su_sfmaxscore;
10860 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010861 }
10862
Bram Moolenaar4770d092006-01-12 23:22:24 +000010863 /* When CTRL-C was hit while searching do show the results. Only clear
10864 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010865 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010866 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010867 {
10868 (void)vgetc();
10869 got_int = FALSE;
10870 }
10871
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010872 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010873 {
10874 if (sps_flags & SPS_BEST)
10875 /* Adjust the word score for how it sounds like. */
10876 rescore_suggestions(su);
10877
Bram Moolenaar4770d092006-01-12 23:22:24 +000010878 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10879 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010880 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010881 }
10882}
10883
10884/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010885 * Load the .sug files for languages that have one and weren't loaded yet.
10886 */
10887 static void
10888suggest_load_files()
10889{
10890 langp_T *lp;
10891 int lpi;
10892 slang_T *slang;
10893 char_u *dotp;
10894 FILE *fd;
10895 char_u buf[MAXWLEN];
10896 int i;
10897 time_t timestamp;
10898 int wcount;
10899 int wordnr;
10900 garray_T ga;
10901 int c;
10902
10903 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010904 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010905 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010906 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010907 slang = lp->lp_slang;
10908 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10909 {
10910 /* Change ".spl" to ".sug" and open the file. When the file isn't
10911 * found silently skip it. Do set "sl_sugloaded" so that we
10912 * don't try again and again. */
10913 slang->sl_sugloaded = TRUE;
10914
10915 dotp = vim_strrchr(slang->sl_fname, '.');
10916 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10917 continue;
10918 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010919 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010920 if (fd == NULL)
10921 goto nextone;
10922
10923 /*
10924 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10925 */
10926 for (i = 0; i < VIMSUGMAGICL; ++i)
10927 buf[i] = getc(fd); /* <fileID> */
10928 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10929 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010930 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010931 slang->sl_fname);
10932 goto nextone;
10933 }
10934 c = getc(fd); /* <versionnr> */
10935 if (c < VIMSUGVERSION)
10936 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010937 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010938 slang->sl_fname);
10939 goto nextone;
10940 }
10941 else if (c > VIMSUGVERSION)
10942 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010943 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010944 slang->sl_fname);
10945 goto nextone;
10946 }
10947
10948 /* Check the timestamp, it must be exactly the same as the one in
10949 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010950 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010951 if (timestamp != slang->sl_sugtime)
10952 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010953 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010954 slang->sl_fname);
10955 goto nextone;
10956 }
10957
10958 /*
10959 * <SUGWORDTREE>: <wordtree>
10960 * Read the trie with the soundfolded words.
10961 */
10962 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10963 FALSE, 0) != 0)
10964 {
10965someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010966 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010967 slang->sl_fname);
10968 slang_clear_sug(slang);
10969 goto nextone;
10970 }
10971
10972 /*
10973 * <SUGTABLE>: <sugwcount> <sugline> ...
10974 *
10975 * Read the table with word numbers. We use a file buffer for
10976 * this, because it's so much like a file with lines. Makes it
10977 * possible to swap the info and save on memory use.
10978 */
10979 slang->sl_sugbuf = open_spellbuf();
10980 if (slang->sl_sugbuf == NULL)
10981 goto someerror;
10982 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010983 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010984 if (wcount < 0)
10985 goto someerror;
10986
10987 /* Read all the wordnr lists into the buffer, one NUL terminated
10988 * list per line. */
10989 ga_init2(&ga, 1, 100);
10990 for (wordnr = 0; wordnr < wcount; ++wordnr)
10991 {
10992 ga.ga_len = 0;
10993 for (;;)
10994 {
10995 c = getc(fd); /* <sugline> */
10996 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10997 goto someerror;
10998 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10999 if (c == NUL)
11000 break;
11001 }
11002 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
11003 ga.ga_data, ga.ga_len, TRUE) == FAIL)
11004 goto someerror;
11005 }
11006 ga_clear(&ga);
11007
11008 /*
11009 * Need to put word counts in the word tries, so that we can find
11010 * a word by its number.
11011 */
11012 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
11013 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
11014
11015nextone:
11016 if (fd != NULL)
11017 fclose(fd);
11018 STRCPY(dotp, ".spl");
11019 }
11020 }
11021}
11022
11023
11024/*
11025 * Fill in the wordcount fields for a trie.
11026 * Returns the total number of words.
11027 */
11028 static void
11029tree_count_words(byts, idxs)
11030 char_u *byts;
11031 idx_T *idxs;
11032{
11033 int depth;
11034 idx_T arridx[MAXWLEN];
11035 int curi[MAXWLEN];
11036 int c;
11037 idx_T n;
11038 int wordcount[MAXWLEN];
11039
11040 arridx[0] = 0;
11041 curi[0] = 1;
11042 wordcount[0] = 0;
11043 depth = 0;
11044 while (depth >= 0 && !got_int)
11045 {
11046 if (curi[depth] > byts[arridx[depth]])
11047 {
11048 /* Done all bytes at this node, go up one level. */
11049 idxs[arridx[depth]] = wordcount[depth];
11050 if (depth > 0)
11051 wordcount[depth - 1] += wordcount[depth];
11052
11053 --depth;
11054 fast_breakcheck();
11055 }
11056 else
11057 {
11058 /* Do one more byte at this node. */
11059 n = arridx[depth] + curi[depth];
11060 ++curi[depth];
11061
11062 c = byts[n];
11063 if (c == 0)
11064 {
11065 /* End of word, count it. */
11066 ++wordcount[depth];
11067
11068 /* Skip over any other NUL bytes (same word with different
11069 * flags). */
11070 while (byts[n + 1] == 0)
11071 {
11072 ++n;
11073 ++curi[depth];
11074 }
11075 }
11076 else
11077 {
11078 /* Normal char, go one level deeper to count the words. */
11079 ++depth;
11080 arridx[depth] = idxs[n];
11081 curi[depth] = 1;
11082 wordcount[depth] = 0;
11083 }
11084 }
11085 }
11086}
11087
11088/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011089 * Free the info put in "*su" by spell_find_suggest().
11090 */
11091 static void
11092spell_find_cleanup(su)
11093 suginfo_T *su;
11094{
11095 int i;
11096
11097 /* Free the suggestions. */
11098 for (i = 0; i < su->su_ga.ga_len; ++i)
11099 vim_free(SUG(su->su_ga, i).st_word);
11100 ga_clear(&su->su_ga);
11101 for (i = 0; i < su->su_sga.ga_len; ++i)
11102 vim_free(SUG(su->su_sga, i).st_word);
11103 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011104
11105 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011106 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107}
11108
11109/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011110 * Make a copy of "word", with the first letter upper or lower cased, to
11111 * "wcopy[MAXWLEN]". "word" must not be empty.
11112 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 */
11114 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011115onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 char_u *wcopy;
11118 int upper; /* TRUE: first letter made upper case */
11119{
11120 char_u *p;
11121 int c;
11122 int l;
11123
11124 p = word;
11125#ifdef FEAT_MBYTE
11126 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011127 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 else
11129#endif
11130 c = *p++;
11131 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011132 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011134 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135#ifdef FEAT_MBYTE
11136 if (has_mbyte)
11137 l = mb_char2bytes(c, wcopy);
11138 else
11139#endif
11140 {
11141 l = 1;
11142 wcopy[0] = c;
11143 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011144 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145}
11146
11147/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011148 * Make a copy of "word" with all the letters upper cased into
11149 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 */
11151 static void
11152allcap_copy(word, wcopy)
11153 char_u *word;
11154 char_u *wcopy;
11155{
11156 char_u *s;
11157 char_u *d;
11158 int c;
11159
11160 d = wcopy;
11161 for (s = word; *s != NUL; )
11162 {
11163#ifdef FEAT_MBYTE
11164 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011165 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 else
11167#endif
11168 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011169
11170#ifdef FEAT_MBYTE
11171 /* We only change ß to SS when we are certain latin1 is used. It
11172 * would cause weird errors in other 8-bit encodings. */
11173 if (enc_latin1like && c == 0xdf)
11174 {
11175 c = 'S';
11176 if (d - wcopy >= MAXWLEN - 1)
11177 break;
11178 *d++ = c;
11179 }
11180 else
11181#endif
11182 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183
11184#ifdef FEAT_MBYTE
11185 if (has_mbyte)
11186 {
11187 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11188 break;
11189 d += mb_char2bytes(c, d);
11190 }
11191 else
11192#endif
11193 {
11194 if (d - wcopy >= MAXWLEN - 1)
11195 break;
11196 *d++ = c;
11197 }
11198 }
11199 *d = NUL;
11200}
11201
11202/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011203 * Try finding suggestions by recognizing specific situations.
11204 */
11205 static void
11206suggest_try_special(su)
11207 suginfo_T *su;
11208{
11209 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011210 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011211 int c;
11212 char_u word[MAXWLEN];
11213
11214 /*
11215 * Recognize a word that is repeated: "the the".
11216 */
11217 p = skiptowhite(su->su_fbadword);
11218 len = p - su->su_fbadword;
11219 p = skipwhite(p);
11220 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11221 {
11222 /* Include badflags: if the badword is onecap or allcap
11223 * use that for the goodword too: "The the" -> "The". */
11224 c = su->su_fbadword[len];
11225 su->su_fbadword[len] = NUL;
11226 make_case_word(su->su_fbadword, word, su->su_badflags);
11227 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011228
11229 /* Give a soundalike score of 0, compute the score as if deleting one
11230 * character. */
11231 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011232 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011233 }
11234}
11235
11236/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011237 * Try finding suggestions by adding/removing/swapping letters.
11238 */
11239 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011240suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 suginfo_T *su;
11242{
11243 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011244 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011245 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011246 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011247 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011248
11249 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011250 * to find matches (esp. REP items). Append some more text, changing
11251 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011253 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011254 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011255 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256
Bram Moolenaar860cae12010-06-05 23:22:07 +020011257 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011258 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011259 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011260
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011261 /* If reloading a spell file fails it's still in the list but
11262 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011263 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011264 continue;
11265
Bram Moolenaar4770d092006-01-12 23:22:24 +000011266 /* Try it for this language. Will add possible suggestions. */
11267 suggest_trie_walk(su, lp, fword, FALSE);
11268 }
11269}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270
Bram Moolenaar4770d092006-01-12 23:22:24 +000011271/* Check the maximum score, if we go over it we won't try this change. */
11272#define TRY_DEEPER(su, stack, depth, add) \
11273 (stack[depth].ts_score + (add) < su->su_maxscore)
11274
11275/*
11276 * Try finding suggestions by adding/removing/swapping letters.
11277 *
11278 * This uses a state machine. At each node in the tree we try various
11279 * operations. When trying if an operation works "depth" is increased and the
11280 * stack[] is used to store info. This allows combinations, thus insert one
11281 * character, replace one and delete another. The number of changes is
11282 * limited by su->su_maxscore.
11283 *
11284 * After implementing this I noticed an article by Kemal Oflazer that
11285 * describes something similar: "Error-tolerant Finite State Recognition with
11286 * Applications to Morphological Analysis and Spelling Correction" (1996).
11287 * The implementation in the article is simplified and requires a stack of
11288 * unknown depth. The implementation here only needs a stack depth equal to
11289 * the length of the word.
11290 *
11291 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11292 * The mechanism is the same, but we find a match with a sound-folded word
11293 * that comes from one or more original words. Each of these words may be
11294 * added, this is done by add_sound_suggest().
11295 * Don't use:
11296 * the prefix tree or the keep-case tree
11297 * "su->su_badlen"
11298 * anything to do with upper and lower case
11299 * anything to do with word or non-word characters ("spell_iswordp()")
11300 * banned words
11301 * word flags (rare, region, compounding)
11302 * word splitting for now
11303 * "similar_chars()"
11304 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11305 */
11306 static void
11307suggest_trie_walk(su, lp, fword, soundfold)
11308 suginfo_T *su;
11309 langp_T *lp;
11310 char_u *fword;
11311 int soundfold;
11312{
11313 char_u tword[MAXWLEN]; /* good word collected so far */
11314 trystate_T stack[MAXWLEN];
11315 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011316 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011317 * words and split word. NUL terminated
11318 * when going deeper but not when coming
11319 * back. */
11320 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11321 trystate_T *sp;
11322 int newscore;
11323 int score;
11324 char_u *byts, *fbyts, *pbyts;
11325 idx_T *idxs, *fidxs, *pidxs;
11326 int depth;
11327 int c, c2, c3;
11328 int n = 0;
11329 int flags;
11330 garray_T *gap;
11331 idx_T arridx;
11332 int len;
11333 char_u *p;
11334 fromto_T *ftp;
11335 int fl = 0, tl;
11336 int repextra = 0; /* extra bytes in fword[] from REP item */
11337 slang_T *slang = lp->lp_slang;
11338 int fword_ends;
11339 int goodword_ends;
11340#ifdef DEBUG_TRIEWALK
11341 /* Stores the name of the change made at each level. */
11342 char_u changename[MAXWLEN][80];
11343#endif
11344 int breakcheckcount = 1000;
11345 int compound_ok;
11346
11347 /*
11348 * Go through the whole case-fold tree, try changes at each node.
11349 * "tword[]" contains the word collected from nodes in the tree.
11350 * "fword[]" the word we are trying to match with (initially the bad
11351 * word).
11352 */
11353 depth = 0;
11354 sp = &stack[0];
11355 vim_memset(sp, 0, sizeof(trystate_T));
11356 sp->ts_curi = 1;
11357
11358 if (soundfold)
11359 {
11360 /* Going through the soundfold tree. */
11361 byts = fbyts = slang->sl_sbyts;
11362 idxs = fidxs = slang->sl_sidxs;
11363 pbyts = NULL;
11364 pidxs = NULL;
11365 sp->ts_prefixdepth = PFD_NOPREFIX;
11366 sp->ts_state = STATE_START;
11367 }
11368 else
11369 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011370 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011371 * When there are postponed prefixes we need to use these first. At
11372 * the end of the prefix we continue in the case-fold tree.
11373 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011374 fbyts = slang->sl_fbyts;
11375 fidxs = slang->sl_fidxs;
11376 pbyts = slang->sl_pbyts;
11377 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011378 if (pbyts != NULL)
11379 {
11380 byts = pbyts;
11381 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011382 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011383 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11384 }
11385 else
11386 {
11387 byts = fbyts;
11388 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011389 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011390 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011391 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011392 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011393
Bram Moolenaar4770d092006-01-12 23:22:24 +000011394 /*
11395 * Loop to find all suggestions. At each round we either:
11396 * - For the current state try one operation, advance "ts_curi",
11397 * increase "depth".
11398 * - When a state is done go to the next, set "ts_state".
11399 * - When all states are tried decrease "depth".
11400 */
11401 while (depth >= 0 && !got_int)
11402 {
11403 sp = &stack[depth];
11404 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011406 case STATE_START:
11407 case STATE_NOPREFIX:
11408 /*
11409 * Start of node: Deal with NUL bytes, which means
11410 * tword[] may end here.
11411 */
11412 arridx = sp->ts_arridx; /* current node in the tree */
11413 len = byts[arridx]; /* bytes in this node */
11414 arridx += sp->ts_curi; /* index of current byte */
11415
11416 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011418 /* Skip over the NUL bytes, we use them later. */
11419 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11420 ;
11421 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422
Bram Moolenaar4770d092006-01-12 23:22:24 +000011423 /* Always past NUL bytes now. */
11424 n = (int)sp->ts_state;
11425 sp->ts_state = STATE_ENDNUL;
11426 sp->ts_save_badflags = su->su_badflags;
11427
11428 /* At end of a prefix or at start of prefixtree: check for
11429 * following word. */
11430 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011431 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011432 /* Set su->su_badflags to the caps type at this position.
11433 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011434#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011435 if (has_mbyte)
11436 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11437 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011438#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011439 n = sp->ts_fidx;
11440 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11441 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011442 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011443#ifdef DEBUG_TRIEWALK
11444 sprintf(changename[depth], "prefix");
11445#endif
11446 go_deeper(stack, depth, 0);
11447 ++depth;
11448 sp = &stack[depth];
11449 sp->ts_prefixdepth = depth - 1;
11450 byts = fbyts;
11451 idxs = fidxs;
11452 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011453
Bram Moolenaar4770d092006-01-12 23:22:24 +000011454 /* Move the prefix to preword[] with the right case
11455 * and make find_keepcap_word() works. */
11456 tword[sp->ts_twordlen] = NUL;
11457 make_case_word(tword + sp->ts_splitoff,
11458 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011459 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011460 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011461 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011462 break;
11463 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011464
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 if (sp->ts_curi > len || byts[arridx] != 0)
11466 {
11467 /* Past bytes in node and/or past NUL bytes. */
11468 sp->ts_state = STATE_ENDNUL;
11469 sp->ts_save_badflags = su->su_badflags;
11470 break;
11471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473 /*
11474 * End of word in tree.
11475 */
11476 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477
Bram Moolenaar4770d092006-01-12 23:22:24 +000011478 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011479
11480 /* Skip words with the NOSUGGEST flag. */
11481 if (flags & WF_NOSUGGEST)
11482 break;
11483
Bram Moolenaar4770d092006-01-12 23:22:24 +000011484 fword_ends = (fword[sp->ts_fidx] == NUL
11485 || (soundfold
11486 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011487 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011488 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011489
Bram Moolenaar4770d092006-01-12 23:22:24 +000011490 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011491 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011492 {
11493 /* There was a prefix before the word. Check that the prefix
11494 * can be used with this word. */
11495 /* Count the length of the NULs in the prefix. If there are
11496 * none this must be the first try without a prefix. */
11497 n = stack[sp->ts_prefixdepth].ts_arridx;
11498 len = pbyts[n++];
11499 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11500 ;
11501 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011502 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011503 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011504 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 if (c == 0)
11506 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011507
Bram Moolenaar4770d092006-01-12 23:22:24 +000011508 /* Use the WF_RARE flag for a rare prefix. */
11509 if (c & WF_RAREPFX)
11510 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011511
Bram Moolenaar4770d092006-01-12 23:22:24 +000011512 /* Tricky: when checking for both prefix and compounding
11513 * we run into the prefix flag first.
11514 * Remember that it's OK, so that we accept the prefix
11515 * when arriving at a compound flag. */
11516 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011517 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011518 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011519
Bram Moolenaar4770d092006-01-12 23:22:24 +000011520 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11521 * appending another compound word below. */
11522 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011523 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011524 goodword_ends = FALSE;
11525 else
11526 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011527
Bram Moolenaar4770d092006-01-12 23:22:24 +000011528 p = NULL;
11529 compound_ok = TRUE;
11530 if (sp->ts_complen > sp->ts_compsplit)
11531 {
11532 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011533 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011534 /* There was a word before this word. When there was no
11535 * change in this word (it was correct) add the first word
11536 * as a suggestion. If this word was corrected too, we
11537 * need to check if a correct word follows. */
11538 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011539 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011540 && STRNCMP(fword + sp->ts_splitfidx,
11541 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011542 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011543 {
11544 preword[sp->ts_prewordlen] = NUL;
11545 newscore = score_wordcount_adj(slang, sp->ts_score,
11546 preword + sp->ts_prewordlen,
11547 sp->ts_prewordlen > 0);
11548 /* Add the suggestion if the score isn't too bad. */
11549 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011550 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011551 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011552 newscore, 0, FALSE,
11553 lp->lp_sallang, FALSE);
11554 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011555 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011556 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011557 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011558 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011559 /* There was a compound word before this word. If this
11560 * word does not support compounding then give up
11561 * (splitting is tried for the word without compound
11562 * flag). */
11563 if (((unsigned)flags >> 24) == 0
11564 || sp->ts_twordlen - sp->ts_splitoff
11565 < slang->sl_compminlen)
11566 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011567#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011568 /* For multi-byte chars check character length against
11569 * COMPOUNDMIN. */
11570 if (has_mbyte
11571 && slang->sl_compminlen > 0
11572 && mb_charlen(tword + sp->ts_splitoff)
11573 < slang->sl_compminlen)
11574 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011575#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011576
Bram Moolenaar4770d092006-01-12 23:22:24 +000011577 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11578 compflags[sp->ts_complen + 1] = NUL;
11579 vim_strncpy(preword + sp->ts_prewordlen,
11580 tword + sp->ts_splitoff,
11581 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011582
11583 /* Verify CHECKCOMPOUNDPATTERN rules. */
11584 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11585 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011586 compound_ok = FALSE;
11587
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011588 if (compound_ok)
11589 {
11590 p = preword;
11591 while (*skiptowhite(p) != NUL)
11592 p = skipwhite(skiptowhite(p));
11593 if (fword_ends && !can_compound(slang, p,
11594 compflags + sp->ts_compsplit))
11595 /* Compound is not allowed. But it may still be
11596 * possible if we add another (short) word. */
11597 compound_ok = FALSE;
11598 }
11599
Bram Moolenaar4770d092006-01-12 23:22:24 +000011600 /* Get pointer to last char of previous word. */
11601 p = preword + sp->ts_prewordlen;
11602 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011603 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605
Bram Moolenaar4770d092006-01-12 23:22:24 +000011606 /*
11607 * Form the word with proper case in preword.
11608 * If there is a word from a previous split, append.
11609 * For the soundfold tree don't change the case, simply append.
11610 */
11611 if (soundfold)
11612 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11613 else if (flags & WF_KEEPCAP)
11614 /* Must find the word in the keep-case tree. */
11615 find_keepcap_word(slang, tword + sp->ts_splitoff,
11616 preword + sp->ts_prewordlen);
11617 else
11618 {
11619 /* Include badflags: If the badword is onecap or allcap
11620 * use that for the goodword too. But if the badword is
11621 * allcap and it's only one char long use onecap. */
11622 c = su->su_badflags;
11623 if ((c & WF_ALLCAP)
11624#ifdef FEAT_MBYTE
11625 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11626#else
11627 && su->su_badlen == 1
11628#endif
11629 )
11630 c = WF_ONECAP;
11631 c |= flags;
11632
11633 /* When appending a compound word after a word character don't
11634 * use Onecap. */
11635 if (p != NULL && spell_iswordp_nmw(p))
11636 c &= ~WF_ONECAP;
11637 make_case_word(tword + sp->ts_splitoff,
11638 preword + sp->ts_prewordlen, c);
11639 }
11640
11641 if (!soundfold)
11642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 /* Don't use a banned word. It may appear again as a good
11644 * word, thus remember it. */
11645 if (flags & WF_BANNED)
11646 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011647 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011648 break;
11649 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011650 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011651 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11652 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011653 {
11654 if (slang->sl_compprog == NULL)
11655 break;
11656 /* the word so far was banned but we may try compounding */
11657 goodword_ends = FALSE;
11658 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660
Bram Moolenaar4770d092006-01-12 23:22:24 +000011661 newscore = 0;
11662 if (!soundfold) /* soundfold words don't have flags */
11663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011665 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011666 newscore += SCORE_REGION;
11667 if (flags & WF_RARE)
11668 newscore += SCORE_RARE;
11669
Bram Moolenaar0c405862005-06-22 22:26:26 +000011670 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011671 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011673 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674
Bram Moolenaar4770d092006-01-12 23:22:24 +000011675 /* TODO: how about splitting in the soundfold tree? */
11676 if (fword_ends
11677 && goodword_ends
11678 && sp->ts_fidx >= sp->ts_fidxtry
11679 && compound_ok)
11680 {
11681 /* The badword also ends: add suggestions. */
11682#ifdef DEBUG_TRIEWALK
11683 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011685 int j;
11686
11687 /* print the stack of changes that brought us here */
11688 smsg("------ %s -------", fword);
11689 for (j = 0; j < depth; ++j)
11690 smsg("%s", changename[j]);
11691 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011692#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011693 if (soundfold)
11694 {
11695 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011696 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011697 add_sound_suggest(su, preword, sp->ts_score, lp);
11698 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011699 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011700 {
11701 /* Give a penalty when changing non-word char to word
11702 * char, e.g., "thes," -> "these". */
11703 p = fword + sp->ts_fidx;
11704 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011705 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011706 {
11707 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011708 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011709 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011710 newscore += SCORE_NONWORD;
11711 }
11712
Bram Moolenaar4770d092006-01-12 23:22:24 +000011713 /* Give a bonus to words seen before. */
11714 score = score_wordcount_adj(slang,
11715 sp->ts_score + newscore,
11716 preword + sp->ts_prewordlen,
11717 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011718
Bram Moolenaar4770d092006-01-12 23:22:24 +000011719 /* Add the suggestion if the score isn't too bad. */
11720 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011721 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011722 add_suggestion(su, &su->su_ga, preword,
11723 sp->ts_fidx - repextra,
11724 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011725
11726 if (su->su_badflags & WF_MIXCAP)
11727 {
11728 /* We really don't know if the word should be
11729 * upper or lower case, add both. */
11730 c = captype(preword, NULL);
11731 if (c == 0 || c == WF_ALLCAP)
11732 {
11733 make_case_word(tword + sp->ts_splitoff,
11734 preword + sp->ts_prewordlen,
11735 c == 0 ? WF_ALLCAP : 0);
11736
11737 add_suggestion(su, &su->su_ga, preword,
11738 sp->ts_fidx - repextra,
11739 score + SCORE_ICASE, 0, FALSE,
11740 lp->lp_sallang, FALSE);
11741 }
11742 }
11743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011745 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011746
Bram Moolenaar4770d092006-01-12 23:22:24 +000011747 /*
11748 * Try word split and/or compounding.
11749 */
11750 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011751#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011752 /* Don't split halfway a character. */
11753 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011754#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011755 )
11756 {
11757 int try_compound;
11758 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011759
Bram Moolenaar4770d092006-01-12 23:22:24 +000011760 /* If past the end of the bad word don't try a split.
11761 * Otherwise try changing the next word. E.g., find
11762 * suggestions for "the the" where the second "the" is
11763 * different. It's done like a split.
11764 * TODO: word split for soundfold words */
11765 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11766 && !soundfold;
11767
11768 /* Get here in several situations:
11769 * 1. The word in the tree ends:
11770 * If the word allows compounding try that. Otherwise try
11771 * a split by inserting a space. For both check that a
11772 * valid words starts at fword[sp->ts_fidx].
11773 * For NOBREAK do like compounding to be able to check if
11774 * the next word is valid.
11775 * 2. The badword does end, but it was due to a change (e.g.,
11776 * a swap). No need to split, but do check that the
11777 * following word is valid.
11778 * 3. The badword and the word in the tree end. It may still
11779 * be possible to compound another (short) word.
11780 */
11781 try_compound = FALSE;
11782 if (!soundfold
11783 && slang->sl_compprog != NULL
11784 && ((unsigned)flags >> 24) != 0
11785 && sp->ts_twordlen - sp->ts_splitoff
11786 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011787#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011788 && (!has_mbyte
11789 || slang->sl_compminlen == 0
11790 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011791 >= slang->sl_compminlen)
11792#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011793 && (slang->sl_compsylmax < MAXWLEN
11794 || sp->ts_complen + 1 - sp->ts_compsplit
11795 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011796 && (can_be_compound(sp, slang,
11797 compflags, ((unsigned)flags >> 24))))
11798
Bram Moolenaar4770d092006-01-12 23:22:24 +000011799 {
11800 try_compound = TRUE;
11801 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11802 compflags[sp->ts_complen + 1] = NUL;
11803 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011804
Bram Moolenaar4770d092006-01-12 23:22:24 +000011805 /* For NOBREAK we never try splitting, it won't make any word
11806 * valid. */
11807 if (slang->sl_nobreak)
11808 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011809
Bram Moolenaar4770d092006-01-12 23:22:24 +000011810 /* If we could add a compound word, and it's also possible to
11811 * split at this point, do the split first and set
11812 * TSF_DIDSPLIT to avoid doing it again. */
11813 else if (!fword_ends
11814 && try_compound
11815 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11816 {
11817 try_compound = FALSE;
11818 sp->ts_flags |= TSF_DIDSPLIT;
11819 --sp->ts_curi; /* do the same NUL again */
11820 compflags[sp->ts_complen] = NUL;
11821 }
11822 else
11823 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011824
Bram Moolenaar4770d092006-01-12 23:22:24 +000011825 if (try_split || try_compound)
11826 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011827 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011828 {
11829 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011830 * words so far are valid for compounding. If there
11831 * is only one word it must not have the NEEDCOMPOUND
11832 * flag. */
11833 if (sp->ts_complen == sp->ts_compsplit
11834 && (flags & WF_NEEDCOMP))
11835 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011836 p = preword;
11837 while (*skiptowhite(p) != NUL)
11838 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011839 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011840 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011841 compflags + sp->ts_compsplit))
11842 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011843
11844 if (slang->sl_nosplitsugs)
11845 newscore += SCORE_SPLIT_NO;
11846 else
11847 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011848
11849 /* Give a bonus to words seen before. */
11850 newscore = score_wordcount_adj(slang, newscore,
11851 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011852 }
11853
Bram Moolenaar4770d092006-01-12 23:22:24 +000011854 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011856 go_deeper(stack, depth, newscore);
11857#ifdef DEBUG_TRIEWALK
11858 if (!try_compound && !fword_ends)
11859 sprintf(changename[depth], "%.*s-%s: split",
11860 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11861 else
11862 sprintf(changename[depth], "%.*s-%s: compound",
11863 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11864#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011866 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011867 sp->ts_state = STATE_SPLITUNDO;
11868
11869 ++depth;
11870 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011871
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011872 /* Append a space to preword when splitting. */
11873 if (!try_compound && !fword_ends)
11874 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011875 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011876 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011877 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011878
11879 /* If the badword has a non-word character at this
11880 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011881 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011882 * character when the word ends. But only when the
11883 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011884 if (((!try_compound && !spell_iswordp_nmw(fword
11885 + sp->ts_fidx))
11886 || fword_ends)
11887 && fword[sp->ts_fidx] != NUL
11888 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011889 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011890 int l;
11891
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011892#ifdef FEAT_MBYTE
11893 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011894 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011895 else
11896#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011897 l = 1;
11898 if (fword_ends)
11899 {
11900 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011901 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011902 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011903 sp->ts_prewordlen += l;
11904 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011905 }
11906 else
11907 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11908 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011909 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011910
Bram Moolenaard12a1322005-08-21 22:08:24 +000011911 /* When compounding include compound flag in
11912 * compflags[] (already set above). When splitting we
11913 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011914 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011915 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011916 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011917 sp->ts_compsplit = sp->ts_complen;
11918 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011919
Bram Moolenaar53805d12005-08-01 07:08:33 +000011920 /* set su->su_badflags to the caps type at this
11921 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011922#ifdef FEAT_MBYTE
11923 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011924 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011925 else
11926#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011927 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011928 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011929 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011932 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011933
11934 /* If there are postponed prefixes, try these too. */
11935 if (pbyts != NULL)
11936 {
11937 byts = pbyts;
11938 idxs = pidxs;
11939 sp->ts_prefixdepth = PFD_PREFIXTREE;
11940 sp->ts_state = STATE_NOPREFIX;
11941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011942 }
11943 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011944 }
11945 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946
Bram Moolenaar4770d092006-01-12 23:22:24 +000011947 case STATE_SPLITUNDO:
11948 /* Undo the changes done for word split or compound word. */
11949 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950
Bram Moolenaar4770d092006-01-12 23:22:24 +000011951 /* Continue looking for NUL bytes. */
11952 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011953
Bram Moolenaar4770d092006-01-12 23:22:24 +000011954 /* In case we went into the prefix tree. */
11955 byts = fbyts;
11956 idxs = fidxs;
11957 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958
Bram Moolenaar4770d092006-01-12 23:22:24 +000011959 case STATE_ENDNUL:
11960 /* Past the NUL bytes in the node. */
11961 su->su_badflags = sp->ts_save_badflags;
11962 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011963#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011965#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011966 )
11967 {
11968 /* The badword ends, can't use STATE_PLAIN. */
11969 sp->ts_state = STATE_DEL;
11970 break;
11971 }
11972 sp->ts_state = STATE_PLAIN;
11973 /*FALLTHROUGH*/
11974
11975 case STATE_PLAIN:
11976 /*
11977 * Go over all possible bytes at this node, add each to tword[]
11978 * and use child node. "ts_curi" is the index.
11979 */
11980 arridx = sp->ts_arridx;
11981 if (sp->ts_curi > byts[arridx])
11982 {
11983 /* Done all bytes at this node, do next state. When still at
11984 * already changed bytes skip the other tricks. */
11985 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011988 sp->ts_state = STATE_FINAL;
11989 }
11990 else
11991 {
11992 arridx += sp->ts_curi++;
11993 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994
Bram Moolenaar4770d092006-01-12 23:22:24 +000011995 /* Normal byte, go one level deeper. If it's not equal to the
11996 * byte in the bad word adjust the score. But don't even try
11997 * when the byte was already changed. And don't try when we
11998 * just deleted this byte, accepting it is always cheaper then
11999 * delete + substitute. */
12000 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000012001#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012002 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012003#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012004 )
12005 newscore = 0;
12006 else
12007 newscore = SCORE_SUBST;
12008 if ((newscore == 0
12009 || (sp->ts_fidx >= sp->ts_fidxtry
12010 && ((sp->ts_flags & TSF_DIDDEL) == 0
12011 || c != fword[sp->ts_delidx])))
12012 && TRY_DEEPER(su, stack, depth, newscore))
12013 {
12014 go_deeper(stack, depth, newscore);
12015#ifdef DEBUG_TRIEWALK
12016 if (newscore > 0)
12017 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12018 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12019 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012021 sprintf(changename[depth], "%.*s-%s: accept %c",
12022 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12023 fword[sp->ts_fidx]);
12024#endif
12025 ++depth;
12026 sp = &stack[depth];
12027 ++sp->ts_fidx;
12028 tword[sp->ts_twordlen++] = c;
12029 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012030#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012031 if (newscore == SCORE_SUBST)
12032 sp->ts_isdiff = DIFF_YES;
12033 if (has_mbyte)
12034 {
12035 /* Multi-byte characters are a bit complicated to
12036 * handle: They differ when any of the bytes differ
12037 * and then their length may also differ. */
12038 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012039 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012040 /* First byte. */
12041 sp->ts_tcharidx = 0;
12042 sp->ts_tcharlen = MB_BYTE2LEN(c);
12043 sp->ts_fcharstart = sp->ts_fidx - 1;
12044 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012045 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012046 }
12047 else if (sp->ts_isdiff == DIFF_INSERT)
12048 /* When inserting trail bytes don't advance in the
12049 * bad word. */
12050 --sp->ts_fidx;
12051 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12052 {
12053 /* Last byte of character. */
12054 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012055 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012056 /* Correct ts_fidx for the byte length of the
12057 * character (we didn't check that before). */
12058 sp->ts_fidx = sp->ts_fcharstart
12059 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012060 fword[sp->ts_fcharstart]);
12061
Bram Moolenaar4770d092006-01-12 23:22:24 +000012062 /* For changing a composing character adjust
12063 * the score from SCORE_SUBST to
12064 * SCORE_SUBCOMP. */
12065 if (enc_utf8
12066 && utf_iscomposing(
12067 mb_ptr2char(tword
12068 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012069 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012070 && utf_iscomposing(
12071 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012072 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012073 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012074 SCORE_SUBST - SCORE_SUBCOMP;
12075
Bram Moolenaar4770d092006-01-12 23:22:24 +000012076 /* For a similar character adjust score from
12077 * SCORE_SUBST to SCORE_SIMILAR. */
12078 else if (!soundfold
12079 && slang->sl_has_map
12080 && similar_chars(slang,
12081 mb_ptr2char(tword
12082 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012083 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012084 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012085 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012086 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012087 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012088 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012089 else if (sp->ts_isdiff == DIFF_INSERT
12090 && sp->ts_twordlen > sp->ts_tcharlen)
12091 {
12092 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12093 c = mb_ptr2char(p);
12094 if (enc_utf8 && utf_iscomposing(c))
12095 {
12096 /* Inserting a composing char doesn't
12097 * count that much. */
12098 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12099 }
12100 else
12101 {
12102 /* If the previous character was the same,
12103 * thus doubling a character, give a bonus
12104 * to the score. Also for the soundfold
12105 * tree (might seem illogical but does
12106 * give better scores). */
12107 mb_ptr_back(tword, p);
12108 if (c == mb_ptr2char(p))
12109 sp->ts_score -= SCORE_INS
12110 - SCORE_INSDUP;
12111 }
12112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012113
Bram Moolenaar4770d092006-01-12 23:22:24 +000012114 /* Starting a new char, reset the length. */
12115 sp->ts_tcharlen = 0;
12116 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012117 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012118 else
12119#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012120 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012121 /* If we found a similar char adjust the score.
12122 * We do this after calling go_deeper() because
12123 * it's slow. */
12124 if (newscore != 0
12125 && !soundfold
12126 && slang->sl_has_map
12127 && similar_chars(slang,
12128 c, fword[sp->ts_fidx - 1]))
12129 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012131 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012132 }
12133 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012134
Bram Moolenaar4770d092006-01-12 23:22:24 +000012135 case STATE_DEL:
12136#ifdef FEAT_MBYTE
12137 /* When past the first byte of a multi-byte char don't try
12138 * delete/insert/swap a character. */
12139 if (has_mbyte && sp->ts_tcharlen > 0)
12140 {
12141 sp->ts_state = STATE_FINAL;
12142 break;
12143 }
12144#endif
12145 /*
12146 * Try skipping one character in the bad word (delete it).
12147 */
12148 sp->ts_state = STATE_INS_PREP;
12149 sp->ts_curi = 1;
12150 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12151 /* Deleting a vowel at the start of a word counts less, see
12152 * soundalike_score(). */
12153 newscore = 2 * SCORE_DEL / 3;
12154 else
12155 newscore = SCORE_DEL;
12156 if (fword[sp->ts_fidx] != NUL
12157 && TRY_DEEPER(su, stack, depth, newscore))
12158 {
12159 go_deeper(stack, depth, newscore);
12160#ifdef DEBUG_TRIEWALK
12161 sprintf(changename[depth], "%.*s-%s: delete %c",
12162 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12163 fword[sp->ts_fidx]);
12164#endif
12165 ++depth;
12166
12167 /* Remember what character we deleted, so that we can avoid
12168 * inserting it again. */
12169 stack[depth].ts_flags |= TSF_DIDDEL;
12170 stack[depth].ts_delidx = sp->ts_fidx;
12171
12172 /* Advance over the character in fword[]. Give a bonus to the
12173 * score if the same character is following "nn" -> "n". It's
12174 * a bit illogical for soundfold tree but it does give better
12175 * results. */
12176#ifdef FEAT_MBYTE
12177 if (has_mbyte)
12178 {
12179 c = mb_ptr2char(fword + sp->ts_fidx);
12180 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12181 if (enc_utf8 && utf_iscomposing(c))
12182 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12183 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12184 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12185 }
12186 else
12187#endif
12188 {
12189 ++stack[depth].ts_fidx;
12190 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12191 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12192 }
12193 break;
12194 }
12195 /*FALLTHROUGH*/
12196
12197 case STATE_INS_PREP:
12198 if (sp->ts_flags & TSF_DIDDEL)
12199 {
12200 /* If we just deleted a byte then inserting won't make sense,
12201 * a substitute is always cheaper. */
12202 sp->ts_state = STATE_SWAP;
12203 break;
12204 }
12205
12206 /* skip over NUL bytes */
12207 n = sp->ts_arridx;
12208 for (;;)
12209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012210 if (sp->ts_curi > byts[n])
12211 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012212 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012213 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012214 break;
12215 }
12216 if (byts[n + sp->ts_curi] != NUL)
12217 {
12218 /* Found a byte to insert. */
12219 sp->ts_state = STATE_INS;
12220 break;
12221 }
12222 ++sp->ts_curi;
12223 }
12224 break;
12225
12226 /*FALLTHROUGH*/
12227
12228 case STATE_INS:
12229 /* Insert one byte. Repeat this for each possible byte at this
12230 * node. */
12231 n = sp->ts_arridx;
12232 if (sp->ts_curi > byts[n])
12233 {
12234 /* Done all bytes at this node, go to next state. */
12235 sp->ts_state = STATE_SWAP;
12236 break;
12237 }
12238
12239 /* Do one more byte at this node, but:
12240 * - Skip NUL bytes.
12241 * - Skip the byte if it's equal to the byte in the word,
12242 * accepting that byte is always better.
12243 */
12244 n += sp->ts_curi++;
12245 c = byts[n];
12246 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12247 /* Inserting a vowel at the start of a word counts less,
12248 * see soundalike_score(). */
12249 newscore = 2 * SCORE_INS / 3;
12250 else
12251 newscore = SCORE_INS;
12252 if (c != fword[sp->ts_fidx]
12253 && TRY_DEEPER(su, stack, depth, newscore))
12254 {
12255 go_deeper(stack, depth, newscore);
12256#ifdef DEBUG_TRIEWALK
12257 sprintf(changename[depth], "%.*s-%s: insert %c",
12258 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12259 c);
12260#endif
12261 ++depth;
12262 sp = &stack[depth];
12263 tword[sp->ts_twordlen++] = c;
12264 sp->ts_arridx = idxs[n];
12265#ifdef FEAT_MBYTE
12266 if (has_mbyte)
12267 {
12268 fl = MB_BYTE2LEN(c);
12269 if (fl > 1)
12270 {
12271 /* There are following bytes for the same character.
12272 * We must find all bytes before trying
12273 * delete/insert/swap/etc. */
12274 sp->ts_tcharlen = fl;
12275 sp->ts_tcharidx = 1;
12276 sp->ts_isdiff = DIFF_INSERT;
12277 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 }
12279 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012280 fl = 1;
12281 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012282#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012283 {
12284 /* If the previous character was the same, thus doubling a
12285 * character, give a bonus to the score. Also for
12286 * soundfold words (illogical but does give a better
12287 * score). */
12288 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012289 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012290 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012292 }
12293 break;
12294
12295 case STATE_SWAP:
12296 /*
12297 * Swap two bytes in the bad word: "12" -> "21".
12298 * We change "fword" here, it's changed back afterwards at
12299 * STATE_UNSWAP.
12300 */
12301 p = fword + sp->ts_fidx;
12302 c = *p;
12303 if (c == NUL)
12304 {
12305 /* End of word, can't swap or replace. */
12306 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012307 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012309
Bram Moolenaar4770d092006-01-12 23:22:24 +000012310 /* Don't swap if the first character is not a word character.
12311 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012312 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012313 {
12314 sp->ts_state = STATE_REP_INI;
12315 break;
12316 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012317
Bram Moolenaar4770d092006-01-12 23:22:24 +000012318#ifdef FEAT_MBYTE
12319 if (has_mbyte)
12320 {
12321 n = mb_cptr2len(p);
12322 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012323 if (p[n] == NUL)
12324 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012325 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012326 c2 = c; /* don't swap non-word char */
12327 else
12328 c2 = mb_ptr2char(p + n);
12329 }
12330 else
12331#endif
12332 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012333 if (p[1] == NUL)
12334 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012335 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012336 c2 = c; /* don't swap non-word char */
12337 else
12338 c2 = p[1];
12339 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012340
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012341 /* When the second character is NUL we can't swap. */
12342 if (c2 == NUL)
12343 {
12344 sp->ts_state = STATE_REP_INI;
12345 break;
12346 }
12347
Bram Moolenaar4770d092006-01-12 23:22:24 +000012348 /* When characters are identical, swap won't do anything.
12349 * Also get here if the second char is not a word character. */
12350 if (c == c2)
12351 {
12352 sp->ts_state = STATE_SWAP3;
12353 break;
12354 }
12355 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12356 {
12357 go_deeper(stack, depth, SCORE_SWAP);
12358#ifdef DEBUG_TRIEWALK
12359 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12360 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12361 c, c2);
12362#endif
12363 sp->ts_state = STATE_UNSWAP;
12364 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012365#ifdef FEAT_MBYTE
12366 if (has_mbyte)
12367 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012368 fl = mb_char2len(c2);
12369 mch_memmove(p, p + n, fl);
12370 mb_char2bytes(c, p + fl);
12371 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012372 }
12373 else
12374#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012375 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012376 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012377 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012378 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012379 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012380 }
12381 else
12382 /* If this swap doesn't work then SWAP3 won't either. */
12383 sp->ts_state = STATE_REP_INI;
12384 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012385
Bram Moolenaar4770d092006-01-12 23:22:24 +000012386 case STATE_UNSWAP:
12387 /* Undo the STATE_SWAP swap: "21" -> "12". */
12388 p = fword + sp->ts_fidx;
12389#ifdef FEAT_MBYTE
12390 if (has_mbyte)
12391 {
12392 n = MB_BYTE2LEN(*p);
12393 c = mb_ptr2char(p + n);
12394 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12395 mb_char2bytes(c, p);
12396 }
12397 else
12398#endif
12399 {
12400 c = *p;
12401 *p = p[1];
12402 p[1] = c;
12403 }
12404 /*FALLTHROUGH*/
12405
12406 case STATE_SWAP3:
12407 /* Swap two bytes, skipping one: "123" -> "321". We change
12408 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12409 p = fword + sp->ts_fidx;
12410#ifdef FEAT_MBYTE
12411 if (has_mbyte)
12412 {
12413 n = mb_cptr2len(p);
12414 c = mb_ptr2char(p);
12415 fl = mb_cptr2len(p + n);
12416 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012417 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012418 c3 = c; /* don't swap non-word char */
12419 else
12420 c3 = mb_ptr2char(p + n + fl);
12421 }
12422 else
12423#endif
12424 {
12425 c = *p;
12426 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012427 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012428 c3 = c; /* don't swap non-word char */
12429 else
12430 c3 = p[2];
12431 }
12432
12433 /* When characters are identical: "121" then SWAP3 result is
12434 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12435 * same as SWAP on next char: "112". Thus skip all swapping.
12436 * Also skip when c3 is NUL.
12437 * Also get here when the third character is not a word character.
12438 * Second character may any char: "a.b" -> "b.a" */
12439 if (c == c3 || c3 == NUL)
12440 {
12441 sp->ts_state = STATE_REP_INI;
12442 break;
12443 }
12444 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12445 {
12446 go_deeper(stack, depth, SCORE_SWAP3);
12447#ifdef DEBUG_TRIEWALK
12448 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12449 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12450 c, c3);
12451#endif
12452 sp->ts_state = STATE_UNSWAP3;
12453 ++depth;
12454#ifdef FEAT_MBYTE
12455 if (has_mbyte)
12456 {
12457 tl = mb_char2len(c3);
12458 mch_memmove(p, p + n + fl, tl);
12459 mb_char2bytes(c2, p + tl);
12460 mb_char2bytes(c, p + fl + tl);
12461 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12462 }
12463 else
12464#endif
12465 {
12466 p[0] = p[2];
12467 p[2] = c;
12468 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12469 }
12470 }
12471 else
12472 sp->ts_state = STATE_REP_INI;
12473 break;
12474
12475 case STATE_UNSWAP3:
12476 /* Undo STATE_SWAP3: "321" -> "123" */
12477 p = fword + sp->ts_fidx;
12478#ifdef FEAT_MBYTE
12479 if (has_mbyte)
12480 {
12481 n = MB_BYTE2LEN(*p);
12482 c2 = mb_ptr2char(p + n);
12483 fl = MB_BYTE2LEN(p[n]);
12484 c = mb_ptr2char(p + n + fl);
12485 tl = MB_BYTE2LEN(p[n + fl]);
12486 mch_memmove(p + fl + tl, p, n);
12487 mb_char2bytes(c, p);
12488 mb_char2bytes(c2, p + tl);
12489 p = p + tl;
12490 }
12491 else
12492#endif
12493 {
12494 c = *p;
12495 *p = p[2];
12496 p[2] = c;
12497 ++p;
12498 }
12499
Bram Moolenaar860cae12010-06-05 23:22:07 +020012500 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012501 {
12502 /* Middle char is not a word char, skip the rotate. First and
12503 * third char were already checked at swap and swap3. */
12504 sp->ts_state = STATE_REP_INI;
12505 break;
12506 }
12507
12508 /* Rotate three characters left: "123" -> "231". We change
12509 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12510 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12511 {
12512 go_deeper(stack, depth, SCORE_SWAP3);
12513#ifdef DEBUG_TRIEWALK
12514 p = fword + sp->ts_fidx;
12515 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12516 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12517 p[0], p[1], p[2]);
12518#endif
12519 sp->ts_state = STATE_UNROT3L;
12520 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012521 p = fword + sp->ts_fidx;
12522#ifdef FEAT_MBYTE
12523 if (has_mbyte)
12524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012525 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012526 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012527 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012528 fl += mb_cptr2len(p + n + fl);
12529 mch_memmove(p, p + n, fl);
12530 mb_char2bytes(c, p + fl);
12531 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012532 }
12533 else
12534#endif
12535 {
12536 c = *p;
12537 *p = p[1];
12538 p[1] = p[2];
12539 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012540 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012541 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012542 }
12543 else
12544 sp->ts_state = STATE_REP_INI;
12545 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546
Bram Moolenaar4770d092006-01-12 23:22:24 +000012547 case STATE_UNROT3L:
12548 /* Undo ROT3L: "231" -> "123" */
12549 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012550#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012551 if (has_mbyte)
12552 {
12553 n = MB_BYTE2LEN(*p);
12554 n += MB_BYTE2LEN(p[n]);
12555 c = mb_ptr2char(p + n);
12556 tl = MB_BYTE2LEN(p[n]);
12557 mch_memmove(p + tl, p, n);
12558 mb_char2bytes(c, p);
12559 }
12560 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012561#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012562 {
12563 c = p[2];
12564 p[2] = p[1];
12565 p[1] = *p;
12566 *p = c;
12567 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568
Bram Moolenaar4770d092006-01-12 23:22:24 +000012569 /* Rotate three bytes right: "123" -> "312". We change "fword"
12570 * here, it's changed back afterwards at STATE_UNROT3R. */
12571 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12572 {
12573 go_deeper(stack, depth, SCORE_SWAP3);
12574#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012575 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012576 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12577 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12578 p[0], p[1], p[2]);
12579#endif
12580 sp->ts_state = STATE_UNROT3R;
12581 ++depth;
12582 p = fword + sp->ts_fidx;
12583#ifdef FEAT_MBYTE
12584 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012585 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012586 n = mb_cptr2len(p);
12587 n += mb_cptr2len(p + n);
12588 c = mb_ptr2char(p + n);
12589 tl = mb_cptr2len(p + n);
12590 mch_memmove(p + tl, p, n);
12591 mb_char2bytes(c, p);
12592 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012593 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012594 else
12595#endif
12596 {
12597 c = p[2];
12598 p[2] = p[1];
12599 p[1] = *p;
12600 *p = c;
12601 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12602 }
12603 }
12604 else
12605 sp->ts_state = STATE_REP_INI;
12606 break;
12607
12608 case STATE_UNROT3R:
12609 /* Undo ROT3R: "312" -> "123" */
12610 p = fword + sp->ts_fidx;
12611#ifdef FEAT_MBYTE
12612 if (has_mbyte)
12613 {
12614 c = mb_ptr2char(p);
12615 tl = MB_BYTE2LEN(*p);
12616 n = MB_BYTE2LEN(p[tl]);
12617 n += MB_BYTE2LEN(p[tl + n]);
12618 mch_memmove(p, p + tl, n);
12619 mb_char2bytes(c, p + n);
12620 }
12621 else
12622#endif
12623 {
12624 c = *p;
12625 *p = p[1];
12626 p[1] = p[2];
12627 p[2] = c;
12628 }
12629 /*FALLTHROUGH*/
12630
12631 case STATE_REP_INI:
12632 /* Check if matching with REP items from the .aff file would work.
12633 * Quickly skip if:
12634 * - there are no REP items and we are not in the soundfold trie
12635 * - the score is going to be too high anyway
12636 * - already applied a REP item or swapped here */
12637 if ((lp->lp_replang == NULL && !soundfold)
12638 || sp->ts_score + SCORE_REP >= su->su_maxscore
12639 || sp->ts_fidx < sp->ts_fidxtry)
12640 {
12641 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012643 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644
Bram Moolenaar4770d092006-01-12 23:22:24 +000012645 /* Use the first byte to quickly find the first entry that may
12646 * match. If the index is -1 there is none. */
12647 if (soundfold)
12648 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12649 else
12650 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012651
Bram Moolenaar4770d092006-01-12 23:22:24 +000012652 if (sp->ts_curi < 0)
12653 {
12654 sp->ts_state = STATE_FINAL;
12655 break;
12656 }
12657
12658 sp->ts_state = STATE_REP;
12659 /*FALLTHROUGH*/
12660
12661 case STATE_REP:
12662 /* Try matching with REP items from the .aff file. For each match
12663 * replace the characters and check if the resulting word is
12664 * valid. */
12665 p = fword + sp->ts_fidx;
12666
12667 if (soundfold)
12668 gap = &slang->sl_repsal;
12669 else
12670 gap = &lp->lp_replang->sl_rep;
12671 while (sp->ts_curi < gap->ga_len)
12672 {
12673 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12674 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012675 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012676 /* past possible matching entries */
12677 sp->ts_curi = gap->ga_len;
12678 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012679 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012680 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12681 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12682 {
12683 go_deeper(stack, depth, SCORE_REP);
12684#ifdef DEBUG_TRIEWALK
12685 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12686 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12687 ftp->ft_from, ftp->ft_to);
12688#endif
12689 /* Need to undo this afterwards. */
12690 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012691
Bram Moolenaar4770d092006-01-12 23:22:24 +000012692 /* Change the "from" to the "to" string. */
12693 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012694 fl = (int)STRLEN(ftp->ft_from);
12695 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012696 if (fl != tl)
12697 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012698 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012699 repextra += tl - fl;
12700 }
12701 mch_memmove(p, ftp->ft_to, tl);
12702 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12703#ifdef FEAT_MBYTE
12704 stack[depth].ts_tcharlen = 0;
12705#endif
12706 break;
12707 }
12708 }
12709
12710 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12711 /* No (more) matches. */
12712 sp->ts_state = STATE_FINAL;
12713
12714 break;
12715
12716 case STATE_REP_UNDO:
12717 /* Undo a REP replacement and continue with the next one. */
12718 if (soundfold)
12719 gap = &slang->sl_repsal;
12720 else
12721 gap = &lp->lp_replang->sl_rep;
12722 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012723 fl = (int)STRLEN(ftp->ft_from);
12724 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012725 p = fword + sp->ts_fidx;
12726 if (fl != tl)
12727 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012728 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012729 repextra -= tl - fl;
12730 }
12731 mch_memmove(p, ftp->ft_from, fl);
12732 sp->ts_state = STATE_REP;
12733 break;
12734
12735 default:
12736 /* Did all possible states at this level, go up one level. */
12737 --depth;
12738
12739 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12740 {
12741 /* Continue in or go back to the prefix tree. */
12742 byts = pbyts;
12743 idxs = pidxs;
12744 }
12745
12746 /* Don't check for CTRL-C too often, it takes time. */
12747 if (--breakcheckcount == 0)
12748 {
12749 ui_breakcheck();
12750 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012751 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 }
12753 }
12754}
12755
Bram Moolenaar4770d092006-01-12 23:22:24 +000012756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012757/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012758 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012760 static void
12761go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012762 trystate_T *stack;
12763 int depth;
12764 int score_add;
12765{
Bram Moolenaarea424162005-06-16 21:51:00 +000012766 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012768 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012769 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012770 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771}
12772
Bram Moolenaar53805d12005-08-01 07:08:33 +000012773#ifdef FEAT_MBYTE
12774/*
12775 * Case-folding may change the number of bytes: Count nr of chars in
12776 * fword[flen] and return the byte length of that many chars in "word".
12777 */
12778 static int
12779nofold_len(fword, flen, word)
12780 char_u *fword;
12781 int flen;
12782 char_u *word;
12783{
12784 char_u *p;
12785 int i = 0;
12786
12787 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12788 ++i;
12789 for (p = word; i > 0; mb_ptr_adv(p))
12790 --i;
12791 return (int)(p - word);
12792}
12793#endif
12794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795/*
12796 * "fword" is a good word with case folded. Find the matching keep-case
12797 * words and put it in "kword".
12798 * Theoretically there could be several keep-case words that result in the
12799 * same case-folded word, but we only find one...
12800 */
12801 static void
12802find_keepcap_word(slang, fword, kword)
12803 slang_T *slang;
12804 char_u *fword;
12805 char_u *kword;
12806{
12807 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12808 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012809 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012810
12811 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012812 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813 int round[MAXWLEN];
12814 int fwordidx[MAXWLEN];
12815 int uwordidx[MAXWLEN];
12816 int kwordlen[MAXWLEN];
12817
12818 int flen, ulen;
12819 int l;
12820 int len;
12821 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012822 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823 char_u *p;
12824 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012825 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826
12827 if (byts == NULL)
12828 {
12829 /* array is empty: "cannot happen" */
12830 *kword = NUL;
12831 return;
12832 }
12833
12834 /* Make an all-cap version of "fword". */
12835 allcap_copy(fword, uword);
12836
12837 /*
12838 * Each character needs to be tried both case-folded and upper-case.
12839 * All this gets very complicated if we keep in mind that changing case
12840 * may change the byte length of a multi-byte character...
12841 */
12842 depth = 0;
12843 arridx[0] = 0;
12844 round[0] = 0;
12845 fwordidx[0] = 0;
12846 uwordidx[0] = 0;
12847 kwordlen[0] = 0;
12848 while (depth >= 0)
12849 {
12850 if (fword[fwordidx[depth]] == NUL)
12851 {
12852 /* We are at the end of "fword". If the tree allows a word to end
12853 * here we have found a match. */
12854 if (byts[arridx[depth] + 1] == 0)
12855 {
12856 kword[kwordlen[depth]] = NUL;
12857 return;
12858 }
12859
12860 /* kword is getting too long, continue one level up */
12861 --depth;
12862 }
12863 else if (++round[depth] > 2)
12864 {
12865 /* tried both fold-case and upper-case character, continue one
12866 * level up */
12867 --depth;
12868 }
12869 else
12870 {
12871 /*
12872 * round[depth] == 1: Try using the folded-case character.
12873 * round[depth] == 2: Try using the upper-case character.
12874 */
12875#ifdef FEAT_MBYTE
12876 if (has_mbyte)
12877 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012878 flen = mb_cptr2len(fword + fwordidx[depth]);
12879 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012880 }
12881 else
12882#endif
12883 ulen = flen = 1;
12884 if (round[depth] == 1)
12885 {
12886 p = fword + fwordidx[depth];
12887 l = flen;
12888 }
12889 else
12890 {
12891 p = uword + uwordidx[depth];
12892 l = ulen;
12893 }
12894
12895 for (tryidx = arridx[depth]; l > 0; --l)
12896 {
12897 /* Perform a binary search in the list of accepted bytes. */
12898 len = byts[tryidx++];
12899 c = *p++;
12900 lo = tryidx;
12901 hi = tryidx + len - 1;
12902 while (lo < hi)
12903 {
12904 m = (lo + hi) / 2;
12905 if (byts[m] > c)
12906 hi = m - 1;
12907 else if (byts[m] < c)
12908 lo = m + 1;
12909 else
12910 {
12911 lo = hi = m;
12912 break;
12913 }
12914 }
12915
12916 /* Stop if there is no matching byte. */
12917 if (hi < lo || byts[lo] != c)
12918 break;
12919
12920 /* Continue at the child (if there is one). */
12921 tryidx = idxs[lo];
12922 }
12923
12924 if (l == 0)
12925 {
12926 /*
12927 * Found the matching char. Copy it to "kword" and go a
12928 * level deeper.
12929 */
12930 if (round[depth] == 1)
12931 {
12932 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12933 flen);
12934 kwordlen[depth + 1] = kwordlen[depth] + flen;
12935 }
12936 else
12937 {
12938 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12939 ulen);
12940 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12941 }
12942 fwordidx[depth + 1] = fwordidx[depth] + flen;
12943 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12944
12945 ++depth;
12946 arridx[depth] = tryidx;
12947 round[depth] = 0;
12948 }
12949 }
12950 }
12951
12952 /* Didn't find it: "cannot happen". */
12953 *kword = NUL;
12954}
12955
12956/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012957 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12958 * su->su_sga.
12959 */
12960 static void
12961score_comp_sal(su)
12962 suginfo_T *su;
12963{
12964 langp_T *lp;
12965 char_u badsound[MAXWLEN];
12966 int i;
12967 suggest_T *stp;
12968 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012969 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012970 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012971
12972 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12973 return;
12974
12975 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012976 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012977 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012978 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012979 if (lp->lp_slang->sl_sal.ga_len > 0)
12980 {
12981 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012982 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012983
12984 for (i = 0; i < su->su_ga.ga_len; ++i)
12985 {
12986 stp = &SUG(su->su_ga, i);
12987
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012988 /* Case-fold the suggested word, sound-fold it and compute the
12989 * sound-a-like score. */
12990 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012991 if (score < SCORE_MAXMAX)
12992 {
12993 /* Add the suggestion. */
12994 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12995 sstp->st_word = vim_strsave(stp->st_word);
12996 if (sstp->st_word != NULL)
12997 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012998 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012999 sstp->st_score = score;
13000 sstp->st_altscore = 0;
13001 sstp->st_orglen = stp->st_orglen;
13002 ++su->su_sga.ga_len;
13003 }
13004 }
13005 }
13006 break;
13007 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013008 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013009}
13010
13011/*
13012 * Combine the list of suggestions in su->su_ga and su->su_sga.
13013 * They are intwined.
13014 */
13015 static void
13016score_combine(su)
13017 suginfo_T *su;
13018{
13019 int i;
13020 int j;
13021 garray_T ga;
13022 garray_T *gap;
13023 langp_T *lp;
13024 suggest_T *stp;
13025 char_u *p;
13026 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013027 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013028 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013029 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013030
13031 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013032 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013033 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013034 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013035 if (lp->lp_slang->sl_sal.ga_len > 0)
13036 {
13037 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013038 slang = lp->lp_slang;
13039 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013040
13041 for (i = 0; i < su->su_ga.ga_len; ++i)
13042 {
13043 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013044 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013045 if (stp->st_altscore == SCORE_MAXMAX)
13046 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13047 else
13048 stp->st_score = (stp->st_score * 3
13049 + stp->st_altscore) / 4;
13050 stp->st_salscore = FALSE;
13051 }
13052 break;
13053 }
13054 }
13055
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013056 if (slang == NULL) /* Using "double" without sound folding. */
13057 {
13058 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13059 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013060 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013061 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013062
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013063 /* Add the alternate score to su_sga. */
13064 for (i = 0; i < su->su_sga.ga_len; ++i)
13065 {
13066 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013067 stp->st_altscore = spell_edit_score(slang,
13068 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013069 if (stp->st_score == SCORE_MAXMAX)
13070 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13071 else
13072 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13073 stp->st_salscore = TRUE;
13074 }
13075
Bram Moolenaar4770d092006-01-12 23:22:24 +000013076 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13077 * for both lists. */
13078 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013079 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013080 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013081 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13082
13083 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13084 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13085 return;
13086
13087 stp = &SUG(ga, 0);
13088 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13089 {
13090 /* round 1: get a suggestion from su_ga
13091 * round 2: get a suggestion from su_sga */
13092 for (round = 1; round <= 2; ++round)
13093 {
13094 gap = round == 1 ? &su->su_ga : &su->su_sga;
13095 if (i < gap->ga_len)
13096 {
13097 /* Don't add a word if it's already there. */
13098 p = SUG(*gap, i).st_word;
13099 for (j = 0; j < ga.ga_len; ++j)
13100 if (STRCMP(stp[j].st_word, p) == 0)
13101 break;
13102 if (j == ga.ga_len)
13103 stp[ga.ga_len++] = SUG(*gap, i);
13104 else
13105 vim_free(p);
13106 }
13107 }
13108 }
13109
13110 ga_clear(&su->su_ga);
13111 ga_clear(&su->su_sga);
13112
13113 /* Truncate the list to the number of suggestions that will be displayed. */
13114 if (ga.ga_len > su->su_maxcount)
13115 {
13116 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13117 vim_free(stp[i].st_word);
13118 ga.ga_len = su->su_maxcount;
13119 }
13120
13121 su->su_ga = ga;
13122}
13123
13124/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013125 * For the goodword in "stp" compute the soundalike score compared to the
13126 * badword.
13127 */
13128 static int
13129stp_sal_score(stp, su, slang, badsound)
13130 suggest_T *stp;
13131 suginfo_T *su;
13132 slang_T *slang;
13133 char_u *badsound; /* sound-folded badword */
13134{
13135 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013136 char_u *pbad;
13137 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013138 char_u badsound2[MAXWLEN];
13139 char_u fword[MAXWLEN];
13140 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013141 char_u goodword[MAXWLEN];
13142 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013143
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013144 lendiff = (int)(su->su_badlen - stp->st_orglen);
13145 if (lendiff >= 0)
13146 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013147 else
13148 {
13149 /* soundfold the bad word with more characters following */
13150 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13151
13152 /* When joining two words the sound often changes a lot. E.g., "t he"
13153 * sounds like "t h" while "the" sounds like "@". Avoid that by
13154 * removing the space. Don't do it when the good word also contains a
13155 * space. */
13156 if (vim_iswhite(su->su_badptr[su->su_badlen])
13157 && *skiptowhite(stp->st_word) == NUL)
13158 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013159 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013160
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013161 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013162 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013163 }
13164
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013165 if (lendiff > 0)
13166 {
13167 /* Add part of the bad word to the good word, so that we soundfold
13168 * what replaces the bad word. */
13169 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013170 vim_strncpy(goodword + stp->st_wordlen,
13171 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013172 pgood = goodword;
13173 }
13174 else
13175 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013176
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013177 /* Sound-fold the word and compute the score for the difference. */
13178 spell_soundfold(slang, pgood, FALSE, goodsound);
13179
13180 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013181}
13182
Bram Moolenaar4770d092006-01-12 23:22:24 +000013183/* structure used to store soundfolded words that add_sound_suggest() has
13184 * handled already. */
13185typedef struct
13186{
13187 short sft_score; /* lowest score used */
13188 char_u sft_word[1]; /* soundfolded word, actually longer */
13189} sftword_T;
13190
13191static sftword_T dumsft;
13192#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13193#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13194
13195/*
13196 * Prepare for calling suggest_try_soundalike().
13197 */
13198 static void
13199suggest_try_soundalike_prep()
13200{
13201 langp_T *lp;
13202 int lpi;
13203 slang_T *slang;
13204
13205 /* Do this for all languages that support sound folding and for which a
13206 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013207 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013208 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013209 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013210 slang = lp->lp_slang;
13211 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13212 /* prepare the hashtable used by add_sound_suggest() */
13213 hash_init(&slang->sl_sounddone);
13214 }
13215}
13216
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013217/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013219 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 */
13221 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013222suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 suginfo_T *su;
13224{
13225 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013226 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013227 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013228 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229
Bram Moolenaar4770d092006-01-12 23:22:24 +000013230 /* Do this for all languages that support sound folding and for which a
13231 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013232 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013234 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013235 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013236 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013237 {
13238 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013239 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240
Bram Moolenaar4770d092006-01-12 23:22:24 +000013241 /* try all kinds of inserts/deletes/swaps/etc. */
13242 /* TODO: also soundfold the next words, so that we can try joining
13243 * and splitting */
13244 suggest_trie_walk(su, lp, salword, TRUE);
13245 }
13246 }
13247}
13248
13249/*
13250 * Finish up after calling suggest_try_soundalike().
13251 */
13252 static void
13253suggest_try_soundalike_finish()
13254{
13255 langp_T *lp;
13256 int lpi;
13257 slang_T *slang;
13258 int todo;
13259 hashitem_T *hi;
13260
13261 /* Do this for all languages that support sound folding and for which a
13262 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013263 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013264 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013265 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013266 slang = lp->lp_slang;
13267 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13268 {
13269 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013270 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013271 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13272 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013273 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013274 vim_free(HI2SFT(hi));
13275 --todo;
13276 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013277
13278 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013279 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013280 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013281 }
13282 }
13283}
13284
13285/*
13286 * A match with a soundfolded word is found. Add the good word(s) that
13287 * produce this soundfolded word.
13288 */
13289 static void
13290add_sound_suggest(su, goodword, score, lp)
13291 suginfo_T *su;
13292 char_u *goodword;
13293 int score; /* soundfold score */
13294 langp_T *lp;
13295{
13296 slang_T *slang = lp->lp_slang; /* language for sound folding */
13297 int sfwordnr;
13298 char_u *nrline;
13299 int orgnr;
13300 char_u theword[MAXWLEN];
13301 int i;
13302 int wlen;
13303 char_u *byts;
13304 idx_T *idxs;
13305 int n;
13306 int wordcount;
13307 int wc;
13308 int goodscore;
13309 hash_T hash;
13310 hashitem_T *hi;
13311 sftword_T *sft;
13312 int bc, gc;
13313 int limit;
13314
13315 /*
13316 * It's very well possible that the same soundfold word is found several
13317 * times with different scores. Since the following is quite slow only do
13318 * the words that have a better score than before. Use a hashtable to
13319 * remember the words that have been done.
13320 */
13321 hash = hash_hash(goodword);
13322 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13323 if (HASHITEM_EMPTY(hi))
13324 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013325 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13326 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013327 if (sft != NULL)
13328 {
13329 sft->sft_score = score;
13330 STRCPY(sft->sft_word, goodword);
13331 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13332 }
13333 }
13334 else
13335 {
13336 sft = HI2SFT(hi);
13337 if (score >= sft->sft_score)
13338 return;
13339 sft->sft_score = score;
13340 }
13341
13342 /*
13343 * Find the word nr in the soundfold tree.
13344 */
13345 sfwordnr = soundfold_find(slang, goodword);
13346 if (sfwordnr < 0)
13347 {
13348 EMSG2(_(e_intern2), "add_sound_suggest()");
13349 return;
13350 }
13351
13352 /*
13353 * go over the list of good words that produce this soundfold word
13354 */
13355 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13356 orgnr = 0;
13357 while (*nrline != NUL)
13358 {
13359 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13360 * previous wordnr. */
13361 orgnr += bytes2offset(&nrline);
13362
13363 byts = slang->sl_fbyts;
13364 idxs = slang->sl_fidxs;
13365
13366 /* Lookup the word "orgnr" one of the two tries. */
13367 n = 0;
13368 wlen = 0;
13369 wordcount = 0;
13370 for (;;)
13371 {
13372 i = 1;
13373 if (wordcount == orgnr && byts[n + 1] == NUL)
13374 break; /* found end of word */
13375
13376 if (byts[n + 1] == NUL)
13377 ++wordcount;
13378
13379 /* skip over the NUL bytes */
13380 for ( ; byts[n + i] == NUL; ++i)
13381 if (i > byts[n]) /* safety check */
13382 {
13383 STRCPY(theword + wlen, "BAD");
13384 goto badword;
13385 }
13386
13387 /* One of the siblings must have the word. */
13388 for ( ; i < byts[n]; ++i)
13389 {
13390 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13391 if (wordcount + wc > orgnr)
13392 break;
13393 wordcount += wc;
13394 }
13395
13396 theword[wlen++] = byts[n + i];
13397 n = idxs[n + i];
13398 }
13399badword:
13400 theword[wlen] = NUL;
13401
13402 /* Go over the possible flags and regions. */
13403 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13404 {
13405 char_u cword[MAXWLEN];
13406 char_u *p;
13407 int flags = (int)idxs[n + i];
13408
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013409 /* Skip words with the NOSUGGEST flag */
13410 if (flags & WF_NOSUGGEST)
13411 continue;
13412
Bram Moolenaar4770d092006-01-12 23:22:24 +000013413 if (flags & WF_KEEPCAP)
13414 {
13415 /* Must find the word in the keep-case tree. */
13416 find_keepcap_word(slang, theword, cword);
13417 p = cword;
13418 }
13419 else
13420 {
13421 flags |= su->su_badflags;
13422 if ((flags & WF_CAPMASK) != 0)
13423 {
13424 /* Need to fix case according to "flags". */
13425 make_case_word(theword, cword, flags);
13426 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 }
13428 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013429 p = theword;
13430 }
13431
13432 /* Add the suggestion. */
13433 if (sps_flags & SPS_DOUBLE)
13434 {
13435 /* Add the suggestion if the score isn't too bad. */
13436 if (score <= su->su_maxscore)
13437 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13438 score, 0, FALSE, slang, FALSE);
13439 }
13440 else
13441 {
13442 /* Add a penalty for words in another region. */
13443 if ((flags & WF_REGION)
13444 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13445 goodscore = SCORE_REGION;
13446 else
13447 goodscore = 0;
13448
13449 /* Add a small penalty for changing the first letter from
13450 * lower to upper case. Helps for "tath" -> "Kath", which is
13451 * less common thatn "tath" -> "path". Don't do it when the
13452 * letter is the same, that has already been counted. */
13453 gc = PTR2CHAR(p);
13454 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013456 bc = PTR2CHAR(su->su_badword);
13457 if (!SPELL_ISUPPER(bc)
13458 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13459 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013460 }
13461
Bram Moolenaar4770d092006-01-12 23:22:24 +000013462 /* Compute the score for the good word. This only does letter
13463 * insert/delete/swap/replace. REP items are not considered,
13464 * which may make the score a bit higher.
13465 * Use a limit for the score to make it work faster. Use
13466 * MAXSCORE(), because RESCORE() will change the score.
13467 * If the limit is very high then the iterative method is
13468 * inefficient, using an array is quicker. */
13469 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13470 if (limit > SCORE_LIMITMAX)
13471 goodscore += spell_edit_score(slang, su->su_badword, p);
13472 else
13473 goodscore += spell_edit_score_limit(slang, su->su_badword,
13474 p, limit);
13475
13476 /* When going over the limit don't bother to do the rest. */
13477 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013479 /* Give a bonus to words seen before. */
13480 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013481
Bram Moolenaar4770d092006-01-12 23:22:24 +000013482 /* Add the suggestion if the score isn't too bad. */
13483 goodscore = RESCORE(goodscore, score);
13484 if (goodscore <= su->su_sfmaxscore)
13485 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13486 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013488 }
13489 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013490 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491 }
13492}
13493
13494/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013495 * Find word "word" in fold-case tree for "slang" and return the word number.
13496 */
13497 static int
13498soundfold_find(slang, word)
13499 slang_T *slang;
13500 char_u *word;
13501{
13502 idx_T arridx = 0;
13503 int len;
13504 int wlen = 0;
13505 int c;
13506 char_u *ptr = word;
13507 char_u *byts;
13508 idx_T *idxs;
13509 int wordnr = 0;
13510
13511 byts = slang->sl_sbyts;
13512 idxs = slang->sl_sidxs;
13513
13514 for (;;)
13515 {
13516 /* First byte is the number of possible bytes. */
13517 len = byts[arridx++];
13518
13519 /* If the first possible byte is a zero the word could end here.
13520 * If the word ends we found the word. If not skip the NUL bytes. */
13521 c = ptr[wlen];
13522 if (byts[arridx] == NUL)
13523 {
13524 if (c == NUL)
13525 break;
13526
13527 /* Skip over the zeros, there can be several. */
13528 while (len > 0 && byts[arridx] == NUL)
13529 {
13530 ++arridx;
13531 --len;
13532 }
13533 if (len == 0)
13534 return -1; /* no children, word should have ended here */
13535 ++wordnr;
13536 }
13537
13538 /* If the word ends we didn't find it. */
13539 if (c == NUL)
13540 return -1;
13541
13542 /* Perform a binary search in the list of accepted bytes. */
13543 if (c == TAB) /* <Tab> is handled like <Space> */
13544 c = ' ';
13545 while (byts[arridx] < c)
13546 {
13547 /* The word count is in the first idxs[] entry of the child. */
13548 wordnr += idxs[idxs[arridx]];
13549 ++arridx;
13550 if (--len == 0) /* end of the bytes, didn't find it */
13551 return -1;
13552 }
13553 if (byts[arridx] != c) /* didn't find the byte */
13554 return -1;
13555
13556 /* Continue at the child (if there is one). */
13557 arridx = idxs[arridx];
13558 ++wlen;
13559
13560 /* One space in the good word may stand for several spaces in the
13561 * checked word. */
13562 if (c == ' ')
13563 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13564 ++wlen;
13565 }
13566
13567 return wordnr;
13568}
13569
13570/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013571 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 */
13573 static void
13574make_case_word(fword, cword, flags)
13575 char_u *fword;
13576 char_u *cword;
13577 int flags;
13578{
13579 if (flags & WF_ALLCAP)
13580 /* Make it all upper-case */
13581 allcap_copy(fword, cword);
13582 else if (flags & WF_ONECAP)
13583 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013584 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013585 else
13586 /* Use goodword as-is. */
13587 STRCPY(cword, fword);
13588}
13589
Bram Moolenaarea424162005-06-16 21:51:00 +000013590/*
13591 * Use map string "map" for languages "lp".
13592 */
13593 static void
13594set_map_str(lp, map)
13595 slang_T *lp;
13596 char_u *map;
13597{
13598 char_u *p;
13599 int headc = 0;
13600 int c;
13601 int i;
13602
13603 if (*map == NUL)
13604 {
13605 lp->sl_has_map = FALSE;
13606 return;
13607 }
13608 lp->sl_has_map = TRUE;
13609
Bram Moolenaar4770d092006-01-12 23:22:24 +000013610 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013611 for (i = 0; i < 256; ++i)
13612 lp->sl_map_array[i] = 0;
13613#ifdef FEAT_MBYTE
13614 hash_init(&lp->sl_map_hash);
13615#endif
13616
13617 /*
13618 * The similar characters are stored separated with slashes:
13619 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13620 * before the same slash. For characters above 255 sl_map_hash is used.
13621 */
13622 for (p = map; *p != NUL; )
13623 {
13624#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013625 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013626#else
13627 c = *p++;
13628#endif
13629 if (c == '/')
13630 headc = 0;
13631 else
13632 {
13633 if (headc == 0)
13634 headc = c;
13635
13636#ifdef FEAT_MBYTE
13637 /* Characters above 255 don't fit in sl_map_array[], put them in
13638 * the hash table. Each entry is the char, a NUL the headchar and
13639 * a NUL. */
13640 if (c >= 256)
13641 {
13642 int cl = mb_char2len(c);
13643 int headcl = mb_char2len(headc);
13644 char_u *b;
13645 hash_T hash;
13646 hashitem_T *hi;
13647
13648 b = alloc((unsigned)(cl + headcl + 2));
13649 if (b == NULL)
13650 return;
13651 mb_char2bytes(c, b);
13652 b[cl] = NUL;
13653 mb_char2bytes(headc, b + cl + 1);
13654 b[cl + 1 + headcl] = NUL;
13655 hash = hash_hash(b);
13656 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13657 if (HASHITEM_EMPTY(hi))
13658 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13659 else
13660 {
13661 /* This should have been checked when generating the .spl
13662 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013663 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013664 vim_free(b);
13665 }
13666 }
13667 else
13668#endif
13669 lp->sl_map_array[c] = headc;
13670 }
13671 }
13672}
13673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674/*
13675 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13676 * lines in the .aff file.
13677 */
13678 static int
13679similar_chars(slang, c1, c2)
13680 slang_T *slang;
13681 int c1;
13682 int c2;
13683{
Bram Moolenaarea424162005-06-16 21:51:00 +000013684 int m1, m2;
13685#ifdef FEAT_MBYTE
13686 char_u buf[MB_MAXBYTES];
13687 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688
Bram Moolenaarea424162005-06-16 21:51:00 +000013689 if (c1 >= 256)
13690 {
13691 buf[mb_char2bytes(c1, buf)] = 0;
13692 hi = hash_find(&slang->sl_map_hash, buf);
13693 if (HASHITEM_EMPTY(hi))
13694 m1 = 0;
13695 else
13696 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13697 }
13698 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013699#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013700 m1 = slang->sl_map_array[c1];
13701 if (m1 == 0)
13702 return FALSE;
13703
13704
13705#ifdef FEAT_MBYTE
13706 if (c2 >= 256)
13707 {
13708 buf[mb_char2bytes(c2, buf)] = 0;
13709 hi = hash_find(&slang->sl_map_hash, buf);
13710 if (HASHITEM_EMPTY(hi))
13711 m2 = 0;
13712 else
13713 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13714 }
13715 else
13716#endif
13717 m2 = slang->sl_map_array[c2];
13718
13719 return m1 == m2;
13720}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721
13722/*
13723 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013724 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 */
13726 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013727add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13728 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013730 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013731 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013732 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013733 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013734 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013735 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013736 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013737 int maxsf; /* su_maxscore applies to soundfold score,
13738 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013740 int goodlen; /* len of goodword changed */
13741 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013743 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013745 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013747 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13748 * "thee the" is added next to changing the first "the" the "thee". */
13749 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013750 pbad = su->su_badptr + badlenarg;
13751 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013752 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013753 goodlen = (int)(pgood - goodword);
13754 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013755 if (goodlen <= 0 || badlen <= 0)
13756 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013757 mb_ptr_back(goodword, pgood);
13758 mb_ptr_back(su->su_badptr, pbad);
13759#ifdef FEAT_MBYTE
13760 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013761 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013762 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13763 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013764 }
13765 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013766#endif
13767 if (*pgood != *pbad)
13768 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013769 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013770
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013771 if (badlen == 0 && goodlen == 0)
13772 /* goodword doesn't change anything; may happen for "the the" changing
13773 * the first "the" to itself. */
13774 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013775
Bram Moolenaar89d40322006-08-29 15:30:07 +000013776 if (gap->ga_len == 0)
13777 i = -1;
13778 else
13779 {
13780 /* Check if the word is already there. Also check the length that is
13781 * being replaced "thes," -> "these" is a different suggestion from
13782 * "thes" -> "these". */
13783 stp = &SUG(*gap, 0);
13784 for (i = gap->ga_len; --i >= 0; ++stp)
13785 if (stp->st_wordlen == goodlen
13786 && stp->st_orglen == badlen
13787 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013789 /*
13790 * Found it. Remember the word with the lowest score.
13791 */
13792 if (stp->st_slang == NULL)
13793 stp->st_slang = slang;
13794
13795 new_sug.st_score = score;
13796 new_sug.st_altscore = altscore;
13797 new_sug.st_had_bonus = had_bonus;
13798
13799 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013800 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013801 /* Only one of the two had the soundalike score computed.
13802 * Need to do that for the other one now, otherwise the
13803 * scores can't be compared. This happens because
13804 * suggest_try_change() doesn't compute the soundalike
13805 * word to keep it fast, while some special methods set
13806 * the soundalike score to zero. */
13807 if (had_bonus)
13808 rescore_one(su, stp);
13809 else
13810 {
13811 new_sug.st_word = stp->st_word;
13812 new_sug.st_wordlen = stp->st_wordlen;
13813 new_sug.st_slang = stp->st_slang;
13814 new_sug.st_orglen = badlen;
13815 rescore_one(su, &new_sug);
13816 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818
Bram Moolenaar89d40322006-08-29 15:30:07 +000013819 if (stp->st_score > new_sug.st_score)
13820 {
13821 stp->st_score = new_sug.st_score;
13822 stp->st_altscore = new_sug.st_altscore;
13823 stp->st_had_bonus = new_sug.st_had_bonus;
13824 }
13825 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013826 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013827 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013828
Bram Moolenaar4770d092006-01-12 23:22:24 +000013829 if (i < 0 && ga_grow(gap, 1) == OK)
13830 {
13831 /* Add a suggestion. */
13832 stp = &SUG(*gap, gap->ga_len);
13833 stp->st_word = vim_strnsave(goodword, goodlen);
13834 if (stp->st_word != NULL)
13835 {
13836 stp->st_wordlen = goodlen;
13837 stp->st_score = score;
13838 stp->st_altscore = altscore;
13839 stp->st_had_bonus = had_bonus;
13840 stp->st_orglen = badlen;
13841 stp->st_slang = slang;
13842 ++gap->ga_len;
13843
13844 /* If we have too many suggestions now, sort the list and keep
13845 * the best suggestions. */
13846 if (gap->ga_len > SUG_MAX_COUNT(su))
13847 {
13848 if (maxsf)
13849 su->su_sfmaxscore = cleanup_suggestions(gap,
13850 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13851 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013852 su->su_maxscore = cleanup_suggestions(gap,
13853 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013854 }
13855 }
13856 }
13857}
13858
13859/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013860 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13861 * for split words, such as "the the". Remove these from the list here.
13862 */
13863 static void
13864check_suggestions(su, gap)
13865 suginfo_T *su;
13866 garray_T *gap; /* either su_ga or su_sga */
13867{
13868 suggest_T *stp;
13869 int i;
13870 char_u longword[MAXWLEN + 1];
13871 int len;
13872 hlf_T attr;
13873
13874 stp = &SUG(*gap, 0);
13875 for (i = gap->ga_len - 1; i >= 0; --i)
13876 {
13877 /* Need to append what follows to check for "the the". */
13878 STRCPY(longword, stp[i].st_word);
13879 len = stp[i].st_wordlen;
13880 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13881 MAXWLEN - len);
13882 attr = HLF_COUNT;
13883 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13884 if (attr != HLF_COUNT)
13885 {
13886 /* Remove this entry. */
13887 vim_free(stp[i].st_word);
13888 --gap->ga_len;
13889 if (i < gap->ga_len)
13890 mch_memmove(stp + i, stp + i + 1,
13891 sizeof(suggest_T) * (gap->ga_len - i));
13892 }
13893 }
13894}
13895
13896
13897/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 * Add a word to be banned.
13899 */
13900 static void
13901add_banned(su, word)
13902 suginfo_T *su;
13903 char_u *word;
13904{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013905 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906 hash_T hash;
13907 hashitem_T *hi;
13908
Bram Moolenaar4770d092006-01-12 23:22:24 +000013909 hash = hash_hash(word);
13910 hi = hash_lookup(&su->su_banned, word, hash);
13911 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013913 s = vim_strsave(word);
13914 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 hash_add_item(&su->su_banned, hi, s, hash);
13916 }
13917}
13918
13919/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013920 * Recompute the score for all suggestions if sound-folding is possible. This
13921 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013922 */
13923 static void
13924rescore_suggestions(su)
13925 suginfo_T *su;
13926{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013927 int i;
13928
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013929 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013930 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013931 rescore_one(su, &SUG(su->su_ga, i));
13932}
13933
13934/*
13935 * Recompute the score for one suggestion if sound-folding is possible.
13936 */
13937 static void
13938rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013939 suginfo_T *su;
13940 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013941{
13942 slang_T *slang = stp->st_slang;
13943 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013944 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013945
13946 /* Only rescore suggestions that have no sal score yet and do have a
13947 * language. */
13948 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13949 {
13950 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013951 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013952 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013953 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013954 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013955 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013956 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013957
13958 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013959 if (stp->st_altscore == SCORE_MAXMAX)
13960 stp->st_altscore = SCORE_BIG;
13961 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13962 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013963 }
13964}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966static int
13967#ifdef __BORLANDC__
13968_RTLENTRYF
13969#endif
13970sug_compare __ARGS((const void *s1, const void *s2));
13971
13972/*
13973 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013974 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 */
13976 static int
13977#ifdef __BORLANDC__
13978_RTLENTRYF
13979#endif
13980sug_compare(s1, s2)
13981 const void *s1;
13982 const void *s2;
13983{
13984 suggest_T *p1 = (suggest_T *)s1;
13985 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013986 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013988 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013989 {
13990 n = p1->st_altscore - p2->st_altscore;
13991 if (n == 0)
13992 n = STRICMP(p1->st_word, p2->st_word);
13993 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013994 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995}
13996
13997/*
13998 * Cleanup the suggestions:
13999 * - Sort on score.
14000 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014001 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014003 static int
14004cleanup_suggestions(gap, maxscore, keep)
14005 garray_T *gap;
14006 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014007 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014009 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 int i;
14011
14012 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014013 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014
14015 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014016 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014017 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014018 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014019 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014020 gap->ga_len = keep;
14021 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014023 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024}
14025
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014026#if defined(FEAT_EVAL) || defined(PROTO)
14027/*
14028 * Soundfold a string, for soundfold().
14029 * Result is in allocated memory, NULL for an error.
14030 */
14031 char_u *
14032eval_soundfold(word)
14033 char_u *word;
14034{
14035 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014036 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014037 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014038
Bram Moolenaar860cae12010-06-05 23:22:07 +020014039 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014040 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014041 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014042 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014043 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014044 if (lp->lp_slang->sl_sal.ga_len > 0)
14045 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014046 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014047 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014048 return vim_strsave(sound);
14049 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014050 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014051
14052 /* No language with sound folding, return word as-is. */
14053 return vim_strsave(word);
14054}
14055#endif
14056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057/*
14058 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014059 *
14060 * There are many ways to turn a word into a sound-a-like representation. The
14061 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14062 * swedish name matching - survey and test of different algorithms" by Klas
14063 * Erikson.
14064 *
14065 * We support two methods:
14066 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14067 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 */
14069 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014070spell_soundfold(slang, inword, folded, res)
14071 slang_T *slang;
14072 char_u *inword;
14073 int folded; /* "inword" is already case-folded */
14074 char_u *res;
14075{
14076 char_u fword[MAXWLEN];
14077 char_u *word;
14078
14079 if (slang->sl_sofo)
14080 /* SOFOFROM and SOFOTO used */
14081 spell_soundfold_sofo(slang, inword, res);
14082 else
14083 {
14084 /* SAL items used. Requires the word to be case-folded. */
14085 if (folded)
14086 word = inword;
14087 else
14088 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014089 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014090 word = fword;
14091 }
14092
14093#ifdef FEAT_MBYTE
14094 if (has_mbyte)
14095 spell_soundfold_wsal(slang, word, res);
14096 else
14097#endif
14098 spell_soundfold_sal(slang, word, res);
14099 }
14100}
14101
14102/*
14103 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14104 * SOFOTO lines.
14105 */
14106 static void
14107spell_soundfold_sofo(slang, inword, res)
14108 slang_T *slang;
14109 char_u *inword;
14110 char_u *res;
14111{
14112 char_u *s;
14113 int ri = 0;
14114 int c;
14115
14116#ifdef FEAT_MBYTE
14117 if (has_mbyte)
14118 {
14119 int prevc = 0;
14120 int *ip;
14121
14122 /* The sl_sal_first[] table contains the translation for chars up to
14123 * 255, sl_sal the rest. */
14124 for (s = inword; *s != NUL; )
14125 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014126 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014127 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14128 c = ' ';
14129 else if (c < 256)
14130 c = slang->sl_sal_first[c];
14131 else
14132 {
14133 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14134 if (ip == NULL) /* empty list, can't match */
14135 c = NUL;
14136 else
14137 for (;;) /* find "c" in the list */
14138 {
14139 if (*ip == 0) /* not found */
14140 {
14141 c = NUL;
14142 break;
14143 }
14144 if (*ip == c) /* match! */
14145 {
14146 c = ip[1];
14147 break;
14148 }
14149 ip += 2;
14150 }
14151 }
14152
14153 if (c != NUL && c != prevc)
14154 {
14155 ri += mb_char2bytes(c, res + ri);
14156 if (ri + MB_MAXBYTES > MAXWLEN)
14157 break;
14158 prevc = c;
14159 }
14160 }
14161 }
14162 else
14163#endif
14164 {
14165 /* The sl_sal_first[] table contains the translation. */
14166 for (s = inword; (c = *s) != NUL; ++s)
14167 {
14168 if (vim_iswhite(c))
14169 c = ' ';
14170 else
14171 c = slang->sl_sal_first[c];
14172 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14173 res[ri++] = c;
14174 }
14175 }
14176
14177 res[ri] = NUL;
14178}
14179
14180 static void
14181spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 slang_T *slang;
14183 char_u *inword;
14184 char_u *res;
14185{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014186 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014188 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014190 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014192 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 int n, k = 0;
14194 int z0;
14195 int k0;
14196 int n0;
14197 int c;
14198 int pri;
14199 int p0 = -333;
14200 int c0;
14201
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014202 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014203 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 if (slang->sl_rem_accents)
14205 {
14206 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014207 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014209 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014210 {
14211 *t++ = ' ';
14212 s = skipwhite(s);
14213 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014214 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014216 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 *t++ = *s;
14218 ++s;
14219 }
14220 }
14221 *t = NUL;
14222 }
14223 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014224 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014226 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227
14228 /*
14229 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014230 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 while ((c = word[i]) != NUL)
14234 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014235 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 n = slang->sl_sal_first[c];
14237 z0 = 0;
14238
14239 if (n >= 0)
14240 {
14241 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014242 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014244 /* Quickly skip entries that don't match the word. Most
14245 * entries are less then three chars, optimize for that. */
14246 k = smp[n].sm_leadlen;
14247 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014249 if (word[i + 1] != s[1])
14250 continue;
14251 if (k > 2)
14252 {
14253 for (j = 2; j < k; ++j)
14254 if (word[i + j] != s[j])
14255 break;
14256 if (j < k)
14257 continue;
14258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 }
14260
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014261 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014263 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014264 while (*pf != NUL && *pf != word[i + k])
14265 ++pf;
14266 if (*pf == NUL)
14267 continue;
14268 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014270 s = smp[n].sm_rules;
14271 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272
14273 p0 = *s;
14274 k0 = k;
14275 while (*s == '-' && k > 1)
14276 {
14277 k--;
14278 s++;
14279 }
14280 if (*s == '<')
14281 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014282 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 {
14284 /* determine priority */
14285 pri = *s - '0';
14286 s++;
14287 }
14288 if (*s == '^' && *(s + 1) == '^')
14289 s++;
14290
14291 if (*s == NUL
14292 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014293 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014294 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014296 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014298 && spell_iswordp(word + i - 1, curwin)
14299 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 {
14301 /* search for followup rules, if: */
14302 /* followup and k > 1 and NO '-' in searchstring */
14303 c0 = word[i + k - 1];
14304 n0 = slang->sl_sal_first[c0];
14305
14306 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 {
14309 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014310 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014312 /* Quickly skip entries that don't match the word.
14313 * */
14314 k0 = smp[n0].sm_leadlen;
14315 if (k0 > 1)
14316 {
14317 if (word[i + k] != s[1])
14318 continue;
14319 if (k0 > 2)
14320 {
14321 pf = word + i + k + 1;
14322 for (j = 2; j < k0; ++j)
14323 if (*pf++ != s[j])
14324 break;
14325 if (j < k0)
14326 continue;
14327 }
14328 }
14329 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014331 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014332 {
14333 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014334 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014335 while (*pf != NUL && *pf != word[i + k0])
14336 ++pf;
14337 if (*pf == NUL)
14338 continue;
14339 ++k0;
14340 }
14341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014343 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 while (*s == '-')
14345 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014346 /* "k0" gets NOT reduced because
14347 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 s++;
14349 }
14350 if (*s == '<')
14351 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014352 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 {
14354 p0 = *s - '0';
14355 s++;
14356 }
14357
14358 if (*s == NUL
14359 /* *s == '^' cuts */
14360 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014361 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014362 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 {
14364 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014365 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367
14368 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 /* rule fits; stop search */
14372 break;
14373 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 }
14375
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 }
14379
14380 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014381 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014382 if (s == NULL)
14383 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014384 pf = smp[n].sm_rules;
14385 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 if (p0 == 1 && z == 0)
14387 {
14388 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14390 || res[reslen - 1] == *s))
14391 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 z0 = 1;
14393 z = 1;
14394 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014395 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 {
14397 word[i + k0] = *s;
14398 k0++;
14399 s++;
14400 }
14401 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014402 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014403
14404 /* new "actual letter" */
14405 c = word[i];
14406 }
14407 else
14408 {
14409 /* no '<' rule used */
14410 i += k - 1;
14411 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014412 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014414 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014415 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 s++;
14417 }
14418 /* new "actual letter" */
14419 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014420 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 {
14422 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014423 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014424 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 i = 0;
14426 z0 = 1;
14427 }
14428 }
14429 break;
14430 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 }
14432 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014433 else if (vim_iswhite(c))
14434 {
14435 c = ' ';
14436 k = 1;
14437 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438
14439 if (z0 == 0)
14440 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014441 if (k && !p0 && reslen < MAXWLEN && c != NUL
14442 && (!slang->sl_collapse || reslen == 0
14443 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014445 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446
14447 i++;
14448 z = 0;
14449 k = 0;
14450 }
14451 }
14452
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014453 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454}
14455
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014456#ifdef FEAT_MBYTE
14457/*
14458 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14459 * Multi-byte version of spell_soundfold().
14460 */
14461 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014462spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014463 slang_T *slang;
14464 char_u *inword;
14465 char_u *res;
14466{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014467 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014468 int word[MAXWLEN];
14469 int wres[MAXWLEN];
14470 int l;
14471 char_u *s;
14472 int *ws;
14473 char_u *t;
14474 int *pf;
14475 int i, j, z;
14476 int reslen;
14477 int n, k = 0;
14478 int z0;
14479 int k0;
14480 int n0;
14481 int c;
14482 int pri;
14483 int p0 = -333;
14484 int c0;
14485 int did_white = FALSE;
14486
14487 /*
14488 * Convert the multi-byte string to a wide-character string.
14489 * Remove accents, if wanted. We actually remove all non-word characters.
14490 * But keep white space.
14491 */
14492 n = 0;
14493 for (s = inword; *s != NUL; )
14494 {
14495 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014496 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014497 if (slang->sl_rem_accents)
14498 {
14499 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14500 {
14501 if (did_white)
14502 continue;
14503 c = ' ';
14504 did_white = TRUE;
14505 }
14506 else
14507 {
14508 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014509 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014510 continue;
14511 }
14512 }
14513 word[n++] = c;
14514 }
14515 word[n] = NUL;
14516
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014517 /*
14518 * This comes from Aspell phonet.cpp.
14519 * Converted from C++ to C. Added support for multi-byte chars.
14520 * Changed to keep spaces.
14521 */
14522 i = reslen = z = 0;
14523 while ((c = word[i]) != NUL)
14524 {
14525 /* Start with the first rule that has the character in the word. */
14526 n = slang->sl_sal_first[c & 0xff];
14527 z0 = 0;
14528
14529 if (n >= 0)
14530 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014531 /* Check all rules for the same index byte.
14532 * If c is 0x300 need extra check for the end of the array, as
14533 * (c & 0xff) is NUL. */
14534 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14535 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014536 {
14537 /* Quickly skip entries that don't match the word. Most
14538 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014539 if (c != ws[0])
14540 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014541 k = smp[n].sm_leadlen;
14542 if (k > 1)
14543 {
14544 if (word[i + 1] != ws[1])
14545 continue;
14546 if (k > 2)
14547 {
14548 for (j = 2; j < k; ++j)
14549 if (word[i + j] != ws[j])
14550 break;
14551 if (j < k)
14552 continue;
14553 }
14554 }
14555
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014556 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014557 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014558 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014559 while (*pf != NUL && *pf != word[i + k])
14560 ++pf;
14561 if (*pf == NUL)
14562 continue;
14563 ++k;
14564 }
14565 s = smp[n].sm_rules;
14566 pri = 5; /* default priority */
14567
14568 p0 = *s;
14569 k0 = k;
14570 while (*s == '-' && k > 1)
14571 {
14572 k--;
14573 s++;
14574 }
14575 if (*s == '<')
14576 s++;
14577 if (VIM_ISDIGIT(*s))
14578 {
14579 /* determine priority */
14580 pri = *s - '0';
14581 s++;
14582 }
14583 if (*s == '^' && *(s + 1) == '^')
14584 s++;
14585
14586 if (*s == NUL
14587 || (*s == '^'
14588 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014589 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014590 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014591 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014592 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014593 && spell_iswordp_w(word + i - 1, curwin)
14594 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014595 {
14596 /* search for followup rules, if: */
14597 /* followup and k > 1 and NO '-' in searchstring */
14598 c0 = word[i + k - 1];
14599 n0 = slang->sl_sal_first[c0 & 0xff];
14600
14601 if (slang->sl_followup && k > 1 && n0 >= 0
14602 && p0 != '-' && word[i + k] != NUL)
14603 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014604 /* Test follow-up rule for "word[i + k]"; loop over
14605 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014606 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14607 == (c0 & 0xff); ++n0)
14608 {
14609 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014610 */
14611 if (c0 != ws[0])
14612 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014613 k0 = smp[n0].sm_leadlen;
14614 if (k0 > 1)
14615 {
14616 if (word[i + k] != ws[1])
14617 continue;
14618 if (k0 > 2)
14619 {
14620 pf = word + i + k + 1;
14621 for (j = 2; j < k0; ++j)
14622 if (*pf++ != ws[j])
14623 break;
14624 if (j < k0)
14625 continue;
14626 }
14627 }
14628 k0 += k - 1;
14629
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014630 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014631 {
14632 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014633 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014634 while (*pf != NUL && *pf != word[i + k0])
14635 ++pf;
14636 if (*pf == NUL)
14637 continue;
14638 ++k0;
14639 }
14640
14641 p0 = 5;
14642 s = smp[n0].sm_rules;
14643 while (*s == '-')
14644 {
14645 /* "k0" gets NOT reduced because
14646 * "if (k0 == k)" */
14647 s++;
14648 }
14649 if (*s == '<')
14650 s++;
14651 if (VIM_ISDIGIT(*s))
14652 {
14653 p0 = *s - '0';
14654 s++;
14655 }
14656
14657 if (*s == NUL
14658 /* *s == '^' cuts */
14659 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014660 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014661 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014662 {
14663 if (k0 == k)
14664 /* this is just a piece of the string */
14665 continue;
14666
14667 if (p0 < pri)
14668 /* priority too low */
14669 continue;
14670 /* rule fits; stop search */
14671 break;
14672 }
14673 }
14674
14675 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14676 == (c0 & 0xff))
14677 continue;
14678 }
14679
14680 /* replace string */
14681 ws = smp[n].sm_to_w;
14682 s = smp[n].sm_rules;
14683 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14684 if (p0 == 1 && z == 0)
14685 {
14686 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014687 if (reslen > 0 && ws != NULL && *ws != NUL
14688 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014689 || wres[reslen - 1] == *ws))
14690 reslen--;
14691 z0 = 1;
14692 z = 1;
14693 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014694 if (ws != NULL)
14695 while (*ws != NUL && word[i + k0] != NUL)
14696 {
14697 word[i + k0] = *ws;
14698 k0++;
14699 ws++;
14700 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014701 if (k > k0)
14702 mch_memmove(word + i + k0, word + i + k,
14703 sizeof(int) * (STRLEN(word + i + k) + 1));
14704
14705 /* new "actual letter" */
14706 c = word[i];
14707 }
14708 else
14709 {
14710 /* no '<' rule used */
14711 i += k - 1;
14712 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014713 if (ws != NULL)
14714 while (*ws != NUL && ws[1] != NUL
14715 && reslen < MAXWLEN)
14716 {
14717 if (reslen == 0 || wres[reslen - 1] != *ws)
14718 wres[reslen++] = *ws;
14719 ws++;
14720 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014721 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014722 if (ws == NULL)
14723 c = NUL;
14724 else
14725 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014726 if (strstr((char *)s, "^^") != NULL)
14727 {
14728 if (c != NUL)
14729 wres[reslen++] = c;
14730 mch_memmove(word, word + i + 1,
14731 sizeof(int) * (STRLEN(word + i + 1) + 1));
14732 i = 0;
14733 z0 = 1;
14734 }
14735 }
14736 break;
14737 }
14738 }
14739 }
14740 else if (vim_iswhite(c))
14741 {
14742 c = ' ';
14743 k = 1;
14744 }
14745
14746 if (z0 == 0)
14747 {
14748 if (k && !p0 && reslen < MAXWLEN && c != NUL
14749 && (!slang->sl_collapse || reslen == 0
14750 || wres[reslen - 1] != c))
14751 /* condense only double letters */
14752 wres[reslen++] = c;
14753
14754 i++;
14755 z = 0;
14756 k = 0;
14757 }
14758 }
14759
14760 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14761 l = 0;
14762 for (n = 0; n < reslen; ++n)
14763 {
14764 l += mb_char2bytes(wres[n], res + l);
14765 if (l + MB_MAXBYTES > MAXWLEN)
14766 break;
14767 }
14768 res[l] = NUL;
14769}
14770#endif
14771
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014772/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014773 * Compute a score for two sound-a-like words.
14774 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14775 * Instead of a generic loop we write out the code. That keeps it fast by
14776 * avoiding checks that will not be possible.
14777 */
14778 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014779soundalike_score(goodstart, badstart)
14780 char_u *goodstart; /* sound-folded good word */
14781 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014782{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014783 char_u *goodsound = goodstart;
14784 char_u *badsound = badstart;
14785 int goodlen;
14786 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014787 int n;
14788 char_u *pl, *ps;
14789 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014790 int score = 0;
14791
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014792 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014793 * counted so much, vowels halfway the word aren't counted at all. */
14794 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14795 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014796 if ((badsound[0] == NUL && goodsound[1] == NUL)
14797 || (goodsound[0] == NUL && badsound[1] == NUL))
14798 /* changing word with vowel to word without a sound */
14799 return SCORE_DEL;
14800 if (badsound[0] == NUL || goodsound[0] == NUL)
14801 /* more than two changes */
14802 return SCORE_MAXMAX;
14803
Bram Moolenaar4770d092006-01-12 23:22:24 +000014804 if (badsound[1] == goodsound[1]
14805 || (badsound[1] != NUL
14806 && goodsound[1] != NUL
14807 && badsound[2] == goodsound[2]))
14808 {
14809 /* handle like a substitute */
14810 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014811 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014812 {
14813 score = 2 * SCORE_DEL / 3;
14814 if (*badsound == '*')
14815 ++badsound;
14816 else
14817 ++goodsound;
14818 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014819 }
14820
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014821 goodlen = (int)STRLEN(goodsound);
14822 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014823
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014824 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014825 * changes. */
14826 n = goodlen - badlen;
14827 if (n < -2 || n > 2)
14828 return SCORE_MAXMAX;
14829
14830 if (n > 0)
14831 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014832 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014833 ps = badsound;
14834 }
14835 else
14836 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014837 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014838 ps = goodsound;
14839 }
14840
14841 /* Skip over the identical part. */
14842 while (*pl == *ps && *pl != NUL)
14843 {
14844 ++pl;
14845 ++ps;
14846 }
14847
14848 switch (n)
14849 {
14850 case -2:
14851 case 2:
14852 /*
14853 * Must delete two characters from "pl".
14854 */
14855 ++pl; /* first delete */
14856 while (*pl == *ps)
14857 {
14858 ++pl;
14859 ++ps;
14860 }
14861 /* strings must be equal after second delete */
14862 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014863 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014864
14865 /* Failed to compare. */
14866 break;
14867
14868 case -1:
14869 case 1:
14870 /*
14871 * Minimal one delete from "pl" required.
14872 */
14873
14874 /* 1: delete */
14875 pl2 = pl + 1;
14876 ps2 = ps;
14877 while (*pl2 == *ps2)
14878 {
14879 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014880 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014881 ++pl2;
14882 ++ps2;
14883 }
14884
14885 /* 2: delete then swap, then rest must be equal */
14886 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14887 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014888 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014889
14890 /* 3: delete then substitute, then the rest must be equal */
14891 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014892 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014893
14894 /* 4: first swap then delete */
14895 if (pl[0] == ps[1] && pl[1] == ps[0])
14896 {
14897 pl2 = pl + 2; /* swap, skip two chars */
14898 ps2 = ps + 2;
14899 while (*pl2 == *ps2)
14900 {
14901 ++pl2;
14902 ++ps2;
14903 }
14904 /* delete a char and then strings must be equal */
14905 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014906 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014907 }
14908
14909 /* 5: first substitute then delete */
14910 pl2 = pl + 1; /* substitute, skip one char */
14911 ps2 = ps + 1;
14912 while (*pl2 == *ps2)
14913 {
14914 ++pl2;
14915 ++ps2;
14916 }
14917 /* delete a char and then strings must be equal */
14918 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014919 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014920
14921 /* Failed to compare. */
14922 break;
14923
14924 case 0:
14925 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014926 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014927 * insert is only possible in combination with a delete.
14928 * 1: check if for identical strings
14929 */
14930 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014931 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014932
14933 /* 2: swap */
14934 if (pl[0] == ps[1] && pl[1] == ps[0])
14935 {
14936 pl2 = pl + 2; /* swap, skip two chars */
14937 ps2 = ps + 2;
14938 while (*pl2 == *ps2)
14939 {
14940 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014941 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014942 ++pl2;
14943 ++ps2;
14944 }
14945 /* 3: swap and swap again */
14946 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14947 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014948 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014949
14950 /* 4: swap and substitute */
14951 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014952 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014953 }
14954
14955 /* 5: substitute */
14956 pl2 = pl + 1;
14957 ps2 = ps + 1;
14958 while (*pl2 == *ps2)
14959 {
14960 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014961 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014962 ++pl2;
14963 ++ps2;
14964 }
14965
14966 /* 6: substitute and swap */
14967 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14968 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014969 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014970
14971 /* 7: substitute and substitute */
14972 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014973 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014974
14975 /* 8: insert then delete */
14976 pl2 = pl;
14977 ps2 = ps + 1;
14978 while (*pl2 == *ps2)
14979 {
14980 ++pl2;
14981 ++ps2;
14982 }
14983 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014984 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014985
14986 /* 9: delete then insert */
14987 pl2 = pl + 1;
14988 ps2 = ps;
14989 while (*pl2 == *ps2)
14990 {
14991 ++pl2;
14992 ++ps2;
14993 }
14994 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014995 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014996
14997 /* Failed to compare. */
14998 break;
14999 }
15000
15001 return SCORE_MAXMAX;
15002}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004/*
15005 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015006 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015007 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000015008 * The algorithm is described by Du and Chang, 1992.
15009 * The implementation of the algorithm comes from Aspell editdist.cpp,
15010 * edit_distance(). It has been converted from C++ to C and modified to
15011 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015012 */
15013 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000015014spell_edit_score(slang, badword, goodword)
15015 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 char_u *badword;
15017 char_u *goodword;
15018{
15019 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015020 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015021 int j, i;
15022 int t;
15023 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015024 int pbc, pgc;
15025#ifdef FEAT_MBYTE
15026 char_u *p;
15027 int wbadword[MAXWLEN];
15028 int wgoodword[MAXWLEN];
15029
15030 if (has_mbyte)
15031 {
15032 /* Get the characters from the multi-byte strings and put them in an
15033 * int array for easy access. */
15034 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015035 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015036 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015037 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015038 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015039 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015040 }
15041 else
15042#endif
15043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015044 badlen = (int)STRLEN(badword) + 1;
15045 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047
15048 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15049#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15051 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015052 if (cnt == NULL)
15053 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015054
15055 CNT(0, 0) = 0;
15056 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015057 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015058
15059 for (i = 1; i <= badlen; ++i)
15060 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015061 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015062 for (j = 1; j <= goodlen; ++j)
15063 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015064#ifdef FEAT_MBYTE
15065 if (has_mbyte)
15066 {
15067 bc = wbadword[i - 1];
15068 gc = wgoodword[j - 1];
15069 }
15070 else
15071#endif
15072 {
15073 bc = badword[i - 1];
15074 gc = goodword[j - 1];
15075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 if (bc == gc)
15077 CNT(i, j) = CNT(i - 1, j - 1);
15078 else
15079 {
15080 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015081 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015082 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15083 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015084 {
15085 /* For a similar character use SCORE_SIMILAR. */
15086 if (slang != NULL
15087 && slang->sl_has_map
15088 && similar_chars(slang, gc, bc))
15089 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15090 else
15091 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15092 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015094 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015095 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015096#ifdef FEAT_MBYTE
15097 if (has_mbyte)
15098 {
15099 pbc = wbadword[i - 2];
15100 pgc = wgoodword[j - 2];
15101 }
15102 else
15103#endif
15104 {
15105 pbc = badword[i - 2];
15106 pgc = goodword[j - 2];
15107 }
15108 if (bc == pgc && pbc == gc)
15109 {
15110 t = SCORE_SWAP + CNT(i - 2, j - 2);
15111 if (t < CNT(i, j))
15112 CNT(i, j) = t;
15113 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015114 }
15115 t = SCORE_DEL + CNT(i - 1, j);
15116 if (t < CNT(i, j))
15117 CNT(i, j) = t;
15118 t = SCORE_INS + CNT(i, j - 1);
15119 if (t < CNT(i, j))
15120 CNT(i, j) = t;
15121 }
15122 }
15123 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015124
15125 i = CNT(badlen - 1, goodlen - 1);
15126 vim_free(cnt);
15127 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015129
Bram Moolenaar4770d092006-01-12 23:22:24 +000015130typedef struct
15131{
15132 int badi;
15133 int goodi;
15134 int score;
15135} limitscore_T;
15136
15137/*
15138 * Like spell_edit_score(), but with a limit on the score to make it faster.
15139 * May return SCORE_MAXMAX when the score is higher than "limit".
15140 *
15141 * This uses a stack for the edits still to be tried.
15142 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15143 * for multi-byte characters.
15144 */
15145 static int
15146spell_edit_score_limit(slang, badword, goodword, limit)
15147 slang_T *slang;
15148 char_u *badword;
15149 char_u *goodword;
15150 int limit;
15151{
15152 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15153 int stackidx;
15154 int bi, gi;
15155 int bi2, gi2;
15156 int bc, gc;
15157 int score;
15158 int score_off;
15159 int minscore;
15160 int round;
15161
15162#ifdef FEAT_MBYTE
15163 /* Multi-byte characters require a bit more work, use a different function
15164 * to avoid testing "has_mbyte" quite often. */
15165 if (has_mbyte)
15166 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15167#endif
15168
15169 /*
15170 * The idea is to go from start to end over the words. So long as
15171 * characters are equal just continue, this always gives the lowest score.
15172 * When there is a difference try several alternatives. Each alternative
15173 * increases "score" for the edit distance. Some of the alternatives are
15174 * pushed unto a stack and tried later, some are tried right away. At the
15175 * end of the word the score for one alternative is known. The lowest
15176 * possible score is stored in "minscore".
15177 */
15178 stackidx = 0;
15179 bi = 0;
15180 gi = 0;
15181 score = 0;
15182 minscore = limit + 1;
15183
15184 for (;;)
15185 {
15186 /* Skip over an equal part, score remains the same. */
15187 for (;;)
15188 {
15189 bc = badword[bi];
15190 gc = goodword[gi];
15191 if (bc != gc) /* stop at a char that's different */
15192 break;
15193 if (bc == NUL) /* both words end */
15194 {
15195 if (score < minscore)
15196 minscore = score;
15197 goto pop; /* do next alternative */
15198 }
15199 ++bi;
15200 ++gi;
15201 }
15202
15203 if (gc == NUL) /* goodword ends, delete badword chars */
15204 {
15205 do
15206 {
15207 if ((score += SCORE_DEL) >= minscore)
15208 goto pop; /* do next alternative */
15209 } while (badword[++bi] != NUL);
15210 minscore = score;
15211 }
15212 else if (bc == NUL) /* badword ends, insert badword chars */
15213 {
15214 do
15215 {
15216 if ((score += SCORE_INS) >= minscore)
15217 goto pop; /* do next alternative */
15218 } while (goodword[++gi] != NUL);
15219 minscore = score;
15220 }
15221 else /* both words continue */
15222 {
15223 /* If not close to the limit, perform a change. Only try changes
15224 * that may lead to a lower score than "minscore".
15225 * round 0: try deleting a char from badword
15226 * round 1: try inserting a char in badword */
15227 for (round = 0; round <= 1; ++round)
15228 {
15229 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15230 if (score_off < minscore)
15231 {
15232 if (score_off + SCORE_EDIT_MIN >= minscore)
15233 {
15234 /* Near the limit, rest of the words must match. We
15235 * can check that right now, no need to push an item
15236 * onto the stack. */
15237 bi2 = bi + 1 - round;
15238 gi2 = gi + round;
15239 while (goodword[gi2] == badword[bi2])
15240 {
15241 if (goodword[gi2] == NUL)
15242 {
15243 minscore = score_off;
15244 break;
15245 }
15246 ++bi2;
15247 ++gi2;
15248 }
15249 }
15250 else
15251 {
15252 /* try deleting/inserting a character later */
15253 stack[stackidx].badi = bi + 1 - round;
15254 stack[stackidx].goodi = gi + round;
15255 stack[stackidx].score = score_off;
15256 ++stackidx;
15257 }
15258 }
15259 }
15260
15261 if (score + SCORE_SWAP < minscore)
15262 {
15263 /* If swapping two characters makes a match then the
15264 * substitution is more expensive, thus there is no need to
15265 * try both. */
15266 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15267 {
15268 /* Swap two characters, that is: skip them. */
15269 gi += 2;
15270 bi += 2;
15271 score += SCORE_SWAP;
15272 continue;
15273 }
15274 }
15275
15276 /* Substitute one character for another which is the same
15277 * thing as deleting a character from both goodword and badword.
15278 * Use a better score when there is only a case difference. */
15279 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15280 score += SCORE_ICASE;
15281 else
15282 {
15283 /* For a similar character use SCORE_SIMILAR. */
15284 if (slang != NULL
15285 && slang->sl_has_map
15286 && similar_chars(slang, gc, bc))
15287 score += SCORE_SIMILAR;
15288 else
15289 score += SCORE_SUBST;
15290 }
15291
15292 if (score < minscore)
15293 {
15294 /* Do the substitution. */
15295 ++gi;
15296 ++bi;
15297 continue;
15298 }
15299 }
15300pop:
15301 /*
15302 * Get here to try the next alternative, pop it from the stack.
15303 */
15304 if (stackidx == 0) /* stack is empty, finished */
15305 break;
15306
15307 /* pop an item from the stack */
15308 --stackidx;
15309 gi = stack[stackidx].goodi;
15310 bi = stack[stackidx].badi;
15311 score = stack[stackidx].score;
15312 }
15313
15314 /* When the score goes over "limit" it may actually be much higher.
15315 * Return a very large number to avoid going below the limit when giving a
15316 * bonus. */
15317 if (minscore > limit)
15318 return SCORE_MAXMAX;
15319 return minscore;
15320}
15321
15322#ifdef FEAT_MBYTE
15323/*
15324 * Multi-byte version of spell_edit_score_limit().
15325 * Keep it in sync with the above!
15326 */
15327 static int
15328spell_edit_score_limit_w(slang, badword, goodword, limit)
15329 slang_T *slang;
15330 char_u *badword;
15331 char_u *goodword;
15332 int limit;
15333{
15334 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15335 int stackidx;
15336 int bi, gi;
15337 int bi2, gi2;
15338 int bc, gc;
15339 int score;
15340 int score_off;
15341 int minscore;
15342 int round;
15343 char_u *p;
15344 int wbadword[MAXWLEN];
15345 int wgoodword[MAXWLEN];
15346
15347 /* Get the characters from the multi-byte strings and put them in an
15348 * int array for easy access. */
15349 bi = 0;
15350 for (p = badword; *p != NUL; )
15351 wbadword[bi++] = mb_cptr2char_adv(&p);
15352 wbadword[bi++] = 0;
15353 gi = 0;
15354 for (p = goodword; *p != NUL; )
15355 wgoodword[gi++] = mb_cptr2char_adv(&p);
15356 wgoodword[gi++] = 0;
15357
15358 /*
15359 * The idea is to go from start to end over the words. So long as
15360 * characters are equal just continue, this always gives the lowest score.
15361 * When there is a difference try several alternatives. Each alternative
15362 * increases "score" for the edit distance. Some of the alternatives are
15363 * pushed unto a stack and tried later, some are tried right away. At the
15364 * end of the word the score for one alternative is known. The lowest
15365 * possible score is stored in "minscore".
15366 */
15367 stackidx = 0;
15368 bi = 0;
15369 gi = 0;
15370 score = 0;
15371 minscore = limit + 1;
15372
15373 for (;;)
15374 {
15375 /* Skip over an equal part, score remains the same. */
15376 for (;;)
15377 {
15378 bc = wbadword[bi];
15379 gc = wgoodword[gi];
15380
15381 if (bc != gc) /* stop at a char that's different */
15382 break;
15383 if (bc == NUL) /* both words end */
15384 {
15385 if (score < minscore)
15386 minscore = score;
15387 goto pop; /* do next alternative */
15388 }
15389 ++bi;
15390 ++gi;
15391 }
15392
15393 if (gc == NUL) /* goodword ends, delete badword chars */
15394 {
15395 do
15396 {
15397 if ((score += SCORE_DEL) >= minscore)
15398 goto pop; /* do next alternative */
15399 } while (wbadword[++bi] != NUL);
15400 minscore = score;
15401 }
15402 else if (bc == NUL) /* badword ends, insert badword chars */
15403 {
15404 do
15405 {
15406 if ((score += SCORE_INS) >= minscore)
15407 goto pop; /* do next alternative */
15408 } while (wgoodword[++gi] != NUL);
15409 minscore = score;
15410 }
15411 else /* both words continue */
15412 {
15413 /* If not close to the limit, perform a change. Only try changes
15414 * that may lead to a lower score than "minscore".
15415 * round 0: try deleting a char from badword
15416 * round 1: try inserting a char in badword */
15417 for (round = 0; round <= 1; ++round)
15418 {
15419 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15420 if (score_off < minscore)
15421 {
15422 if (score_off + SCORE_EDIT_MIN >= minscore)
15423 {
15424 /* Near the limit, rest of the words must match. We
15425 * can check that right now, no need to push an item
15426 * onto the stack. */
15427 bi2 = bi + 1 - round;
15428 gi2 = gi + round;
15429 while (wgoodword[gi2] == wbadword[bi2])
15430 {
15431 if (wgoodword[gi2] == NUL)
15432 {
15433 minscore = score_off;
15434 break;
15435 }
15436 ++bi2;
15437 ++gi2;
15438 }
15439 }
15440 else
15441 {
15442 /* try deleting a character from badword later */
15443 stack[stackidx].badi = bi + 1 - round;
15444 stack[stackidx].goodi = gi + round;
15445 stack[stackidx].score = score_off;
15446 ++stackidx;
15447 }
15448 }
15449 }
15450
15451 if (score + SCORE_SWAP < minscore)
15452 {
15453 /* If swapping two characters makes a match then the
15454 * substitution is more expensive, thus there is no need to
15455 * try both. */
15456 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15457 {
15458 /* Swap two characters, that is: skip them. */
15459 gi += 2;
15460 bi += 2;
15461 score += SCORE_SWAP;
15462 continue;
15463 }
15464 }
15465
15466 /* Substitute one character for another which is the same
15467 * thing as deleting a character from both goodword and badword.
15468 * Use a better score when there is only a case difference. */
15469 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15470 score += SCORE_ICASE;
15471 else
15472 {
15473 /* For a similar character use SCORE_SIMILAR. */
15474 if (slang != NULL
15475 && slang->sl_has_map
15476 && similar_chars(slang, gc, bc))
15477 score += SCORE_SIMILAR;
15478 else
15479 score += SCORE_SUBST;
15480 }
15481
15482 if (score < minscore)
15483 {
15484 /* Do the substitution. */
15485 ++gi;
15486 ++bi;
15487 continue;
15488 }
15489 }
15490pop:
15491 /*
15492 * Get here to try the next alternative, pop it from the stack.
15493 */
15494 if (stackidx == 0) /* stack is empty, finished */
15495 break;
15496
15497 /* pop an item from the stack */
15498 --stackidx;
15499 gi = stack[stackidx].goodi;
15500 bi = stack[stackidx].badi;
15501 score = stack[stackidx].score;
15502 }
15503
15504 /* When the score goes over "limit" it may actually be much higher.
15505 * Return a very large number to avoid going below the limit when giving a
15506 * bonus. */
15507 if (minscore > limit)
15508 return SCORE_MAXMAX;
15509 return minscore;
15510}
15511#endif
15512
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015513/*
15514 * ":spellinfo"
15515 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015516 void
15517ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015518 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015519{
15520 int lpi;
15521 langp_T *lp;
15522 char_u *p;
15523
15524 if (no_spell_checking(curwin))
15525 return;
15526
15527 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015528 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015529 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015530 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015531 msg_puts((char_u *)"file: ");
15532 msg_puts(lp->lp_slang->sl_fname);
15533 msg_putchar('\n');
15534 p = lp->lp_slang->sl_info;
15535 if (p != NULL)
15536 {
15537 msg_puts(p);
15538 msg_putchar('\n');
15539 }
15540 }
15541 msg_end();
15542}
15543
Bram Moolenaar4770d092006-01-12 23:22:24 +000015544#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15545#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015546#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015547#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15548#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015549
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015550/*
15551 * ":spelldump"
15552 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015553 void
15554ex_spelldump(eap)
15555 exarg_T *eap;
15556{
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015557 if (no_spell_checking(curwin))
15558 return;
15559
15560 /* Create a new empty buffer by splitting the window. */
15561 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar860cae12010-06-05 23:22:07 +020015562 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015563 return;
15564
Bram Moolenaar860cae12010-06-05 23:22:07 +020015565 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015566
15567 /* Delete the empty line that we started with. */
15568 if (curbuf->b_ml.ml_line_count > 1)
15569 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15570
15571 redraw_later(NOT_VALID);
15572}
15573
15574/*
15575 * Go through all possible words and:
15576 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15577 * "ic" and "dir" are not used.
15578 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15579 */
15580 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015581spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015582 char_u *pat; /* leading part of the word */
15583 int ic; /* ignore case */
15584 int *dir; /* direction for adding matches */
15585 int dumpflags_arg; /* DUMPFLAG_* */
15586{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015587 langp_T *lp;
15588 slang_T *slang;
15589 idx_T arridx[MAXWLEN];
15590 int curi[MAXWLEN];
15591 char_u word[MAXWLEN];
15592 int c;
15593 char_u *byts;
15594 idx_T *idxs;
15595 linenr_T lnum = 0;
15596 int round;
15597 int depth;
15598 int n;
15599 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015600 char_u *region_names = NULL; /* region names being used */
15601 int do_region = TRUE; /* dump region names and numbers */
15602 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015603 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015604 int dumpflags = dumpflags_arg;
15605 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015606
Bram Moolenaard0131a82006-03-04 21:46:13 +000015607 /* When ignoring case or when the pattern starts with capital pass this on
15608 * to dump_word(). */
15609 if (pat != NULL)
15610 {
15611 if (ic)
15612 dumpflags |= DUMPFLAG_ICASE;
15613 else
15614 {
15615 n = captype(pat, NULL);
15616 if (n == WF_ONECAP)
15617 dumpflags |= DUMPFLAG_ONECAP;
15618 else if (n == WF_ALLCAP
15619#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015620 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015621#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015622 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015623#endif
15624 )
15625 dumpflags |= DUMPFLAG_ALLCAP;
15626 }
15627 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015628
Bram Moolenaar7887d882005-07-01 22:33:52 +000015629 /* Find out if we can support regions: All languages must support the same
15630 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015631 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015632 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015633 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015634 p = lp->lp_slang->sl_regions;
15635 if (p[0] != 0)
15636 {
15637 if (region_names == NULL) /* first language with regions */
15638 region_names = p;
15639 else if (STRCMP(region_names, p) != 0)
15640 {
15641 do_region = FALSE; /* region names are different */
15642 break;
15643 }
15644 }
15645 }
15646
15647 if (do_region && region_names != NULL)
15648 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015649 if (pat == NULL)
15650 {
15651 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15652 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15653 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015654 }
15655 else
15656 do_region = FALSE;
15657
15658 /*
15659 * Loop over all files loaded for the entries in 'spelllang'.
15660 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015661 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015662 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015663 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015664 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015665 if (slang->sl_fbyts == NULL) /* reloading failed */
15666 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015667
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015668 if (pat == NULL)
15669 {
15670 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15671 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15672 }
15673
15674 /* When matching with a pattern and there are no prefixes only use
15675 * parts of the tree that match "pat". */
15676 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015677 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015678 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015679 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015680
15681 /* round 1: case-folded tree
15682 * round 2: keep-case tree */
15683 for (round = 1; round <= 2; ++round)
15684 {
15685 if (round == 1)
15686 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015687 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015688 byts = slang->sl_fbyts;
15689 idxs = slang->sl_fidxs;
15690 }
15691 else
15692 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015693 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015694 byts = slang->sl_kbyts;
15695 idxs = slang->sl_kidxs;
15696 }
15697 if (byts == NULL)
15698 continue; /* array is empty */
15699
15700 depth = 0;
15701 arridx[0] = 0;
15702 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015703 while (depth >= 0 && !got_int
15704 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015705 {
15706 if (curi[depth] > byts[arridx[depth]])
15707 {
15708 /* Done all bytes at this node, go up one level. */
15709 --depth;
15710 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015711 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015712 }
15713 else
15714 {
15715 /* Do one more byte at this node. */
15716 n = arridx[depth] + curi[depth];
15717 ++curi[depth];
15718 c = byts[n];
15719 if (c == 0)
15720 {
15721 /* End of word, deal with the word.
15722 * Don't use keep-case words in the fold-case tree,
15723 * they will appear in the keep-case tree.
15724 * Only use the word when the region matches. */
15725 flags = (int)idxs[n];
15726 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015727 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015728 && (do_region
15729 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015730 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015731 & lp->lp_region) != 0))
15732 {
15733 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015734 if (!do_region)
15735 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015736
15737 /* Dump the basic word if there is no prefix or
15738 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015739 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015740 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015741 {
15742 dump_word(slang, word, pat, dir,
15743 dumpflags, flags, lnum);
15744 if (pat == NULL)
15745 ++lnum;
15746 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015747
15748 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015749 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015750 lnum = dump_prefixes(slang, word, pat, dir,
15751 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015752 }
15753 }
15754 else
15755 {
15756 /* Normal char, go one level deeper. */
15757 word[depth++] = c;
15758 arridx[depth] = idxs[n];
15759 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015760
15761 /* Check if this characters matches with the pattern.
15762 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015763 * Always ignore case here, dump_word() will check
15764 * proper case later. This isn't exactly right when
15765 * length changes for multi-byte characters with
15766 * ignore case... */
15767 if (depth <= patlen
15768 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015769 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015770 }
15771 }
15772 }
15773 }
15774 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015775}
15776
15777/*
15778 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015779 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015780 */
15781 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015782dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015783 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015784 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015785 char_u *pat;
15786 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015787 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015788 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015789 linenr_T lnum;
15790{
15791 int keepcap = FALSE;
15792 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015793 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015794 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015795 char_u badword[MAXWLEN + 10];
15796 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015797 int flags = wordflags;
15798
15799 if (dumpflags & DUMPFLAG_ONECAP)
15800 flags |= WF_ONECAP;
15801 if (dumpflags & DUMPFLAG_ALLCAP)
15802 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015803
Bram Moolenaar4770d092006-01-12 23:22:24 +000015804 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015805 {
15806 /* Need to fix case according to "flags". */
15807 make_case_word(word, cword, flags);
15808 p = cword;
15809 }
15810 else
15811 {
15812 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015813 if ((dumpflags & DUMPFLAG_KEEPCASE)
15814 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015815 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015816 keepcap = TRUE;
15817 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015818 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015819
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015820 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015821 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015822 /* Add flags and regions after a slash. */
15823 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015824 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015825 STRCPY(badword, p);
15826 STRCAT(badword, "/");
15827 if (keepcap)
15828 STRCAT(badword, "=");
15829 if (flags & WF_BANNED)
15830 STRCAT(badword, "!");
15831 else if (flags & WF_RARE)
15832 STRCAT(badword, "?");
15833 if (flags & WF_REGION)
15834 for (i = 0; i < 7; ++i)
15835 if (flags & (0x10000 << i))
15836 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15837 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015838 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015839
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015840 if (dumpflags & DUMPFLAG_COUNT)
15841 {
15842 hashitem_T *hi;
15843
15844 /* Include the word count for ":spelldump!". */
15845 hi = hash_find(&slang->sl_wordcount, tw);
15846 if (!HASHITEM_EMPTY(hi))
15847 {
15848 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15849 tw, HI2WC(hi)->wc_count);
15850 p = IObuff;
15851 }
15852 }
15853
15854 ml_append(lnum, p, (colnr_T)0, FALSE);
15855 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015856 else if (((dumpflags & DUMPFLAG_ICASE)
15857 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15858 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015859 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015860 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015861 /* if dir was BACKWARD then honor it just once */
15862 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015863}
15864
15865/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015866 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15867 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015868 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015869 * Return the updated line number.
15870 */
15871 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015872dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015873 slang_T *slang;
15874 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015875 char_u *pat;
15876 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015877 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015878 int flags; /* flags with prefix ID */
15879 linenr_T startlnum;
15880{
15881 idx_T arridx[MAXWLEN];
15882 int curi[MAXWLEN];
15883 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015884 char_u word_up[MAXWLEN];
15885 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015886 int c;
15887 char_u *byts;
15888 idx_T *idxs;
15889 linenr_T lnum = startlnum;
15890 int depth;
15891 int n;
15892 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015893 int i;
15894
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015895 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015896 * upper-case letter in word_up[]. */
15897 c = PTR2CHAR(word);
15898 if (SPELL_TOUPPER(c) != c)
15899 {
15900 onecap_copy(word, word_up, TRUE);
15901 has_word_up = TRUE;
15902 }
15903
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015904 byts = slang->sl_pbyts;
15905 idxs = slang->sl_pidxs;
15906 if (byts != NULL) /* array not is empty */
15907 {
15908 /*
15909 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015910 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015911 */
15912 depth = 0;
15913 arridx[0] = 0;
15914 curi[0] = 1;
15915 while (depth >= 0 && !got_int)
15916 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015917 n = arridx[depth];
15918 len = byts[n];
15919 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015920 {
15921 /* Done all bytes at this node, go up one level. */
15922 --depth;
15923 line_breakcheck();
15924 }
15925 else
15926 {
15927 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015928 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015929 ++curi[depth];
15930 c = byts[n];
15931 if (c == 0)
15932 {
15933 /* End of prefix, find out how many IDs there are. */
15934 for (i = 1; i < len; ++i)
15935 if (byts[n + i] != 0)
15936 break;
15937 curi[depth] += i - 1;
15938
Bram Moolenaar53805d12005-08-01 07:08:33 +000015939 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15940 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015941 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015942 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015943 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015944 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015945 : flags, lnum);
15946 if (lnum != 0)
15947 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015948 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015949
15950 /* Check for prefix that matches the word when the
15951 * first letter is upper-case, but only if the prefix has
15952 * a condition. */
15953 if (has_word_up)
15954 {
15955 c = valid_word_prefix(i, n, flags, word_up, slang,
15956 TRUE);
15957 if (c != 0)
15958 {
15959 vim_strncpy(prefix + depth, word_up,
15960 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015961 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015962 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015963 : flags, lnum);
15964 if (lnum != 0)
15965 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015966 }
15967 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015968 }
15969 else
15970 {
15971 /* Normal char, go one level deeper. */
15972 prefix[depth++] = c;
15973 arridx[depth] = idxs[n];
15974 curi[depth] = 1;
15975 }
15976 }
15977 }
15978 }
15979
15980 return lnum;
15981}
15982
Bram Moolenaar95529562005-08-25 21:21:38 +000015983/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015984 * Move "p" to the end of word "start".
15985 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015986 */
15987 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020015988spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015989 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015990 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000015991{
15992 char_u *p = start;
15993
Bram Moolenaar860cae12010-06-05 23:22:07 +020015994 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000015995 mb_ptr_adv(p);
15996 return p;
15997}
15998
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015999#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016000/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000016001 * For Insert mode completion CTRL-X s:
16002 * Find start of the word in front of column "startcol".
16003 * We don't check if it is badly spelled, with completion we can only change
16004 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016005 * Returns the column number of the word.
16006 */
16007 int
16008spell_word_start(startcol)
16009 int startcol;
16010{
16011 char_u *line;
16012 char_u *p;
16013 int col = 0;
16014
Bram Moolenaar95529562005-08-25 21:21:38 +000016015 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016016 return startcol;
16017
16018 /* Find a word character before "startcol". */
16019 line = ml_get_curline();
16020 for (p = line + startcol; p > line; )
16021 {
16022 mb_ptr_back(line, p);
16023 if (spell_iswordp_nmw(p))
16024 break;
16025 }
16026
16027 /* Go back to start of the word. */
16028 while (p > line)
16029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016030 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016031 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016032 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016033 break;
16034 col = 0;
16035 }
16036
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016037 return col;
16038}
16039
16040/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016041 * Need to check for 'spellcapcheck' now, the word is removed before
16042 * expand_spelling() is called. Therefore the ugly global variable.
16043 */
16044static int spell_expand_need_cap;
16045
16046 void
16047spell_expand_check_cap(col)
16048 colnr_T col;
16049{
16050 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16051}
16052
16053/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016054 * Get list of spelling suggestions.
16055 * Used for Insert mode completion CTRL-X ?.
16056 * Returns the number of matches. The matches are in "matchp[]", array of
16057 * allocated strings.
16058 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016059 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016060expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016061 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016062 char_u *pat;
16063 char_u ***matchp;
16064{
16065 garray_T ga;
16066
Bram Moolenaar4770d092006-01-12 23:22:24 +000016067 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016068 *matchp = ga.ga_data;
16069 return ga.ga_len;
16070}
16071#endif
16072
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016073#endif /* FEAT_SPELL */