blob: 6d4d47ea213b717fe80f0f7a8dda8aaf8bd82172 [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#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaare19defe2005-03-21 08:23:33 +0000308#endif
309
310#include "vim.h"
311
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000312#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000313
Bram Moolenaar4770d092006-01-12 23:22:24 +0000314#ifndef UNIX /* it's in os_unix.h for Unix */
315# include <time.h> /* for time_t */
316#endif
317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000318#define MAXWLEN 250 /* Assume max. word len is this many bytes.
319 Some places assume a word length fits in a
320 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000321
Bram Moolenaare52325c2005-08-22 22:54:29 +0000322/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000323 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000324#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000325typedef int idx_T;
326#else
327typedef long idx_T;
328#endif
329
330/* Flags used for a word. Only the lowest byte can be used, the region byte
331 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000332#define WF_REGION 0x01 /* region byte follows */
333#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
334#define WF_ALLCAP 0x04 /* word must be all capitals */
335#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000336#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000337#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000338#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000339#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000340
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000341/* for <flags2>, shifted up one byte to be used in wn_flags */
342#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000343#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000344#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000345#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000346#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
347#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000348
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000349/* only used for su_badflags */
350#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
351
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000352#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000353
Bram Moolenaar53805d12005-08-01 07:08:33 +0000354/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000355#define WFP_RARE 0x01 /* rare prefix */
356#define WFP_NC 0x02 /* prefix is not combining */
357#define WFP_UP 0x04 /* to-upper prefix */
358#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
359#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000360
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000361/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
362 * byte) and prefcondnr (two bytes). */
363#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
364#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
365#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
366#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
367 * COMPOUNDPERMITFLAG */
368#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
369 * COMPOUNDFORBIDFLAG */
370
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000371
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000372/* flags for <compoptions> */
373#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
374#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
375#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
376#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
377
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000378/* Special byte values for <byte>. Some are only used in the tree for
379 * postponed prefixes, some only in the other trees. This is a bit messy... */
380#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000381 * postponed prefix: no <pflags> */
382#define BY_INDEX 1 /* child is shared, index follows */
383#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
384 * postponed prefix: <pflags> follows */
385#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
386 * follow; never used in prefix tree */
387#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000388
Bram Moolenaar4770d092006-01-12 23:22:24 +0000389/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
390 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 * One replacement: from "ft_from" to "ft_to". */
392typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000394 char_u *ft_from;
395 char_u *ft_to;
396} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000398/* Info from "SAL" entries in ".aff" file used in sl_sal.
399 * The info is split for quick processing by spell_soundfold().
400 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
401typedef struct salitem_S
402{
403 char_u *sm_lead; /* leading letters */
404 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000405 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000406 char_u *sm_rules; /* rules like ^, $, priority */
407 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000408#ifdef FEAT_MBYTE
409 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000410 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000411 int *sm_to_w; /* wide character copy of "sm_to" */
412#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000413} salitem_T;
414
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000415#ifdef FEAT_MBYTE
416typedef int salfirst_T;
417#else
418typedef short salfirst_T;
419#endif
420
Bram Moolenaar5195e452005-08-19 20:32:47 +0000421/* Values for SP_*ERROR are negative, positive values are used by
422 * read_cnt_string(). */
423#define SP_TRUNCERROR -1 /* spell file truncated error */
424#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000425#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000428 * Structure used to store words and other info for one language, loaded from
429 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000430 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
431 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
432 *
433 * The "byts" array stores the possible bytes in each tree node, preceded by
434 * the number of possible bytes, sorted on byte value:
435 * <len> <byte1> <byte2> ...
436 * The "idxs" array stores the index of the child node corresponding to the
437 * byte in "byts".
438 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000439 * the flags, region mask and affixID for the word. There may be several
440 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000441 */
442typedef struct slang_S slang_T;
443struct slang_S
444{
445 slang_T *sl_next; /* next language */
446 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000447 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000448 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000449
Bram Moolenaar51485f02005-06-04 21:55:20 +0000450 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000451 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000452 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000453 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000454 char_u *sl_pbyts; /* prefix tree word bytes */
455 idx_T *sl_pidxs; /* prefix tree word indexes */
456
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000457 char_u *sl_info; /* infotext string or NULL */
458
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000461 char_u *sl_midword; /* MIDWORD string or NULL */
462
Bram Moolenaar4770d092006-01-12 23:22:24 +0000463 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
464
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000465 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000466 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000467 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000468 int sl_compoptions; /* COMP_* flags */
469 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000470 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000472 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000473 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000474 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000475 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000476 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
477 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000478
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000479 int sl_prefixcnt; /* number of items in "sl_prefprog" */
480 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482 garray_T sl_rep; /* list of fromto_T entries from REP lines */
483 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
484 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000486 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487 there is none */
488 int sl_followup; /* SAL followup */
489 int sl_collapse; /* SAL collapse_result */
490 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000491 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
492 * "sl_sal_first" maps chars, when has_mbyte
493 * "sl_sal" is a list of wide char lists. */
494 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
495 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000496 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000497
498 /* Info from the .sug file. Loaded on demand. */
499 time_t sl_sugtime; /* timestamp for .sug file */
500 char_u *sl_sbyts; /* soundfolded word bytes */
501 idx_T *sl_sidxs; /* soundfolded word indexes */
502 buf_T *sl_sugbuf; /* buffer with word number table */
503 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
504 load */
505
Bram Moolenaarea424162005-06-16 21:51:00 +0000506 int sl_has_map; /* TRUE if there is a MAP line */
507#ifdef FEAT_MBYTE
508 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
509 int sl_map_array[256]; /* MAP for first 256 chars */
510#else
511 char_u sl_map_array[256]; /* MAP for first 256 chars */
512#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000513 hashtab_T sl_sounddone; /* table with soundfolded words that have
514 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000515};
516
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000517/* First language that is loaded, start of the linked list of loaded
518 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000519static slang_T *first_lang = NULL;
520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000521/* Flags used in .spl file for soundsalike flags. */
522#define SAL_F0LLOWUP 1
523#define SAL_COLLAPSE 2
524#define SAL_REM_ACCENTS 4
525
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000526/*
527 * Structure used in "b_langp", filled from 'spelllang'.
528 */
529typedef struct langp_S
530{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000531 slang_T *lp_slang; /* info for this language */
532 slang_T *lp_sallang; /* language used for sound folding or NULL */
533 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000534 int lp_region; /* bitmask for region or REGION_ALL */
535} langp_T;
536
537#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
538
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000539#define REGION_ALL 0xff /* word valid in all regions */
540
Bram Moolenaar5195e452005-08-19 20:32:47 +0000541#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
542#define VIMSPELLMAGICL 8
543#define VIMSPELLVERSION 50
544
Bram Moolenaar4770d092006-01-12 23:22:24 +0000545#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
546#define VIMSUGMAGICL 6
547#define VIMSUGVERSION 1
548
Bram Moolenaar5195e452005-08-19 20:32:47 +0000549/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
550#define SN_REGION 0 /* <regionname> section */
551#define SN_CHARFLAGS 1 /* charflags section */
552#define SN_MIDWORD 2 /* <midword> section */
553#define SN_PREFCOND 3 /* <prefcond> section */
554#define SN_REP 4 /* REP items section */
555#define SN_SAL 5 /* SAL items section */
556#define SN_SOFO 6 /* soundfolding section */
557#define SN_MAP 7 /* MAP items section */
558#define SN_COMPOUND 8 /* compound words section */
559#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000560#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000561#define SN_SUGFILE 11 /* timestamp for .sug file */
562#define SN_REPSAL 12 /* REPSAL items section */
563#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000564#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000565#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000566#define SN_END 255 /* end of sections */
567
568#define SNF_REQUIRED 1 /* <sectionflags>: required section */
569
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000570/* Result values. Lower number is accepted over higher one. */
571#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000572#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573#define SP_RARE 1
574#define SP_LOCAL 2
575#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000576
Bram Moolenaar7887d882005-07-01 22:33:52 +0000577/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000578static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000579
Bram Moolenaar4770d092006-01-12 23:22:24 +0000580typedef struct wordcount_S
581{
582 short_u wc_count; /* nr of times word was seen */
583 char_u wc_word[1]; /* word, actually longer */
584} wordcount_T;
585
586static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000587#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000588#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
589#define MAXWORDCOUNT 0xffff
590
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000591/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000592 * Information used when looking for suggestions.
593 */
594typedef struct suginfo_S
595{
596 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000599 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601 char_u *su_badptr; /* start of bad word in line */
602 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000603 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
605 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000606 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000608 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609} suginfo_T;
610
611/* One word suggestion. Used in "si_ga". */
612typedef struct suggest_S
613{
614 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000615 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000616 int st_orglen; /* length of replaced text */
617 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 int st_altscore; /* used when st_score compares equal */
619 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000620 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000621 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622} suggest_T;
623
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625
Bram Moolenaar4770d092006-01-12 23:22:24 +0000626/* TRUE if a word appears in the list of banned words. */
627#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
628
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000629/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000630 * what is displayed, because when rescore_suggestions() is called the score
631 * may change and wrong suggestions may be removed later. */
632#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000633
634/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
635 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000636#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000637
638/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000639#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000640#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000641#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000642#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000644#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000645#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000646#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#define SCORE_SUBST 93 /* substitute a character */
648#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000649#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000650#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000651#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000652#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000653#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000654#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000655#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000656#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000657
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000658#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000659#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
660 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000661
Bram Moolenaar4770d092006-01-12 23:22:24 +0000662#define SCORE_COMMON1 30 /* subtracted for words seen before */
663#define SCORE_COMMON2 40 /* subtracted for words often seen */
664#define SCORE_COMMON3 50 /* subtracted for words very often seen */
665#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
666#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
667
668/* When trying changed soundfold words it becomes slow when trying more than
669 * two changes. With less then two changes it's slightly faster but we miss a
670 * few good suggestions. In rare cases we need to try three of four changes.
671 */
672#define SCORE_SFMAX1 200 /* maximum score for first try */
673#define SCORE_SFMAX2 300 /* maximum score for second try */
674#define SCORE_SFMAX3 400 /* maximum score for third try */
675
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000676#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000677#define SCORE_MAXMAX 999999 /* accept any score */
678#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
679
680/* for spell_edit_score_limit() we need to know the minimum value of
681 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
682#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000683
684/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000685 * Structure to store info for word matching.
686 */
687typedef struct matchinf_S
688{
689 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000690
691 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000692 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000693 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000694 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000695 char_u *mi_cend; /* char after what was used for
696 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697
698 /* case-folded text */
699 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000700 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000701
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000702 /* for when checking word after a prefix */
703 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000704 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000705 int mi_prefcnt; /* number of entries at mi_prefarridx */
706 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000707#ifdef FEAT_MBYTE
708 int mi_cprefixlen; /* byte length of prefix in original
709 case */
710#else
711# define mi_cprefixlen mi_prefixlen /* it's the same value */
712#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000713
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000714 /* for when checking a compound word */
715 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000716 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
717 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000718 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000719
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000720 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000721 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000722 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200723 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000724
725 /* for NOBREAK */
726 int mi_result2; /* "mi_resul" without following word */
727 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000728} matchinf_T;
729
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000730/*
731 * The tables used for recognizing word characters according to spelling.
732 * These are only used for the first 256 characters of 'encoding'.
733 */
734typedef struct spelltab_S
735{
736 char_u st_isw[256]; /* flags: is word char */
737 char_u st_isu[256]; /* flags: is uppercase char */
738 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000739 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000740} spelltab_T;
741
742static spelltab_T spelltab;
743static int did_set_spelltab;
744
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745#define CF_WORD 0x01
746#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000747
748static void clear_spell_chartab __ARGS((spelltab_T *sp));
749static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200750static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000751static int spell_iswordp_nmw __ARGS((char_u *p));
752#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000753static int spell_mb_isword_class __ARGS((int cl));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200754static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000755#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000756static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000757
758/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000760 */
761typedef enum
762{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000763 STATE_START = 0, /* At start of node check for NUL bytes (goodword
764 * ends); if badword ends there is a match, otherwise
765 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000766 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000767 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000768 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
769 STATE_PLAIN, /* Use each byte of the node. */
770 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000771 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000772 STATE_INS, /* Insert a byte in the bad word. */
773 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000774 STATE_UNSWAP, /* Undo swap two characters. */
775 STATE_SWAP3, /* Swap two characters over three. */
776 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000778 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000779 STATE_REP_INI, /* Prepare for using REP items. */
780 STATE_REP, /* Use matching REP items from the .aff file. */
781 STATE_REP_UNDO, /* Undo a REP item replacement. */
782 STATE_FINAL /* End of this node. */
783} state_T;
784
785/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000786 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000787 */
788typedef struct trystate_S
789{
Bram Moolenaarea424162005-06-16 21:51:00 +0000790 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000791 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000792 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000793 short ts_curi; /* index in list of child nodes */
794 char_u ts_fidx; /* index in fword[], case-folded bad word */
795 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
796 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000797 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000798 * PFD_PREFIXTREE or PFD_NOPREFIX */
799 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000800#ifdef FEAT_MBYTE
801 char_u ts_tcharlen; /* number of bytes in tword character */
802 char_u ts_tcharidx; /* current byte index in tword character */
803 char_u ts_isdiff; /* DIFF_ values */
804 char_u ts_fcharstart; /* index in fword where badword char started */
805#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000806 char_u ts_prewordlen; /* length of word in "preword[]" */
807 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000808 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000809 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000810 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000811 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000812 char_u ts_delidx; /* index in fword for char that was deleted,
813 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000814} trystate_T;
815
Bram Moolenaarea424162005-06-16 21:51:00 +0000816/* values for ts_isdiff */
817#define DIFF_NONE 0 /* no different byte (yet) */
818#define DIFF_YES 1 /* different byte found */
819#define DIFF_INSERT 2 /* inserting character */
820
Bram Moolenaard12a1322005-08-21 22:08:24 +0000821/* values for ts_flags */
822#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
823#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000824#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000825
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000826/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000827#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000828#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000829#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000830
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000831/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000832#define FIND_FOLDWORD 0 /* find word case-folded */
833#define FIND_KEEPWORD 1 /* find keep-case word */
834#define FIND_PREFIX 2 /* find word after prefix */
835#define FIND_COMPOUND 3 /* find case-folded compound word */
836#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000837
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000838static slang_T *slang_alloc __ARGS((char_u *lang));
839static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000840static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000841static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000842static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000843static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000845static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
846static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000847static 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 +0000848static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000849static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000850static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000851static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000852static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000853static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000854static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000855static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000856static 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 +0000857static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000858static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
859static int read_charflags_section __ARGS((FILE *fd));
860static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000861static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000862static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000863static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
864static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
865static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
867static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000868static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000869static int init_syl_tab __ARGS((slang_T *slang));
870static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000871static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
872static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000873#ifdef FEAT_MBYTE
874static int *mb_str2wide __ARGS((char_u *s));
875#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000876static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200877static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
878static void clear_midword __ARGS((win_T *buf));
879static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880static int find_region __ARGS((char_u *rp, char_u *region));
881static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000883static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000884static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000885static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000887static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000888static 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 +0000889#ifdef FEAT_EVAL
890static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
891#endif
892static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000893static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
894static void suggest_load_files __ARGS((void));
895static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000896static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000897static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000898static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000899static void suggest_try_special __ARGS((suginfo_T *su));
900static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
902static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000903#ifdef FEAT_MBYTE
904static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000906static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000907static void score_comp_sal __ARGS((suginfo_T *su));
908static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000909static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000910static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000911static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000912static void suggest_try_soundalike_finish __ARGS((void));
913static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
914static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000916static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static 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));
919static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000920static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000921static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000922static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000923static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000924static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
925static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
926static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000927#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000929#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000931static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
932static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
933#ifdef FEAT_MBYTE
934static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
935#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000936static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
937static 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 +0000938static buf_T *open_spellbuf __ARGS((void));
939static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000940
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000941/*
942 * Use our own character-case definitions, because the current locale may
943 * differ from what the .spl file uses.
944 * These must not be called with negative number!
945 */
946#ifndef FEAT_MBYTE
947/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000948# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
949# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000950# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
951#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000952# if defined(HAVE_WCHAR_H)
953# include <wchar.h> /* for towupper() and towlower() */
954# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000955/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
956 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
957 * the "w" library function for characters above 255 if available. */
958# ifdef HAVE_TOWLOWER
959# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000960 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000961# else
962# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000963 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000964# endif
965
966# ifdef HAVE_TOWUPPER
967# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000968 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000969# else
970# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000971 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000972# endif
973
974# ifdef HAVE_ISWUPPER
975# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
976 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
977# else
978# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000979 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000980# endif
981#endif
982
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000983
984static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000985static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000986static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000987static char *e_affname = N_("Affix name too long in %s line %d: %s");
988static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
989static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000990static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000992/* Remember what "z?" replaced. */
993static char_u *repl_from = NULL;
994static char_u *repl_to = NULL;
995
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000996/*
997 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000998 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000999 * "*attrp" is set to the highlight index for a badly spelled word. For a
1000 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001002 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001003 * "capcol" is used to check for a Capitalised word after the end of a
1004 * sentence. If it's zero then perform the check. Return the column where to
1005 * check next, or -1 when no sentence end was found. If it's NULL then don't
1006 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001007 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 * Returns the length of the word in bytes, also when it's OK, so that the
1009 * caller can skip over the word.
1010 */
1011 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001015 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001016 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001017 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018{
1019 matchinf_T mi; /* Most things are put in "mi" so that it can
1020 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001021 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001023 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001024 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001025 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001026
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001027 /* A word never starts at a space or a control character. Return quickly
1028 * then, skipping over the character. */
1029 if (*ptr <= ' ')
1030 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001031
1032 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001033 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001034 return 1;
1035
Bram Moolenaar5195e452005-08-19 20:32:47 +00001036 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001038 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001039 * 0X99FF. But always do check spelling to find "3GPP" and "11
1040 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001043 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1044 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045 else
1046 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001047 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001049
Bram Moolenaar0c405862005-06-22 22:26:26 +00001050 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001052 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001053 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001057 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001058 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001059
Bram Moolenaar860cae12010-06-05 23:22:07 +02001060 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001061 {
1062 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001063 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001064 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001065 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001066 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001067 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (capcol != NULL)
1069 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001070
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 /* We always use the characters up to the next non-word character,
1072 * also for bad words. */
1073 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001076 mi.mi_capflags = 0;
1077 mi.mi_cend = NULL;
1078 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001079
Bram Moolenaar5195e452005-08-19 20:32:47 +00001080 /* case-fold the word with one non-word character, so that we can check
1081 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001082 if (*mi.mi_fend != NUL)
1083 mb_ptr_adv(mi.mi_fend);
1084
1085 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1086 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001087 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001088
1089 /* The word is bad unless we recognize it. */
1090 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001091 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001092
1093 /*
1094 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001095 * We check them all, because a word may be matched longer in another
1096 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001097 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001098 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001099 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001100 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001101
1102 /* If reloading fails the language is still in the list but everything
1103 * has been cleared. */
1104 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1105 continue;
1106
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001107 /* Check for a matching word in case-folded words. */
1108 find_word(&mi, FIND_FOLDWORD);
1109
1110 /* Check for a matching word in keep-case words. */
1111 find_word(&mi, FIND_KEEPWORD);
1112
1113 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001114 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001115
1116 /* For a NOBREAK language, may want to use a word without a following
1117 * word as a backup. */
1118 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1119 && mi.mi_result2 != SP_BAD)
1120 {
1121 mi.mi_result = mi.mi_result2;
1122 mi.mi_end = mi.mi_end2;
1123 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001124
1125 /* Count the word in the first language where it's found to be OK. */
1126 if (count_word && mi.mi_result == SP_OK)
1127 {
1128 count_common_word(mi.mi_lp->lp_slang, ptr,
1129 (int)(mi.mi_end - ptr), 1);
1130 count_word = FALSE;
1131 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001132 }
1133
1134 if (mi.mi_result != SP_OK)
1135 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001136 /* If we found a number skip over it. Allows for "42nd". Do flag
1137 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001138 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001139 {
1140 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1141 return nrlen;
1142 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001143
1144 /* When we are at a non-word character there is no error, just
1145 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001146 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001147 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001148 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001149 {
1150 regmatch_T regmatch;
1151
1152 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001153 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001154 regmatch.rm_ic = FALSE;
1155 if (vim_regexec(&regmatch, ptr, 0))
1156 *capcol = (int)(regmatch.endp[0] - ptr);
1157 }
1158
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001159#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001160 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001161 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001162#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001163 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001165 else if (mi.mi_end == ptr)
1166 /* Always include at least one character. Required for when there
1167 * is a mixup in "midword". */
1168 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001169 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001170 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 {
1172 char_u *p, *fp;
1173 int save_result = mi.mi_result;
1174
1175 /* First language in 'spelllang' is NOBREAK. Find first position
1176 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001178 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001179 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001180 p = mi.mi_word;
1181 fp = mi.mi_fword;
1182 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001183 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001184 mb_ptr_adv(p);
1185 mb_ptr_adv(fp);
1186 if (p >= mi.mi_end)
1187 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001188 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001189 find_word(&mi, FIND_COMPOUND);
1190 if (mi.mi_result != SP_BAD)
1191 {
1192 mi.mi_end = p;
1193 break;
1194 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001195 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001196 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001199
1200 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001201 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001202 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001204 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001205 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001206 }
1207
Bram Moolenaar5195e452005-08-19 20:32:47 +00001208 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1209 {
1210 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001211 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001212 return wrongcaplen;
1213 }
1214
Bram Moolenaar51485f02005-06-04 21:55:20 +00001215 return (int)(mi.mi_end - ptr);
1216}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001217
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218/*
1219 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1221 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1222 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1223 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 *
1225 * For a match mip->mi_result is updated.
1226 */
1227 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001232 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001234 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 int endidxcnt = 0;
1236 int len;
1237 int wlen = 0;
1238 int flen;
1239 int c;
1240 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001241 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242#ifdef FEAT_MBYTE
1243 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001244#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001245 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001246 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 slang_T *slang = mip->mi_lp->lp_slang;
1248 unsigned flags;
1249 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001250 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001251 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001252 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001253 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001254
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001255 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001256 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001257 /* Check for word with matching case in keep-case tree. */
1258 ptr = mip->mi_word;
1259 flen = 9999; /* no case folding, always enough bytes */
1260 byts = slang->sl_kbyts;
1261 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001262
1263 if (mode == FIND_KEEPCOMPOUND)
1264 /* Skip over the previously found word(s). */
1265 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001266 }
1267 else
1268 {
1269 /* Check for case-folded in case-folded tree. */
1270 ptr = mip->mi_fword;
1271 flen = mip->mi_fwordlen; /* available case-folded bytes */
1272 byts = slang->sl_fbyts;
1273 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001274
1275 if (mode == FIND_PREFIX)
1276 {
1277 /* Skip over the prefix. */
1278 wlen = mip->mi_prefixlen;
1279 flen -= mip->mi_prefixlen;
1280 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001281 else if (mode == FIND_COMPOUND)
1282 {
1283 /* Skip over the previously found word(s). */
1284 wlen = mip->mi_compoff;
1285 flen -= mip->mi_compoff;
1286 }
1287
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001288 }
1289
Bram Moolenaar51485f02005-06-04 21:55:20 +00001290 if (byts == NULL)
1291 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001292
Bram Moolenaar51485f02005-06-04 21:55:20 +00001293 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001294 * Repeat advancing in the tree until:
1295 * - there is a byte that doesn't match,
1296 * - we reach the end of the tree,
1297 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001298 */
1299 for (;;)
1300 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001301 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001302 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001303
1304 len = byts[arridx++];
1305
1306 /* If the first possible byte is a zero the word could end here.
1307 * Remember this index, we first check for the longest word. */
1308 if (byts[arridx] == 0)
1309 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001310 if (endidxcnt == MAXWLEN)
1311 {
1312 /* Must be a corrupted spell file. */
1313 EMSG(_(e_format));
1314 return;
1315 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001316 endlen[endidxcnt] = wlen;
1317 endidx[endidxcnt++] = arridx++;
1318 --len;
1319
1320 /* Skip over the zeros, there can be several flag/region
1321 * combinations. */
1322 while (len > 0 && byts[arridx] == 0)
1323 {
1324 ++arridx;
1325 --len;
1326 }
1327 if (len == 0)
1328 break; /* no children, word must end here */
1329 }
1330
1331 /* Stop looking at end of the line. */
1332 if (ptr[wlen] == NUL)
1333 break;
1334
1335 /* Perform a binary search in the list of accepted bytes. */
1336 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001337 if (c == TAB) /* <Tab> is handled like <Space> */
1338 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001339 lo = arridx;
1340 hi = arridx + len - 1;
1341 while (lo < hi)
1342 {
1343 m = (lo + hi) / 2;
1344 if (byts[m] > c)
1345 hi = m - 1;
1346 else if (byts[m] < c)
1347 lo = m + 1;
1348 else
1349 {
1350 lo = hi = m;
1351 break;
1352 }
1353 }
1354
1355 /* Stop if there is no matching byte. */
1356 if (hi < lo || byts[lo] != c)
1357 break;
1358
1359 /* Continue at the child (if there is one). */
1360 arridx = idxs[lo];
1361 ++wlen;
1362 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001363
1364 /* One space in the good word may stand for several spaces in the
1365 * checked word. */
1366 if (c == ' ')
1367 {
1368 for (;;)
1369 {
1370 if (flen <= 0 && *mip->mi_fend != NUL)
1371 flen = fold_more(mip);
1372 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1373 break;
1374 ++wlen;
1375 --flen;
1376 }
1377 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001378 }
1379
1380 /*
1381 * Verify that one of the possible endings is valid. Try the longest
1382 * first.
1383 */
1384 while (endidxcnt > 0)
1385 {
1386 --endidxcnt;
1387 arridx = endidx[endidxcnt];
1388 wlen = endlen[endidxcnt];
1389
1390#ifdef FEAT_MBYTE
1391 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1392 continue; /* not at first byte of character */
1393#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001394 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001395 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001396 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 continue; /* next char is a word character */
1398 word_ends = FALSE;
1399 }
1400 else
1401 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001402 /* The prefix flag is before compound flags. Once a valid prefix flag
1403 * has been found we try compound flags. */
1404 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001405
1406#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001407 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001408 {
1409 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001410 * when folding case. This can be slow, take a shortcut when the
1411 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001412 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 if (STRNCMP(ptr, p, wlen) != 0)
1414 {
1415 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1416 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001417 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001418 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001419 }
1420#endif
1421
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001422 /* Check flags and region. For FIND_PREFIX check the condition and
1423 * prefix ID.
1424 * Repeat this if there are more flags/region alternatives until there
1425 * is a match. */
1426 res = SP_BAD;
1427 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1428 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001429 {
1430 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001431
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001432 /* For the fold-case tree check that the case of the checked word
1433 * matches with what the word in the tree requires.
1434 * For keep-case tree the case is always right. For prefixes we
1435 * don't bother to check. */
1436 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001437 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001438 if (mip->mi_cend != mip->mi_word + wlen)
1439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001440 /* mi_capflags was set for a different word length, need
1441 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001442 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 }
1445
Bram Moolenaar0c405862005-06-22 22:26:26 +00001446 if (mip->mi_capflags == WF_KEEPCAP
1447 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001448 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449 }
1450
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001451 /* When mode is FIND_PREFIX the word must support the prefix:
1452 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001453 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001454 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001455 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001456 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001457 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001458 mip->mi_word + mip->mi_cprefixlen, slang,
1459 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001460 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001461 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462
1463 /* Use the WF_RARE flag for a rare prefix. */
1464 if (c & WF_RAREPFX)
1465 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001466 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001467 }
1468
Bram Moolenaar78622822005-08-23 21:00:13 +00001469 if (slang->sl_nobreak)
1470 {
1471 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1472 && (flags & WF_BANNED) == 0)
1473 {
1474 /* NOBREAK: found a valid following word. That's all we
1475 * need to know, so return. */
1476 mip->mi_result = SP_OK;
1477 break;
1478 }
1479 }
1480
1481 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1482 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001483 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001484 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001485 * COMPOUNDMIN reject it quickly.
1486 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001487 * that's too short... Myspell compatibility requires this
1488 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001489 if (((unsigned)flags >> 24) == 0
1490 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001491 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001492#ifdef FEAT_MBYTE
1493 /* For multi-byte chars check character length against
1494 * COMPOUNDMIN. */
1495 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001496 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001497 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1498 wlen - mip->mi_compoff) < slang->sl_compminlen)
1499 continue;
1500#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001501
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001502 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001503 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001504 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1505 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001506 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001507 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001508
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001509 /* Don't allow compounding on a side where an affix was added,
1510 * unless COMPOUNDPERMITFLAG was used. */
1511 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1512 continue;
1513 if (!word_ends && (flags & WF_NOCOMPAFT))
1514 continue;
1515
Bram Moolenaard12a1322005-08-21 22:08:24 +00001516 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001517 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001518 ? slang->sl_compstartflags
1519 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001520 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001521 continue;
1522
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001523 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1524 * discard the compound word. */
1525 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1526 continue;
1527
Bram Moolenaare52325c2005-08-22 22:54:29 +00001528 if (mode == FIND_COMPOUND)
1529 {
1530 int capflags;
1531
1532 /* Need to check the caps type of the appended compound
1533 * word. */
1534#ifdef FEAT_MBYTE
1535 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1536 mip->mi_compoff) != 0)
1537 {
1538 /* case folding may have changed the length */
1539 p = mip->mi_word;
1540 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1541 mb_ptr_adv(p);
1542 }
1543 else
1544#endif
1545 p = mip->mi_word + mip->mi_compoff;
1546 capflags = captype(p, mip->mi_word + wlen);
1547 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1548 && (flags & WF_FIXCAP) != 0))
1549 continue;
1550
1551 if (capflags != WF_ALLCAP)
1552 {
1553 /* When the character before the word is a word
1554 * character we do not accept a Onecap word. We do
1555 * accept a no-caps word, even when the dictionary
1556 * word specifies ONECAP. */
1557 mb_ptr_back(mip->mi_word, p);
1558 if (spell_iswordp_nmw(p)
1559 ? capflags == WF_ONECAP
1560 : (flags & WF_ONECAP) != 0
1561 && capflags != WF_ONECAP)
1562 continue;
1563 }
1564 }
1565
Bram Moolenaar5195e452005-08-19 20:32:47 +00001566 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001567 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001568 * the number of syllables must not be too large. */
1569 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1570 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1571 if (word_ends)
1572 {
1573 char_u fword[MAXWLEN];
1574
1575 if (slang->sl_compsylmax < MAXWLEN)
1576 {
1577 /* "fword" is only needed for checking syllables. */
1578 if (ptr == mip->mi_word)
1579 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1580 else
1581 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1582 }
1583 if (!can_compound(slang, fword, mip->mi_compflags))
1584 continue;
1585 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001586 else if (slang->sl_comprules != NULL
1587 && !match_compoundrule(slang, mip->mi_compflags))
1588 /* The compound flags collected so far do not match any
1589 * COMPOUNDRULE, discard the compounded word. */
1590 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001591 }
1592
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001593 /* Check NEEDCOMPOUND: can't use word without compounding. */
1594 else if (flags & WF_NEEDCOMP)
1595 continue;
1596
Bram Moolenaar78622822005-08-23 21:00:13 +00001597 nobreak_result = SP_OK;
1598
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001599 if (!word_ends)
1600 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001601 int save_result = mip->mi_result;
1602 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001603 langp_T *save_lp = mip->mi_lp;
1604 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001605
1606 /* Check that a valid word follows. If there is one and we
1607 * are compounding, it will set "mi_result", thus we are
1608 * always finished here. For NOBREAK we only check that a
1609 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001610 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001611 if (slang->sl_nobreak)
1612 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001613
1614 /* Find following word in case-folded tree. */
1615 mip->mi_compoff = endlen[endidxcnt];
1616#ifdef FEAT_MBYTE
1617 if (has_mbyte && mode == FIND_KEEPWORD)
1618 {
1619 /* Compute byte length in case-folded word from "wlen":
1620 * byte length in keep-case word. Length may change when
1621 * folding case. This can be slow, take a shortcut when
1622 * the case-folded word is equal to the keep-case word. */
1623 p = mip->mi_fword;
1624 if (STRNCMP(ptr, p, wlen) != 0)
1625 {
1626 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1627 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001628 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001629 }
1630 }
1631#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001632 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001633 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001634 if (flags & WF_COMPROOT)
1635 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001636
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001637 /* For NOBREAK we need to try all NOBREAK languages, at least
1638 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001639 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001640 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001641 if (slang->sl_nobreak)
1642 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001643 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001644 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1645 || !mip->mi_lp->lp_slang->sl_nobreak)
1646 continue;
1647 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001648
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649 find_word(mip, FIND_COMPOUND);
1650
1651 /* When NOBREAK any word that matches is OK. Otherwise we
1652 * need to find the longest match, thus try with keep-case
1653 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001654 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1655 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001656 /* Find following word in keep-case tree. */
1657 mip->mi_compoff = wlen;
1658 find_word(mip, FIND_KEEPCOMPOUND);
1659
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001660#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1661 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1662 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1664 {
1665 /* Check for following word with prefix. */
1666 mip->mi_compoff = c;
1667 find_prefix(mip, FIND_COMPOUND);
1668 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001669#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001670 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001671
1672 if (!slang->sl_nobreak)
1673 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001674 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001675 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001676 if (flags & WF_COMPROOT)
1677 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001678 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001679
Bram Moolenaar78622822005-08-23 21:00:13 +00001680 if (slang->sl_nobreak)
1681 {
1682 nobreak_result = mip->mi_result;
1683 mip->mi_result = save_result;
1684 mip->mi_end = save_end;
1685 }
1686 else
1687 {
1688 if (mip->mi_result == SP_OK)
1689 break;
1690 continue;
1691 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001692 }
1693
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001694 if (flags & WF_BANNED)
1695 res = SP_BANNED;
1696 else if (flags & WF_REGION)
1697 {
1698 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001699 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001700 res = SP_OK;
1701 else
1702 res = SP_LOCAL;
1703 }
1704 else if (flags & WF_RARE)
1705 res = SP_RARE;
1706 else
1707 res = SP_OK;
1708
Bram Moolenaar78622822005-08-23 21:00:13 +00001709 /* Always use the longest match and the best result. For NOBREAK
1710 * we separately keep the longest match without a following good
1711 * word as a fall-back. */
1712 if (nobreak_result == SP_BAD)
1713 {
1714 if (mip->mi_result2 > res)
1715 {
1716 mip->mi_result2 = res;
1717 mip->mi_end2 = mip->mi_word + wlen;
1718 }
1719 else if (mip->mi_result2 == res
1720 && mip->mi_end2 < mip->mi_word + wlen)
1721 mip->mi_end2 = mip->mi_word + wlen;
1722 }
1723 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 {
1725 mip->mi_result = res;
1726 mip->mi_end = mip->mi_word + wlen;
1727 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001728 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001729 mip->mi_end = mip->mi_word + wlen;
1730
Bram Moolenaar78622822005-08-23 21:00:13 +00001731 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001732 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001733 }
1734
Bram Moolenaar78622822005-08-23 21:00:13 +00001735 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001736 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001737 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001738}
1739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001740/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001741 * Return TRUE if there is a match between the word ptr[wlen] and
1742 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1743 * word.
1744 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1745 * end of ptr[wlen] and the second part matches after it.
1746 */
1747 static int
1748match_checkcompoundpattern(ptr, wlen, gap)
1749 char_u *ptr;
1750 int wlen;
1751 garray_T *gap; /* &sl_comppat */
1752{
1753 int i;
1754 char_u *p;
1755 int len;
1756
1757 for (i = 0; i + 1 < gap->ga_len; i += 2)
1758 {
1759 p = ((char_u **)gap->ga_data)[i + 1];
1760 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1761 {
1762 /* Second part matches at start of following compound word, now
1763 * check if first part matches at end of previous word. */
1764 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001765 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001766 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1767 return TRUE;
1768 }
1769 }
1770 return FALSE;
1771}
1772
1773/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001774 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1775 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001776 */
1777 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001778can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001779 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001780 char_u *word;
1781 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001782{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001783 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001784#ifdef FEAT_MBYTE
1785 char_u uflags[MAXWLEN * 2];
1786 int i;
1787#endif
1788 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001789
1790 if (slang->sl_compprog == NULL)
1791 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001792#ifdef FEAT_MBYTE
1793 if (enc_utf8)
1794 {
1795 /* Need to convert the single byte flags to utf8 characters. */
1796 p = uflags;
1797 for (i = 0; flags[i] != NUL; ++i)
1798 p += mb_char2bytes(flags[i], p);
1799 *p = NUL;
1800 p = uflags;
1801 }
1802 else
1803#endif
1804 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001805 regmatch.regprog = slang->sl_compprog;
1806 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001807 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001808 return FALSE;
1809
Bram Moolenaare52325c2005-08-22 22:54:29 +00001810 /* Count the number of syllables. This may be slow, do it last. If there
1811 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001812 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001813 if (slang->sl_compsylmax < MAXWLEN
1814 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001815 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001816 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001817}
1818
1819/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001820 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1821 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1822 * lines if they don't contain wildcards.
1823 */
1824 static int
1825can_be_compound(sp, slang, compflags, flag)
1826 trystate_T *sp;
1827 slang_T *slang;
1828 char_u *compflags;
1829 int flag;
1830{
1831 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1832 * then it can't possibly compound. */
1833 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1834 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1835 return FALSE;
1836
1837 /* If there are no wildcards, we can check if the flags collected so far
1838 * possibly can form a match with COMPOUNDRULE patterns. This only
1839 * makes sense when we have two or more words. */
1840 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1841 {
1842 int v;
1843
1844 compflags[sp->ts_complen] = flag;
1845 compflags[sp->ts_complen + 1] = NUL;
1846 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1847 compflags[sp->ts_complen] = NUL;
1848 return v;
1849 }
1850
1851 return TRUE;
1852}
1853
1854
1855/*
1856 * Return TRUE if the compound flags in compflags[] match the start of any
1857 * compound rule. This is used to stop trying a compound if the flags
1858 * collected so far can't possibly match any compound rule.
1859 * Caller must check that slang->sl_comprules is not NULL.
1860 */
1861 static int
1862match_compoundrule(slang, compflags)
1863 slang_T *slang;
1864 char_u *compflags;
1865{
1866 char_u *p;
1867 int i;
1868 int c;
1869
1870 /* loop over all the COMPOUNDRULE entries */
1871 for (p = slang->sl_comprules; *p != NUL; ++p)
1872 {
1873 /* loop over the flags in the compound word we have made, match
1874 * them against the current rule entry */
1875 for (i = 0; ; ++i)
1876 {
1877 c = compflags[i];
1878 if (c == NUL)
1879 /* found a rule that matches for the flags we have so far */
1880 return TRUE;
1881 if (*p == '/' || *p == NUL)
1882 break; /* end of rule, it's too short */
1883 if (*p == '[')
1884 {
1885 int match = FALSE;
1886
1887 /* compare against all the flags in [] */
1888 ++p;
1889 while (*p != ']' && *p != NUL)
1890 if (*p++ == c)
1891 match = TRUE;
1892 if (!match)
1893 break; /* none matches */
1894 }
1895 else if (*p != c)
1896 break; /* flag of word doesn't match flag in pattern */
1897 ++p;
1898 }
1899
1900 /* Skip to the next "/", where the next pattern starts. */
1901 p = vim_strchr(p, '/');
1902 if (p == NULL)
1903 break;
1904 }
1905
1906 /* Checked all the rules and none of them match the flags, so there
1907 * can't possibly be a compound starting with these flags. */
1908 return FALSE;
1909}
1910
1911/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001912 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1913 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001914 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001915 */
1916 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001917valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001918 int totprefcnt; /* nr of prefix IDs */
1919 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001920 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001921 char_u *word;
1922 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001923 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001924{
1925 int prefcnt;
1926 int pidx;
1927 regprog_T *rp;
1928 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001929 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001930
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001931 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001932 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1933 {
1934 pidx = slang->sl_pidxs[arridx + prefcnt];
1935
1936 /* Check the prefix ID. */
1937 if (prefid != (pidx & 0xff))
1938 continue;
1939
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001940 /* Check if the prefix doesn't combine and the word already has a
1941 * suffix. */
1942 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1943 continue;
1944
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001945 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001946 * stored in the two bytes above the prefix ID byte. */
1947 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001948 if (rp != NULL)
1949 {
1950 regmatch.regprog = rp;
1951 regmatch.rm_ic = FALSE;
1952 if (!vim_regexec(&regmatch, word, 0))
1953 continue;
1954 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001955 else if (cond_req)
1956 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001957
Bram Moolenaar53805d12005-08-01 07:08:33 +00001958 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001959 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001960 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001961 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001962}
1963
1964/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001965 * Check if the word at "mip->mi_word" has a matching prefix.
1966 * If it does, then check the following word.
1967 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001968 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1969 * prefix in a compound word.
1970 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971 * For a match mip->mi_result is updated.
1972 */
1973 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001974find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001976 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977{
1978 idx_T arridx = 0;
1979 int len;
1980 int wlen = 0;
1981 int flen;
1982 int c;
1983 char_u *ptr;
1984 idx_T lo, hi, m;
1985 slang_T *slang = mip->mi_lp->lp_slang;
1986 char_u *byts;
1987 idx_T *idxs;
1988
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001989 byts = slang->sl_pbyts;
1990 if (byts == NULL)
1991 return; /* array is empty */
1992
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001993 /* We use the case-folded word here, since prefixes are always
1994 * case-folded. */
1995 ptr = mip->mi_fword;
1996 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001997 if (mode == FIND_COMPOUND)
1998 {
1999 /* Skip over the previously found word(s). */
2000 ptr += mip->mi_compoff;
2001 flen -= mip->mi_compoff;
2002 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002003 idxs = slang->sl_pidxs;
2004
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002005 /*
2006 * Repeat advancing in the tree until:
2007 * - there is a byte that doesn't match,
2008 * - we reach the end of the tree,
2009 * - or we reach the end of the line.
2010 */
2011 for (;;)
2012 {
2013 if (flen == 0 && *mip->mi_fend != NUL)
2014 flen = fold_more(mip);
2015
2016 len = byts[arridx++];
2017
2018 /* If the first possible byte is a zero the prefix could end here.
2019 * Check if the following word matches and supports the prefix. */
2020 if (byts[arridx] == 0)
2021 {
2022 /* There can be several prefixes with different conditions. We
2023 * try them all, since we don't know which one will give the
2024 * longest match. The word is the same each time, pass the list
2025 * of possible prefixes to find_word(). */
2026 mip->mi_prefarridx = arridx;
2027 mip->mi_prefcnt = len;
2028 while (len > 0 && byts[arridx] == 0)
2029 {
2030 ++arridx;
2031 --len;
2032 }
2033 mip->mi_prefcnt -= len;
2034
2035 /* Find the word that comes after the prefix. */
2036 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002037 if (mode == FIND_COMPOUND)
2038 /* Skip over the previously found word(s). */
2039 mip->mi_prefixlen += mip->mi_compoff;
2040
Bram Moolenaar53805d12005-08-01 07:08:33 +00002041#ifdef FEAT_MBYTE
2042 if (has_mbyte)
2043 {
2044 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002045 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2046 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002047 }
2048 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002049 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002050#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002051 find_word(mip, FIND_PREFIX);
2052
2053
2054 if (len == 0)
2055 break; /* no children, word must end here */
2056 }
2057
2058 /* Stop looking at end of the line. */
2059 if (ptr[wlen] == NUL)
2060 break;
2061
2062 /* Perform a binary search in the list of accepted bytes. */
2063 c = ptr[wlen];
2064 lo = arridx;
2065 hi = arridx + len - 1;
2066 while (lo < hi)
2067 {
2068 m = (lo + hi) / 2;
2069 if (byts[m] > c)
2070 hi = m - 1;
2071 else if (byts[m] < c)
2072 lo = m + 1;
2073 else
2074 {
2075 lo = hi = m;
2076 break;
2077 }
2078 }
2079
2080 /* Stop if there is no matching byte. */
2081 if (hi < lo || byts[lo] != c)
2082 break;
2083
2084 /* Continue at the child (if there is one). */
2085 arridx = idxs[lo];
2086 ++wlen;
2087 --flen;
2088 }
2089}
2090
2091/*
2092 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002093 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002094 * Return the length of the folded chars in bytes.
2095 */
2096 static int
2097fold_more(mip)
2098 matchinf_T *mip;
2099{
2100 int flen;
2101 char_u *p;
2102
2103 p = mip->mi_fend;
2104 do
2105 {
2106 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002107 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002108
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002109 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002110 if (*mip->mi_fend != NUL)
2111 mb_ptr_adv(mip->mi_fend);
2112
2113 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2114 mip->mi_fword + mip->mi_fwordlen,
2115 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002116 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002117 mip->mi_fwordlen += flen;
2118 return flen;
2119}
2120
2121/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002122 * Check case flags for a word. Return TRUE if the word has the requested
2123 * case.
2124 */
2125 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002126spell_valid_case(wordflags, treeflags)
2127 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 int treeflags; /* flags for the word in the spell tree */
2129{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002130 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002131 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002132 && ((treeflags & WF_ONECAP) == 0
2133 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134}
2135
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002136/*
2137 * Return TRUE if spell checking is not enabled.
2138 */
2139 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002140no_spell_checking(wp)
2141 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002142{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002143 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2144 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002145 {
2146 EMSG(_("E756: Spell checking is not enabled"));
2147 return TRUE;
2148 }
2149 return FALSE;
2150}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002151
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002152/*
2153 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002154 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2155 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002156 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2157 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002158 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159 */
2160 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002161spell_move_to(wp, dir, allwords, curline, attrp)
2162 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002164 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002165 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002166 hlf_T *attrp; /* return: attributes of bad word or NULL
2167 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002169 linenr_T lnum;
2170 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002171 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002172 char_u *line;
2173 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002174 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002175 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002176 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002177# ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002178 int has_syntax = syntax_present(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002179# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002180 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002181 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002182 char_u *buf = NULL;
2183 int buflen = 0;
2184 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002185 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002186 int found_one = FALSE;
2187 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002188
Bram Moolenaar95529562005-08-25 21:21:38 +00002189 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002190 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002191
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002192 /*
2193 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002194 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002195 *
2196 * When searching backwards, we continue in the line to find the last
2197 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002198 *
2199 * We concatenate the start of the next line, so that wrapped words work
2200 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2201 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002202 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002203 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002204 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002205
2206 while (!got_int)
2207 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002208 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002209
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002210 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002211 if (buflen < len + MAXWLEN + 2)
2212 {
2213 vim_free(buf);
2214 buflen = len + MAXWLEN + 2;
2215 buf = alloc(buflen);
2216 if (buf == NULL)
2217 break;
2218 }
2219
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002220 /* In first line check first word for Capital. */
2221 if (lnum == 1)
2222 capcol = 0;
2223
2224 /* For checking first word with a capital skip white space. */
2225 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002226 capcol = (int)(skipwhite(line) - line);
2227 else if (curline && wp == curwin)
2228 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002229 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002230 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002231 if (check_need_cap(lnum, col))
2232 capcol = col;
2233
2234 /* Need to get the line again, may have looked at the previous
2235 * one. */
2236 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2237 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002238
Bram Moolenaar0c405862005-06-22 22:26:26 +00002239 /* Copy the line into "buf" and append the start of the next line if
2240 * possible. */
2241 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002242 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002243 spell_cat_line(buf + STRLEN(buf),
2244 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245
2246 p = buf + skip;
2247 endp = buf + len;
2248 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002249 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002250 /* When searching backward don't search after the cursor. Unless
2251 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002252 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002253 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002254 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002255 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002256 break;
2257
2258 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002259 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002260 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002261
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002262 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002263 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002264 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002266 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267 /* When searching forward only accept a bad word after
2268 * the cursor. */
2269 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002270 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002271 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002272 && (wrapped
2273 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002274 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002275 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002276 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002277# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002278 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002279 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002280 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002281 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002282 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002283 if (!can_spell)
2284 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002285 }
2286 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002287#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002288 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002289
Bram Moolenaar51485f02005-06-04 21:55:20 +00002290 if (can_spell)
2291 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002292 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002293 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002294 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002295#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002297#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002298 if (dir == FORWARD)
2299 {
2300 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002301 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002302 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002303 if (attrp != NULL)
2304 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002305 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002306 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002307 else if (curline)
2308 /* Insert mode completion: put cursor after
2309 * the bad word. */
2310 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002311 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002312 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002314 else
2315 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002317 }
2318
Bram Moolenaar51485f02005-06-04 21:55:20 +00002319 /* advance to character after the word */
2320 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002321 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 }
2323
Bram Moolenaar5195e452005-08-19 20:32:47 +00002324 if (dir == BACKWARD && found_pos.lnum != 0)
2325 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002326 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002327 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002328 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002329 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 }
2331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002333 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002336 if (dir == BACKWARD)
2337 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002338 /* If we are back at the starting line and searched it again there
2339 * is no match, give up. */
2340 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002341 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002342
2343 if (lnum > 1)
2344 --lnum;
2345 else if (!p_ws)
2346 break; /* at first line and 'nowrapscan' */
2347 else
2348 {
2349 /* Wrap around to the end of the buffer. May search the
2350 * starting line again and accept the last match. */
2351 lnum = wp->w_buffer->b_ml.ml_line_count;
2352 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002353 if (!shortmess(SHM_SEARCH))
2354 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002355 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002356 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002357 }
2358 else
2359 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002360 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2361 ++lnum;
2362 else if (!p_ws)
2363 break; /* at first line and 'nowrapscan' */
2364 else
2365 {
2366 /* Wrap around to the start of the buffer. May search the
2367 * starting line again and accept the first match. */
2368 lnum = 1;
2369 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002370 if (!shortmess(SHM_SEARCH))
2371 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002372 }
2373
2374 /* If we are back at the starting line and there is no match then
2375 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002376 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002377 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002378
2379 /* Skip the characters at the start of the next line that were
2380 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002381 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002382 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002383 else
2384 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002386 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002387 --capcol;
2388
2389 /* But after empty line check first word in next line */
2390 if (*skipwhite(line) == NUL)
2391 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002392 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002393
2394 line_breakcheck();
2395 }
2396
Bram Moolenaar0c405862005-06-22 22:26:26 +00002397 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002398 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002399}
2400
2401/*
2402 * For spell checking: concatenate the start of the following line "line" into
2403 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002404 * Keep the blanks at the start of the next line, this is used in win_line()
2405 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002406 */
2407 void
2408spell_cat_line(buf, line, maxlen)
2409 char_u *buf;
2410 char_u *line;
2411 int maxlen;
2412{
2413 char_u *p;
2414 int n;
2415
2416 p = skipwhite(line);
2417 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2418 p = skipwhite(p + 1);
2419
2420 if (*p != NUL)
2421 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002422 /* Only worth concatenating if there is something else than spaces to
2423 * concatenate. */
2424 n = (int)(p - line) + 1;
2425 if (n < maxlen - 1)
2426 {
2427 vim_memset(buf, ' ', n);
2428 vim_strncpy(buf + n, p, maxlen - 1 - n);
2429 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002430 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002431}
2432
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002433/*
2434 * Structure used for the cookie argument of do_in_runtimepath().
2435 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002436typedef struct spelload_S
2437{
2438 char_u sl_lang[MAXWLEN + 1]; /* language name */
2439 slang_T *sl_slang; /* resulting slang_T struct */
2440 int sl_nobreak; /* NOBREAK language found */
2441} spelload_T;
2442
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002444 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002445 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002447 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002448spell_load_lang(lang)
2449 char_u *lang;
2450{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002453 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002454#ifdef FEAT_AUTOCMD
2455 int round;
2456#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002457
Bram Moolenaarb765d632005-06-07 21:00:02 +00002458 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002459 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002460 STRCPY(sl.sl_lang, lang);
2461 sl.sl_slang = NULL;
2462 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002463
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002464#ifdef FEAT_AUTOCMD
2465 /* We may retry when no spell file is found for the language, an
2466 * autocommand may load it then. */
2467 for (round = 1; round <= 2; ++round)
2468#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002470 /*
2471 * Find the first spell file for "lang" in 'runtimepath' and load it.
2472 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002473 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002474 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002475 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002476
2477 if (r == FAIL && *sl.sl_lang != NUL)
2478 {
2479 /* Try loading the ASCII version. */
2480 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2481 "spell/%s.ascii.spl", lang);
2482 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2483
2484#ifdef FEAT_AUTOCMD
2485 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2486 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2487 curbuf->b_fname, FALSE, curbuf))
2488 continue;
2489 break;
2490#endif
2491 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002492#ifdef FEAT_AUTOCMD
2493 break;
2494#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002495 }
2496
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002497 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002498 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002499 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2500 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002501 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002502 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002503 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002504 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002505 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002506 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002507 }
2508}
2509
2510/*
2511 * Return the encoding used for spell checking: Use 'encoding', except that we
2512 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2513 */
2514 static char_u *
2515spell_enc()
2516{
2517
2518#ifdef FEAT_MBYTE
2519 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2520 return p_enc;
2521#endif
2522 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002523}
2524
2525/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002526 * Get the name of the .spl file for the internal wordlist into
2527 * "fname[MAXPATHL]".
2528 */
2529 static void
2530int_wordlist_spl(fname)
2531 char_u *fname;
2532{
2533 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2534 int_wordlist, spell_enc());
2535}
2536
2537/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002538 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002539 * Caller must fill "sl_next".
2540 */
2541 static slang_T *
2542slang_alloc(lang)
2543 char_u *lang;
2544{
2545 slang_T *lp;
2546
Bram Moolenaar51485f02005-06-04 21:55:20 +00002547 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002548 if (lp != NULL)
2549 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002550 if (lang != NULL)
2551 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002552 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002553 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002554 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002555 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002556 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002558
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559 return lp;
2560}
2561
2562/*
2563 * Free the contents of an slang_T and the structure itself.
2564 */
2565 static void
2566slang_free(lp)
2567 slang_T *lp;
2568{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002570 vim_free(lp->sl_fname);
2571 slang_clear(lp);
2572 vim_free(lp);
2573}
2574
2575/*
2576 * Clear an slang_T so that the file can be reloaded.
2577 */
2578 static void
2579slang_clear(lp)
2580 slang_T *lp;
2581{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002582 garray_T *gap;
2583 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002584 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002585 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002586 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002587
Bram Moolenaar51485f02005-06-04 21:55:20 +00002588 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002589 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002590 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002592 vim_free(lp->sl_pbyts);
2593 lp->sl_pbyts = NULL;
2594
Bram Moolenaar51485f02005-06-04 21:55:20 +00002595 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002596 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002597 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002598 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002599 vim_free(lp->sl_pidxs);
2600 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601
Bram Moolenaar4770d092006-01-12 23:22:24 +00002602 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002603 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002604 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2605 while (gap->ga_len > 0)
2606 {
2607 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2608 vim_free(ftp->ft_from);
2609 vim_free(ftp->ft_to);
2610 }
2611 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002613
2614 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002615 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002616 {
2617 /* "ga_len" is set to 1 without adding an item for latin1 */
2618 if (gap->ga_data != NULL)
2619 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2620 for (i = 0; i < gap->ga_len; ++i)
2621 vim_free(((int **)gap->ga_data)[i]);
2622 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002623 else
2624 /* SAL items: free salitem_T items */
2625 while (gap->ga_len > 0)
2626 {
2627 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2628 vim_free(smp->sm_lead);
2629 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2630 vim_free(smp->sm_to);
2631#ifdef FEAT_MBYTE
2632 vim_free(smp->sm_lead_w);
2633 vim_free(smp->sm_oneof_w);
2634 vim_free(smp->sm_to_w);
2635#endif
2636 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002637 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002638
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002639 for (i = 0; i < lp->sl_prefixcnt; ++i)
2640 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002641 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002642 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002643 lp->sl_prefprog = NULL;
2644
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002645 vim_free(lp->sl_info);
2646 lp->sl_info = NULL;
2647
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002648 vim_free(lp->sl_midword);
2649 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002650
Bram Moolenaar5195e452005-08-19 20:32:47 +00002651 vim_free(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002652 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002653 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002654 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002655 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002656 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002657 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002658 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002659
2660 vim_free(lp->sl_syllable);
2661 lp->sl_syllable = NULL;
2662 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002663
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002664 ga_clear_strings(&lp->sl_comppat);
2665
Bram Moolenaar4770d092006-01-12 23:22:24 +00002666 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2667 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002668
Bram Moolenaar4770d092006-01-12 23:22:24 +00002669#ifdef FEAT_MBYTE
2670 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002671#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002672
Bram Moolenaar4770d092006-01-12 23:22:24 +00002673 /* Clear info from .sug file. */
2674 slang_clear_sug(lp);
2675
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002677 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 lp->sl_compsylmax = MAXWLEN;
2679 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680}
2681
2682/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002683 * Clear the info from the .sug file in "lp".
2684 */
2685 static void
2686slang_clear_sug(lp)
2687 slang_T *lp;
2688{
2689 vim_free(lp->sl_sbyts);
2690 lp->sl_sbyts = NULL;
2691 vim_free(lp->sl_sidxs);
2692 lp->sl_sidxs = NULL;
2693 close_spellbuf(lp->sl_sugbuf);
2694 lp->sl_sugbuf = NULL;
2695 lp->sl_sugloaded = FALSE;
2696 lp->sl_sugtime = 0;
2697}
2698
2699/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002700 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002701 * Invoked through do_in_runtimepath().
2702 */
2703 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002704spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002705 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002706 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002707{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002708 spelload_T *slp = (spelload_T *)cookie;
2709 slang_T *slang;
2710
2711 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2712 if (slang != NULL)
2713 {
2714 /* When a previously loaded file has NOBREAK also use it for the
2715 * ".add" files. */
2716 if (slp->sl_nobreak && slang->sl_add)
2717 slang->sl_nobreak = TRUE;
2718 else if (slang->sl_nobreak)
2719 slp->sl_nobreak = TRUE;
2720
2721 slp->sl_slang = slang;
2722 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002723}
2724
2725/*
2726 * Load one spell file and store the info into a slang_T.
2727 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002728 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002729 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2730 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2731 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2732 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002733 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002734 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2735 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002737 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002738 static slang_T *
2739spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002740 char_u *fname;
2741 char_u *lang;
2742 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002744{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002745 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002746 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002748 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002749 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002750 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002751 char_u *save_sourcing_name = sourcing_name;
2752 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002753 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002754 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002755 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756
Bram Moolenaarb765d632005-06-07 21:00:02 +00002757 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002758 if (fd == NULL)
2759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 if (!silent)
2761 EMSG2(_(e_notopen), fname);
2762 else if (p_verbose > 2)
2763 {
2764 verbose_enter();
2765 smsg((char_u *)e_notopen, fname);
2766 verbose_leave();
2767 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002768 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002770 if (p_verbose > 2)
2771 {
2772 verbose_enter();
2773 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2774 verbose_leave();
2775 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776
Bram Moolenaarb765d632005-06-07 21:00:02 +00002777 if (old_lp == NULL)
2778 {
2779 lp = slang_alloc(lang);
2780 if (lp == NULL)
2781 goto endFAIL;
2782
2783 /* Remember the file name, used to reload the file when it's updated. */
2784 lp->sl_fname = vim_strsave(fname);
2785 if (lp->sl_fname == NULL)
2786 goto endFAIL;
2787
2788 /* Check for .add.spl. */
2789 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2790 }
2791 else
2792 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002793
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 /* Set sourcing_name, so that error messages mention the file name. */
2795 sourcing_name = fname;
2796 sourcing_lnum = 0;
2797
Bram Moolenaar4770d092006-01-12 23:22:24 +00002798 /*
2799 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002800 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801 for (i = 0; i < VIMSPELLMAGICL; ++i)
2802 buf[i] = getc(fd); /* <fileID> */
2803 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2804 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002805 EMSG(_("E757: This does not look like a spell file"));
2806 goto endFAIL;
2807 }
2808 c = getc(fd); /* <versionnr> */
2809 if (c < VIMSPELLVERSION)
2810 {
2811 EMSG(_("E771: Old spell file, needs to be updated"));
2812 goto endFAIL;
2813 }
2814 else if (c > VIMSPELLVERSION)
2815 {
2816 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002817 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 }
2819
Bram Moolenaar5195e452005-08-19 20:32:47 +00002820
2821 /*
2822 * <SECTIONS>: <section> ... <sectionend>
2823 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2824 */
2825 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002826 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002827 n = getc(fd); /* <sectionID> or <sectionend> */
2828 if (n == SN_END)
2829 break;
2830 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002831 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002832 if (len < 0)
2833 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002834
Bram Moolenaar5195e452005-08-19 20:32:47 +00002835 res = 0;
2836 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002837 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002838 case SN_INFO:
2839 lp->sl_info = read_string(fd, len); /* <infotext> */
2840 if (lp->sl_info == NULL)
2841 goto endFAIL;
2842 break;
2843
Bram Moolenaar5195e452005-08-19 20:32:47 +00002844 case SN_REGION:
2845 res = read_region_section(fd, lp, len);
2846 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002847
Bram Moolenaar5195e452005-08-19 20:32:47 +00002848 case SN_CHARFLAGS:
2849 res = read_charflags_section(fd);
2850 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002851
Bram Moolenaar5195e452005-08-19 20:32:47 +00002852 case SN_MIDWORD:
2853 lp->sl_midword = read_string(fd, len); /* <midword> */
2854 if (lp->sl_midword == NULL)
2855 goto endFAIL;
2856 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002857
Bram Moolenaar5195e452005-08-19 20:32:47 +00002858 case SN_PREFCOND:
2859 res = read_prefcond_section(fd, lp);
2860 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002861
Bram Moolenaar5195e452005-08-19 20:32:47 +00002862 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002863 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2864 break;
2865
2866 case SN_REPSAL:
2867 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002868 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002869
Bram Moolenaar5195e452005-08-19 20:32:47 +00002870 case SN_SAL:
2871 res = read_sal_section(fd, lp);
2872 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002873
Bram Moolenaar5195e452005-08-19 20:32:47 +00002874 case SN_SOFO:
2875 res = read_sofo_section(fd, lp);
2876 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002877
Bram Moolenaar5195e452005-08-19 20:32:47 +00002878 case SN_MAP:
2879 p = read_string(fd, len); /* <mapstr> */
2880 if (p == NULL)
2881 goto endFAIL;
2882 set_map_str(lp, p);
2883 vim_free(p);
2884 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002885
Bram Moolenaar4770d092006-01-12 23:22:24 +00002886 case SN_WORDS:
2887 res = read_words_section(fd, lp, len);
2888 break;
2889
2890 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002891 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002892 break;
2893
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002894 case SN_NOSPLITSUGS:
2895 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2896 break;
2897
Bram Moolenaar5195e452005-08-19 20:32:47 +00002898 case SN_COMPOUND:
2899 res = read_compound(fd, lp, len);
2900 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002901
Bram Moolenaar78622822005-08-23 21:00:13 +00002902 case SN_NOBREAK:
2903 lp->sl_nobreak = TRUE;
2904 break;
2905
Bram Moolenaar5195e452005-08-19 20:32:47 +00002906 case SN_SYLLABLE:
2907 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2908 if (lp->sl_syllable == NULL)
2909 goto endFAIL;
2910 if (init_syl_tab(lp) == FAIL)
2911 goto endFAIL;
2912 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002913
Bram Moolenaar5195e452005-08-19 20:32:47 +00002914 default:
2915 /* Unsupported section. When it's required give an error
2916 * message. When it's not required skip the contents. */
2917 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002918 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002920 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002921 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002922 while (--len >= 0)
2923 if (getc(fd) < 0)
2924 goto truncerr;
2925 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002926 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002927someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002928 if (res == SP_FORMERROR)
2929 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002930 EMSG(_(e_format));
2931 goto endFAIL;
2932 }
2933 if (res == SP_TRUNCERROR)
2934 {
2935truncerr:
2936 EMSG(_(e_spell_trunc));
2937 goto endFAIL;
2938 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002939 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002941 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002942
Bram Moolenaar4770d092006-01-12 23:22:24 +00002943 /* <LWORDTREE> */
2944 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2945 if (res != 0)
2946 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002947
Bram Moolenaar4770d092006-01-12 23:22:24 +00002948 /* <KWORDTREE> */
2949 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2950 if (res != 0)
2951 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002952
Bram Moolenaar4770d092006-01-12 23:22:24 +00002953 /* <PREFIXTREE> */
2954 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2955 lp->sl_prefixcnt);
2956 if (res != 0)
2957 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002958
Bram Moolenaarb765d632005-06-07 21:00:02 +00002959 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002960 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002961 {
2962 lp->sl_next = first_lang;
2963 first_lang = lp;
2964 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002965
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002966 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002967
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002968endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002969 if (lang != NULL)
2970 /* truncating the name signals the error to spell_load_lang() */
2971 *lang = NUL;
2972 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002973 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002974 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002975
2976endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002977 if (fd != NULL)
2978 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002979 sourcing_name = save_sourcing_name;
2980 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981
2982 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002983}
2984
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002985/*
2986 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002987 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002988 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002989 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2990 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002991 */
2992 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002993read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002994 FILE *fd;
2995 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002996 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002997{
2998 int cnt = 0;
2999 int i;
3000 char_u *str;
3001
3002 /* read the length bytes, MSB first */
3003 for (i = 0; i < cnt_bytes; ++i)
3004 cnt = (cnt << 8) + getc(fd);
3005 if (cnt < 0)
3006 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003007 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003008 return NULL;
3009 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003010 *cntp = cnt;
3011 if (cnt == 0)
3012 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003013
Bram Moolenaar5195e452005-08-19 20:32:47 +00003014 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003015 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003016 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003017 return str;
3018}
3019
Bram Moolenaar7887d882005-07-01 22:33:52 +00003020/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003021 * Read SN_REGION: <regionname> ...
3022 * Return SP_*ERROR flags.
3023 */
3024 static int
3025read_region_section(fd, lp, len)
3026 FILE *fd;
3027 slang_T *lp;
3028 int len;
3029{
3030 int i;
3031
3032 if (len > 16)
3033 return SP_FORMERROR;
3034 for (i = 0; i < len; ++i)
3035 lp->sl_regions[i] = getc(fd); /* <regionname> */
3036 lp->sl_regions[len] = NUL;
3037 return 0;
3038}
3039
3040/*
3041 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3042 * <folcharslen> <folchars>
3043 * Return SP_*ERROR flags.
3044 */
3045 static int
3046read_charflags_section(fd)
3047 FILE *fd;
3048{
3049 char_u *flags;
3050 char_u *fol;
3051 int flagslen, follen;
3052
3053 /* <charflagslen> <charflags> */
3054 flags = read_cnt_string(fd, 1, &flagslen);
3055 if (flagslen < 0)
3056 return flagslen;
3057
3058 /* <folcharslen> <folchars> */
3059 fol = read_cnt_string(fd, 2, &follen);
3060 if (follen < 0)
3061 {
3062 vim_free(flags);
3063 return follen;
3064 }
3065
3066 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3067 if (flags != NULL && fol != NULL)
3068 set_spell_charflags(flags, flagslen, fol);
3069
3070 vim_free(flags);
3071 vim_free(fol);
3072
3073 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3074 if ((flags == NULL) != (fol == NULL))
3075 return SP_FORMERROR;
3076 return 0;
3077}
3078
3079/*
3080 * Read SN_PREFCOND section.
3081 * Return SP_*ERROR flags.
3082 */
3083 static int
3084read_prefcond_section(fd, lp)
3085 FILE *fd;
3086 slang_T *lp;
3087{
3088 int cnt;
3089 int i;
3090 int n;
3091 char_u *p;
3092 char_u buf[MAXWLEN + 1];
3093
3094 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003095 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003096 if (cnt <= 0)
3097 return SP_FORMERROR;
3098
3099 lp->sl_prefprog = (regprog_T **)alloc_clear(
3100 (unsigned)sizeof(regprog_T *) * cnt);
3101 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003102 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003103 lp->sl_prefixcnt = cnt;
3104
3105 for (i = 0; i < cnt; ++i)
3106 {
3107 /* <prefcond> : <condlen> <condstr> */
3108 n = getc(fd); /* <condlen> */
3109 if (n < 0 || n >= MAXWLEN)
3110 return SP_FORMERROR;
3111
3112 /* When <condlen> is zero we have an empty condition. Otherwise
3113 * compile the regexp program used to check for the condition. */
3114 if (n > 0)
3115 {
3116 buf[0] = '^'; /* always match at one position only */
3117 p = buf + 1;
3118 while (n-- > 0)
3119 *p++ = getc(fd); /* <condstr> */
3120 *p = NUL;
3121 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3122 }
3123 }
3124 return 0;
3125}
3126
3127/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003128 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003129 * Return SP_*ERROR flags.
3130 */
3131 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003132read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003133 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003134 garray_T *gap;
3135 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003136{
3137 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003138 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003139 int i;
3140
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003141 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003142 if (cnt < 0)
3143 return SP_TRUNCERROR;
3144
Bram Moolenaar5195e452005-08-19 20:32:47 +00003145 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003146 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003147
3148 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3149 for (; gap->ga_len < cnt; ++gap->ga_len)
3150 {
3151 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3152 ftp->ft_from = read_cnt_string(fd, 1, &i);
3153 if (i < 0)
3154 return i;
3155 if (i == 0)
3156 return SP_FORMERROR;
3157 ftp->ft_to = read_cnt_string(fd, 1, &i);
3158 if (i <= 0)
3159 {
3160 vim_free(ftp->ft_from);
3161 if (i < 0)
3162 return i;
3163 return SP_FORMERROR;
3164 }
3165 }
3166
3167 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003168 for (i = 0; i < 256; ++i)
3169 first[i] = -1;
3170 for (i = 0; i < gap->ga_len; ++i)
3171 {
3172 ftp = &((fromto_T *)gap->ga_data)[i];
3173 if (first[*ftp->ft_from] == -1)
3174 first[*ftp->ft_from] = i;
3175 }
3176 return 0;
3177}
3178
3179/*
3180 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3181 * Return SP_*ERROR flags.
3182 */
3183 static int
3184read_sal_section(fd, slang)
3185 FILE *fd;
3186 slang_T *slang;
3187{
3188 int i;
3189 int cnt;
3190 garray_T *gap;
3191 salitem_T *smp;
3192 int ccnt;
3193 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003194 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003195
3196 slang->sl_sofo = FALSE;
3197
3198 i = getc(fd); /* <salflags> */
3199 if (i & SAL_F0LLOWUP)
3200 slang->sl_followup = TRUE;
3201 if (i & SAL_COLLAPSE)
3202 slang->sl_collapse = TRUE;
3203 if (i & SAL_REM_ACCENTS)
3204 slang->sl_rem_accents = TRUE;
3205
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003206 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003207 if (cnt < 0)
3208 return SP_TRUNCERROR;
3209
3210 gap = &slang->sl_sal;
3211 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003212 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003213 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003214
3215 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3216 for (; gap->ga_len < cnt; ++gap->ga_len)
3217 {
3218 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3219 ccnt = getc(fd); /* <salfromlen> */
3220 if (ccnt < 0)
3221 return SP_TRUNCERROR;
3222 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003223 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003224 smp->sm_lead = p;
3225
3226 /* Read up to the first special char into sm_lead. */
3227 for (i = 0; i < ccnt; ++i)
3228 {
3229 c = getc(fd); /* <salfrom> */
3230 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3231 break;
3232 *p++ = c;
3233 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003234 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003235 *p++ = NUL;
3236
3237 /* Put (abc) chars in sm_oneof, if any. */
3238 if (c == '(')
3239 {
3240 smp->sm_oneof = p;
3241 for (++i; i < ccnt; ++i)
3242 {
3243 c = getc(fd); /* <salfrom> */
3244 if (c == ')')
3245 break;
3246 *p++ = c;
3247 }
3248 *p++ = NUL;
3249 if (++i < ccnt)
3250 c = getc(fd);
3251 }
3252 else
3253 smp->sm_oneof = NULL;
3254
3255 /* Any following chars go in sm_rules. */
3256 smp->sm_rules = p;
3257 if (i < ccnt)
3258 /* store the char we got while checking for end of sm_lead */
3259 *p++ = c;
3260 for (++i; i < ccnt; ++i)
3261 *p++ = getc(fd); /* <salfrom> */
3262 *p++ = NUL;
3263
3264 /* <saltolen> <salto> */
3265 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3266 if (ccnt < 0)
3267 {
3268 vim_free(smp->sm_lead);
3269 return ccnt;
3270 }
3271
3272#ifdef FEAT_MBYTE
3273 if (has_mbyte)
3274 {
3275 /* convert the multi-byte strings to wide char strings */
3276 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3277 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3278 if (smp->sm_oneof == NULL)
3279 smp->sm_oneof_w = NULL;
3280 else
3281 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3282 if (smp->sm_to == NULL)
3283 smp->sm_to_w = NULL;
3284 else
3285 smp->sm_to_w = mb_str2wide(smp->sm_to);
3286 if (smp->sm_lead_w == NULL
3287 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3288 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3289 {
3290 vim_free(smp->sm_lead);
3291 vim_free(smp->sm_to);
3292 vim_free(smp->sm_lead_w);
3293 vim_free(smp->sm_oneof_w);
3294 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003295 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003296 }
3297 }
3298#endif
3299 }
3300
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003301 if (gap->ga_len > 0)
3302 {
3303 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3304 * that we need to check the index every time. */
3305 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3306 if ((p = alloc(1)) == NULL)
3307 return SP_OTHERERROR;
3308 p[0] = NUL;
3309 smp->sm_lead = p;
3310 smp->sm_leadlen = 0;
3311 smp->sm_oneof = NULL;
3312 smp->sm_rules = p;
3313 smp->sm_to = NULL;
3314#ifdef FEAT_MBYTE
3315 if (has_mbyte)
3316 {
3317 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3318 smp->sm_leadlen = 0;
3319 smp->sm_oneof_w = NULL;
3320 smp->sm_to_w = NULL;
3321 }
3322#endif
3323 ++gap->ga_len;
3324 }
3325
Bram Moolenaar5195e452005-08-19 20:32:47 +00003326 /* Fill the first-index table. */
3327 set_sal_first(slang);
3328
3329 return 0;
3330}
3331
3332/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003333 * Read SN_WORDS: <word> ...
3334 * Return SP_*ERROR flags.
3335 */
3336 static int
3337read_words_section(fd, lp, len)
3338 FILE *fd;
3339 slang_T *lp;
3340 int len;
3341{
3342 int done = 0;
3343 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003344 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003345 char_u word[MAXWLEN];
3346
3347 while (done < len)
3348 {
3349 /* Read one word at a time. */
3350 for (i = 0; ; ++i)
3351 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003352 c = getc(fd);
3353 if (c == EOF)
3354 return SP_TRUNCERROR;
3355 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003356 if (word[i] == NUL)
3357 break;
3358 if (i == MAXWLEN - 1)
3359 return SP_FORMERROR;
3360 }
3361
3362 /* Init the count to 10. */
3363 count_common_word(lp, word, -1, 10);
3364 done += i + 1;
3365 }
3366 return 0;
3367}
3368
3369/*
3370 * Add a word to the hashtable of common words.
3371 * If it's already there then the counter is increased.
3372 */
3373 static void
3374count_common_word(lp, word, len, count)
3375 slang_T *lp;
3376 char_u *word;
3377 int len; /* word length, -1 for upto NUL */
3378 int count; /* 1 to count once, 10 to init */
3379{
3380 hash_T hash;
3381 hashitem_T *hi;
3382 wordcount_T *wc;
3383 char_u buf[MAXWLEN];
3384 char_u *p;
3385
3386 if (len == -1)
3387 p = word;
3388 else
3389 {
3390 vim_strncpy(buf, word, len);
3391 p = buf;
3392 }
3393
3394 hash = hash_hash(p);
3395 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3396 if (HASHITEM_EMPTY(hi))
3397 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003398 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003399 if (wc == NULL)
3400 return;
3401 STRCPY(wc->wc_word, p);
3402 wc->wc_count = count;
3403 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3404 }
3405 else
3406 {
3407 wc = HI2WC(hi);
3408 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3409 wc->wc_count = MAXWORDCOUNT;
3410 }
3411}
3412
3413/*
3414 * Adjust the score of common words.
3415 */
3416 static int
3417score_wordcount_adj(slang, score, word, split)
3418 slang_T *slang;
3419 int score;
3420 char_u *word;
3421 int split; /* word was split, less bonus */
3422{
3423 hashitem_T *hi;
3424 wordcount_T *wc;
3425 int bonus;
3426 int newscore;
3427
3428 hi = hash_find(&slang->sl_wordcount, word);
3429 if (!HASHITEM_EMPTY(hi))
3430 {
3431 wc = HI2WC(hi);
3432 if (wc->wc_count < SCORE_THRES2)
3433 bonus = SCORE_COMMON1;
3434 else if (wc->wc_count < SCORE_THRES3)
3435 bonus = SCORE_COMMON2;
3436 else
3437 bonus = SCORE_COMMON3;
3438 if (split)
3439 newscore = score - bonus / 2;
3440 else
3441 newscore = score - bonus;
3442 if (newscore < 0)
3443 return 0;
3444 return newscore;
3445 }
3446 return score;
3447}
3448
3449/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003450 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3451 * Return SP_*ERROR flags.
3452 */
3453 static int
3454read_sofo_section(fd, slang)
3455 FILE *fd;
3456 slang_T *slang;
3457{
3458 int cnt;
3459 char_u *from, *to;
3460 int res;
3461
3462 slang->sl_sofo = TRUE;
3463
3464 /* <sofofromlen> <sofofrom> */
3465 from = read_cnt_string(fd, 2, &cnt);
3466 if (cnt < 0)
3467 return cnt;
3468
3469 /* <sofotolen> <sofoto> */
3470 to = read_cnt_string(fd, 2, &cnt);
3471 if (cnt < 0)
3472 {
3473 vim_free(from);
3474 return cnt;
3475 }
3476
3477 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3478 if (from != NULL && to != NULL)
3479 res = set_sofo(slang, from, to);
3480 else if (from != NULL || to != NULL)
3481 res = SP_FORMERROR; /* only one of two strings is an error */
3482 else
3483 res = 0;
3484
3485 vim_free(from);
3486 vim_free(to);
3487 return res;
3488}
3489
3490/*
3491 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003492 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003493 * Returns SP_*ERROR flags.
3494 */
3495 static int
3496read_compound(fd, slang, len)
3497 FILE *fd;
3498 slang_T *slang;
3499 int len;
3500{
3501 int todo = len;
3502 int c;
3503 int atstart;
3504 char_u *pat;
3505 char_u *pp;
3506 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003507 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003508 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003509 int cnt;
3510 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003511
3512 if (todo < 2)
3513 return SP_FORMERROR; /* need at least two bytes */
3514
3515 --todo;
3516 c = getc(fd); /* <compmax> */
3517 if (c < 2)
3518 c = MAXWLEN;
3519 slang->sl_compmax = c;
3520
3521 --todo;
3522 c = getc(fd); /* <compminlen> */
3523 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003524 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003525 slang->sl_compminlen = c;
3526
3527 --todo;
3528 c = getc(fd); /* <compsylmax> */
3529 if (c < 1)
3530 c = MAXWLEN;
3531 slang->sl_compsylmax = c;
3532
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003533 c = getc(fd); /* <compoptions> */
3534 if (c != 0)
3535 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3536 else
3537 {
3538 --todo;
3539 c = getc(fd); /* only use the lower byte for now */
3540 --todo;
3541 slang->sl_compoptions = c;
3542
3543 gap = &slang->sl_comppat;
3544 c = get2c(fd); /* <comppatcount> */
3545 todo -= 2;
3546 ga_init2(gap, sizeof(char_u *), c);
3547 if (ga_grow(gap, c) == OK)
3548 while (--c >= 0)
3549 {
3550 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3551 read_cnt_string(fd, 1, &cnt);
3552 /* <comppatlen> <comppattext> */
3553 if (cnt < 0)
3554 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003555 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003556 }
3557 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003558 if (todo < 0)
3559 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003560
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003561 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003562 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003563 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3564 * Conversion to utf-8 may double the size. */
3565 c = todo * 2 + 7;
3566#ifdef FEAT_MBYTE
3567 if (enc_utf8)
3568 c += todo * 2;
3569#endif
3570 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003571 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003572 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003573
Bram Moolenaard12a1322005-08-21 22:08:24 +00003574 /* We also need a list of all flags that can appear at the start and one
3575 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003576 cp = alloc(todo + 1);
3577 if (cp == NULL)
3578 {
3579 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003580 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003581 }
3582 slang->sl_compstartflags = cp;
3583 *cp = NUL;
3584
Bram Moolenaard12a1322005-08-21 22:08:24 +00003585 ap = alloc(todo + 1);
3586 if (ap == NULL)
3587 {
3588 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003589 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003590 }
3591 slang->sl_compallflags = ap;
3592 *ap = NUL;
3593
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003594 /* And a list of all patterns in their original form, for checking whether
3595 * compounding may work in match_compoundrule(). This is freed when we
3596 * encounter a wildcard, the check doesn't work then. */
3597 crp = alloc(todo + 1);
3598 slang->sl_comprules = crp;
3599
Bram Moolenaar5195e452005-08-19 20:32:47 +00003600 pp = pat;
3601 *pp++ = '^';
3602 *pp++ = '\\';
3603 *pp++ = '(';
3604
3605 atstart = 1;
3606 while (todo-- > 0)
3607 {
3608 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003609 if (c == EOF)
3610 {
3611 vim_free(pat);
3612 return SP_TRUNCERROR;
3613 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003614
3615 /* Add all flags to "sl_compallflags". */
3616 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003617 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003618 {
3619 *ap++ = c;
3620 *ap = NUL;
3621 }
3622
Bram Moolenaar5195e452005-08-19 20:32:47 +00003623 if (atstart != 0)
3624 {
3625 /* At start of item: copy flags to "sl_compstartflags". For a
3626 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3627 if (c == '[')
3628 atstart = 2;
3629 else if (c == ']')
3630 atstart = 0;
3631 else
3632 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003633 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003634 {
3635 *cp++ = c;
3636 *cp = NUL;
3637 }
3638 if (atstart == 1)
3639 atstart = 0;
3640 }
3641 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003642
3643 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3644 if (crp != NULL)
3645 {
3646 if (c == '+' || c == '*')
3647 {
3648 vim_free(slang->sl_comprules);
3649 slang->sl_comprules = NULL;
3650 crp = NULL;
3651 }
3652 else
3653 *crp++ = c;
3654 }
3655
Bram Moolenaar5195e452005-08-19 20:32:47 +00003656 if (c == '/') /* slash separates two items */
3657 {
3658 *pp++ = '\\';
3659 *pp++ = '|';
3660 atstart = 1;
3661 }
3662 else /* normal char, "[abc]" and '*' are copied as-is */
3663 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003664 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003665 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003666#ifdef FEAT_MBYTE
3667 if (enc_utf8)
3668 pp += mb_char2bytes(c, pp);
3669 else
3670#endif
3671 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003672 }
3673 }
3674
3675 *pp++ = '\\';
3676 *pp++ = ')';
3677 *pp++ = '$';
3678 *pp = NUL;
3679
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003680 if (crp != NULL)
3681 *crp = NUL;
3682
Bram Moolenaar5195e452005-08-19 20:32:47 +00003683 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3684 vim_free(pat);
3685 if (slang->sl_compprog == NULL)
3686 return SP_FORMERROR;
3687
3688 return 0;
3689}
3690
Bram Moolenaar6de68532005-08-24 22:08:48 +00003691/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003692 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003693 * Like strchr() but independent of locale.
3694 */
3695 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003696byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003697 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003698 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003699{
3700 char_u *p;
3701
3702 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003703 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003704 return TRUE;
3705 return FALSE;
3706}
3707
Bram Moolenaar5195e452005-08-19 20:32:47 +00003708#define SY_MAXLEN 30
3709typedef struct syl_item_S
3710{
3711 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3712 int sy_len;
3713} syl_item_T;
3714
3715/*
3716 * Truncate "slang->sl_syllable" at the first slash and put the following items
3717 * in "slang->sl_syl_items".
3718 */
3719 static int
3720init_syl_tab(slang)
3721 slang_T *slang;
3722{
3723 char_u *p;
3724 char_u *s;
3725 int l;
3726 syl_item_T *syl;
3727
3728 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3729 p = vim_strchr(slang->sl_syllable, '/');
3730 while (p != NULL)
3731 {
3732 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003733 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003734 break;
3735 s = p;
3736 p = vim_strchr(p, '/');
3737 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003738 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003739 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003740 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003741 if (l >= SY_MAXLEN)
3742 return SP_FORMERROR;
3743 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003744 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003745 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3746 + slang->sl_syl_items.ga_len++;
3747 vim_strncpy(syl->sy_chars, s, l);
3748 syl->sy_len = l;
3749 }
3750 return OK;
3751}
3752
3753/*
3754 * Count the number of syllables in "word".
3755 * When "word" contains spaces the syllables after the last space are counted.
3756 * Returns zero if syllables are not defines.
3757 */
3758 static int
3759count_syllables(slang, word)
3760 slang_T *slang;
3761 char_u *word;
3762{
3763 int cnt = 0;
3764 int skip = FALSE;
3765 char_u *p;
3766 int len;
3767 int i;
3768 syl_item_T *syl;
3769 int c;
3770
3771 if (slang->sl_syllable == NULL)
3772 return 0;
3773
3774 for (p = word; *p != NUL; p += len)
3775 {
3776 /* When running into a space reset counter. */
3777 if (*p == ' ')
3778 {
3779 len = 1;
3780 cnt = 0;
3781 continue;
3782 }
3783
3784 /* Find longest match of syllable items. */
3785 len = 0;
3786 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3787 {
3788 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3789 if (syl->sy_len > len
3790 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3791 len = syl->sy_len;
3792 }
3793 if (len != 0) /* found a match, count syllable */
3794 {
3795 ++cnt;
3796 skip = FALSE;
3797 }
3798 else
3799 {
3800 /* No recognized syllable item, at least a syllable char then? */
3801#ifdef FEAT_MBYTE
3802 c = mb_ptr2char(p);
3803 len = (*mb_ptr2len)(p);
3804#else
3805 c = *p;
3806 len = 1;
3807#endif
3808 if (vim_strchr(slang->sl_syllable, c) == NULL)
3809 skip = FALSE; /* No, search for next syllable */
3810 else if (!skip)
3811 {
3812 ++cnt; /* Yes, count it */
3813 skip = TRUE; /* don't count following syllable chars */
3814 }
3815 }
3816 }
3817 return cnt;
3818}
3819
3820/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003821 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003822 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003823 */
3824 static int
3825set_sofo(lp, from, to)
3826 slang_T *lp;
3827 char_u *from;
3828 char_u *to;
3829{
3830 int i;
3831
3832#ifdef FEAT_MBYTE
3833 garray_T *gap;
3834 char_u *s;
3835 char_u *p;
3836 int c;
3837 int *inp;
3838
3839 if (has_mbyte)
3840 {
3841 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3842 * characters. The index is the low byte of the character.
3843 * The list contains from-to pairs with a terminating NUL.
3844 * sl_sal_first[] is used for latin1 "from" characters. */
3845 gap = &lp->sl_sal;
3846 ga_init2(gap, sizeof(int *), 1);
3847 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003848 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003849 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3850 gap->ga_len = 256;
3851
3852 /* First count the number of items for each list. Temporarily use
3853 * sl_sal_first[] for this. */
3854 for (p = from, s = to; *p != NUL && *s != NUL; )
3855 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003856 c = mb_cptr2char_adv(&p);
3857 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003858 if (c >= 256)
3859 ++lp->sl_sal_first[c & 0xff];
3860 }
3861 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003862 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003863
3864 /* Allocate the lists. */
3865 for (i = 0; i < 256; ++i)
3866 if (lp->sl_sal_first[i] > 0)
3867 {
3868 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3869 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003870 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003871 ((int **)gap->ga_data)[i] = (int *)p;
3872 *(int *)p = 0;
3873 }
3874
3875 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3876 * list. */
3877 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3878 for (p = from, s = to; *p != NUL && *s != NUL; )
3879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003880 c = mb_cptr2char_adv(&p);
3881 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003882 if (c >= 256)
3883 {
3884 /* Append the from-to chars at the end of the list with
3885 * the low byte. */
3886 inp = ((int **)gap->ga_data)[c & 0xff];
3887 while (*inp != 0)
3888 ++inp;
3889 *inp++ = c; /* from char */
3890 *inp++ = i; /* to char */
3891 *inp++ = NUL; /* NUL at the end */
3892 }
3893 else
3894 /* mapping byte to char is done in sl_sal_first[] */
3895 lp->sl_sal_first[c] = i;
3896 }
3897 }
3898 else
3899#endif
3900 {
3901 /* mapping bytes to bytes is done in sl_sal_first[] */
3902 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003903 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003904
3905 for (i = 0; to[i] != NUL; ++i)
3906 lp->sl_sal_first[from[i]] = to[i];
3907 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3908 }
3909
Bram Moolenaar5195e452005-08-19 20:32:47 +00003910 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003911}
3912
3913/*
3914 * Fill the first-index table for "lp".
3915 */
3916 static void
3917set_sal_first(lp)
3918 slang_T *lp;
3919{
3920 salfirst_T *sfirst;
3921 int i;
3922 salitem_T *smp;
3923 int c;
3924 garray_T *gap = &lp->sl_sal;
3925
3926 sfirst = lp->sl_sal_first;
3927 for (i = 0; i < 256; ++i)
3928 sfirst[i] = -1;
3929 smp = (salitem_T *)gap->ga_data;
3930 for (i = 0; i < gap->ga_len; ++i)
3931 {
3932#ifdef FEAT_MBYTE
3933 if (has_mbyte)
3934 /* Use the lowest byte of the first character. For latin1 it's
3935 * the character, for other encodings it should differ for most
3936 * characters. */
3937 c = *smp[i].sm_lead_w & 0xff;
3938 else
3939#endif
3940 c = *smp[i].sm_lead;
3941 if (sfirst[c] == -1)
3942 {
3943 sfirst[c] = i;
3944#ifdef FEAT_MBYTE
3945 if (has_mbyte)
3946 {
3947 int n;
3948
3949 /* Make sure all entries with this byte are following each
3950 * other. Move the ones that are in the wrong position. Do
3951 * keep the same ordering! */
3952 while (i + 1 < gap->ga_len
3953 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3954 /* Skip over entry with same index byte. */
3955 ++i;
3956
3957 for (n = 1; i + n < gap->ga_len; ++n)
3958 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3959 {
3960 salitem_T tsal;
3961
3962 /* Move entry with same index byte after the entries
3963 * we already found. */
3964 ++i;
3965 --n;
3966 tsal = smp[i + n];
3967 mch_memmove(smp + i + 1, smp + i,
3968 sizeof(salitem_T) * n);
3969 smp[i] = tsal;
3970 }
3971 }
3972#endif
3973 }
3974 }
3975}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003976
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003977#ifdef FEAT_MBYTE
3978/*
3979 * Turn a multi-byte string into a wide character string.
3980 * Return it in allocated memory (NULL for out-of-memory)
3981 */
3982 static int *
3983mb_str2wide(s)
3984 char_u *s;
3985{
3986 int *res;
3987 char_u *p;
3988 int i = 0;
3989
3990 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3991 if (res != NULL)
3992 {
3993 for (p = s; *p != NUL; )
3994 res[i++] = mb_ptr2char_adv(&p);
3995 res[i] = NUL;
3996 }
3997 return res;
3998}
3999#endif
4000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004001/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004002 * Read a tree from the .spl or .sug file.
4003 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4004 * This is skipped when the tree has zero length.
4005 * Returns zero when OK, SP_ value for an error.
4006 */
4007 static int
4008spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4009 FILE *fd;
4010 char_u **bytsp;
4011 idx_T **idxsp;
4012 int prefixtree; /* TRUE for the prefix tree */
4013 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4014{
4015 int len;
4016 int idx;
4017 char_u *bp;
4018 idx_T *ip;
4019
4020 /* The tree size was computed when writing the file, so that we can
4021 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004022 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004023 if (len < 0)
4024 return SP_TRUNCERROR;
4025 if (len > 0)
4026 {
4027 /* Allocate the byte array. */
4028 bp = lalloc((long_u)len, TRUE);
4029 if (bp == NULL)
4030 return SP_OTHERERROR;
4031 *bytsp = bp;
4032
4033 /* Allocate the index array. */
4034 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4035 if (ip == NULL)
4036 return SP_OTHERERROR;
4037 *idxsp = ip;
4038
4039 /* Recursively read the tree and store it in the array. */
4040 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4041 if (idx < 0)
4042 return idx;
4043 }
4044 return 0;
4045}
4046
4047/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 * Read one row of siblings from the spell file and store it in the byte array
4049 * "byts" and index array "idxs". Recursively read the children.
4050 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004051 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004053 * Returns the index (>= 0) following the siblings.
4054 * Returns SP_TRUNCERROR if the file is shorter than expected.
4055 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004056 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004057 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004058read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004059 FILE *fd;
4060 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004061 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004063 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004064 int prefixtree; /* TRUE for reading PREFIXTREE */
4065 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004066{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004067 int len;
4068 int i;
4069 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004070 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004072 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 len = getc(fd); /* <siblingcount> */
4076 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004077 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004078
4079 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004080 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004081 byts[idx++] = len;
4082
4083 /* Read the byte values, flag/region bytes and shared indexes. */
4084 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004086 c = getc(fd); /* <byte> */
4087 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004088 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004089 if (c <= BY_SPECIAL)
4090 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004091 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 {
4093 /* No flags, all regions. */
4094 idxs[idx] = 0;
4095 c = 0;
4096 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004097 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004098 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004099 if (prefixtree)
4100 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004101 /* Read the optional pflags byte, the prefix ID and the
4102 * condition nr. In idxs[] store the prefix ID in the low
4103 * byte, the condition index shifted up 8 bits, the flags
4104 * shifted up 24 bits. */
4105 if (c == BY_FLAGS)
4106 c = getc(fd) << 24; /* <pflags> */
4107 else
4108 c = 0;
4109
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004110 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004111
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004112 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004113 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004114 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004115 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004116 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004117 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004118 {
4119 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004120 * idxs[] the flags go in the low two bytes, region above
4121 * that and prefix ID above the region. */
4122 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004123 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004124 if (c2 == BY_FLAGS2)
4125 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004126 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004127 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004128 if (c & WF_AFX)
4129 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004130 }
4131
Bram Moolenaar51485f02005-06-04 21:55:20 +00004132 idxs[idx] = c;
4133 c = 0;
4134 }
4135 else /* c == BY_INDEX */
4136 {
4137 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004138 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004139 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004140 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141 idxs[idx] = n + SHARED_MASK;
4142 c = getc(fd); /* <xbyte> */
4143 }
4144 }
4145 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004146 }
4147
Bram Moolenaar51485f02005-06-04 21:55:20 +00004148 /* Recursively read the children for non-shared siblings.
4149 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4150 * remove SHARED_MASK) */
4151 for (i = 1; i <= len; ++i)
4152 if (byts[startidx + i] != 0)
4153 {
4154 if (idxs[startidx + i] & SHARED_MASK)
4155 idxs[startidx + i] &= ~SHARED_MASK;
4156 else
4157 {
4158 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004159 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004160 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004161 if (idx < 0)
4162 break;
4163 }
4164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004165
Bram Moolenaar51485f02005-06-04 21:55:20 +00004166 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167}
4168
4169/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004170 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004171 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004172 */
4173 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004174did_set_spelllang(wp)
4175 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004176{
4177 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004178 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004179 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004180 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004181 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004182 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004183 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004184 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004185 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004187 int len;
4188 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004189 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004190 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004191 char_u *use_region = NULL;
4192 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004193 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004194 int i, j;
4195 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004196 static int recursive = FALSE;
4197 char_u *ret_msg = NULL;
4198 char_u *spl_copy;
4199
4200 /* We don't want to do this recursively. May happen when a language is
4201 * not available and the SpellFileMissing autocommand opens a new buffer
4202 * in which 'spell' is set. */
4203 if (recursive)
4204 return NULL;
4205 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004206
4207 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004208 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004210 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4211 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004212 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004213 if (spl_copy == NULL)
4214 goto theend;
4215
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004216 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004217 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004218 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004219 /* Get one language name. */
4220 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004221 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004223
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004224 /* If the name ends in ".spl" use it as the name of the spell file.
4225 * If there is a region name let "region" point to it and remove it
4226 * from the name. */
4227 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4228 {
4229 filename = TRUE;
4230
Bram Moolenaarb6356332005-07-18 21:40:44 +00004231 /* Locate a region and remove it from the file name. */
4232 p = vim_strchr(gettail(lang), '_');
4233 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4234 && !ASCII_ISALPHA(p[3]))
4235 {
4236 vim_strncpy(region_cp, p + 1, 2);
4237 mch_memmove(p, p + 3, len - (p - lang) - 2);
4238 len -= 3;
4239 region = region_cp;
4240 }
4241 else
4242 dont_use_region = TRUE;
4243
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004244 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004245 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4246 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004247 break;
4248 }
4249 else
4250 {
4251 filename = FALSE;
4252 if (len > 3 && lang[len - 3] == '_')
4253 {
4254 region = lang + len - 2;
4255 len -= 3;
4256 lang[len] = NUL;
4257 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004258 else
4259 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004260
4261 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4263 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004264 break;
4265 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004266
Bram Moolenaarb6356332005-07-18 21:40:44 +00004267 if (region != NULL)
4268 {
4269 /* If the region differs from what was used before then don't
4270 * use it for 'spellfile'. */
4271 if (use_region != NULL && STRCMP(region, use_region) != 0)
4272 dont_use_region = TRUE;
4273 use_region = region;
4274 }
4275
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004276 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004277 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004278 {
4279 if (filename)
4280 (void)spell_load_file(lang, lang, NULL, FALSE);
4281 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004282 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004283 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004284#ifdef FEAT_AUTOCMD
4285 /* SpellFileMissing autocommands may do anything, including
4286 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004287 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004288 {
4289 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4290 goto theend;
4291 }
4292#endif
4293 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004294 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004295
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004296 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004297 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004298 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004299 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4300 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4301 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004302 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004303 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004304 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004305 {
4306 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004308 if (c == REGION_ALL)
4309 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004311 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004313 /* This addition file is for other regions. */
4314 region_mask = 0;
4315 }
4316 else
4317 /* This is probably an error. Give a warning and
4318 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004319 smsg((char_u *)
4320 _("Warning: region %s not supported"),
4321 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004322 }
4323 else
4324 region_mask = 1 << c;
4325 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004326
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004327 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004328 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004329 if (ga_grow(&ga, 1) == FAIL)
4330 {
4331 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004332 ret_msg = e_outofmem;
4333 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004334 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004336 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4337 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004338 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004339 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004340 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004341 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004342 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004343 }
4344
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004345 /* round 0: load int_wordlist, if possible.
4346 * round 1: load first name in 'spellfile'.
4347 * round 2: load second name in 'spellfile.
4348 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004349 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004350 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004352 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004353 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004354 /* Internal wordlist, if there is one. */
4355 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004356 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004357 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004358 }
4359 else
4360 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004361 /* One entry in 'spellfile'. */
4362 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4363 STRCAT(spf_name, ".spl");
4364
4365 /* If it was already found above then skip it. */
4366 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004367 {
4368 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4369 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004370 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004371 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004372 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004373 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004374 }
4375
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004376 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4378 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004379 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004380 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004382 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004383 * region name, the region is ignored otherwise. for int_wordlist
4384 * use an arbitrary name. */
4385 if (round == 0)
4386 STRCPY(lang, "internal wordlist");
4387 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004388 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004389 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004390 p = vim_strchr(lang, '.');
4391 if (p != NULL)
4392 *p = NUL; /* truncate at ".encoding.add" */
4393 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004394 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004395
4396 /* If one of the languages has NOBREAK we assume the addition
4397 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004398 if (slang != NULL && nobreak)
4399 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004401 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004402 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004403 region_mask = REGION_ALL;
4404 if (use_region != NULL && !dont_use_region)
4405 {
4406 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004408 if (c != REGION_ALL)
4409 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004411 /* This spell file is for other regions. */
4412 region_mask = 0;
4413 }
4414
4415 if (region_mask != 0)
4416 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004417 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4418 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4419 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004420 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4421 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004422 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 }
4425 }
4426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004427 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004428 ga_clear(&wp->w_s->b_langp);
4429 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004430
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 /* For each language figure out what language to use for sound folding and
4432 * REP items. If the language doesn't support it itself use another one
4433 * with the same name. E.g. for "en-math" use "en". */
4434 for (i = 0; i < ga.ga_len; ++i)
4435 {
4436 lp = LANGP_ENTRY(ga, i);
4437
4438 /* sound folding */
4439 if (lp->lp_slang->sl_sal.ga_len > 0)
4440 /* language does sound folding itself */
4441 lp->lp_sallang = lp->lp_slang;
4442 else
4443 /* find first similar language that does sound folding */
4444 for (j = 0; j < ga.ga_len; ++j)
4445 {
4446 lp2 = LANGP_ENTRY(ga, j);
4447 if (lp2->lp_slang->sl_sal.ga_len > 0
4448 && STRNCMP(lp->lp_slang->sl_name,
4449 lp2->lp_slang->sl_name, 2) == 0)
4450 {
4451 lp->lp_sallang = lp2->lp_slang;
4452 break;
4453 }
4454 }
4455
4456 /* REP items */
4457 if (lp->lp_slang->sl_rep.ga_len > 0)
4458 /* language has REP items itself */
4459 lp->lp_replang = lp->lp_slang;
4460 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004461 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004462 for (j = 0; j < ga.ga_len; ++j)
4463 {
4464 lp2 = LANGP_ENTRY(ga, j);
4465 if (lp2->lp_slang->sl_rep.ga_len > 0
4466 && STRNCMP(lp->lp_slang->sl_name,
4467 lp2->lp_slang->sl_name, 2) == 0)
4468 {
4469 lp->lp_replang = lp2->lp_slang;
4470 break;
4471 }
4472 }
4473 }
4474
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004475theend:
4476 vim_free(spl_copy);
4477 recursive = FALSE;
4478 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479}
4480
4481/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004482 * Clear the midword characters for buffer "buf".
4483 */
4484 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004485clear_midword(wp)
4486 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004487{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004488 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004489#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004490 vim_free(wp->w_s->b_spell_ismw_mb);
4491 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004492#endif
4493}
4494
4495/*
4496 * Use the "sl_midword" field of language "lp" for buffer "buf".
4497 * They add up to any currently used midword characters.
4498 */
4499 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004500use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004501 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004502 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004503{
4504 char_u *p;
4505
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004506 if (lp->sl_midword == NULL) /* there aren't any */
4507 return;
4508
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004509 for (p = lp->sl_midword; *p != NUL; )
4510#ifdef FEAT_MBYTE
4511 if (has_mbyte)
4512 {
4513 int c, l, n;
4514 char_u *bp;
4515
4516 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004517 l = (*mb_ptr2len)(p);
4518 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519 wp->w_s->b_spell_ismw[c] = TRUE;
4520 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004521 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004522 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004523 else
4524 {
4525 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004526 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4527 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004528 if (bp != NULL)
4529 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004530 vim_free(wp->w_s->b_spell_ismw_mb);
4531 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004532 vim_strncpy(bp + n, p, l);
4533 }
4534 }
4535 p += l;
4536 }
4537 else
4538#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004539 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004540}
4541
4542/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004543 * Find the region "region[2]" in "rp" (points to "sl_regions").
4544 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004545 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004546 */
4547 static int
4548find_region(rp, region)
4549 char_u *rp;
4550 char_u *region;
4551{
4552 int i;
4553
4554 for (i = 0; ; i += 2)
4555 {
4556 if (rp[i] == NUL)
4557 return REGION_ALL;
4558 if (rp[i] == region[0] && rp[i + 1] == region[1])
4559 break;
4560 }
4561 return i / 2;
4562}
4563
4564/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004565 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004566 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004567 * Word WF_ONECAP
4568 * W WORD WF_ALLCAP
4569 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 */
4571 static int
4572captype(word, end)
4573 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575{
4576 char_u *p;
4577 int c;
4578 int firstcap;
4579 int allcap;
4580 int past_second = FALSE; /* past second word char */
4581
4582 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004583 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585 return 0; /* only non-word characters, illegal word */
4586#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004587 if (has_mbyte)
4588 c = mb_ptr2char_adv(&p);
4589 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004590#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004591 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004592 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593
4594 /*
4595 * Need to check all letters to find a word with mixed upper/lower.
4596 * But a word with an upper char only at start is a ONECAP.
4597 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004598 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004599 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004601 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004602 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004603 {
4604 /* UUl -> KEEPCAP */
4605 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607 allcap = FALSE;
4608 }
4609 else if (!allcap)
4610 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004611 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004612 past_second = TRUE;
4613 }
4614
4615 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004616 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004617 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004618 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004619 return 0;
4620}
4621
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004622/*
4623 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4624 * capital. So that make_case_word() can turn WOrd into Word.
4625 * Add ALLCAP for "WOrD".
4626 */
4627 static int
4628badword_captype(word, end)
4629 char_u *word;
4630 char_u *end;
4631{
4632 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004633 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004634 int l, u;
4635 int first;
4636 char_u *p;
4637
4638 if (flags & WF_KEEPCAP)
4639 {
4640 /* Count the number of UPPER and lower case letters. */
4641 l = u = 0;
4642 first = FALSE;
4643 for (p = word; p < end; mb_ptr_adv(p))
4644 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004645 c = PTR2CHAR(p);
4646 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004647 {
4648 ++u;
4649 if (p == word)
4650 first = TRUE;
4651 }
4652 else
4653 ++l;
4654 }
4655
4656 /* If there are more UPPER than lower case letters suggest an
4657 * ALLCAP word. Otherwise, if the first letter is UPPER then
4658 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4659 * require three upper case letters. */
4660 if (u > l && u > 2)
4661 flags |= WF_ALLCAP;
4662 else if (first)
4663 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004664
4665 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4666 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004667 }
4668 return flags;
4669}
4670
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004671# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4672/*
4673 * Free all languages.
4674 */
4675 void
4676spell_free_all()
4677{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004678 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004679 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004680 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004681
Bram Moolenaar860cae12010-06-05 23:22:07 +02004682 /* Go through all buffers and handle 'spelllang'. */ //<VN>
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004683 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004684 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004685
4686 while (first_lang != NULL)
4687 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004688 slang = first_lang;
4689 first_lang = slang->sl_next;
4690 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004691 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004692
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004693 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004694 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004695 /* Delete the internal wordlist and its .spl file */
4696 mch_remove(int_wordlist);
4697 int_wordlist_spl(fname);
4698 mch_remove(fname);
4699 vim_free(int_wordlist);
4700 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004701 }
4702
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004703 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004704
4705 vim_free(repl_to);
4706 repl_to = NULL;
4707 vim_free(repl_from);
4708 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004709}
4710# endif
4711
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004712# if defined(FEAT_MBYTE) || defined(PROTO)
4713/*
4714 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004715 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004716 */
4717 void
4718spell_reload()
4719{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004720 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004721
Bram Moolenaarea408852005-06-25 22:49:46 +00004722 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 init_spell_chartab();
4724
4725 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004726 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004727
4728 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004729 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004730 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004731 /* Only load the wordlists when 'spelllang' is set and there is a
4732 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004733 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004734 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004736 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004737 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004738# ifdef FEAT_WINDOWS
4739 break;
4740# endif
4741 }
4742 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743 }
4744}
4745# endif
4746
Bram Moolenaarb765d632005-06-07 21:00:02 +00004747/*
4748 * Reload the spell file "fname" if it's loaded.
4749 */
4750 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004752 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004754{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004755 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004758 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004759 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004760 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004761 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004762 slang_clear(slang);
4763 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004764 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004765 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004766 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004768 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770
4771 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004772 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004774 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004775}
4776
4777
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778/*
4779 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780 */
4781
Bram Moolenaar51485f02005-06-04 21:55:20 +00004782#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004783 and .dic file. */
4784/*
4785 * Main structure to store the contents of a ".aff" file.
4786 */
4787typedef struct afffile_S
4788{
4789 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004790 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004791 unsigned af_rare; /* RARE ID for rare word */
4792 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004793 unsigned af_bad; /* BAD ID for banned word */
4794 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004795 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004796 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004797 unsigned af_comproot; /* COMPOUNDROOT ID */
4798 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4799 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004800 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004801 int af_pfxpostpone; /* postpone prefixes without chop string and
4802 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004803 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4804 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004805 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806} afffile_T;
4807
Bram Moolenaar6de68532005-08-24 22:08:48 +00004808#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004809#define AFT_LONG 1 /* flags are two characters */
4810#define AFT_CAPLONG 2 /* flags are one or two characters */
4811#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004812
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004813typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004814/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4815struct affentry_S
4816{
4817 affentry_T *ae_next; /* next affix with same name/number */
4818 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4819 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004820 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 char_u *ae_cond; /* condition (NULL for ".") */
4822 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004823 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4824 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004825};
4826
Bram Moolenaar6de68532005-08-24 22:08:48 +00004827#ifdef FEAT_MBYTE
4828# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4829#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004830# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004831#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004832
Bram Moolenaar51485f02005-06-04 21:55:20 +00004833/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4834typedef struct affheader_S
4835{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004836 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4837 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4838 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004839 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004840 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004841 affentry_T *ah_first; /* first affix entry */
4842} affheader_T;
4843
4844#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4845
Bram Moolenaar6de68532005-08-24 22:08:48 +00004846/* Flag used in compound items. */
4847typedef struct compitem_S
4848{
4849 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4850 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4851 int ci_newID; /* affix ID after renumbering. */
4852} compitem_T;
4853
4854#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4855
Bram Moolenaar51485f02005-06-04 21:55:20 +00004856/*
4857 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004858 * the need to keep track of each allocated thing, everything is freed all at
4859 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004860 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4861 * "sb_data" is correct for systems where pointers must be aligned on
4862 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004863 */
4864#define SBLOCKSIZE 16000 /* size of sb_data */
4865typedef struct sblock_S sblock_T;
4866struct sblock_S
4867{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004868 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004869 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004870 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004871};
4872
4873/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004874 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004876typedef struct wordnode_S wordnode_T;
4877struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004878{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004879 union /* shared to save space */
4880 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004881 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004882 int index; /* index in written nodes (valid after first
4883 round) */
4884 } wn_u1;
4885 union /* shared to save space */
4886 {
4887 wordnode_T *next; /* next node with same hash key */
4888 wordnode_T *wnode; /* parent node that will write this node */
4889 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004890 wordnode_T *wn_child; /* child (next byte in word) */
4891 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4892 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004893 int wn_refs; /* Nr. of references to this node. Only
4894 relevant for first node in a list of
4895 siblings, in following siblings it is
4896 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004898
4899 /* Info for when "wn_byte" is NUL.
4900 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4901 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4902 * "wn_region" the LSW of the wordnr. */
4903 char_u wn_affixID; /* supported/required prefix ID or 0 */
4904 short_u wn_flags; /* WF_ flags */
4905 short wn_region; /* region mask */
4906
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004907#ifdef SPELL_PRINTTREE
4908 int wn_nr; /* sequence nr for printing */
4909#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004910};
4911
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004912#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4913
Bram Moolenaar51485f02005-06-04 21:55:20 +00004914#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916/*
4917 * Info used while reading the spell files.
4918 */
4919typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004920{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004922 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004923
Bram Moolenaar51485f02005-06-04 21:55:20 +00004924 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004925 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004926
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004927 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004928
Bram Moolenaar4770d092006-01-12 23:22:24 +00004929 long si_sugtree; /* creating the soundfolding trie */
4930
Bram Moolenaar51485f02005-06-04 21:55:20 +00004931 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004932 long si_blocks_cnt; /* memory blocks allocated */
4933 long si_compress_cnt; /* words to add before lowering
4934 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004935 wordnode_T *si_first_free; /* List of nodes that have been freed during
4936 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004937 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004938#ifdef SPELL_PRINTTREE
4939 int si_wordnode_nr; /* sequence nr for nodes */
4940#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004941 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004942
Bram Moolenaar51485f02005-06-04 21:55:20 +00004943 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004944 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004945 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004946 int si_region; /* region mask */
4947 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004948 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004949 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004950 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004951 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004952 int si_region_count; /* number of regions supported (1 when there
4953 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004954 char_u si_region_name[16]; /* region names; used only if
4955 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956
4957 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004958 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004960 char_u *si_sofofr; /* SOFOFROM text */
4961 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004962 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004963 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004964 int si_followup; /* soundsalike: ? */
4965 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004966 hashtab_T si_commonwords; /* hashtable for common words */
4967 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004968 int si_rem_accents; /* soundsalike: remove accents */
4969 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004970 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004971 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004972 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004973 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004974 int si_compoptions; /* COMP_ flags */
4975 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4976 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004977 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004978 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004979 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004980 garray_T si_prefcond; /* table with conditions for postponed
4981 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004982 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004983 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004984} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004985
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004986static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004987static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004988static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004989static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004990static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4991static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4992static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004993static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004994static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4995static void aff_check_number __ARGS((int spinval, int affval, char *name));
4996static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004997static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004998static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4999static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00005000static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005001static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005002static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005003static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005004static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005005static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005006static 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 +00005007static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5008static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5009static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005010static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005011static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005012static 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 +00005013static 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 +00005014static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005015static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005016static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5017static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5018static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005019static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005020static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005021static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005022static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005023static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5024static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5025static int sug_maketable __ARGS((spellinfo_T *spin));
5026static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5027static int offset2bytes __ARGS((int nr, char_u *buf));
5028static int bytes2offset __ARGS((char_u **pp));
5029static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005030static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005031static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005032static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005033
Bram Moolenaar53805d12005-08-01 07:08:33 +00005034/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5035 * but it must be negative to indicate the prefix tree to tree_add_word().
5036 * Use a negative number with the lower 8 bits zero. */
5037#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005038
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005039/* flags for "condit" argument of store_aff_word() */
5040#define CONDIT_COMB 1 /* affix must combine */
5041#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5042#define CONDIT_SUF 4 /* add a suffix for matching flags */
5043#define CONDIT_AFF 8 /* word already has an affix */
5044
Bram Moolenaar5195e452005-08-19 20:32:47 +00005045/*
5046 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5047 */
5048static long compress_start = 30000; /* memory / SBLOCKSIZE */
5049static long compress_inc = 100; /* memory / SBLOCKSIZE */
5050static long compress_added = 500000; /* word count */
5051
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005052#ifdef SPELL_PRINTTREE
5053/*
5054 * For debugging the tree code: print the current tree in a (more or less)
5055 * readable format, so that we can see what happens when adding a word and/or
5056 * compressing the tree.
5057 * Based on code from Olaf Seibert.
5058 */
5059#define PRINTLINESIZE 1000
5060#define PRINTWIDTH 6
5061
5062#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5063 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5064
5065static char line1[PRINTLINESIZE];
5066static char line2[PRINTLINESIZE];
5067static char line3[PRINTLINESIZE];
5068
5069 static void
5070spell_clear_flags(wordnode_T *node)
5071{
5072 wordnode_T *np;
5073
5074 for (np = node; np != NULL; np = np->wn_sibling)
5075 {
5076 np->wn_u1.index = FALSE;
5077 spell_clear_flags(np->wn_child);
5078 }
5079}
5080
5081 static void
5082spell_print_node(wordnode_T *node, int depth)
5083{
5084 if (node->wn_u1.index)
5085 {
5086 /* Done this node before, print the reference. */
5087 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5088 PRINTSOME(line2, depth, " ", 0, 0);
5089 PRINTSOME(line3, depth, " ", 0, 0);
5090 msg(line1);
5091 msg(line2);
5092 msg(line3);
5093 }
5094 else
5095 {
5096 node->wn_u1.index = TRUE;
5097
5098 if (node->wn_byte != NUL)
5099 {
5100 if (node->wn_child != NULL)
5101 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5102 else
5103 /* Cannot happen? */
5104 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5105 }
5106 else
5107 PRINTSOME(line1, depth, " $ ", 0, 0);
5108
5109 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5110
5111 if (node->wn_sibling != NULL)
5112 PRINTSOME(line3, depth, " | ", 0, 0);
5113 else
5114 PRINTSOME(line3, depth, " ", 0, 0);
5115
5116 if (node->wn_byte == NUL)
5117 {
5118 msg(line1);
5119 msg(line2);
5120 msg(line3);
5121 }
5122
5123 /* do the children */
5124 if (node->wn_byte != NUL && node->wn_child != NULL)
5125 spell_print_node(node->wn_child, depth + 1);
5126
5127 /* do the siblings */
5128 if (node->wn_sibling != NULL)
5129 {
5130 /* get rid of all parent details except | */
5131 STRCPY(line1, line3);
5132 STRCPY(line2, line3);
5133 spell_print_node(node->wn_sibling, depth);
5134 }
5135 }
5136}
5137
5138 static void
5139spell_print_tree(wordnode_T *root)
5140{
5141 if (root != NULL)
5142 {
5143 /* Clear the "wn_u1.index" fields, used to remember what has been
5144 * done. */
5145 spell_clear_flags(root);
5146
5147 /* Recursively print the tree. */
5148 spell_print_node(root, 0);
5149 }
5150}
5151#endif /* SPELL_PRINTTREE */
5152
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005153/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005154 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005155 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005156 */
5157 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005158spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005159 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005160 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005161{
5162 FILE *fd;
5163 afffile_T *aff;
5164 char_u rline[MAXLINELEN];
5165 char_u *line;
5166 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005167#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005168 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005169 int itemcnt;
5170 char_u *p;
5171 int lnum = 0;
5172 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005173 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005174 int aff_todo = 0;
5175 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005176 char_u *low = NULL;
5177 char_u *fol = NULL;
5178 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005180 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005181 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005182 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005184 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005185 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005186 int compminlen = 0; /* COMPOUNDMIN value */
5187 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005188 int compoptions = 0; /* COMP_ flags */
5189 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005190 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005191 concatenated */
5192 char_u *midword = NULL; /* MIDWORD value */
5193 char_u *syllable = NULL; /* SYLLABLE value */
5194 char_u *sofofrom = NULL; /* SOFOFROM value */
5195 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196
Bram Moolenaar51485f02005-06-04 21:55:20 +00005197 /*
5198 * Open the file.
5199 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005200 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005201 if (fd == NULL)
5202 {
5203 EMSG2(_(e_notopen), fname);
5204 return NULL;
5205 }
5206
Bram Moolenaar4770d092006-01-12 23:22:24 +00005207 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5208 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 /* Only do REP lines when not done in another .aff file already. */
5211 do_rep = spin->si_rep.ga_len == 0;
5212
Bram Moolenaar4770d092006-01-12 23:22:24 +00005213 /* Only do REPSAL lines when not done in another .aff file already. */
5214 do_repsal = spin->si_repsal.ga_len == 0;
5215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216 /* Only do SAL lines when not done in another .aff file already. */
5217 do_sal = spin->si_sal.ga_len == 0;
5218
5219 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005220 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221
Bram Moolenaar51485f02005-06-04 21:55:20 +00005222 /*
5223 * Allocate and init the afffile_T structure.
5224 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005225 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005226 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005227 {
5228 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005229 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005230 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005231 hash_init(&aff->af_pref);
5232 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005233 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234
5235 /*
5236 * Read all the lines in the file one by one.
5237 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005238 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005239 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005240 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005241 ++lnum;
5242
5243 /* Skip comment lines. */
5244 if (*rline == '#')
5245 continue;
5246
5247 /* Convert from "SET" to 'encoding' when needed. */
5248 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005249#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005250 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005252 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005253 if (pc == NULL)
5254 {
5255 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5256 fname, lnum, rline);
5257 continue;
5258 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 line = pc;
5260 }
5261 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005262#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005263 {
5264 pc = NULL;
5265 line = rline;
5266 }
5267
5268 /* Split the line up in white separated items. Put a NUL after each
5269 * item. */
5270 itemcnt = 0;
5271 for (p = line; ; )
5272 {
5273 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5274 ++p;
5275 if (*p == NUL)
5276 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005277 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005278 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005279 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005280 /* A few items have arbitrary text argument, don't split them. */
5281 if (itemcnt == 2 && spell_info_item(items[0]))
5282 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5283 ++p;
5284 else
5285 while (*p > ' ') /* skip until white space or CR/NL */
5286 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005287 if (*p == NUL)
5288 break;
5289 *p++ = NUL;
5290 }
5291
5292 /* Handle non-empty lines. */
5293 if (itemcnt > 0)
5294 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005295 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005296 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005297#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005298 /* Setup for conversion from "ENC" to 'encoding'. */
5299 aff->af_enc = enc_canonize(items[1]);
5300 if (aff->af_enc != NULL && !spin->si_ascii
5301 && convert_setup(&spin->si_conv, aff->af_enc,
5302 p_enc) == FAIL)
5303 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5304 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005305 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005306#else
5307 smsg((char_u *)_("Conversion in %s not supported"), fname);
5308#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005309 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005310 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005311 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005312 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005313 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005314 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005315 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005316 aff->af_flagtype = AFT_NUM;
5317 else if (STRCMP(items[1], "caplong") == 0)
5318 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005319 else
5320 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5321 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005322 if (aff->af_rare != 0
5323 || aff->af_keepcase != 0
5324 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005325 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005326 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005327 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005328 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005329 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005330 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005331 || aff->af_suff.ht_used > 0
5332 || aff->af_pref.ht_used > 0)
5333 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5334 fname, lnum, items[1]);
5335 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005336 else if (spell_info_item(items[0]))
5337 {
5338 p = (char_u *)getroom(spin,
5339 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5340 + STRLEN(items[0])
5341 + STRLEN(items[1]) + 3, FALSE);
5342 if (p != NULL)
5343 {
5344 if (spin->si_info != NULL)
5345 {
5346 STRCPY(p, spin->si_info);
5347 STRCAT(p, "\n");
5348 }
5349 STRCAT(p, items[0]);
5350 STRCAT(p, " ");
5351 STRCAT(p, items[1]);
5352 spin->si_info = p;
5353 }
5354 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005355 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005356 && midword == NULL)
5357 {
5358 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005359 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005360 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005362 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005363 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005364 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005365 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5366 || is_aff_rule(items, itemcnt, "RARE", 2))
5367 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005368 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005369 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005370 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005371 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005372 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005373 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5374 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005375 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005376 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005377 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005378 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005379 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005380 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5381 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5382 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005383 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005384 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5385 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005386 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005387 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005388 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005389 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005390 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5391 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005392 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005393 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005394 && aff->af_circumfix == 0)
5395 {
5396 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5397 fname, lnum);
5398 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005399 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005400 && aff->af_nosuggest == 0)
5401 {
5402 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5403 fname, lnum);
5404 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005405 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5406 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005407 && aff->af_needcomp == 0)
5408 {
5409 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5410 fname, lnum);
5411 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005412 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005413 && aff->af_comproot == 0)
5414 {
5415 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5416 fname, lnum);
5417 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005418 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5419 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005420 {
5421 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5422 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005423 if (aff->af_pref.ht_used > 0)
5424 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5425 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005426 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005427 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5428 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005429 {
5430 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5431 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005432 if (aff->af_pref.ht_used > 0)
5433 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5434 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005435 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005436 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005437 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005438 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005439 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005440 * "Na" into "Na+", "1234" into "1234+". */
5441 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005442 if (p != NULL)
5443 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005444 STRCPY(p, items[1]);
5445 STRCAT(p, "+");
5446 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005447 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005448 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005449 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5450 {
5451 /* We don't use the count, but do check that it's a number and
5452 * not COMPOUNDRULE mistyped. */
5453 if (atoi((char *)items[1]) == 0)
5454 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5455 fname, lnum, items[1]);
5456 }
5457 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005458 {
5459 /* Concatenate this string to previously defined ones, using a
5460 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005461 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005462 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005463 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005464 p = getroom(spin, l, FALSE);
5465 if (p != NULL)
5466 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005467 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005468 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005469 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005470 STRCAT(p, "/");
5471 }
5472 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005473 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005474 }
5475 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005476 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005477 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005478 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005479 compmax = atoi((char *)items[1]);
5480 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005481 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005482 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005483 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005484 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005485 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005486 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005487 compminlen = atoi((char *)items[1]);
5488 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005489 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5490 fname, lnum, items[1]);
5491 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005492 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005493 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005494 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005495 compsylmax = atoi((char *)items[1]);
5496 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005497 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5498 fname, lnum, items[1]);
5499 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005500 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005501 {
5502 compoptions |= COMP_CHECKDUP;
5503 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005504 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005505 {
5506 compoptions |= COMP_CHECKREP;
5507 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005508 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005509 {
5510 compoptions |= COMP_CHECKCASE;
5511 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005512 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005513 {
5514 compoptions |= COMP_CHECKTRIPLE;
5515 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005516 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005517 {
5518 if (atoi((char *)items[1]) == 0)
5519 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5520 fname, lnum, items[1]);
5521 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005522 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005523 {
5524 garray_T *gap = &spin->si_comppat;
5525 int i;
5526
5527 /* Only add the couple if it isn't already there. */
5528 for (i = 0; i < gap->ga_len - 1; i += 2)
5529 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5530 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5531 items[2]) == 0)
5532 break;
5533 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5534 {
5535 ((char_u **)(gap->ga_data))[gap->ga_len++]
5536 = getroom_save(spin, items[1]);
5537 ((char_u **)(gap->ga_data))[gap->ga_len++]
5538 = getroom_save(spin, items[2]);
5539 }
5540 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005541 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005542 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005543 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005544 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005545 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005546 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005547 {
5548 spin->si_nobreak = TRUE;
5549 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005550 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005551 {
5552 spin->si_nosplitsugs = TRUE;
5553 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005554 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005555 {
5556 spin->si_nosugfile = TRUE;
5557 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005558 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005559 {
5560 aff->af_pfxpostpone = TRUE;
5561 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005562 else if ((STRCMP(items[0], "PFX") == 0
5563 || STRCMP(items[0], "SFX") == 0)
5564 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005565 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005566 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005567 int lasti = 4;
5568 char_u key[AH_KEY_LEN];
5569
5570 if (*items[0] == 'P')
5571 tp = &aff->af_pref;
5572 else
5573 tp = &aff->af_suff;
5574
5575 /* Myspell allows the same affix name to be used multiple
5576 * times. The affix files that do this have an undocumented
5577 * "S" flag on all but the last block, thus we check for that
5578 * and store it in ah_follows. */
5579 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5580 hi = hash_find(tp, key);
5581 if (!HASHITEM_EMPTY(hi))
5582 {
5583 cur_aff = HI2AH(hi);
5584 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5585 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5586 fname, lnum, items[1]);
5587 if (!cur_aff->ah_follows)
5588 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5589 fname, lnum, items[1]);
5590 }
5591 else
5592 {
5593 /* New affix letter. */
5594 cur_aff = (affheader_T *)getroom(spin,
5595 sizeof(affheader_T), TRUE);
5596 if (cur_aff == NULL)
5597 break;
5598 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5599 fname, lnum);
5600 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5601 break;
5602 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005603 || cur_aff->ah_flag == aff->af_rare
5604 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005605 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005606 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005607 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005608 || cur_aff->ah_flag == aff->af_needcomp
5609 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005610 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 +00005611 fname, lnum, items[1]);
5612 STRCPY(cur_aff->ah_key, items[1]);
5613 hash_add(tp, cur_aff->ah_key);
5614
5615 cur_aff->ah_combine = (*items[2] == 'Y');
5616 }
5617
5618 /* Check for the "S" flag, which apparently means that another
5619 * block with the same affix name is following. */
5620 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5621 {
5622 ++lasti;
5623 cur_aff->ah_follows = TRUE;
5624 }
5625 else
5626 cur_aff->ah_follows = FALSE;
5627
Bram Moolenaar8db73182005-06-17 21:51:16 +00005628 /* Myspell allows extra text after the item, but that might
5629 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005630 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005631 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005632
Bram Moolenaar95529562005-08-25 21:21:38 +00005633 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005634 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5635 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005636
Bram Moolenaar95529562005-08-25 21:21:38 +00005637 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005638 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005639 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005640 {
5641 /* Use a new number in the .spl file later, to be able
5642 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005643 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005644 cur_aff->ah_newID = ++spin->si_newprefID;
5645
5646 /* We only really use ah_newID if the prefix is
5647 * postponed. We know that only after handling all
5648 * the items. */
5649 did_postpone_prefix = FALSE;
5650 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005651 else
5652 /* Did use the ID in a previous block. */
5653 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005654 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005655
Bram Moolenaar51485f02005-06-04 21:55:20 +00005656 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005657 }
5658 else if ((STRCMP(items[0], "PFX") == 0
5659 || STRCMP(items[0], "SFX") == 0)
5660 && aff_todo > 0
5661 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005662 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005663 {
5664 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005665 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005666 int lasti = 5;
5667
Bram Moolenaar8db73182005-06-17 21:51:16 +00005668 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005669 * mean mistakes go unnoticed. Require a comment-starter.
5670 * Hunspell uses a "-" item. */
5671 if (itemcnt > lasti && *items[lasti] != '#'
5672 && (STRCMP(items[lasti], "-") != 0
5673 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005674 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005675
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005676 /* New item for an affix letter. */
5677 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005678 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005679 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005680 if (aff_entry == NULL)
5681 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005682
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005683 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005684 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005685 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005686 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005687 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005689 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005690 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5691 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005692 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005693 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005694 aff_process_flags(aff, aff_entry);
5695 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005696 }
5697
Bram Moolenaar51485f02005-06-04 21:55:20 +00005698 /* Don't use an affix entry with non-ASCII characters when
5699 * "spin->si_ascii" is TRUE. */
5700 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005701 || has_non_ascii(aff_entry->ae_add)))
5702 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005703 aff_entry->ae_next = cur_aff->ah_first;
5704 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005705
5706 if (STRCMP(items[4], ".") != 0)
5707 {
5708 char_u buf[MAXLINELEN];
5709
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005710 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005711 if (*items[0] == 'P')
5712 sprintf((char *)buf, "^%s", items[4]);
5713 else
5714 sprintf((char *)buf, "%s$", items[4]);
5715 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005716 RE_MAGIC + RE_STRING + RE_STRICT);
5717 if (aff_entry->ae_prog == NULL)
5718 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5719 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005720 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005721
5722 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005723 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005724 * Can't be done for an affix with flags, ignoring
5725 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005726 if (*items[0] == 'P' && aff->af_pfxpostpone
5727 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005728 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005729 /* When the chop string is one lower-case letter and
5730 * the add string ends in the upper-case letter we set
5731 * the "upper" flag, clear "ae_chop" and remove the
5732 * letters from "ae_add". The condition must either
5733 * be empty or start with the same letter. */
5734 if (aff_entry->ae_chop != NULL
5735 && aff_entry->ae_add != NULL
5736#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005737 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005738 aff_entry->ae_chop)] == NUL
5739#else
5740 && aff_entry->ae_chop[1] == NUL
5741#endif
5742 )
5743 {
5744 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005745
Bram Moolenaar53805d12005-08-01 07:08:33 +00005746 c = PTR2CHAR(aff_entry->ae_chop);
5747 c_up = SPELL_TOUPPER(c);
5748 if (c_up != c
5749 && (aff_entry->ae_cond == NULL
5750 || PTR2CHAR(aff_entry->ae_cond) == c))
5751 {
5752 p = aff_entry->ae_add
5753 + STRLEN(aff_entry->ae_add);
5754 mb_ptr_back(aff_entry->ae_add, p);
5755 if (PTR2CHAR(p) == c_up)
5756 {
5757 upper = TRUE;
5758 aff_entry->ae_chop = NULL;
5759 *p = NUL;
5760
5761 /* The condition is matched with the
5762 * actual word, thus must check for the
5763 * upper-case letter. */
5764 if (aff_entry->ae_cond != NULL)
5765 {
5766 char_u buf[MAXLINELEN];
5767#ifdef FEAT_MBYTE
5768 if (has_mbyte)
5769 {
5770 onecap_copy(items[4], buf, TRUE);
5771 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005772 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005773 }
5774 else
5775#endif
5776 *aff_entry->ae_cond = c_up;
5777 if (aff_entry->ae_cond != NULL)
5778 {
5779 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005780 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005781 vim_free(aff_entry->ae_prog);
5782 aff_entry->ae_prog = vim_regcomp(
5783 buf, RE_MAGIC + RE_STRING);
5784 }
5785 }
5786 }
5787 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005788 }
5789
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005790 if (aff_entry->ae_chop == NULL
5791 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005792 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005793 int idx;
5794 char_u **pp;
5795 int n;
5796
Bram Moolenaar6de68532005-08-24 22:08:48 +00005797 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005798 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5799 --idx)
5800 {
5801 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5802 if (str_equal(p, aff_entry->ae_cond))
5803 break;
5804 }
5805 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5806 {
5807 /* Not found, add a new condition. */
5808 idx = spin->si_prefcond.ga_len++;
5809 pp = ((char_u **)spin->si_prefcond.ga_data)
5810 + idx;
5811 if (aff_entry->ae_cond == NULL)
5812 *pp = NULL;
5813 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005814 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005815 aff_entry->ae_cond);
5816 }
5817
5818 /* Add the prefix to the prefix tree. */
5819 if (aff_entry->ae_add == NULL)
5820 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005821 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005822 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005823
Bram Moolenaar53805d12005-08-01 07:08:33 +00005824 /* PFX_FLAGS is a negative number, so that
5825 * tree_add_word() knows this is the prefix tree. */
5826 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005827 if (!cur_aff->ah_combine)
5828 n |= WFP_NC;
5829 if (upper)
5830 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005831 if (aff_entry->ae_comppermit)
5832 n |= WFP_COMPPERMIT;
5833 if (aff_entry->ae_compforbid)
5834 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005835 tree_add_word(spin, p, spin->si_prefroot, n,
5836 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005837 did_postpone_prefix = TRUE;
5838 }
5839
5840 /* Didn't actually use ah_newID, backup si_newprefID. */
5841 if (aff_todo == 0 && !did_postpone_prefix)
5842 {
5843 --spin->si_newprefID;
5844 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005845 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005846 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005847 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005848 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005849 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005850 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005851 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005852 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005853 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005854 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005855 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005856 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005857 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005858 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005859 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005860 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005861 else if (is_aff_rule(items, itemcnt, "REP", 2)
5862 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005863 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005864 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005865 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005866 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005867 fname, lnum);
5868 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005869 else if ((STRCMP(items[0], "REP") == 0
5870 || STRCMP(items[0], "REPSAL") == 0)
5871 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005872 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005873 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005874 /* Myspell ignores extra arguments, we require it starts with
5875 * # to detect mistakes. */
5876 if (itemcnt > 3 && items[3][0] != '#')
5877 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005878 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005879 {
5880 /* Replace underscore with space (can't include a space
5881 * directly). */
5882 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5883 if (*p == '_')
5884 *p = ' ';
5885 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5886 if (*p == '_')
5887 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005888 add_fromto(spin, items[0][3] == 'S'
5889 ? &spin->si_repsal
5890 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005891 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005892 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005893 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005894 {
5895 /* MAP item or count */
5896 if (!found_map)
5897 {
5898 /* First line contains the count. */
5899 found_map = TRUE;
5900 if (!isdigit(*items[1]))
5901 smsg((char_u *)_("Expected MAP count in %s line %d"),
5902 fname, lnum);
5903 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005904 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005905 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005906 int c;
5907
5908 /* Check that every character appears only once. */
5909 for (p = items[1]; *p != NUL; )
5910 {
5911#ifdef FEAT_MBYTE
5912 c = mb_ptr2char_adv(&p);
5913#else
5914 c = *p++;
5915#endif
5916 if ((spin->si_map.ga_len > 0
5917 && vim_strchr(spin->si_map.ga_data, c)
5918 != NULL)
5919 || vim_strchr(p, c) != NULL)
5920 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5921 fname, lnum);
5922 }
5923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005924 /* We simply concatenate all the MAP strings, separated by
5925 * slashes. */
5926 ga_concat(&spin->si_map, items[1]);
5927 ga_append(&spin->si_map, '/');
5928 }
5929 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005930 /* Accept "SAL from to" and "SAL from to #comment". */
5931 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005932 {
5933 if (do_sal)
5934 {
5935 /* SAL item (sounds-a-like)
5936 * Either one of the known keys or a from-to pair. */
5937 if (STRCMP(items[1], "followup") == 0)
5938 spin->si_followup = sal_to_bool(items[2]);
5939 else if (STRCMP(items[1], "collapse_result") == 0)
5940 spin->si_collapse = sal_to_bool(items[2]);
5941 else if (STRCMP(items[1], "remove_accents") == 0)
5942 spin->si_rem_accents = sal_to_bool(items[2]);
5943 else
5944 /* when "to" is "_" it means empty */
5945 add_fromto(spin, &spin->si_sal, items[1],
5946 STRCMP(items[2], "_") == 0 ? (char_u *)""
5947 : items[2]);
5948 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005949 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005950 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005951 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005952 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005953 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005954 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005955 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005956 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005957 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005958 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005959 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005960 else if (STRCMP(items[0], "COMMON") == 0)
5961 {
5962 int i;
5963
5964 for (i = 1; i < itemcnt; ++i)
5965 {
5966 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5967 items[i])))
5968 {
5969 p = vim_strsave(items[i]);
5970 if (p == NULL)
5971 break;
5972 hash_add(&spin->si_commonwords, p);
5973 }
5974 }
5975 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005976 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005977 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005978 fname, lnum, items[0]);
5979 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005980 }
5981
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005982 if (fol != NULL || low != NULL || upp != NULL)
5983 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005984 if (spin->si_clear_chartab)
5985 {
5986 /* Clear the char type tables, don't want to use any of the
5987 * currently used spell properties. */
5988 init_spell_chartab();
5989 spin->si_clear_chartab = FALSE;
5990 }
5991
Bram Moolenaar3982c542005-06-08 21:56:31 +00005992 /*
5993 * Don't write a word table for an ASCII file, so that we don't check
5994 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005995 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005996 * mb_get_class(), the list of chars in the file will be incomplete.
5997 */
5998 if (!spin->si_ascii
5999#ifdef FEAT_MBYTE
6000 && !enc_utf8
6001#endif
6002 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006003 {
6004 if (fol == NULL || low == NULL || upp == NULL)
6005 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6006 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006007 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006008 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006009
6010 vim_free(fol);
6011 vim_free(low);
6012 vim_free(upp);
6013 }
6014
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006015 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006016 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006017 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006018 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006019 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006020 }
6021
Bram Moolenaar6de68532005-08-24 22:08:48 +00006022 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006023 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006024 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6025 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006026 }
6027
Bram Moolenaar6de68532005-08-24 22:08:48 +00006028 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006029 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006030 if (syllable == NULL)
6031 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6032 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6033 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006034 }
6035
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006036 if (compoptions != 0)
6037 {
6038 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6039 spin->si_compoptions |= compoptions;
6040 }
6041
Bram Moolenaar6de68532005-08-24 22:08:48 +00006042 if (compflags != NULL)
6043 process_compflags(spin, aff, compflags);
6044
6045 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006046 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006047 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006048 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006049 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006050 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006051 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006052 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006053 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006054 }
6055
Bram Moolenaar6de68532005-08-24 22:08:48 +00006056 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006057 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006058 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6059 spin->si_syllable = syllable;
6060 }
6061
6062 if (sofofrom != NULL || sofoto != NULL)
6063 {
6064 if (sofofrom == NULL || sofoto == NULL)
6065 smsg((char_u *)_("Missing SOFO%s line in %s"),
6066 sofofrom == NULL ? "FROM" : "TO", fname);
6067 else if (spin->si_sal.ga_len > 0)
6068 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006069 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006070 {
6071 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6072 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6073 spin->si_sofofr = sofofrom;
6074 spin->si_sofoto = sofoto;
6075 }
6076 }
6077
6078 if (midword != NULL)
6079 {
6080 aff_check_string(spin->si_midword, midword, "MIDWORD");
6081 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006082 }
6083
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006084 vim_free(pc);
6085 fclose(fd);
6086 return aff;
6087}
6088
6089/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006090 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6091 * a comment is following after item "mincount".
6092 */
6093 static int
6094is_aff_rule(items, itemcnt, rulename, mincount)
6095 char_u **items;
6096 int itemcnt;
6097 char *rulename;
6098 int mincount;
6099{
6100 return (STRCMP(items[0], rulename) == 0
6101 && (itemcnt == mincount
6102 || (itemcnt > mincount && items[mincount][0] == '#')));
6103}
6104
6105/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006106 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6107 * ae_flags to ae_comppermit and ae_compforbid.
6108 */
6109 static void
6110aff_process_flags(affile, entry)
6111 afffile_T *affile;
6112 affentry_T *entry;
6113{
6114 char_u *p;
6115 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006116 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006117
6118 if (entry->ae_flags != NULL
6119 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6120 {
6121 for (p = entry->ae_flags; *p != NUL; )
6122 {
6123 prevp = p;
6124 flag = get_affitem(affile->af_flagtype, &p);
6125 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6126 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006127 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006128 p = prevp;
6129 if (flag == affile->af_comppermit)
6130 entry->ae_comppermit = TRUE;
6131 else
6132 entry->ae_compforbid = TRUE;
6133 }
6134 if (affile->af_flagtype == AFT_NUM && *p == ',')
6135 ++p;
6136 }
6137 if (*entry->ae_flags == NUL)
6138 entry->ae_flags = NULL; /* nothing left */
6139 }
6140}
6141
6142/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143 * Return TRUE if "s" is the name of an info item in the affix file.
6144 */
6145 static int
6146spell_info_item(s)
6147 char_u *s;
6148{
6149 return STRCMP(s, "NAME") == 0
6150 || STRCMP(s, "HOME") == 0
6151 || STRCMP(s, "VERSION") == 0
6152 || STRCMP(s, "AUTHOR") == 0
6153 || STRCMP(s, "EMAIL") == 0
6154 || STRCMP(s, "COPYRIGHT") == 0;
6155}
6156
6157/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006158 * Turn an affix flag name into a number, according to the FLAG type.
6159 * returns zero for failure.
6160 */
6161 static unsigned
6162affitem2flag(flagtype, item, fname, lnum)
6163 int flagtype;
6164 char_u *item;
6165 char_u *fname;
6166 int lnum;
6167{
6168 unsigned res;
6169 char_u *p = item;
6170
6171 res = get_affitem(flagtype, &p);
6172 if (res == 0)
6173 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006174 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006175 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6176 fname, lnum, item);
6177 else
6178 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6179 fname, lnum, item);
6180 }
6181 if (*p != NUL)
6182 {
6183 smsg((char_u *)_(e_affname), fname, lnum, item);
6184 return 0;
6185 }
6186
6187 return res;
6188}
6189
6190/*
6191 * Get one affix name from "*pp" and advance the pointer.
6192 * Returns zero for an error, still advances the pointer then.
6193 */
6194 static unsigned
6195get_affitem(flagtype, pp)
6196 int flagtype;
6197 char_u **pp;
6198{
6199 int res;
6200
Bram Moolenaar95529562005-08-25 21:21:38 +00006201 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006202 {
6203 if (!VIM_ISDIGIT(**pp))
6204 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006205 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006206 return 0;
6207 }
6208 res = getdigits(pp);
6209 }
6210 else
6211 {
6212#ifdef FEAT_MBYTE
6213 res = mb_ptr2char_adv(pp);
6214#else
6215 res = *(*pp)++;
6216#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006217 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006218 && res >= 'A' && res <= 'Z'))
6219 {
6220 if (**pp == NUL)
6221 return 0;
6222#ifdef FEAT_MBYTE
6223 res = mb_ptr2char_adv(pp) + (res << 16);
6224#else
6225 res = *(*pp)++ + (res << 16);
6226#endif
6227 }
6228 }
6229 return res;
6230}
6231
6232/*
6233 * Process the "compflags" string used in an affix file and append it to
6234 * spin->si_compflags.
6235 * The processing involves changing the affix names to ID numbers, so that
6236 * they fit in one byte.
6237 */
6238 static void
6239process_compflags(spin, aff, compflags)
6240 spellinfo_T *spin;
6241 afffile_T *aff;
6242 char_u *compflags;
6243{
6244 char_u *p;
6245 char_u *prevp;
6246 unsigned flag;
6247 compitem_T *ci;
6248 int id;
6249 int len;
6250 char_u *tp;
6251 char_u key[AH_KEY_LEN];
6252 hashitem_T *hi;
6253
6254 /* Make room for the old and the new compflags, concatenated with a / in
6255 * between. Processing it makes it shorter, but we don't know by how
6256 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006257 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006258 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006259 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006260 p = getroom(spin, len, FALSE);
6261 if (p == NULL)
6262 return;
6263 if (spin->si_compflags != NULL)
6264 {
6265 STRCPY(p, spin->si_compflags);
6266 STRCAT(p, "/");
6267 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006268 spin->si_compflags = p;
6269 tp = p + STRLEN(p);
6270
6271 for (p = compflags; *p != NUL; )
6272 {
6273 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6274 /* Copy non-flag characters directly. */
6275 *tp++ = *p++;
6276 else
6277 {
6278 /* First get the flag number, also checks validity. */
6279 prevp = p;
6280 flag = get_affitem(aff->af_flagtype, &p);
6281 if (flag != 0)
6282 {
6283 /* Find the flag in the hashtable. If it was used before, use
6284 * the existing ID. Otherwise add a new entry. */
6285 vim_strncpy(key, prevp, p - prevp);
6286 hi = hash_find(&aff->af_comp, key);
6287 if (!HASHITEM_EMPTY(hi))
6288 id = HI2CI(hi)->ci_newID;
6289 else
6290 {
6291 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6292 if (ci == NULL)
6293 break;
6294 STRCPY(ci->ci_key, key);
6295 ci->ci_flag = flag;
6296 /* Avoid using a flag ID that has a special meaning in a
6297 * regexp (also inside []). */
6298 do
6299 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006300 check_renumber(spin);
6301 id = spin->si_newcompID--;
6302 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006303 ci->ci_newID = id;
6304 hash_add(&aff->af_comp, ci->ci_key);
6305 }
6306 *tp++ = id;
6307 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006308 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006309 ++p;
6310 }
6311 }
6312
6313 *tp = NUL;
6314}
6315
6316/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006317 * Check that the new IDs for postponed affixes and compounding don't overrun
6318 * each other. We have almost 255 available, but start at 0-127 to avoid
6319 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6320 * When that is used up an error message is given.
6321 */
6322 static void
6323check_renumber(spin)
6324 spellinfo_T *spin;
6325{
6326 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6327 {
6328 spin->si_newprefID = 127;
6329 spin->si_newcompID = 255;
6330 }
6331}
6332
6333/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006334 * Return TRUE if flag "flag" appears in affix list "afflist".
6335 */
6336 static int
6337flag_in_afflist(flagtype, afflist, flag)
6338 int flagtype;
6339 char_u *afflist;
6340 unsigned flag;
6341{
6342 char_u *p;
6343 unsigned n;
6344
6345 switch (flagtype)
6346 {
6347 case AFT_CHAR:
6348 return vim_strchr(afflist, flag) != NULL;
6349
Bram Moolenaar95529562005-08-25 21:21:38 +00006350 case AFT_CAPLONG:
6351 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006352 for (p = afflist; *p != NUL; )
6353 {
6354#ifdef FEAT_MBYTE
6355 n = mb_ptr2char_adv(&p);
6356#else
6357 n = *p++;
6358#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006359 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006360 && *p != NUL)
6361#ifdef FEAT_MBYTE
6362 n = mb_ptr2char_adv(&p) + (n << 16);
6363#else
6364 n = *p++ + (n << 16);
6365#endif
6366 if (n == flag)
6367 return TRUE;
6368 }
6369 break;
6370
Bram Moolenaar95529562005-08-25 21:21:38 +00006371 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006372 for (p = afflist; *p != NUL; )
6373 {
6374 n = getdigits(&p);
6375 if (n == flag)
6376 return TRUE;
6377 if (*p != NUL) /* skip over comma */
6378 ++p;
6379 }
6380 break;
6381 }
6382 return FALSE;
6383}
6384
6385/*
6386 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6387 */
6388 static void
6389aff_check_number(spinval, affval, name)
6390 int spinval;
6391 int affval;
6392 char *name;
6393{
6394 if (spinval != 0 && spinval != affval)
6395 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6396}
6397
6398/*
6399 * Give a warning when "spinval" and "affval" strings are set and not the same.
6400 */
6401 static void
6402aff_check_string(spinval, affval, name)
6403 char_u *spinval;
6404 char_u *affval;
6405 char *name;
6406{
6407 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6408 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6409}
6410
6411/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006412 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6413 * NULL as equal.
6414 */
6415 static int
6416str_equal(s1, s2)
6417 char_u *s1;
6418 char_u *s2;
6419{
6420 if (s1 == NULL || s2 == NULL)
6421 return s1 == s2;
6422 return STRCMP(s1, s2) == 0;
6423}
6424
6425/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006426 * Add a from-to item to "gap". Used for REP and SAL items.
6427 * They are stored case-folded.
6428 */
6429 static void
6430add_fromto(spin, gap, from, to)
6431 spellinfo_T *spin;
6432 garray_T *gap;
6433 char_u *from;
6434 char_u *to;
6435{
6436 fromto_T *ftp;
6437 char_u word[MAXWLEN];
6438
6439 if (ga_grow(gap, 1) == OK)
6440 {
6441 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006442 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006443 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006444 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006445 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006446 ++gap->ga_len;
6447 }
6448}
6449
6450/*
6451 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6452 */
6453 static int
6454sal_to_bool(s)
6455 char_u *s;
6456{
6457 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6458}
6459
6460/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006461 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6462 * When "s" is NULL FALSE is returned.
6463 */
6464 static int
6465has_non_ascii(s)
6466 char_u *s;
6467{
6468 char_u *p;
6469
6470 if (s != NULL)
6471 for (p = s; *p != NUL; ++p)
6472 if (*p >= 128)
6473 return TRUE;
6474 return FALSE;
6475}
6476
6477/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006478 * Free the structure filled by spell_read_aff().
6479 */
6480 static void
6481spell_free_aff(aff)
6482 afffile_T *aff;
6483{
6484 hashtab_T *ht;
6485 hashitem_T *hi;
6486 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006487 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006488 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006489
6490 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006491
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006492 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006493 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6494 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006495 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006496 for (hi = ht->ht_array; todo > 0; ++hi)
6497 {
6498 if (!HASHITEM_EMPTY(hi))
6499 {
6500 --todo;
6501 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6503 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006504 }
6505 }
6506 if (ht == &aff->af_suff)
6507 break;
6508 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006509
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006510 hash_clear(&aff->af_pref);
6511 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006512 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006513}
6514
6515/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518 */
6519 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006520spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006521 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006522 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006523 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006524{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006525 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006526 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006527 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006528 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006529 char_u store_afflist[MAXWLEN];
6530 int pfxlen;
6531 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006532 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006533 char_u *pc;
6534 char_u *w;
6535 int l;
6536 hash_T hash;
6537 hashitem_T *hi;
6538 FILE *fd;
6539 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006540 int non_ascii = 0;
6541 int retval = OK;
6542 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006543 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006544 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006545
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 /*
6547 * Open the file.
6548 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006549 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006550 if (fd == NULL)
6551 {
6552 EMSG2(_(e_notopen), fname);
6553 return FAIL;
6554 }
6555
Bram Moolenaar51485f02005-06-04 21:55:20 +00006556 /* The hashtable is only used to detect duplicated words. */
6557 hash_init(&ht);
6558
Bram Moolenaar4770d092006-01-12 23:22:24 +00006559 vim_snprintf((char *)IObuff, IOSIZE,
6560 _("Reading dictionary file %s ..."), fname);
6561 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006563 /* start with a message for the first line */
6564 spin->si_msg_count = 999999;
6565
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006566 /* Read and ignore the first line: word count. */
6567 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006568 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006569 EMSG2(_("E760: No word count in %s"), fname);
6570
6571 /*
6572 * Read all the lines in the file one by one.
6573 * The words are converted to 'encoding' here, before being added to
6574 * the hashtable.
6575 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006576 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006577 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006578 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006579 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006580 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006581 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006582
Bram Moolenaar51485f02005-06-04 21:55:20 +00006583 /* Remove CR, LF and white space from the end. White space halfway
6584 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006585 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006586 while (l > 0 && line[l - 1] <= ' ')
6587 --l;
6588 if (l == 0)
6589 continue; /* empty line */
6590 line[l] = NUL;
6591
Bram Moolenaarb765d632005-06-07 21:00:02 +00006592#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006593 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006594 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006595 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006596 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006597 if (pc == NULL)
6598 {
6599 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6600 fname, lnum, line);
6601 continue;
6602 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006603 w = pc;
6604 }
6605 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006606#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006607 {
6608 pc = NULL;
6609 w = line;
6610 }
6611
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006612 /* Truncate the word at the "/", set "afflist" to what follows.
6613 * Replace "\/" by "/" and "\\" by "\". */
6614 afflist = NULL;
6615 for (p = w; *p != NUL; mb_ptr_adv(p))
6616 {
6617 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006618 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006619 else if (*p == '/')
6620 {
6621 *p = NUL;
6622 afflist = p + 1;
6623 break;
6624 }
6625 }
6626
6627 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6628 if (spin->si_ascii && has_non_ascii(w))
6629 {
6630 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006631 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006632 continue;
6633 }
6634
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006635 /* This takes time, print a message every 10000 words. */
6636 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006637 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006638 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006639 vim_snprintf((char *)message, sizeof(message),
6640 _("line %6d, word %6d - %s"),
6641 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6642 msg_start();
6643 msg_puts_long_attr(message, 0);
6644 msg_clr_eos();
6645 msg_didout = FALSE;
6646 msg_col = 0;
6647 out_flush();
6648 }
6649
Bram Moolenaar51485f02005-06-04 21:55:20 +00006650 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006651 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006652 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006653 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006654 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006655 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006656 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006658
Bram Moolenaar51485f02005-06-04 21:55:20 +00006659 hash = hash_hash(dw);
6660 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006662 {
6663 if (p_verbose > 0)
6664 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006665 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006666 else if (duplicate == 0)
6667 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6668 fname, lnum, dw);
6669 ++duplicate;
6670 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006671 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006672 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006673
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006674 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006675 store_afflist[0] = NUL;
6676 pfxlen = 0;
6677 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006678 if (afflist != NULL)
6679 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006680 /* Extract flags from the affix list. */
6681 flags |= get_affix_flags(affile, afflist);
6682
Bram Moolenaar6de68532005-08-24 22:08:48 +00006683 if (affile->af_needaffix != 0 && flag_in_afflist(
6684 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006685 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006686
6687 if (affile->af_pfxpostpone)
6688 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006689 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006690
Bram Moolenaar5195e452005-08-19 20:32:47 +00006691 if (spin->si_compflags != NULL)
6692 /* Need to store the list of compound flags with the word.
6693 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006694 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006695 }
6696
Bram Moolenaar51485f02005-06-04 21:55:20 +00006697 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006698 if (store_word(spin, dw, flags, spin->si_region,
6699 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006700 retval = FAIL;
6701
6702 if (afflist != NULL)
6703 {
6704 /* Find all matching suffixes and add the resulting words.
6705 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006706 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006707 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006708 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006709 retval = FAIL;
6710
6711 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006712 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006713 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006714 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 retval = FAIL;
6716 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006717
6718 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006719 }
6720
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006721 if (duplicate > 0)
6722 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006723 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006724 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6725 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006726 hash_clear(&ht);
6727
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006728 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006729 return retval;
6730}
6731
6732/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006733 * Check for affix flags in "afflist" that are turned into word flags.
6734 * Return WF_ flags.
6735 */
6736 static int
6737get_affix_flags(affile, afflist)
6738 afffile_T *affile;
6739 char_u *afflist;
6740{
6741 int flags = 0;
6742
6743 if (affile->af_keepcase != 0 && flag_in_afflist(
6744 affile->af_flagtype, afflist, affile->af_keepcase))
6745 flags |= WF_KEEPCAP | WF_FIXCAP;
6746 if (affile->af_rare != 0 && flag_in_afflist(
6747 affile->af_flagtype, afflist, affile->af_rare))
6748 flags |= WF_RARE;
6749 if (affile->af_bad != 0 && flag_in_afflist(
6750 affile->af_flagtype, afflist, affile->af_bad))
6751 flags |= WF_BANNED;
6752 if (affile->af_needcomp != 0 && flag_in_afflist(
6753 affile->af_flagtype, afflist, affile->af_needcomp))
6754 flags |= WF_NEEDCOMP;
6755 if (affile->af_comproot != 0 && flag_in_afflist(
6756 affile->af_flagtype, afflist, affile->af_comproot))
6757 flags |= WF_COMPROOT;
6758 if (affile->af_nosuggest != 0 && flag_in_afflist(
6759 affile->af_flagtype, afflist, affile->af_nosuggest))
6760 flags |= WF_NOSUGGEST;
6761 return flags;
6762}
6763
6764/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006765 * Get the list of prefix IDs from the affix list "afflist".
6766 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006767 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6768 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006769 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006770 static int
6771get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006772 afffile_T *affile;
6773 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006774 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006775{
6776 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006777 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006778 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006779 int id;
6780 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006781 hashitem_T *hi;
6782
Bram Moolenaar6de68532005-08-24 22:08:48 +00006783 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006784 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006785 prevp = p;
6786 if (get_affitem(affile->af_flagtype, &p) != 0)
6787 {
6788 /* A flag is a postponed prefix flag if it appears in "af_pref"
6789 * and it's ID is not zero. */
6790 vim_strncpy(key, prevp, p - prevp);
6791 hi = hash_find(&affile->af_pref, key);
6792 if (!HASHITEM_EMPTY(hi))
6793 {
6794 id = HI2AH(hi)->ah_newID;
6795 if (id != 0)
6796 store_afflist[cnt++] = id;
6797 }
6798 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006799 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006800 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006801 }
6802
Bram Moolenaar5195e452005-08-19 20:32:47 +00006803 store_afflist[cnt] = NUL;
6804 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006805}
6806
6807/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006808 * Get the list of compound IDs from the affix list "afflist" that are used
6809 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006810 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006811 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006812 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006813get_compflags(affile, afflist, store_afflist)
6814 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006815 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006816 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006817{
6818 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006819 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006820 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006821 char_u key[AH_KEY_LEN];
6822 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006823
Bram Moolenaar6de68532005-08-24 22:08:48 +00006824 for (p = afflist; *p != NUL; )
6825 {
6826 prevp = p;
6827 if (get_affitem(affile->af_flagtype, &p) != 0)
6828 {
6829 /* A flag is a compound flag if it appears in "af_comp". */
6830 vim_strncpy(key, prevp, p - prevp);
6831 hi = hash_find(&affile->af_comp, key);
6832 if (!HASHITEM_EMPTY(hi))
6833 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6834 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006835 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006836 ++p;
6837 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006838
Bram Moolenaar5195e452005-08-19 20:32:47 +00006839 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006840}
6841
6842/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006843 * Apply affixes to a word and store the resulting words.
6844 * "ht" is the hashtable with affentry_T that need to be applied, either
6845 * prefixes or suffixes.
6846 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6847 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006848 *
6849 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006850 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006851 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006852store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006853 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006854 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006855 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006856 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006857 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006858 hashtab_T *ht;
6859 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006860 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006861 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006862 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006863 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6864 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006865{
6866 int todo;
6867 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006868 affheader_T *ah;
6869 affentry_T *ae;
6870 regmatch_T regmatch;
6871 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006872 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006873 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006874 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006875 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006876 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006877 int use_pfxlen;
6878 int need_affix;
6879 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006880 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006881 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006882 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006884 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006885 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006886 {
6887 if (!HASHITEM_EMPTY(hi))
6888 {
6889 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006890 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006891
Bram Moolenaar51485f02005-06-04 21:55:20 +00006892 /* Check that the affix combines, if required, and that the word
6893 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006894 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6895 && flag_in_afflist(affile->af_flagtype, afflist,
6896 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006897 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006898 /* Loop over all affix entries with this name. */
6899 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006900 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006901 /* Check the condition. It's not logical to match case
6902 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006903 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006904 * Another requirement from Myspell is that the chop
6905 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006906 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006907 * prefixes with a chop string and/or flags.
6908 * When a previously added affix had CIRCUMFIX this one
6909 * must have it too, if it had not then this one must not
6910 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006911 regmatch.regprog = ae->ae_prog;
6912 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006913 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006914 || ae->ae_chop != NULL
6915 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006916 && (ae->ae_chop == NULL
6917 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006918 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006919 || vim_regexec(&regmatch, word, (colnr_T)0))
6920 && (((condit & CONDIT_CFIX) == 0)
6921 == ((condit & CONDIT_AFF) == 0
6922 || ae->ae_flags == NULL
6923 || !flag_in_afflist(affile->af_flagtype,
6924 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006925 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006926 /* Match. Remove the chop and add the affix. */
6927 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006928 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006929 /* prefix: chop/add at the start of the word */
6930 if (ae->ae_add == NULL)
6931 *newword = NUL;
6932 else
6933 STRCPY(newword, ae->ae_add);
6934 p = word;
6935 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006936 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006937 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006938#ifdef FEAT_MBYTE
6939 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006940 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006941 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006942 for ( ; i > 0; --i)
6943 mb_ptr_adv(p);
6944 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006945 else
6946#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006947 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006948 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006949 STRCAT(newword, p);
6950 }
6951 else
6952 {
6953 /* suffix: chop/add at the end of the word */
6954 STRCPY(newword, word);
6955 if (ae->ae_chop != NULL)
6956 {
6957 /* Remove chop string. */
6958 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006959 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006960 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006961 mb_ptr_back(newword, p);
6962 *p = NUL;
6963 }
6964 if (ae->ae_add != NULL)
6965 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006966 }
6967
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006968 use_flags = flags;
6969 use_pfxlist = pfxlist;
6970 use_pfxlen = pfxlen;
6971 need_affix = FALSE;
6972 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6973 if (ae->ae_flags != NULL)
6974 {
6975 /* Extract flags from the affix list. */
6976 use_flags |= get_affix_flags(affile, ae->ae_flags);
6977
6978 if (affile->af_needaffix != 0 && flag_in_afflist(
6979 affile->af_flagtype, ae->ae_flags,
6980 affile->af_needaffix))
6981 need_affix = TRUE;
6982
6983 /* When there is a CIRCUMFIX flag the other affix
6984 * must also have it and we don't add the word
6985 * with one affix. */
6986 if (affile->af_circumfix != 0 && flag_in_afflist(
6987 affile->af_flagtype, ae->ae_flags,
6988 affile->af_circumfix))
6989 {
6990 use_condit |= CONDIT_CFIX;
6991 if ((condit & CONDIT_CFIX) == 0)
6992 need_affix = TRUE;
6993 }
6994
6995 if (affile->af_pfxpostpone
6996 || spin->si_compflags != NULL)
6997 {
6998 if (affile->af_pfxpostpone)
6999 /* Get prefix IDS from the affix list. */
7000 use_pfxlen = get_pfxlist(affile,
7001 ae->ae_flags, store_afflist);
7002 else
7003 use_pfxlen = 0;
7004 use_pfxlist = store_afflist;
7005
7006 /* Combine the prefix IDs. Avoid adding the
7007 * same ID twice. */
7008 for (i = 0; i < pfxlen; ++i)
7009 {
7010 for (j = 0; j < use_pfxlen; ++j)
7011 if (pfxlist[i] == use_pfxlist[j])
7012 break;
7013 if (j == use_pfxlen)
7014 use_pfxlist[use_pfxlen++] = pfxlist[i];
7015 }
7016
7017 if (spin->si_compflags != NULL)
7018 /* Get compound IDS from the affix list. */
7019 get_compflags(affile, ae->ae_flags,
7020 use_pfxlist + use_pfxlen);
7021
7022 /* Combine the list of compound flags.
7023 * Concatenate them to the prefix IDs list.
7024 * Avoid adding the same ID twice. */
7025 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7026 {
7027 for (j = use_pfxlen;
7028 use_pfxlist[j] != NUL; ++j)
7029 if (pfxlist[i] == use_pfxlist[j])
7030 break;
7031 if (use_pfxlist[j] == NUL)
7032 {
7033 use_pfxlist[j++] = pfxlist[i];
7034 use_pfxlist[j] = NUL;
7035 }
7036 }
7037 }
7038 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007039
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007040 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007041 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007042 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007043 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007044 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007045 use_pfxlist = pfx_pfxlist;
7046 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007047
7048 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007049 if (spin->si_prefroot != NULL
7050 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007051 {
7052 /* ... add a flag to indicate an affix was used. */
7053 use_flags |= WF_HAS_AFF;
7054
7055 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007056 * affixes is not allowed. But do use the
7057 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007058 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007059 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007060 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007061
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007062 /* When compounding is supported and there is no
7063 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7064 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007065 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007066 {
7067 if (xht != NULL)
7068 use_flags |= WF_NOCOMPAFT;
7069 else
7070 use_flags |= WF_NOCOMPBEF;
7071 }
7072
Bram Moolenaar51485f02005-06-04 21:55:20 +00007073 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007074 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007075 spin->si_region, use_pfxlist,
7076 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007077 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007078
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007079 /* When added a prefix or a first suffix and the affix
7080 * has flags may add a(nother) suffix. RECURSIVE! */
7081 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7082 if (store_aff_word(spin, newword, ae->ae_flags,
7083 affile, &affile->af_suff, xht,
7084 use_condit & (xht == NULL
7085 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007086 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007088
7089 /* When added a suffix and combining is allowed also
7090 * try adding a prefix additionally. Both for the
7091 * word flags and for the affix flags. RECURSIVE! */
7092 if (xht != NULL && ah->ah_combine)
7093 {
7094 if (store_aff_word(spin, newword,
7095 afflist, affile,
7096 xht, NULL, use_condit,
7097 use_flags, use_pfxlist,
7098 pfxlen) == FAIL
7099 || (ae->ae_flags != NULL
7100 && store_aff_word(spin, newword,
7101 ae->ae_flags, affile,
7102 xht, NULL, use_condit,
7103 use_flags, use_pfxlist,
7104 pfxlen) == FAIL))
7105 retval = FAIL;
7106 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007107 }
7108 }
7109 }
7110 }
7111 }
7112
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007113 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007114}
7115
7116/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007117 * Read a file with a list of words.
7118 */
7119 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007120spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007121 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007122 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007123{
7124 FILE *fd;
7125 long lnum = 0;
7126 char_u rline[MAXLINELEN];
7127 char_u *line;
7128 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007129 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007130 int l;
7131 int retval = OK;
7132 int did_word = FALSE;
7133 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007134 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007135 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007136
7137 /*
7138 * Open the file.
7139 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007140 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007141 if (fd == NULL)
7142 {
7143 EMSG2(_(e_notopen), fname);
7144 return FAIL;
7145 }
7146
Bram Moolenaar4770d092006-01-12 23:22:24 +00007147 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7148 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007149
7150 /*
7151 * Read all the lines in the file one by one.
7152 */
7153 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7154 {
7155 line_breakcheck();
7156 ++lnum;
7157
7158 /* Skip comment lines. */
7159 if (*rline == '#')
7160 continue;
7161
7162 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007163 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007164 while (l > 0 && rline[l - 1] <= ' ')
7165 --l;
7166 if (l == 0)
7167 continue; /* empty or blank line */
7168 rline[l] = NUL;
7169
Bram Moolenaar9c102382006-05-03 21:26:49 +00007170 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007171 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007172#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007173 if (spin->si_conv.vc_type != CONV_NONE)
7174 {
7175 pc = string_convert(&spin->si_conv, rline, NULL);
7176 if (pc == NULL)
7177 {
7178 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7179 fname, lnum, rline);
7180 continue;
7181 }
7182 line = pc;
7183 }
7184 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007185#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007186 {
7187 pc = NULL;
7188 line = rline;
7189 }
7190
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007191 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007192 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007193 ++line;
7194 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007195 {
7196 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007197 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7198 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007200 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7201 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202 else
7203 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007204#ifdef FEAT_MBYTE
7205 char_u *enc;
7206
Bram Moolenaar51485f02005-06-04 21:55:20 +00007207 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007208 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007209 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007210 if (enc != NULL && !spin->si_ascii
7211 && convert_setup(&spin->si_conv, enc,
7212 p_enc) == FAIL)
7213 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007214 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007215 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007216 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007217#else
7218 smsg((char_u *)_("Conversion in %s not supported"), fname);
7219#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007221 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007222 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007223
Bram Moolenaar3982c542005-06-08 21:56:31 +00007224 if (STRNCMP(line, "regions=", 8) == 0)
7225 {
7226 if (spin->si_region_count > 1)
7227 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7228 fname, lnum, line);
7229 else
7230 {
7231 line += 8;
7232 if (STRLEN(line) > 16)
7233 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7234 fname, lnum, line);
7235 else
7236 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007237 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007238 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007239
7240 /* Adjust the mask for a word valid in all regions. */
7241 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007242 }
7243 }
7244 continue;
7245 }
7246
Bram Moolenaar7887d882005-07-01 22:33:52 +00007247 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7248 fname, lnum, line - 1);
7249 continue;
7250 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007251
Bram Moolenaar7887d882005-07-01 22:33:52 +00007252 flags = 0;
7253 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007254
Bram Moolenaar7887d882005-07-01 22:33:52 +00007255 /* Check for flags and region after a slash. */
7256 p = vim_strchr(line, '/');
7257 if (p != NULL)
7258 {
7259 *p++ = NUL;
7260 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007261 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007262 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007263 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007264 else if (*p == '!') /* Bad, bad, wicked word. */
7265 flags |= WF_BANNED;
7266 else if (*p == '?') /* Rare word. */
7267 flags |= WF_RARE;
7268 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007269 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007270 if ((flags & WF_REGION) == 0) /* first one */
7271 regionmask = 0;
7272 flags |= WF_REGION;
7273
7274 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007275 if (l > spin->si_region_count)
7276 {
7277 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007278 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007279 break;
7280 }
7281 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007282 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007283 else
7284 {
7285 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7286 fname, lnum, p);
7287 break;
7288 }
7289 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007290 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007291 }
7292
7293 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7294 if (spin->si_ascii && has_non_ascii(line))
7295 {
7296 ++non_ascii;
7297 continue;
7298 }
7299
7300 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007301 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007302 {
7303 retval = FAIL;
7304 break;
7305 }
7306 did_word = TRUE;
7307 }
7308
7309 vim_free(pc);
7310 fclose(fd);
7311
Bram Moolenaar4770d092006-01-12 23:22:24 +00007312 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007313 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007314 vim_snprintf((char *)IObuff, IOSIZE,
7315 _("Ignored %d words with non-ASCII characters"), non_ascii);
7316 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007317 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007318
Bram Moolenaar51485f02005-06-04 21:55:20 +00007319 return retval;
7320}
7321
7322/*
7323 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007324 * This avoids calling free() for every little struct we use (and keeping
7325 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007326 * The memory is cleared to all zeros.
7327 * Returns NULL when out of memory.
7328 */
7329 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007330getroom(spin, len, align)
7331 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007332 size_t len; /* length needed */
7333 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007334{
7335 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007336 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007337
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007338 if (align && bl != NULL)
7339 /* Round size up for alignment. On some systems structures need to be
7340 * aligned to the size of a pointer (e.g., SPARC). */
7341 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7342 & ~(sizeof(char *) - 1);
7343
Bram Moolenaar51485f02005-06-04 21:55:20 +00007344 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7345 {
7346 /* Allocate a block of memory. This is not freed until much later. */
7347 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7348 if (bl == NULL)
7349 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007350 bl->sb_next = spin->si_blocks;
7351 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007352 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007353 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007354 }
7355
7356 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007357 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007358
7359 return p;
7360}
7361
7362/*
7363 * Make a copy of a string into memory allocated with getroom().
7364 */
7365 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007366getroom_save(spin, s)
7367 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368 char_u *s;
7369{
7370 char_u *sc;
7371
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007372 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007373 if (sc != NULL)
7374 STRCPY(sc, s);
7375 return sc;
7376}
7377
7378
7379/*
7380 * Free the list of allocated sblock_T.
7381 */
7382 static void
7383free_blocks(bl)
7384 sblock_T *bl;
7385{
7386 sblock_T *next;
7387
7388 while (bl != NULL)
7389 {
7390 next = bl->sb_next;
7391 vim_free(bl);
7392 bl = next;
7393 }
7394}
7395
7396/*
7397 * Allocate the root of a word tree.
7398 */
7399 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007400wordtree_alloc(spin)
7401 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007402{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007403 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007404}
7405
7406/*
7407 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007408 * Always store it in the case-folded tree. For a keep-case word this is
7409 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7410 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007411 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007412 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7413 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007414 */
7415 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007416store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007417 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007418 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007419 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007420 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007421 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007422 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007423{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007424 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007425 int ct = captype(word, word + len);
7426 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007427 int res = OK;
7428 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007431 for (p = pfxlist; res == OK; ++p)
7432 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007433 if (!need_affix || (p != NULL && *p != NUL))
7434 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007435 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007436 if (p == NULL || *p == NUL)
7437 break;
7438 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007439 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007440
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007441 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007442 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007443 for (p = pfxlist; res == OK; ++p)
7444 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007445 if (!need_affix || (p != NULL && *p != NUL))
7446 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007447 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007448 if (p == NULL || *p == NUL)
7449 break;
7450 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007451 ++spin->si_keepwcount;
7452 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007453 return res;
7454}
7455
7456/*
7457 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007458 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007459 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007460 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007461 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007462 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007463tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007464 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007465 char_u *word;
7466 wordnode_T *root;
7467 int flags;
7468 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007469 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007470{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007471 wordnode_T *node = root;
7472 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007473 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007474 wordnode_T **prev = NULL;
7475 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007476
Bram Moolenaar51485f02005-06-04 21:55:20 +00007477 /* Add each byte of the word to the tree, including the NUL at the end. */
7478 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007479 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007480 /* When there is more than one reference to this node we need to make
7481 * a copy, so that we can modify it. Copy the whole list of siblings
7482 * (we don't optimize for a partly shared list of siblings). */
7483 if (node != NULL && node->wn_refs > 1)
7484 {
7485 --node->wn_refs;
7486 copyprev = prev;
7487 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7488 {
7489 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007490 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007491 if (np == NULL)
7492 return FAIL;
7493 np->wn_child = copyp->wn_child;
7494 if (np->wn_child != NULL)
7495 ++np->wn_child->wn_refs; /* child gets extra ref */
7496 np->wn_byte = copyp->wn_byte;
7497 if (np->wn_byte == NUL)
7498 {
7499 np->wn_flags = copyp->wn_flags;
7500 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007501 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007502 }
7503
7504 /* Link the new node in the list, there will be one ref. */
7505 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007506 if (copyprev != NULL)
7507 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007508 copyprev = &np->wn_sibling;
7509
7510 /* Let "node" point to the head of the copied list. */
7511 if (copyp == node)
7512 node = np;
7513 }
7514 }
7515
Bram Moolenaar51485f02005-06-04 21:55:20 +00007516 /* Look for the sibling that has the same character. They are sorted
7517 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007518 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007519 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007520 while (node != NULL
7521 && (node->wn_byte < word[i]
7522 || (node->wn_byte == NUL
7523 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007524 ? node->wn_affixID < (unsigned)affixID
7525 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007526 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007527 && (spin->si_sugtree
7528 ? (node->wn_region & 0xffff) < region
7529 : node->wn_affixID
7530 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007531 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007532 prev = &node->wn_sibling;
7533 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007534 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007535 if (node == NULL
7536 || node->wn_byte != word[i]
7537 || (word[i] == NUL
7538 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007539 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007540 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007541 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007542 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007543 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007544 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007545 if (np == NULL)
7546 return FAIL;
7547 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007548
7549 /* If "node" is NULL this is a new child or the end of the sibling
7550 * list: ref count is one. Otherwise use ref count of sibling and
7551 * make ref count of sibling one (matters when inserting in front
7552 * of the list of siblings). */
7553 if (node == NULL)
7554 np->wn_refs = 1;
7555 else
7556 {
7557 np->wn_refs = node->wn_refs;
7558 node->wn_refs = 1;
7559 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007560 if (prev != NULL)
7561 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007562 np->wn_sibling = node;
7563 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007564 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007565
Bram Moolenaar51485f02005-06-04 21:55:20 +00007566 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007567 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007568 node->wn_flags = flags;
7569 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007570 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007571 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007572 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007573 prev = &node->wn_child;
7574 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007575 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007576#ifdef SPELL_PRINTTREE
7577 smsg("Added \"%s\"", word);
7578 spell_print_tree(root->wn_sibling);
7579#endif
7580
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007581 /* count nr of words added since last message */
7582 ++spin->si_msg_count;
7583
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007584 if (spin->si_compress_cnt > 1)
7585 {
7586 if (--spin->si_compress_cnt == 1)
7587 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007588 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007589 }
7590
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007591 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007592 * When we have allocated lots of memory we need to compress the word tree
7593 * to free up some room. But compression is slow, and we might actually
7594 * need that room, thus only compress in the following situations:
7595 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007596 * "compress_start" blocks.
7597 * 2. When compressed before and used "compress_inc" blocks before
7598 * adding "compress_added" words (si_compress_cnt > 1).
7599 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007600 * (si_compress_cnt == 1) and the number of free nodes drops below the
7601 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007602 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007603#ifndef SPELL_PRINTTREE
7604 if (spin->si_compress_cnt == 1
7605 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007606 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007607#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007608 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007609 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007610 * when the freed up room has been used and another "compress_inc"
7611 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007612 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007613 spin->si_blocks_cnt -= compress_inc;
7614 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007615
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007616 if (spin->si_verbose)
7617 {
7618 msg_start();
7619 msg_puts((char_u *)_(msg_compressing));
7620 msg_clr_eos();
7621 msg_didout = FALSE;
7622 msg_col = 0;
7623 out_flush();
7624 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007625
7626 /* Compress both trees. Either they both have many nodes, which makes
7627 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007628 * compression goes fast. But when filling the souldfold word tree
7629 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007630 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007631 if (affixID >= 0)
7632 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007633 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007634
7635 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007636}
7637
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007638/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007639 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7640 * Sets "sps_flags".
7641 */
7642 int
7643spell_check_msm()
7644{
7645 char_u *p = p_msm;
7646 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007647 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007648 long added = 0;
7649
7650 if (!VIM_ISDIGIT(*p))
7651 return FAIL;
7652 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7653 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7654 if (*p != ',')
7655 return FAIL;
7656 ++p;
7657 if (!VIM_ISDIGIT(*p))
7658 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007659 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007660 if (*p != ',')
7661 return FAIL;
7662 ++p;
7663 if (!VIM_ISDIGIT(*p))
7664 return FAIL;
7665 added = getdigits(&p) * 1024;
7666 if (*p != NUL)
7667 return FAIL;
7668
Bram Moolenaar89d40322006-08-29 15:30:07 +00007669 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007670 return FAIL;
7671
7672 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007673 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007674 compress_added = added;
7675 return OK;
7676}
7677
7678
7679/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007680 * Get a wordnode_T, either from the list of previously freed nodes or
7681 * allocate a new one.
7682 */
7683 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007684get_wordnode(spin)
7685 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007686{
7687 wordnode_T *n;
7688
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007689 if (spin->si_first_free == NULL)
7690 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007691 else
7692 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007693 n = spin->si_first_free;
7694 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007695 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007696 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007697 }
7698#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007699 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007700#endif
7701 return n;
7702}
7703
7704/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007705 * Decrement the reference count on a node (which is the head of a list of
7706 * siblings). If the reference count becomes zero free the node and its
7707 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007708 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007709 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007710 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007711deref_wordnode(spin, node)
7712 spellinfo_T *spin;
7713 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007714{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007715 wordnode_T *np;
7716 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007717
7718 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007719 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007720 for (np = node; np != NULL; np = np->wn_sibling)
7721 {
7722 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007723 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007724 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007725 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007726 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007727 ++cnt; /* length field */
7728 }
7729 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007730}
7731
7732/*
7733 * Free a wordnode_T for re-use later.
7734 * Only the "wn_child" field becomes invalid.
7735 */
7736 static void
7737free_wordnode(spin, n)
7738 spellinfo_T *spin;
7739 wordnode_T *n;
7740{
7741 n->wn_child = spin->si_first_free;
7742 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007743 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007744}
7745
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007746/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007747 * Compress a tree: find tails that are identical and can be shared.
7748 */
7749 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007750wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007751 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007752 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007753{
7754 hashtab_T ht;
7755 int n;
7756 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007757 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007758
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007759 /* Skip the root itself, it's not actually used. The first sibling is the
7760 * start of the tree. */
7761 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007762 {
7763 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007764 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007765
7766#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007767 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007768#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007769 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007770 if (tot > 1000000)
7771 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007772 else if (tot == 0)
7773 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007774 else
7775 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007776 vim_snprintf((char *)IObuff, IOSIZE,
7777 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7778 n, tot, tot - n, perc);
7779 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007780 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007781#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007782 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007783#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007784 hash_clear(&ht);
7785 }
7786}
7787
7788/*
7789 * Compress a node, its siblings and its children, depth first.
7790 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007791 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007792 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007793node_compress(spin, node, ht, tot)
7794 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007795 wordnode_T *node;
7796 hashtab_T *ht;
7797 int *tot; /* total count of nodes before compressing,
7798 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007799{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007800 wordnode_T *np;
7801 wordnode_T *tp;
7802 wordnode_T *child;
7803 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007804 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007805 int len = 0;
7806 unsigned nr, n;
7807 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007808
Bram Moolenaar51485f02005-06-04 21:55:20 +00007809 /*
7810 * Go through the list of siblings. Compress each child and then try
7811 * finding an identical child to replace it.
7812 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007813 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007814 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007815 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007816 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007817 ++len;
7818 if ((child = np->wn_child) != NULL)
7819 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007820 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007821 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007822
7823 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007824 hash = hash_hash(child->wn_u1.hashkey);
7825 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007826 if (!HASHITEM_EMPTY(hi))
7827 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007828 /* There are children we encountered before with a hash value
7829 * identical to the current child. Now check if there is one
7830 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007831 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007832 if (node_equal(child, tp))
7833 {
7834 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007835 * current one. This means the current child and all
7836 * its siblings is unlinked from the tree. */
7837 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007838 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007840 break;
7841 }
7842 if (tp == NULL)
7843 {
7844 /* No other child with this hash value equals the child of
7845 * the node, add it to the linked list after the first
7846 * item. */
7847 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007848 child->wn_u2.next = tp->wn_u2.next;
7849 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007850 }
7851 }
7852 else
7853 /* No other child has this hash value, add it to the
7854 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007855 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007856 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007857 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007858 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007859
7860 /*
7861 * Make a hash key for the node and its siblings, so that we can quickly
7862 * find a lookalike node. This must be done after compressing the sibling
7863 * list, otherwise the hash key would become invalid by the compression.
7864 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007865 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007866 nr = 0;
7867 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007868 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007869 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007870 /* end node: use wn_flags, wn_region and wn_affixID */
7871 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007872 else
7873 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007874 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007875 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007876 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007877
7878 /* Avoid NUL bytes, it terminates the hash key. */
7879 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007880 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007881 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007882 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007883 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007884 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007885 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007886 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7887 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007888
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007889 /* Check for CTRL-C pressed now and then. */
7890 fast_breakcheck();
7891
Bram Moolenaar51485f02005-06-04 21:55:20 +00007892 return compressed;
7893}
7894
7895/*
7896 * Return TRUE when two nodes have identical siblings and children.
7897 */
7898 static int
7899node_equal(n1, n2)
7900 wordnode_T *n1;
7901 wordnode_T *n2;
7902{
7903 wordnode_T *p1;
7904 wordnode_T *p2;
7905
7906 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7907 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7908 if (p1->wn_byte != p2->wn_byte
7909 || (p1->wn_byte == NUL
7910 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007911 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007912 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007913 : (p1->wn_child != p2->wn_child)))
7914 break;
7915
7916 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007917}
7918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007919static int
7920#ifdef __BORLANDC__
7921_RTLENTRYF
7922#endif
7923rep_compare __ARGS((const void *s1, const void *s2));
7924
7925/*
7926 * Function given to qsort() to sort the REP items on "from" string.
7927 */
7928 static int
7929#ifdef __BORLANDC__
7930_RTLENTRYF
7931#endif
7932rep_compare(s1, s2)
7933 const void *s1;
7934 const void *s2;
7935{
7936 fromto_T *p1 = (fromto_T *)s1;
7937 fromto_T *p2 = (fromto_T *)s2;
7938
7939 return STRCMP(p1->ft_from, p2->ft_from);
7940}
7941
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007942/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007943 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007944 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007945 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007946 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007947write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007948 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007949 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007950{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007951 FILE *fd;
7952 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007953 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007954 wordnode_T *tree;
7955 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 int i;
7957 int l;
7958 garray_T *gap;
7959 fromto_T *ftp;
7960 char_u *p;
7961 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007962 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007963 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007964 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007965
Bram Moolenaarb765d632005-06-07 21:00:02 +00007966 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007967 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007968 {
7969 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007970 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007971 }
7972
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007974 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007975 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007976 if (fwv != (size_t)1)
7977 /* Catch first write error, don't try writing more. */
7978 goto theend;
7979
Bram Moolenaar5195e452005-08-19 20:32:47 +00007980 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007981
Bram Moolenaar5195e452005-08-19 20:32:47 +00007982 /*
7983 * <SECTIONS>: <section> ... <sectionend>
7984 */
7985
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007986 /* SN_INFO: <infotext> */
7987 if (spin->si_info != NULL)
7988 {
7989 putc(SN_INFO, fd); /* <sectionID> */
7990 putc(0, fd); /* <sectionflags> */
7991
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007992 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007993 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007994 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007995 }
7996
Bram Moolenaar5195e452005-08-19 20:32:47 +00007997 /* SN_REGION: <regionname> ...
7998 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007999 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008000 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008001 putc(SN_REGION, fd); /* <sectionID> */
8002 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8003 l = spin->si_region_count * 2;
8004 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008005 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008006 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008007 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008008 }
8009 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008010 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008011
Bram Moolenaar5195e452005-08-19 20:32:47 +00008012 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8013 *
8014 * The table with character flags and the table for case folding.
8015 * This makes sure the same characters are recognized as word characters
8016 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008017 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008018 * 'encoding'.
8019 * Also skip this for an .add.spl file, the main spell file must contain
8020 * the table (avoids that it conflicts). File is shorter too.
8021 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008022 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008023 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008024 char_u folchars[128 * 8];
8025 int flags;
8026
Bram Moolenaard12a1322005-08-21 22:08:24 +00008027 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008028 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8029
8030 /* Form the <folchars> string first, we need to know its length. */
8031 l = 0;
8032 for (i = 128; i < 256; ++i)
8033 {
8034#ifdef FEAT_MBYTE
8035 if (has_mbyte)
8036 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8037 else
8038#endif
8039 folchars[l++] = spelltab.st_fold[i];
8040 }
8041 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8042
8043 fputc(128, fd); /* <charflagslen> */
8044 for (i = 128; i < 256; ++i)
8045 {
8046 flags = 0;
8047 if (spelltab.st_isw[i])
8048 flags |= CF_WORD;
8049 if (spelltab.st_isu[i])
8050 flags |= CF_UPPER;
8051 fputc(flags, fd); /* <charflags> */
8052 }
8053
8054 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008055 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008056 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008057
Bram Moolenaar5195e452005-08-19 20:32:47 +00008058 /* SN_MIDWORD: <midword> */
8059 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008060 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008061 putc(SN_MIDWORD, fd); /* <sectionID> */
8062 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8063
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008064 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008065 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008066 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8067 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008068 }
8069
Bram Moolenaar5195e452005-08-19 20:32:47 +00008070 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8071 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008072 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008073 putc(SN_PREFCOND, fd); /* <sectionID> */
8074 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8075
8076 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8077 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8078
8079 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008080 }
8081
Bram Moolenaar5195e452005-08-19 20:32:47 +00008082 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008083 * SN_SAL: <salflags> <salcount> <sal> ...
8084 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008085
Bram Moolenaar5195e452005-08-19 20:32:47 +00008086 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008087 * round 2: SN_SAL section (unless SN_SOFO is used)
8088 * round 3: SN_REPSAL section */
8089 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008090 {
8091 if (round == 1)
8092 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008093 else if (round == 2)
8094 {
8095 /* Don't write SN_SAL when using a SN_SOFO section */
8096 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8097 continue;
8098 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008100 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008101 gap = &spin->si_repsal;
8102
8103 /* Don't write the section if there are no items. */
8104 if (gap->ga_len == 0)
8105 continue;
8106
8107 /* Sort the REP/REPSAL items. */
8108 if (round != 2)
8109 qsort(gap->ga_data, (size_t)gap->ga_len,
8110 sizeof(fromto_T), rep_compare);
8111
8112 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8113 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008114
Bram Moolenaar5195e452005-08-19 20:32:47 +00008115 /* This is for making suggestions, section is not required. */
8116 putc(0, fd); /* <sectionflags> */
8117
8118 /* Compute the length of what follows. */
8119 l = 2; /* count <repcount> or <salcount> */
8120 for (i = 0; i < gap->ga_len; ++i)
8121 {
8122 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008123 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8124 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008125 }
8126 if (round == 2)
8127 ++l; /* count <salflags> */
8128 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8129
8130 if (round == 2)
8131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008132 i = 0;
8133 if (spin->si_followup)
8134 i |= SAL_F0LLOWUP;
8135 if (spin->si_collapse)
8136 i |= SAL_COLLAPSE;
8137 if (spin->si_rem_accents)
8138 i |= SAL_REM_ACCENTS;
8139 putc(i, fd); /* <salflags> */
8140 }
8141
8142 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8143 for (i = 0; i < gap->ga_len; ++i)
8144 {
8145 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8146 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8147 ftp = &((fromto_T *)gap->ga_data)[i];
8148 for (rr = 1; rr <= 2; ++rr)
8149 {
8150 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008151 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008152 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008153 if (l > 0)
8154 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008155 }
8156 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008157
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008158 }
8159
Bram Moolenaar5195e452005-08-19 20:32:47 +00008160 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8161 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008162 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8163 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008164 putc(SN_SOFO, fd); /* <sectionID> */
8165 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008166
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008167 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008168 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8169 /* <sectionlen> */
8170
8171 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008172 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008174 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008175 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008176 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008177 }
8178
Bram Moolenaar4770d092006-01-12 23:22:24 +00008179 /* SN_WORDS: <word> ...
8180 * This is for making suggestions, section is not required. */
8181 if (spin->si_commonwords.ht_used > 0)
8182 {
8183 putc(SN_WORDS, fd); /* <sectionID> */
8184 putc(0, fd); /* <sectionflags> */
8185
8186 /* round 1: count the bytes
8187 * round 2: write the bytes */
8188 for (round = 1; round <= 2; ++round)
8189 {
8190 int todo;
8191 int len = 0;
8192 hashitem_T *hi;
8193
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008194 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008195 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8196 if (!HASHITEM_EMPTY(hi))
8197 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008198 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008199 len += l;
8200 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008201 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008202 --todo;
8203 }
8204 if (round == 1)
8205 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8206 }
8207 }
8208
Bram Moolenaar5195e452005-08-19 20:32:47 +00008209 /* SN_MAP: <mapstr>
8210 * This is for making suggestions, section is not required. */
8211 if (spin->si_map.ga_len > 0)
8212 {
8213 putc(SN_MAP, fd); /* <sectionID> */
8214 putc(0, fd); /* <sectionflags> */
8215 l = spin->si_map.ga_len;
8216 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008217 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008218 /* <mapstr> */
8219 }
8220
Bram Moolenaar4770d092006-01-12 23:22:24 +00008221 /* SN_SUGFILE: <timestamp>
8222 * This is used to notify that a .sug file may be available and at the
8223 * same time allows for checking that a .sug file that is found matches
8224 * with this .spl file. That's because the word numbers must be exactly
8225 * right. */
8226 if (!spin->si_nosugfile
8227 && (spin->si_sal.ga_len > 0
8228 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8229 {
8230 putc(SN_SUGFILE, fd); /* <sectionID> */
8231 putc(0, fd); /* <sectionflags> */
8232 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8233
8234 /* Set si_sugtime and write it to the file. */
8235 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008236 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008237 }
8238
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008239 /* SN_NOSPLITSUGS: nothing
8240 * This is used to notify that no suggestions with word splits are to be
8241 * made. */
8242 if (spin->si_nosplitsugs)
8243 {
8244 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8245 putc(0, fd); /* <sectionflags> */
8246 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8247 }
8248
Bram Moolenaar5195e452005-08-19 20:32:47 +00008249 /* SN_COMPOUND: compound info.
8250 * We don't mark it required, when not supported all compound words will
8251 * be bad words. */
8252 if (spin->si_compflags != NULL)
8253 {
8254 putc(SN_COMPOUND, fd); /* <sectionID> */
8255 putc(0, fd); /* <sectionflags> */
8256
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008257 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008258 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008259 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008260 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8261
Bram Moolenaar5195e452005-08-19 20:32:47 +00008262 putc(spin->si_compmax, fd); /* <compmax> */
8263 putc(spin->si_compminlen, fd); /* <compminlen> */
8264 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008265 putc(0, fd); /* for Vim 7.0b compatibility */
8266 putc(spin->si_compoptions, fd); /* <compoptions> */
8267 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8268 /* <comppatcount> */
8269 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8270 {
8271 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008272 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008273 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8274 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008275 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008276 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008277 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008278 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008279 }
8280
Bram Moolenaar78622822005-08-23 21:00:13 +00008281 /* SN_NOBREAK: NOBREAK flag */
8282 if (spin->si_nobreak)
8283 {
8284 putc(SN_NOBREAK, fd); /* <sectionID> */
8285 putc(0, fd); /* <sectionflags> */
8286
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008287 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008288 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8289 }
8290
Bram Moolenaar5195e452005-08-19 20:32:47 +00008291 /* SN_SYLLABLE: syllable info.
8292 * We don't mark it required, when not supported syllables will not be
8293 * counted. */
8294 if (spin->si_syllable != NULL)
8295 {
8296 putc(SN_SYLLABLE, fd); /* <sectionID> */
8297 putc(0, fd); /* <sectionflags> */
8298
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008299 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008300 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008301 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8302 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008303 }
8304
8305 /* end of <SECTIONS> */
8306 putc(SN_END, fd); /* <sectionend> */
8307
Bram Moolenaar50cde822005-06-05 21:54:54 +00008308
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008309 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008310 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008311 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008312 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008313 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008314 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008315 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008316 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008317 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008318 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008319 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008320 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008321
Bram Moolenaar0c405862005-06-22 22:26:26 +00008322 /* Clear the index and wnode fields in the tree. */
8323 clear_node(tree);
8324
Bram Moolenaar51485f02005-06-04 21:55:20 +00008325 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008326 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008327 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008328 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008329
Bram Moolenaar51485f02005-06-04 21:55:20 +00008330 /* number of nodes in 4 bytes */
8331 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008332 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008333
Bram Moolenaar51485f02005-06-04 21:55:20 +00008334 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008335 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008336 }
8337
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008338 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008339 if (putc(0, fd) == EOF)
8340 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008341theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008342 if (fclose(fd) == EOF)
8343 retval = FAIL;
8344
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008345 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008346 retval = FAIL;
8347 if (retval == FAIL)
8348 EMSG(_(e_write));
8349
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008350 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008351}
8352
8353/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008354 * Clear the index and wnode fields of "node", it siblings and its
8355 * children. This is needed because they are a union with other items to save
8356 * space.
8357 */
8358 static void
8359clear_node(node)
8360 wordnode_T *node;
8361{
8362 wordnode_T *np;
8363
8364 if (node != NULL)
8365 for (np = node; np != NULL; np = np->wn_sibling)
8366 {
8367 np->wn_u1.index = 0;
8368 np->wn_u2.wnode = NULL;
8369
8370 if (np->wn_byte != NUL)
8371 clear_node(np->wn_child);
8372 }
8373}
8374
8375
8376/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008377 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008378 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008379 * This first writes the list of possible bytes (siblings). Then for each
8380 * byte recursively write the children.
8381 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008382 * NOTE: The code here must match the code in read_tree_node(), since
8383 * assumptions are made about the indexes (so that we don't have to write them
8384 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008385 *
8386 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008387 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008388 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008389put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008390 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008391 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008392 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008393 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008394 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008395{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008396 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008397 int siblingcount = 0;
8398 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008399 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008400
Bram Moolenaar51485f02005-06-04 21:55:20 +00008401 /* If "node" is zero the tree is empty. */
8402 if (node == NULL)
8403 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008404
Bram Moolenaar51485f02005-06-04 21:55:20 +00008405 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008406 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008407
8408 /* Count the number of siblings. */
8409 for (np = node; np != NULL; np = np->wn_sibling)
8410 ++siblingcount;
8411
8412 /* Write the sibling count. */
8413 if (fd != NULL)
8414 putc(siblingcount, fd); /* <siblingcount> */
8415
8416 /* Write each sibling byte and optionally extra info. */
8417 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008418 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008419 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008420 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008421 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008422 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008423 /* For a NUL byte (end of word) write the flags etc. */
8424 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008425 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008426 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008427 * associated condition nr (stored in wn_region). The
8428 * byte value is misused to store the "rare" and "not
8429 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008430 if (np->wn_flags == (short_u)PFX_FLAGS)
8431 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008432 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008433 {
8434 putc(BY_FLAGS, fd); /* <byte> */
8435 putc(np->wn_flags, fd); /* <pflags> */
8436 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008437 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008438 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008439 }
8440 else
8441 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008442 /* For word trees we write the flag/region items. */
8443 flags = np->wn_flags;
8444 if (regionmask != 0 && np->wn_region != regionmask)
8445 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008446 if (np->wn_affixID != 0)
8447 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008448 if (flags == 0)
8449 {
8450 /* word without flags or region */
8451 putc(BY_NOFLAGS, fd); /* <byte> */
8452 }
8453 else
8454 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008455 if (np->wn_flags >= 0x100)
8456 {
8457 putc(BY_FLAGS2, fd); /* <byte> */
8458 putc(flags, fd); /* <flags> */
8459 putc((unsigned)flags >> 8, fd); /* <flags2> */
8460 }
8461 else
8462 {
8463 putc(BY_FLAGS, fd); /* <byte> */
8464 putc(flags, fd); /* <flags> */
8465 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008466 if (flags & WF_REGION)
8467 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008468 if (flags & WF_AFX)
8469 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008470 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008471 }
8472 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008473 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008474 else
8475 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008476 if (np->wn_child->wn_u1.index != 0
8477 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008478 {
8479 /* The child is written elsewhere, write the reference. */
8480 if (fd != NULL)
8481 {
8482 putc(BY_INDEX, fd); /* <byte> */
8483 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008484 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008485 }
8486 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008487 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008488 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008489 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008490
Bram Moolenaar51485f02005-06-04 21:55:20 +00008491 if (fd != NULL)
8492 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8493 {
8494 EMSG(_(e_write));
8495 return 0;
8496 }
8497 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008498 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008499
8500 /* Space used in the array when reading: one for each sibling and one for
8501 * the count. */
8502 newindex += siblingcount + 1;
8503
8504 /* Recursively dump the children of each sibling. */
8505 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008506 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8507 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008508 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008509
8510 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008511}
8512
8513
8514/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008515 * ":mkspell [-ascii] outfile infile ..."
8516 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008517 */
8518 void
8519ex_mkspell(eap)
8520 exarg_T *eap;
8521{
8522 int fcount;
8523 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008524 char_u *arg = eap->arg;
8525 int ascii = FALSE;
8526
8527 if (STRNCMP(arg, "-ascii", 6) == 0)
8528 {
8529 ascii = TRUE;
8530 arg = skipwhite(arg + 6);
8531 }
8532
8533 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8534 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008536 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008537 FreeWild(fcount, fnames);
8538 }
8539}
8540
8541/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008542 * Create the .sug file.
8543 * Uses the soundfold info in "spin".
8544 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8545 */
8546 static void
8547spell_make_sugfile(spin, wfname)
8548 spellinfo_T *spin;
8549 char_u *wfname;
8550{
8551 char_u fname[MAXPATHL];
8552 int len;
8553 slang_T *slang;
8554 int free_slang = FALSE;
8555
8556 /*
8557 * Read back the .spl file that was written. This fills the required
8558 * info for soundfolding. This also uses less memory than the
8559 * pointer-linked version of the trie. And it avoids having two versions
8560 * of the code for the soundfolding stuff.
8561 * It might have been done already by spell_reload_one().
8562 */
8563 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8564 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8565 break;
8566 if (slang == NULL)
8567 {
8568 spell_message(spin, (char_u *)_("Reading back spell file..."));
8569 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8570 if (slang == NULL)
8571 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008572 free_slang = TRUE;
8573 }
8574
8575 /*
8576 * Clear the info in "spin" that is used.
8577 */
8578 spin->si_blocks = NULL;
8579 spin->si_blocks_cnt = 0;
8580 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8581 spin->si_free_count = 0;
8582 spin->si_first_free = NULL;
8583 spin->si_foldwcount = 0;
8584
8585 /*
8586 * Go through the trie of good words, soundfold each word and add it to
8587 * the soundfold trie.
8588 */
8589 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8590 if (sug_filltree(spin, slang) == FAIL)
8591 goto theend;
8592
8593 /*
8594 * Create the table which links each soundfold word with a list of the
8595 * good words it may come from. Creates buffer "spin->si_spellbuf".
8596 * This also removes the wordnr from the NUL byte entries to make
8597 * compression possible.
8598 */
8599 if (sug_maketable(spin) == FAIL)
8600 goto theend;
8601
8602 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8603 (long)spin->si_spellbuf->b_ml.ml_line_count);
8604
8605 /*
8606 * Compress the soundfold trie.
8607 */
8608 spell_message(spin, (char_u *)_(msg_compressing));
8609 wordtree_compress(spin, spin->si_foldroot);
8610
8611 /*
8612 * Write the .sug file.
8613 * Make the file name by changing ".spl" to ".sug".
8614 */
8615 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008616 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008617 fname[len - 2] = 'u';
8618 fname[len - 1] = 'g';
8619 sug_write(spin, fname);
8620
8621theend:
8622 if (free_slang)
8623 slang_free(slang);
8624 free_blocks(spin->si_blocks);
8625 close_spellbuf(spin->si_spellbuf);
8626}
8627
8628/*
8629 * Build the soundfold trie for language "slang".
8630 */
8631 static int
8632sug_filltree(spin, slang)
8633 spellinfo_T *spin;
8634 slang_T *slang;
8635{
8636 char_u *byts;
8637 idx_T *idxs;
8638 int depth;
8639 idx_T arridx[MAXWLEN];
8640 int curi[MAXWLEN];
8641 char_u tword[MAXWLEN];
8642 char_u tsalword[MAXWLEN];
8643 int c;
8644 idx_T n;
8645 unsigned words_done = 0;
8646 int wordcount[MAXWLEN];
8647
8648 /* We use si_foldroot for the souldfolded trie. */
8649 spin->si_foldroot = wordtree_alloc(spin);
8650 if (spin->si_foldroot == NULL)
8651 return FAIL;
8652
8653 /* let tree_add_word() know we're adding to the soundfolded tree */
8654 spin->si_sugtree = TRUE;
8655
8656 /*
8657 * Go through the whole case-folded tree, soundfold each word and put it
8658 * in the trie.
8659 */
8660 byts = slang->sl_fbyts;
8661 idxs = slang->sl_fidxs;
8662
8663 arridx[0] = 0;
8664 curi[0] = 1;
8665 wordcount[0] = 0;
8666
8667 depth = 0;
8668 while (depth >= 0 && !got_int)
8669 {
8670 if (curi[depth] > byts[arridx[depth]])
8671 {
8672 /* Done all bytes at this node, go up one level. */
8673 idxs[arridx[depth]] = wordcount[depth];
8674 if (depth > 0)
8675 wordcount[depth - 1] += wordcount[depth];
8676
8677 --depth;
8678 line_breakcheck();
8679 }
8680 else
8681 {
8682
8683 /* Do one more byte at this node. */
8684 n = arridx[depth] + curi[depth];
8685 ++curi[depth];
8686
8687 c = byts[n];
8688 if (c == 0)
8689 {
8690 /* Sound-fold the word. */
8691 tword[depth] = NUL;
8692 spell_soundfold(slang, tword, TRUE, tsalword);
8693
8694 /* We use the "flags" field for the MSB of the wordnr,
8695 * "region" for the LSB of the wordnr. */
8696 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8697 words_done >> 16, words_done & 0xffff,
8698 0) == FAIL)
8699 return FAIL;
8700
8701 ++words_done;
8702 ++wordcount[depth];
8703
8704 /* Reset the block count each time to avoid compression
8705 * kicking in. */
8706 spin->si_blocks_cnt = 0;
8707
8708 /* Skip over any other NUL bytes (same word with different
8709 * flags). */
8710 while (byts[n + 1] == 0)
8711 {
8712 ++n;
8713 ++curi[depth];
8714 }
8715 }
8716 else
8717 {
8718 /* Normal char, go one level deeper. */
8719 tword[depth++] = c;
8720 arridx[depth] = idxs[n];
8721 curi[depth] = 1;
8722 wordcount[depth] = 0;
8723 }
8724 }
8725 }
8726
8727 smsg((char_u *)_("Total number of words: %d"), words_done);
8728
8729 return OK;
8730}
8731
8732/*
8733 * Make the table that links each word in the soundfold trie to the words it
8734 * can be produced from.
8735 * This is not unlike lines in a file, thus use a memfile to be able to access
8736 * the table efficiently.
8737 * Returns FAIL when out of memory.
8738 */
8739 static int
8740sug_maketable(spin)
8741 spellinfo_T *spin;
8742{
8743 garray_T ga;
8744 int res = OK;
8745
8746 /* Allocate a buffer, open a memline for it and create the swap file
8747 * (uses a temp file, not a .swp file). */
8748 spin->si_spellbuf = open_spellbuf();
8749 if (spin->si_spellbuf == NULL)
8750 return FAIL;
8751
8752 /* Use a buffer to store the line info, avoids allocating many small
8753 * pieces of memory. */
8754 ga_init2(&ga, 1, 100);
8755
8756 /* recursively go through the tree */
8757 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8758 res = FAIL;
8759
8760 ga_clear(&ga);
8761 return res;
8762}
8763
8764/*
8765 * Fill the table for one node and its children.
8766 * Returns the wordnr at the start of the node.
8767 * Returns -1 when out of memory.
8768 */
8769 static int
8770sug_filltable(spin, node, startwordnr, gap)
8771 spellinfo_T *spin;
8772 wordnode_T *node;
8773 int startwordnr;
8774 garray_T *gap; /* place to store line of numbers */
8775{
8776 wordnode_T *p, *np;
8777 int wordnr = startwordnr;
8778 int nr;
8779 int prev_nr;
8780
8781 for (p = node; p != NULL; p = p->wn_sibling)
8782 {
8783 if (p->wn_byte == NUL)
8784 {
8785 gap->ga_len = 0;
8786 prev_nr = 0;
8787 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8788 {
8789 if (ga_grow(gap, 10) == FAIL)
8790 return -1;
8791
8792 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8793 /* Compute the offset from the previous nr and store the
8794 * offset in a way that it takes a minimum number of bytes.
8795 * It's a bit like utf-8, but without the need to mark
8796 * following bytes. */
8797 nr -= prev_nr;
8798 prev_nr += nr;
8799 gap->ga_len += offset2bytes(nr,
8800 (char_u *)gap->ga_data + gap->ga_len);
8801 }
8802
8803 /* add the NUL byte */
8804 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8805
8806 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8807 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8808 return -1;
8809 ++wordnr;
8810
8811 /* Remove extra NUL entries, we no longer need them. We don't
8812 * bother freeing the nodes, the won't be reused anyway. */
8813 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8814 p->wn_sibling = p->wn_sibling->wn_sibling;
8815
8816 /* Clear the flags on the remaining NUL node, so that compression
8817 * works a lot better. */
8818 p->wn_flags = 0;
8819 p->wn_region = 0;
8820 }
8821 else
8822 {
8823 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8824 if (wordnr == -1)
8825 return -1;
8826 }
8827 }
8828 return wordnr;
8829}
8830
8831/*
8832 * Convert an offset into a minimal number of bytes.
8833 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8834 * bytes.
8835 */
8836 static int
8837offset2bytes(nr, buf)
8838 int nr;
8839 char_u *buf;
8840{
8841 int rem;
8842 int b1, b2, b3, b4;
8843
8844 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8845 b1 = nr % 255 + 1;
8846 rem = nr / 255;
8847 b2 = rem % 255 + 1;
8848 rem = rem / 255;
8849 b3 = rem % 255 + 1;
8850 b4 = rem / 255 + 1;
8851
8852 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8853 {
8854 buf[0] = 0xe0 + b4;
8855 buf[1] = b3;
8856 buf[2] = b2;
8857 buf[3] = b1;
8858 return 4;
8859 }
8860 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8861 {
8862 buf[0] = 0xc0 + b3;
8863 buf[1] = b2;
8864 buf[2] = b1;
8865 return 3;
8866 }
8867 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8868 {
8869 buf[0] = 0x80 + b2;
8870 buf[1] = b1;
8871 return 2;
8872 }
8873 /* 1 byte */
8874 buf[0] = b1;
8875 return 1;
8876}
8877
8878/*
8879 * Opposite of offset2bytes().
8880 * "pp" points to the bytes and is advanced over it.
8881 * Returns the offset.
8882 */
8883 static int
8884bytes2offset(pp)
8885 char_u **pp;
8886{
8887 char_u *p = *pp;
8888 int nr;
8889 int c;
8890
8891 c = *p++;
8892 if ((c & 0x80) == 0x00) /* 1 byte */
8893 {
8894 nr = c - 1;
8895 }
8896 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8897 {
8898 nr = (c & 0x3f) - 1;
8899 nr = nr * 255 + (*p++ - 1);
8900 }
8901 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8902 {
8903 nr = (c & 0x1f) - 1;
8904 nr = nr * 255 + (*p++ - 1);
8905 nr = nr * 255 + (*p++ - 1);
8906 }
8907 else /* 4 bytes */
8908 {
8909 nr = (c & 0x0f) - 1;
8910 nr = nr * 255 + (*p++ - 1);
8911 nr = nr * 255 + (*p++ - 1);
8912 nr = nr * 255 + (*p++ - 1);
8913 }
8914
8915 *pp = p;
8916 return nr;
8917}
8918
8919/*
8920 * Write the .sug file in "fname".
8921 */
8922 static void
8923sug_write(spin, fname)
8924 spellinfo_T *spin;
8925 char_u *fname;
8926{
8927 FILE *fd;
8928 wordnode_T *tree;
8929 int nodecount;
8930 int wcount;
8931 char_u *line;
8932 linenr_T lnum;
8933 int len;
8934
8935 /* Create the file. Note that an existing file is silently overwritten! */
8936 fd = mch_fopen((char *)fname, "w");
8937 if (fd == NULL)
8938 {
8939 EMSG2(_(e_notopen), fname);
8940 return;
8941 }
8942
8943 vim_snprintf((char *)IObuff, IOSIZE,
8944 _("Writing suggestion file %s ..."), fname);
8945 spell_message(spin, IObuff);
8946
8947 /*
8948 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8949 */
8950 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8951 {
8952 EMSG(_(e_write));
8953 goto theend;
8954 }
8955 putc(VIMSUGVERSION, fd); /* <versionnr> */
8956
8957 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008958 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008959
8960 /*
8961 * <SUGWORDTREE>
8962 */
8963 spin->si_memtot = 0;
8964 tree = spin->si_foldroot->wn_sibling;
8965
8966 /* Clear the index and wnode fields in the tree. */
8967 clear_node(tree);
8968
8969 /* Count the number of nodes. Needed to be able to allocate the
8970 * memory when reading the nodes. Also fills in index for shared
8971 * nodes. */
8972 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8973
8974 /* number of nodes in 4 bytes */
8975 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8976 spin->si_memtot += nodecount + nodecount * sizeof(int);
8977
8978 /* Write the nodes. */
8979 (void)put_node(fd, tree, 0, 0, FALSE);
8980
8981 /*
8982 * <SUGTABLE>: <sugwcount> <sugline> ...
8983 */
8984 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8985 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8986
8987 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8988 {
8989 /* <sugline>: <sugnr> ... NUL */
8990 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008991 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008992 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8993 {
8994 EMSG(_(e_write));
8995 goto theend;
8996 }
8997 spin->si_memtot += len;
8998 }
8999
9000 /* Write another byte to check for errors. */
9001 if (putc(0, fd) == EOF)
9002 EMSG(_(e_write));
9003
9004 vim_snprintf((char *)IObuff, IOSIZE,
9005 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9006 spell_message(spin, IObuff);
9007
9008theend:
9009 /* close the file */
9010 fclose(fd);
9011}
9012
9013/*
9014 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9015 * list and only contains text lines. Can use a swapfile to reduce memory
9016 * use.
9017 * Most other fields are invalid! Esp. watch out for string options being
9018 * NULL and there is no undo info.
9019 * Returns NULL when out of memory.
9020 */
9021 static buf_T *
9022open_spellbuf()
9023{
9024 buf_T *buf;
9025
9026 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9027 if (buf != NULL)
9028 {
9029 buf->b_spell = TRUE;
9030 buf->b_p_swf = TRUE; /* may create a swap file */
9031 ml_open(buf);
9032 ml_open_file(buf); /* create swap file now */
9033 }
9034 return buf;
9035}
9036
9037/*
9038 * Close the buffer used for spell info.
9039 */
9040 static void
9041close_spellbuf(buf)
9042 buf_T *buf;
9043{
9044 if (buf != NULL)
9045 {
9046 ml_close(buf, TRUE);
9047 vim_free(buf);
9048 }
9049}
9050
9051
9052/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009053 * Create a Vim spell file from one or more word lists.
9054 * "fnames[0]" is the output file name.
9055 * "fnames[fcount - 1]" is the last input file name.
9056 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9057 * and ".spl" is appended to make the output file name.
9058 */
9059 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009060mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009061 int fcount;
9062 char_u **fnames;
9063 int ascii; /* -ascii argument given */
9064 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009066{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009067 char_u fname[MAXPATHL];
9068 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009069 char_u **innames;
9070 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009071 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009072 int i;
9073 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009074 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009075 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009076 spellinfo_T spin;
9077
9078 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009080 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 spin.si_followup = TRUE;
9082 spin.si_rem_accents = TRUE;
9083 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009084 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9086 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009087 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009088 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009089 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009090 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009091
Bram Moolenaarb765d632005-06-07 21:00:02 +00009092 /* default: fnames[0] is output file, following are input files */
9093 innames = &fnames[1];
9094 incount = fcount - 1;
9095
9096 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009097 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009098 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009099 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9100 {
9101 /* For ":mkspell path/en.latin1.add" output file is
9102 * "path/en.latin1.add.spl". */
9103 innames = &fnames[0];
9104 incount = 1;
9105 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9106 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009107 else if (fcount == 1)
9108 {
9109 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9110 innames = &fnames[0];
9111 incount = 1;
9112 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9113 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9114 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009115 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9116 {
9117 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009119 }
9120 else
9121 /* Name should be language, make the file name from it. */
9122 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9123 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9124
9125 /* Check for .ascii.spl. */
9126 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9127 spin.si_ascii = TRUE;
9128
9129 /* Check for .add.spl. */
9130 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9131 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009132 }
9133
Bram Moolenaarb765d632005-06-07 21:00:02 +00009134 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009135 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009136 else if (vim_strchr(gettail(wfname), '_') != NULL)
9137 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009138 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009139 EMSG(_("E754: Only up to 8 regions supported"));
9140 else
9141 {
9142 /* Check for overwriting before doing things that may take a lot of
9143 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009144 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009145 {
9146 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009147 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009148 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009149 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009150 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009151 EMSG2(_(e_isadir2), wfname);
9152 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009153 }
9154
9155 /*
9156 * Init the aff and dic pointers.
9157 * Get the region names if there are more than 2 arguments.
9158 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009159 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009160 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009161 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009162
Bram Moolenaar3982c542005-06-08 21:56:31 +00009163 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009164 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009165 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009166 if (STRLEN(gettail(innames[i])) < 5
9167 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009168 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9170 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009171 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009172 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9173 spin.si_region_name[i * 2 + 1] =
9174 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009175 }
9176 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009177 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009178
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009179 spin.si_foldroot = wordtree_alloc(&spin);
9180 spin.si_keeproot = wordtree_alloc(&spin);
9181 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009182 if (spin.si_foldroot == NULL
9183 || spin.si_keeproot == NULL
9184 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009185 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009186 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009187 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009188 }
9189
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009190 /* When not producing a .add.spl file clear the character table when
9191 * we encounter one in the .aff file. This means we dump the current
9192 * one in the .spl file if the .aff file doesn't define one. That's
9193 * better than guessing the contents, the table will match a
9194 * previously loaded spell file. */
9195 if (!spin.si_add)
9196 spin.si_clear_chartab = TRUE;
9197
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009198 /*
9199 * Read all the .aff and .dic files.
9200 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009201 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009202 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009203 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009204 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009205 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009206 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009207
Bram Moolenaarb765d632005-06-07 21:00:02 +00009208 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009209 if (mch_stat((char *)fname, &st) >= 0)
9210 {
9211 /* Read the .aff file. Will init "spin->si_conv" based on the
9212 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009213 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009214 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009215 error = TRUE;
9216 else
9217 {
9218 /* Read the .dic file and store the words in the trees. */
9219 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009220 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009221 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009222 error = TRUE;
9223 }
9224 }
9225 else
9226 {
9227 /* No .aff file, try reading the file as a word list. Store
9228 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009229 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009230 error = TRUE;
9231 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009232
Bram Moolenaarb765d632005-06-07 21:00:02 +00009233#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009234 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009235 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009236#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009237 }
9238
Bram Moolenaar78622822005-08-23 21:00:13 +00009239 if (spin.si_compflags != NULL && spin.si_nobreak)
9240 MSG(_("Warning: both compounding and NOBREAK specified"));
9241
Bram Moolenaar4770d092006-01-12 23:22:24 +00009242 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009243 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009244 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009245 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009246 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009247 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009248 wordtree_compress(&spin, spin.si_foldroot);
9249 wordtree_compress(&spin, spin.si_keeproot);
9250 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009251 }
9252
Bram Moolenaar4770d092006-01-12 23:22:24 +00009253 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009254 {
9255 /*
9256 * Write the info in the spell file.
9257 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009258 vim_snprintf((char *)IObuff, IOSIZE,
9259 _("Writing spell file %s ..."), wfname);
9260 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009261
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009262 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009263
Bram Moolenaar4770d092006-01-12 23:22:24 +00009264 spell_message(&spin, (char_u *)_("Done!"));
9265 vim_snprintf((char *)IObuff, IOSIZE,
9266 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9267 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009268
Bram Moolenaar4770d092006-01-12 23:22:24 +00009269 /*
9270 * If the file is loaded need to reload it.
9271 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009272 if (!error)
9273 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009274 }
9275
9276 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009278 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 ga_clear(&spin.si_sal);
9280 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009281 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009282 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009283 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009284
9285 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009286 for (i = 0; i < incount; ++i)
9287 if (afile[i] != NULL)
9288 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009289
9290 /* Free all the bits and pieces at once. */
9291 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009292
9293 /*
9294 * If there is soundfolding info and no NOSUGFILE item create the
9295 * .sug file with the soundfolded word trie.
9296 */
9297 if (spin.si_sugtime != 0 && !error && !got_int)
9298 spell_make_sugfile(&spin, wfname);
9299
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009300 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009301}
9302
Bram Moolenaar4770d092006-01-12 23:22:24 +00009303/*
9304 * Display a message for spell file processing when 'verbose' is set or using
9305 * ":mkspell". "str" can be IObuff.
9306 */
9307 static void
9308spell_message(spin, str)
9309 spellinfo_T *spin;
9310 char_u *str;
9311{
9312 if (spin->si_verbose || p_verbose > 2)
9313 {
9314 if (!spin->si_verbose)
9315 verbose_enter();
9316 MSG(str);
9317 out_flush();
9318 if (!spin->si_verbose)
9319 verbose_leave();
9320 }
9321}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009322
9323/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009324 * ":[count]spellgood {word}"
9325 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009326 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009327 */
9328 void
9329ex_spell(eap)
9330 exarg_T *eap;
9331{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009332 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009333 eap->forceit ? 0 : (int)eap->line2,
9334 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009335}
9336
9337/*
9338 * Add "word[len]" to 'spellfile' as a good or bad word.
9339 */
9340 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009341spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009342 char_u *word;
9343 int len;
9344 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009345 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009346 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009347 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009348{
Bram Moolenaara3917072006-09-14 08:48:14 +00009349 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009350 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009351 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009352 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009353 char_u fnamebuf[MAXPATHL];
9354 char_u line[MAXWLEN * 2];
9355 long fpos, fpos_next = 0;
9356 int i;
9357 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009358
Bram Moolenaar89d40322006-08-29 15:30:07 +00009359 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009360 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009361 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009362 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009363 int_wordlist = vim_tempname('s');
9364 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009365 return;
9366 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009367 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009368 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009369 else
9370 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009371 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009372 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009373 {
9374 init_spellfile();
9375 new_spf = TRUE;
9376 }
9377
Bram Moolenaar860cae12010-06-05 23:22:07 +02009378 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009379 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009380 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009381 return;
9382 }
9383
Bram Moolenaar860cae12010-06-05 23:22:07 +02009384 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009385 {
9386 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009387 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009388 break;
9389 if (*spf == NUL)
9390 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009391 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009392 return;
9393 }
9394 }
9395
Bram Moolenaarb765d632005-06-07 21:00:02 +00009396 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009397 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009398 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9399 buf = NULL;
9400 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009401 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009402 EMSG(_(e_bufloaded));
9403 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009404 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009405
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009406 fname = fnamebuf;
9407 }
9408
Bram Moolenaard0131a82006-03-04 21:46:13 +00009409 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009410 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009411 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009412 * since its flags sort before the one with WF_BANNED. */
9413 fd = mch_fopen((char *)fname, "r");
9414 if (fd != NULL)
9415 {
9416 while (!vim_fgets(line, MAXWLEN * 2, fd))
9417 {
9418 fpos = fpos_next;
9419 fpos_next = ftell(fd);
9420 if (STRNCMP(word, line, len) == 0
9421 && (line[len] == '/' || line[len] < ' '))
9422 {
9423 /* Found duplicate word. Remove it by writing a '#' at
9424 * the start of the line. Mixing reading and writing
9425 * doesn't work for all systems, close the file first. */
9426 fclose(fd);
9427 fd = mch_fopen((char *)fname, "r+");
9428 if (fd == NULL)
9429 break;
9430 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009431 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009432 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009433 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009434 {
9435 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009436 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009437 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009438 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009439 fseek(fd, fpos_next, SEEK_SET);
9440 }
9441 }
9442 fclose(fd);
9443 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009444 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009445
9446 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009447 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009448 fd = mch_fopen((char *)fname, "a");
9449 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009450 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009451 char_u *p;
9452
Bram Moolenaard0131a82006-03-04 21:46:13 +00009453 /* We just initialized the 'spellfile' option and can't open the
9454 * file. We may need to create the "spell" directory first. We
9455 * already checked the runtime directory is writable in
9456 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009457 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009458 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009459 int c = *p;
9460
Bram Moolenaard0131a82006-03-04 21:46:13 +00009461 /* The directory doesn't exist. Try creating it and opening
9462 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009463 *p = NUL;
9464 vim_mkdir(fname, 0755);
9465 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009466 fd = mch_fopen((char *)fname, "a");
9467 }
9468 }
9469
9470 if (fd == NULL)
9471 EMSG2(_(e_notopen), fname);
9472 else
9473 {
9474 if (bad)
9475 fprintf(fd, "%.*s/!\n", len, word);
9476 else
9477 fprintf(fd, "%.*s\n", len, word);
9478 fclose(fd);
9479
9480 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9481 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009482 }
9483 }
9484
Bram Moolenaard0131a82006-03-04 21:46:13 +00009485 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009486 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009487 /* Update the .add.spl file. */
9488 mkspell(1, &fname, FALSE, TRUE, TRUE);
9489
9490 /* If the .add file is edited somewhere, reload it. */
9491 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009492 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009493
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009494 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009495 }
9496}
9497
9498/*
9499 * Initialize 'spellfile' for the current buffer.
9500 */
9501 static void
9502init_spellfile()
9503{
9504 char_u buf[MAXPATHL];
9505 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009506 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009507 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009508 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009509 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009510 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009511
Bram Moolenaar860cae12010-06-05 23:22:07 +02009512 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009513 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009514 /* Find the end of the language name. Exclude the region. If there
9515 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009516 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009517 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009518 if (vim_ispathsep(*lend))
9519 {
9520 aspath = TRUE;
9521 lstart = lend + 1;
9522 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009523
9524 /* Loop over all entries in 'runtimepath'. Use the first one where we
9525 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009526 rtp = p_rtp;
9527 while (*rtp != NUL)
9528 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009529 if (aspath)
9530 /* Use directory of an entry with path, e.g., for
9531 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009532 vim_strncpy(buf, curbuf->b_s.b_p_spl, lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009533 else
9534 /* Copy the path from 'runtimepath' to buf[]. */
9535 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009536 if (filewritable(buf) == 2)
9537 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009538 /* Use the first language name from 'spelllang' and the
9539 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009540 if (aspath)
Bram Moolenaar860cae12010-06-05 23:22:07 +02009541 vim_strncpy(buf, curbuf->b_s.b_p_spl, lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009542 else
9543 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009544 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009545 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009546 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9547 if (!filewritable(buf) != 2)
9548 vim_mkdir(buf, 0755);
9549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009550 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009551 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009552 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009553 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009554 l = (int)STRLEN(buf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009555 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009556 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9557 fname != NULL
9558 && strstr((char *)gettail(fname), ".ascii.") != NULL
9559 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009560 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9561 break;
9562 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009563 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009564 }
9565 }
9566}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009567
Bram Moolenaar51485f02005-06-04 21:55:20 +00009568
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009569/*
9570 * Init the chartab used for spelling for ASCII.
9571 * EBCDIC is not supported!
9572 */
9573 static void
9574clear_spell_chartab(sp)
9575 spelltab_T *sp;
9576{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009577 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009578
9579 /* Init everything to FALSE. */
9580 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9581 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9582 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009583 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009584 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009585 sp->st_upper[i] = i;
9586 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009587
9588 /* We include digits. A word shouldn't start with a digit, but handling
9589 * that is done separately. */
9590 for (i = '0'; i <= '9'; ++i)
9591 sp->st_isw[i] = TRUE;
9592 for (i = 'A'; i <= 'Z'; ++i)
9593 {
9594 sp->st_isw[i] = TRUE;
9595 sp->st_isu[i] = TRUE;
9596 sp->st_fold[i] = i + 0x20;
9597 }
9598 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009599 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009600 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009601 sp->st_upper[i] = i - 0x20;
9602 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009603}
9604
9605/*
9606 * Init the chartab used for spelling. Only depends on 'encoding'.
9607 * Called once while starting up and when 'encoding' changes.
9608 * The default is to use isalpha(), but the spell file should define the word
9609 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009610 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009611 */
9612 void
9613init_spell_chartab()
9614{
9615 int i;
9616
9617 did_set_spelltab = FALSE;
9618 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009619#ifdef FEAT_MBYTE
9620 if (enc_dbcs)
9621 {
9622 /* DBCS: assume double-wide characters are word characters. */
9623 for (i = 128; i <= 255; ++i)
9624 if (MB_BYTE2LEN(i) == 2)
9625 spelltab.st_isw[i] = TRUE;
9626 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009627 else if (enc_utf8)
9628 {
9629 for (i = 128; i < 256; ++i)
9630 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009631 int f = utf_fold(i);
9632 int u = utf_toupper(i);
9633
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009634 spelltab.st_isu[i] = utf_isupper(i);
9635 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009636 /* The folded/upper-cased value is different between latin1 and
9637 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9638 * value for utf-8 to avoid this. */
9639 spelltab.st_fold[i] = (f < 256) ? f : i;
9640 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009641 }
9642 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009643 else
9644#endif
9645 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009646 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009647 for (i = 128; i < 256; ++i)
9648 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009649 if (MB_ISUPPER(i))
9650 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009651 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009652 spelltab.st_isu[i] = TRUE;
9653 spelltab.st_fold[i] = MB_TOLOWER(i);
9654 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009655 else if (MB_ISLOWER(i))
9656 {
9657 spelltab.st_isw[i] = TRUE;
9658 spelltab.st_upper[i] = MB_TOUPPER(i);
9659 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660 }
9661 }
9662}
9663
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009664/*
9665 * Set the spell character tables from strings in the affix file.
9666 */
9667 static int
9668set_spell_chartab(fol, low, upp)
9669 char_u *fol;
9670 char_u *low;
9671 char_u *upp;
9672{
9673 /* We build the new tables here first, so that we can compare with the
9674 * previous one. */
9675 spelltab_T new_st;
9676 char_u *pf = fol, *pl = low, *pu = upp;
9677 int f, l, u;
9678
9679 clear_spell_chartab(&new_st);
9680
9681 while (*pf != NUL)
9682 {
9683 if (*pl == NUL || *pu == NUL)
9684 {
9685 EMSG(_(e_affform));
9686 return FAIL;
9687 }
9688#ifdef FEAT_MBYTE
9689 f = mb_ptr2char_adv(&pf);
9690 l = mb_ptr2char_adv(&pl);
9691 u = mb_ptr2char_adv(&pu);
9692#else
9693 f = *pf++;
9694 l = *pl++;
9695 u = *pu++;
9696#endif
9697 /* Every character that appears is a word character. */
9698 if (f < 256)
9699 new_st.st_isw[f] = TRUE;
9700 if (l < 256)
9701 new_st.st_isw[l] = TRUE;
9702 if (u < 256)
9703 new_st.st_isw[u] = TRUE;
9704
9705 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9706 * case-folding */
9707 if (l < 256 && l != f)
9708 {
9709 if (f >= 256)
9710 {
9711 EMSG(_(e_affrange));
9712 return FAIL;
9713 }
9714 new_st.st_fold[l] = f;
9715 }
9716
9717 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009718 * case-folding, it's upper case and the "UPP" is the upper case of
9719 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009720 if (u < 256 && u != f)
9721 {
9722 if (f >= 256)
9723 {
9724 EMSG(_(e_affrange));
9725 return FAIL;
9726 }
9727 new_st.st_fold[u] = f;
9728 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009729 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009730 }
9731 }
9732
9733 if (*pl != NUL || *pu != NUL)
9734 {
9735 EMSG(_(e_affform));
9736 return FAIL;
9737 }
9738
9739 return set_spell_finish(&new_st);
9740}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009741
9742/*
9743 * Set the spell character tables from strings in the .spl file.
9744 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009745 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009746set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009747 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009748 int cnt; /* length of "flags" */
9749 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009750{
9751 /* We build the new tables here first, so that we can compare with the
9752 * previous one. */
9753 spelltab_T new_st;
9754 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009755 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009756 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009757
9758 clear_spell_chartab(&new_st);
9759
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009760 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009761 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009762 if (i < cnt)
9763 {
9764 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9765 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9766 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009767
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009768 if (*p != NUL)
9769 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009770#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009771 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009772#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009773 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009774#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009775 new_st.st_fold[i + 128] = c;
9776 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9777 new_st.st_upper[c] = i + 128;
9778 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009779 }
9780
Bram Moolenaar5195e452005-08-19 20:32:47 +00009781 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009782}
9783
9784 static int
9785set_spell_finish(new_st)
9786 spelltab_T *new_st;
9787{
9788 int i;
9789
9790 if (did_set_spelltab)
9791 {
9792 /* check that it's the same table */
9793 for (i = 0; i < 256; ++i)
9794 {
9795 if (spelltab.st_isw[i] != new_st->st_isw[i]
9796 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009797 || spelltab.st_fold[i] != new_st->st_fold[i]
9798 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009799 {
9800 EMSG(_("E763: Word characters differ between spell files"));
9801 return FAIL;
9802 }
9803 }
9804 }
9805 else
9806 {
9807 /* copy the new spelltab into the one being used */
9808 spelltab = *new_st;
9809 did_set_spelltab = TRUE;
9810 }
9811
9812 return OK;
9813}
9814
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009815/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009816 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009817 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009818 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009819 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009820 */
9821 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009822spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009823 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009824 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009825{
Bram Moolenaarea408852005-06-25 22:49:46 +00009826#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009827 char_u *s;
9828 int l;
9829 int c;
9830
9831 if (has_mbyte)
9832 {
9833 l = MB_BYTE2LEN(*p);
9834 s = p;
9835 if (l == 1)
9836 {
9837 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009838 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009839 {
9840 s = p + 1; /* skip a mid-word character */
9841 l = MB_BYTE2LEN(*s);
9842 }
9843 }
9844 else
9845 {
9846 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009847 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9848 : (wp->w_s->b_spell_ismw_mb != NULL
9849 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009850 {
9851 s = p + l;
9852 l = MB_BYTE2LEN(*s);
9853 }
9854 }
9855
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009856 c = mb_ptr2char(s);
9857 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009858 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009859 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009860 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009861#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009862
Bram Moolenaar860cae12010-06-05 23:22:07 +02009863 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009864}
9865
9866/*
9867 * Return TRUE if "p" points to a word character.
9868 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9869 */
9870 static int
9871spell_iswordp_nmw(p)
9872 char_u *p;
9873{
9874#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009875 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009876
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009877 if (has_mbyte)
9878 {
9879 c = mb_ptr2char(p);
9880 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009881 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009882 return spelltab.st_isw[c];
9883 }
9884#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009885 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009886}
9887
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009888#ifdef FEAT_MBYTE
9889/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009890 * Return TRUE if word class indicates a word character.
9891 * Only for characters above 255.
9892 * Unicode subscript and superscript are not considered word characters.
9893 */
9894 static int
9895spell_mb_isword_class(cl)
9896 int cl;
9897{
9898 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9899}
9900
9901/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009902 * Return TRUE if "p" points to a word character.
9903 * Wide version of spell_iswordp().
9904 */
9905 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009906spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009907 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009908 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009909{
9910 int *s;
9911
Bram Moolenaar860cae12010-06-05 23:22:07 +02009912 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9913 : (wp->w_s->b_spell_ismw_mb != NULL
9914 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009915 s = p + 1;
9916 else
9917 s = p;
9918
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009919 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009920 {
9921 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009922 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009923 if (enc_dbcs)
9924 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9925 return 0;
9926 }
9927 return spelltab.st_isw[*s];
9928}
9929#endif
9930
Bram Moolenaarea408852005-06-25 22:49:46 +00009931/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009932 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009933 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009934 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009935 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009936write_spell_prefcond(fd, gap)
9937 FILE *fd;
9938 garray_T *gap;
9939{
9940 int i;
9941 char_u *p;
9942 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009943 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009944 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009945
Bram Moolenaar5195e452005-08-19 20:32:47 +00009946 if (fd != NULL)
9947 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9948
9949 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009950
9951 for (i = 0; i < gap->ga_len; ++i)
9952 {
9953 /* <prefcond> : <condlen> <condstr> */
9954 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009955 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009956 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009957 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009958 if (fd != NULL)
9959 {
9960 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009961 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009962 }
9963 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009964 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009965 else if (fd != NULL)
9966 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009967 }
9968
Bram Moolenaar5195e452005-08-19 20:32:47 +00009969 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009970}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009971
9972/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009973 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9974 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009975 * When using a multi-byte 'encoding' the length may change!
9976 * Returns FAIL when something wrong.
9977 */
9978 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009979spell_casefold(str, len, buf, buflen)
9980 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009981 int len;
9982 char_u *buf;
9983 int buflen;
9984{
9985 int i;
9986
9987 if (len >= buflen)
9988 {
9989 buf[0] = NUL;
9990 return FAIL; /* result will not fit */
9991 }
9992
9993#ifdef FEAT_MBYTE
9994 if (has_mbyte)
9995 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009996 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009997 char_u *p;
9998 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009999
10000 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010001 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010002 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010003 if (outi + MB_MAXBYTES > buflen)
10004 {
10005 buf[outi] = NUL;
10006 return FAIL;
10007 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010008 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010009 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010010 }
10011 buf[outi] = NUL;
10012 }
10013 else
10014#endif
10015 {
10016 /* Be quick for non-multibyte encodings. */
10017 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010018 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010019 buf[i] = NUL;
10020 }
10021
10022 return OK;
10023}
10024
Bram Moolenaar4770d092006-01-12 23:22:24 +000010025/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010026#define SPS_BEST 1
10027#define SPS_FAST 2
10028#define SPS_DOUBLE 4
10029
Bram Moolenaar4770d092006-01-12 23:22:24 +000010030static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10031static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010032
10033/*
10034 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010035 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010036 */
10037 int
10038spell_check_sps()
10039{
10040 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010041 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010042 char_u buf[MAXPATHL];
10043 int f;
10044
10045 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010046 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010047
10048 for (p = p_sps; *p != NUL; )
10049 {
10050 copy_option_part(&p, buf, MAXPATHL, ",");
10051
10052 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010053 if (VIM_ISDIGIT(*buf))
10054 {
10055 s = buf;
10056 sps_limit = getdigits(&s);
10057 if (*s != NUL && !VIM_ISDIGIT(*s))
10058 f = -1;
10059 }
10060 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010061 f = SPS_BEST;
10062 else if (STRCMP(buf, "fast") == 0)
10063 f = SPS_FAST;
10064 else if (STRCMP(buf, "double") == 0)
10065 f = SPS_DOUBLE;
10066 else if (STRNCMP(buf, "expr:", 5) != 0
10067 && STRNCMP(buf, "file:", 5) != 0)
10068 f = -1;
10069
10070 if (f == -1 || (sps_flags != 0 && f != 0))
10071 {
10072 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010073 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010074 return FAIL;
10075 }
10076 if (f != 0)
10077 sps_flags = f;
10078 }
10079
10080 if (sps_flags == 0)
10081 sps_flags = SPS_BEST;
10082
10083 return OK;
10084}
10085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086/*
10087 * "z?": Find badly spelled word under or after the cursor.
10088 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010089 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010090 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 */
10092 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010093spell_suggest(count)
10094 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095{
10096 char_u *line;
10097 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 char_u wcopy[MAXWLEN + 2];
10099 char_u *p;
10100 int i;
10101 int c;
10102 suginfo_T sug;
10103 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010104 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010105 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010106 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010107 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010108 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010109 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010111 if (no_spell_checking(curwin))
10112 return;
10113
10114#ifdef FEAT_VISUAL
10115 if (VIsual_active)
10116 {
10117 /* Use the Visually selected text as the bad word. But reject
10118 * a multi-line selection. */
10119 if (curwin->w_cursor.lnum != VIsual.lnum)
10120 {
10121 vim_beep();
10122 return;
10123 }
10124 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10125 if (badlen < 0)
10126 badlen = -badlen;
10127 else
10128 curwin->w_cursor.col = VIsual.col;
10129 ++badlen;
10130 end_visual_mode();
10131 }
10132 else
10133#endif
10134 /* Find the start of the badly spelled word. */
10135 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010136 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010138 /* No bad word or it starts after the cursor: use the word under the
10139 * cursor. */
10140 curwin->w_cursor = prev_cursor;
10141 line = ml_get_curline();
10142 p = line + curwin->w_cursor.col;
10143 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010144 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010145 mb_ptr_back(line, p);
10146 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010147 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010148 mb_ptr_adv(p);
10149
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010150 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010151 {
10152 beep_flush();
10153 return;
10154 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010155 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 }
10157
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010158 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010160 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010161 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010162
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010163 /* Make a copy of current line since autocommands may free the line. */
10164 line = vim_strsave(ml_get_curline());
10165 if (line == NULL)
10166 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010167
Bram Moolenaar5195e452005-08-19 20:32:47 +000010168 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10169 * 'spellsuggest', whatever is smaller. */
10170 if (sps_limit > (int)Rows - 2)
10171 limit = (int)Rows - 2;
10172 else
10173 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010174 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010175 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176
10177 if (sug.su_ga.ga_len == 0)
10178 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010179 else if (count > 0)
10180 {
10181 if (count > sug.su_ga.ga_len)
10182 smsg((char_u *)_("Sorry, only %ld suggestions"),
10183 (long)sug.su_ga.ga_len);
10184 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 else
10186 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010187 vim_free(repl_from);
10188 repl_from = NULL;
10189 vim_free(repl_to);
10190 repl_to = NULL;
10191
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010192#ifdef FEAT_RIGHTLEFT
10193 /* When 'rightleft' is set the list is drawn right-left. */
10194 cmdmsg_rl = curwin->w_p_rl;
10195 if (cmdmsg_rl)
10196 msg_col = Columns - 1;
10197#endif
10198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010199 /* List the suggestions. */
10200 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010201 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010202 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10204 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010205#ifdef FEAT_RIGHTLEFT
10206 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10207 {
10208 /* And now the rabbit from the high hat: Avoid showing the
10209 * untranslated message rightleft. */
10210 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10211 sug.su_badlen, sug.su_badptr);
10212 }
10213#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 msg_puts(IObuff);
10215 msg_clr_eos();
10216 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 msg_scroll = TRUE;
10219 for (i = 0; i < sug.su_ga.ga_len; ++i)
10220 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010221 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222
10223 /* The suggested word may replace only part of the bad word, add
10224 * the not replaced part. */
10225 STRCPY(wcopy, stp->st_word);
10226 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010227 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 sug.su_badptr + stp->st_orglen,
10229 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010230 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10231#ifdef FEAT_RIGHTLEFT
10232 if (cmdmsg_rl)
10233 rl_mirror(IObuff);
10234#endif
10235 msg_puts(IObuff);
10236
10237 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010238 msg_puts(IObuff);
10239
10240 /* The word may replace more than "su_badlen". */
10241 if (sug.su_badlen < stp->st_orglen)
10242 {
10243 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10244 stp->st_orglen, sug.su_badptr);
10245 msg_puts(IObuff);
10246 }
10247
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010248 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010249 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010250 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010251 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010252 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010253 stp->st_salscore ? "s " : "",
10254 stp->st_score, stp->st_altscore);
10255 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010256 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010257 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010258#ifdef FEAT_RIGHTLEFT
10259 if (cmdmsg_rl)
10260 /* Mirror the numbers, but keep the leading space. */
10261 rl_mirror(IObuff + 1);
10262#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010263 msg_advance(30);
10264 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 msg_putchar('\n');
10267 }
10268
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010269#ifdef FEAT_RIGHTLEFT
10270 cmdmsg_rl = FALSE;
10271 msg_col = 0;
10272#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010273 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010274 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010275 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010276 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010277 lines_left = Rows; /* avoid more prompt */
10278 /* don't delay for 'smd' in normal_cmd() */
10279 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010280 }
10281
Bram Moolenaard12a1322005-08-21 22:08:24 +000010282 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10283 {
10284 /* Save the from and to text for :spellrepall. */
10285 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010286 if (sug.su_badlen > stp->st_orglen)
10287 {
10288 /* Replacing less than "su_badlen", append the remainder to
10289 * repl_to. */
10290 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10291 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10292 sug.su_badlen - stp->st_orglen,
10293 sug.su_badptr + stp->st_orglen);
10294 repl_to = vim_strsave(IObuff);
10295 }
10296 else
10297 {
10298 /* Replacing su_badlen or more, use the whole word. */
10299 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10300 repl_to = vim_strsave(stp->st_word);
10301 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010302
10303 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010304 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10305 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010306 if (p != NULL)
10307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010308 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010309 mch_memmove(p, line, c);
10310 STRCPY(p + c, stp->st_word);
10311 STRCAT(p, sug.su_badptr + stp->st_orglen);
10312 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10313 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010314
10315 /* For redo we use a change-word command. */
10316 ResetRedobuff();
10317 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010318 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010319 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010320 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010321
10322 /* After this "p" may be invalid. */
10323 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010324 }
10325 }
10326 else
10327 curwin->w_cursor = prev_cursor;
10328
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010329 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010330skip:
10331 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010332}
10333
10334/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010335 * Check if the word at line "lnum" column "col" is required to start with a
10336 * capital. This uses 'spellcapcheck' of the current buffer.
10337 */
10338 static int
10339check_need_cap(lnum, col)
10340 linenr_T lnum;
10341 colnr_T col;
10342{
10343 int need_cap = FALSE;
10344 char_u *line;
10345 char_u *line_copy = NULL;
10346 char_u *p;
10347 colnr_T endcol;
10348 regmatch_T regmatch;
10349
Bram Moolenaar860cae12010-06-05 23:22:07 +020010350 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010351 return FALSE;
10352
10353 line = ml_get_curline();
10354 endcol = 0;
10355 if ((int)(skipwhite(line) - line) >= (int)col)
10356 {
10357 /* At start of line, check if previous line is empty or sentence
10358 * ends there. */
10359 if (lnum == 1)
10360 need_cap = TRUE;
10361 else
10362 {
10363 line = ml_get(lnum - 1);
10364 if (*skipwhite(line) == NUL)
10365 need_cap = TRUE;
10366 else
10367 {
10368 /* Append a space in place of the line break. */
10369 line_copy = concat_str(line, (char_u *)" ");
10370 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010371 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010372 }
10373 }
10374 }
10375 else
10376 endcol = col;
10377
10378 if (endcol > 0)
10379 {
10380 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010381 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010382 regmatch.rm_ic = FALSE;
10383 p = line + endcol;
10384 for (;;)
10385 {
10386 mb_ptr_back(line, p);
10387 if (p == line || spell_iswordp_nmw(p))
10388 break;
10389 if (vim_regexec(&regmatch, p, 0)
10390 && regmatch.endp[0] == line + endcol)
10391 {
10392 need_cap = TRUE;
10393 break;
10394 }
10395 }
10396 }
10397
10398 vim_free(line_copy);
10399
10400 return need_cap;
10401}
10402
10403
10404/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010405 * ":spellrepall"
10406 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010407 void
10408ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010409 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010410{
10411 pos_T pos = curwin->w_cursor;
10412 char_u *frompat;
10413 int addlen;
10414 char_u *line;
10415 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010416 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010417 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010418
10419 if (repl_from == NULL || repl_to == NULL)
10420 {
10421 EMSG(_("E752: No previous spell replacement"));
10422 return;
10423 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010424 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010426 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010427 if (frompat == NULL)
10428 return;
10429 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10430 p_ws = FALSE;
10431
Bram Moolenaar5195e452005-08-19 20:32:47 +000010432 sub_nsubs = 0;
10433 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010434 curwin->w_cursor.lnum = 0;
10435 while (!got_int)
10436 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010437 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010438 || u_save_cursor() == FAIL)
10439 break;
10440
10441 /* Only replace when the right word isn't there yet. This happens
10442 * when changing "etc" to "etc.". */
10443 line = ml_get_curline();
10444 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10445 repl_to, STRLEN(repl_to)) != 0)
10446 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010447 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010448 if (p == NULL)
10449 break;
10450 mch_memmove(p, line, curwin->w_cursor.col);
10451 STRCPY(p + curwin->w_cursor.col, repl_to);
10452 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10453 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10454 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010455
10456 if (curwin->w_cursor.lnum != prev_lnum)
10457 {
10458 ++sub_nlines;
10459 prev_lnum = curwin->w_cursor.lnum;
10460 }
10461 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010462 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010463 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010464 }
10465
10466 p_ws = save_ws;
10467 curwin->w_cursor = pos;
10468 vim_free(frompat);
10469
Bram Moolenaar5195e452005-08-19 20:32:47 +000010470 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010471 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010472 else
10473 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010474}
10475
10476/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010477 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10478 * a list of allocated strings.
10479 */
10480 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010481spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010482 garray_T *gap;
10483 char_u *word;
10484 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010485 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010486 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010487{
10488 suginfo_T sug;
10489 int i;
10490 suggest_T *stp;
10491 char_u *wcopy;
10492
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010493 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010494
10495 /* Make room in "gap". */
10496 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010497 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010498 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010499 for (i = 0; i < sug.su_ga.ga_len; ++i)
10500 {
10501 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010502
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010503 /* The suggested word may replace only part of "word", add the not
10504 * replaced part. */
10505 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010506 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010507 if (wcopy == NULL)
10508 break;
10509 STRCPY(wcopy, stp->st_word);
10510 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10511 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10512 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010513 }
10514
10515 spell_find_cleanup(&sug);
10516}
10517
10518/*
10519 * Find spell suggestions for the word at the start of "badptr".
10520 * Return the suggestions in "su->su_ga".
10521 * The maximum number of suggestions is "maxcount".
10522 * Note: does use info for the current window.
10523 * This is based on the mechanisms of Aspell, but completely reimplemented.
10524 */
10525 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010526spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010527 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010528 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010529 suginfo_T *su;
10530 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010531 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010532 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010533 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010534{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010535 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010536 char_u buf[MAXPATHL];
10537 char_u *p;
10538 int do_combine = FALSE;
10539 char_u *sps_copy;
10540#ifdef FEAT_EVAL
10541 static int expr_busy = FALSE;
10542#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010543 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010544 int i;
10545 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010546
10547 /*
10548 * Set the info in "*su".
10549 */
10550 vim_memset(su, 0, sizeof(suginfo_T));
10551 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10552 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010553 if (*badptr == NUL)
10554 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010555 hash_init(&su->su_banned);
10556
10557 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010558 if (badlen != 0)
10559 su->su_badlen = badlen;
10560 else
10561 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010562 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010563 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010564
10565 if (su->su_badlen >= MAXWLEN)
10566 su->su_badlen = MAXWLEN - 1; /* just in case */
10567 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10568 (void)spell_casefold(su->su_badptr, su->su_badlen,
10569 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010570 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010571 su->su_badflags = badword_captype(su->su_badptr,
10572 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010573 if (need_cap)
10574 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010575
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010576 /* Find the default language for sound folding. We simply use the first
10577 * one in 'spelllang' that supports sound folding. That's good for when
10578 * using multiple files for one language, it's not that bad when mixing
10579 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010580 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010581 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010582 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010583 if (lp->lp_sallang != NULL)
10584 {
10585 su->su_sallang = lp->lp_sallang;
10586 break;
10587 }
10588 }
10589
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010590 /* Soundfold the bad word with the default sound folding, so that we don't
10591 * have to do this many times. */
10592 if (su->su_sallang != NULL)
10593 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10594 su->su_sal_badword);
10595
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010596 /* If the word is not capitalised and spell_check() doesn't consider the
10597 * word to be bad then it might need to be capitalised. Add a suggestion
10598 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010599 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010600 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010601 {
10602 make_case_word(su->su_badword, buf, WF_ONECAP);
10603 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010604 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010605 }
10606
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010607 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010608 if (banbadword)
10609 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010610
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010611 /* Make a copy of 'spellsuggest', because the expression may change it. */
10612 sps_copy = vim_strsave(p_sps);
10613 if (sps_copy == NULL)
10614 return;
10615
10616 /* Loop over the items in 'spellsuggest'. */
10617 for (p = sps_copy; *p != NUL; )
10618 {
10619 copy_option_part(&p, buf, MAXPATHL, ",");
10620
10621 if (STRNCMP(buf, "expr:", 5) == 0)
10622 {
10623#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010624 /* Evaluate an expression. Skip this when called recursively,
10625 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010626 if (!expr_busy)
10627 {
10628 expr_busy = TRUE;
10629 spell_suggest_expr(su, buf + 5);
10630 expr_busy = FALSE;
10631 }
10632#endif
10633 }
10634 else if (STRNCMP(buf, "file:", 5) == 0)
10635 /* Use list of suggestions in a file. */
10636 spell_suggest_file(su, buf + 5);
10637 else
10638 {
10639 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010640 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010641 if (sps_flags & SPS_DOUBLE)
10642 do_combine = TRUE;
10643 }
10644 }
10645
10646 vim_free(sps_copy);
10647
10648 if (do_combine)
10649 /* Combine the two list of suggestions. This must be done last,
10650 * because sorting changes the order again. */
10651 score_combine(su);
10652}
10653
10654#ifdef FEAT_EVAL
10655/*
10656 * Find suggestions by evaluating expression "expr".
10657 */
10658 static void
10659spell_suggest_expr(su, expr)
10660 suginfo_T *su;
10661 char_u *expr;
10662{
10663 list_T *list;
10664 listitem_T *li;
10665 int score;
10666 char_u *p;
10667
10668 /* The work is split up in a few parts to avoid having to export
10669 * suginfo_T.
10670 * First evaluate the expression and get the resulting list. */
10671 list = eval_spell_expr(su->su_badword, expr);
10672 if (list != NULL)
10673 {
10674 /* Loop over the items in the list. */
10675 for (li = list->lv_first; li != NULL; li = li->li_next)
10676 if (li->li_tv.v_type == VAR_LIST)
10677 {
10678 /* Get the word and the score from the items. */
10679 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010680 if (score >= 0 && score <= su->su_maxscore)
10681 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10682 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010683 }
10684 list_unref(list);
10685 }
10686
Bram Moolenaar4770d092006-01-12 23:22:24 +000010687 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10688 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010689 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10690}
10691#endif
10692
10693/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010694 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010695 */
10696 static void
10697spell_suggest_file(su, fname)
10698 suginfo_T *su;
10699 char_u *fname;
10700{
10701 FILE *fd;
10702 char_u line[MAXWLEN * 2];
10703 char_u *p;
10704 int len;
10705 char_u cword[MAXWLEN];
10706
10707 /* Open the file. */
10708 fd = mch_fopen((char *)fname, "r");
10709 if (fd == NULL)
10710 {
10711 EMSG2(_(e_notopen), fname);
10712 return;
10713 }
10714
10715 /* Read it line by line. */
10716 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10717 {
10718 line_breakcheck();
10719
10720 p = vim_strchr(line, '/');
10721 if (p == NULL)
10722 continue; /* No Tab found, just skip the line. */
10723 *p++ = NUL;
10724 if (STRICMP(su->su_badword, line) == 0)
10725 {
10726 /* Match! Isolate the good word, until CR or NL. */
10727 for (len = 0; p[len] >= ' '; ++len)
10728 ;
10729 p[len] = NUL;
10730
10731 /* If the suggestion doesn't have specific case duplicate the case
10732 * of the bad word. */
10733 if (captype(p, NULL) == 0)
10734 {
10735 make_case_word(p, cword, su->su_badflags);
10736 p = cword;
10737 }
10738
10739 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010740 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010741 }
10742 }
10743
10744 fclose(fd);
10745
Bram Moolenaar4770d092006-01-12 23:22:24 +000010746 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10747 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010748 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10749}
10750
10751/*
10752 * Find suggestions for the internal method indicated by "sps_flags".
10753 */
10754 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010755spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010756 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010757 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010758{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010759 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010760 * Load the .sug file(s) that are available and not done yet.
10761 */
10762 suggest_load_files();
10763
10764 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010765 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010766 *
10767 * Set a maximum score to limit the combination of operations that is
10768 * tried.
10769 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010770 suggest_try_special(su);
10771
10772 /*
10773 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10774 * from the .aff file and inserting a space (split the word).
10775 */
10776 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010777
10778 /* For the resulting top-scorers compute the sound-a-like score. */
10779 if (sps_flags & SPS_DOUBLE)
10780 score_comp_sal(su);
10781
10782 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010783 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010784 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010785 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010786 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010787 if (sps_flags & SPS_BEST)
10788 /* Adjust the word score for the suggestions found so far for how
10789 * they sounds like. */
10790 rescore_suggestions(su);
10791
10792 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010793 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010794 * for the soundfold word, limits the changes that are being tried,
10795 * and "su_sfmaxscore" the rescored score, which is set by
10796 * cleanup_suggestions().
10797 * First find words with a small edit distance, because this is much
10798 * faster and often already finds the top-N suggestions. If we didn't
10799 * find many suggestions try again with a higher edit distance.
10800 * "sl_sounddone" is used to avoid doing the same word twice.
10801 */
10802 suggest_try_soundalike_prep();
10803 su->su_maxscore = SCORE_SFMAX1;
10804 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010805 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010806 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10807 {
10808 /* We didn't find enough matches, try again, allowing more
10809 * changes to the soundfold word. */
10810 su->su_maxscore = SCORE_SFMAX2;
10811 suggest_try_soundalike(su);
10812 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10813 {
10814 /* Still didn't find enough matches, try again, allowing even
10815 * more changes to the soundfold word. */
10816 su->su_maxscore = SCORE_SFMAX3;
10817 suggest_try_soundalike(su);
10818 }
10819 }
10820 su->su_maxscore = su->su_sfmaxscore;
10821 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010822 }
10823
Bram Moolenaar4770d092006-01-12 23:22:24 +000010824 /* When CTRL-C was hit while searching do show the results. Only clear
10825 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010826 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010827 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010828 {
10829 (void)vgetc();
10830 got_int = FALSE;
10831 }
10832
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010833 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010834 {
10835 if (sps_flags & SPS_BEST)
10836 /* Adjust the word score for how it sounds like. */
10837 rescore_suggestions(su);
10838
Bram Moolenaar4770d092006-01-12 23:22:24 +000010839 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10840 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010841 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010842 }
10843}
10844
10845/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010846 * Load the .sug files for languages that have one and weren't loaded yet.
10847 */
10848 static void
10849suggest_load_files()
10850{
10851 langp_T *lp;
10852 int lpi;
10853 slang_T *slang;
10854 char_u *dotp;
10855 FILE *fd;
10856 char_u buf[MAXWLEN];
10857 int i;
10858 time_t timestamp;
10859 int wcount;
10860 int wordnr;
10861 garray_T ga;
10862 int c;
10863
10864 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010865 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010866 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010867 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010868 slang = lp->lp_slang;
10869 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10870 {
10871 /* Change ".spl" to ".sug" and open the file. When the file isn't
10872 * found silently skip it. Do set "sl_sugloaded" so that we
10873 * don't try again and again. */
10874 slang->sl_sugloaded = TRUE;
10875
10876 dotp = vim_strrchr(slang->sl_fname, '.');
10877 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10878 continue;
10879 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010880 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010881 if (fd == NULL)
10882 goto nextone;
10883
10884 /*
10885 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10886 */
10887 for (i = 0; i < VIMSUGMAGICL; ++i)
10888 buf[i] = getc(fd); /* <fileID> */
10889 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10890 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010891 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010892 slang->sl_fname);
10893 goto nextone;
10894 }
10895 c = getc(fd); /* <versionnr> */
10896 if (c < VIMSUGVERSION)
10897 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010898 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010899 slang->sl_fname);
10900 goto nextone;
10901 }
10902 else if (c > VIMSUGVERSION)
10903 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010904 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010905 slang->sl_fname);
10906 goto nextone;
10907 }
10908
10909 /* Check the timestamp, it must be exactly the same as the one in
10910 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010911 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010912 if (timestamp != slang->sl_sugtime)
10913 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010914 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010915 slang->sl_fname);
10916 goto nextone;
10917 }
10918
10919 /*
10920 * <SUGWORDTREE>: <wordtree>
10921 * Read the trie with the soundfolded words.
10922 */
10923 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10924 FALSE, 0) != 0)
10925 {
10926someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010927 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010928 slang->sl_fname);
10929 slang_clear_sug(slang);
10930 goto nextone;
10931 }
10932
10933 /*
10934 * <SUGTABLE>: <sugwcount> <sugline> ...
10935 *
10936 * Read the table with word numbers. We use a file buffer for
10937 * this, because it's so much like a file with lines. Makes it
10938 * possible to swap the info and save on memory use.
10939 */
10940 slang->sl_sugbuf = open_spellbuf();
10941 if (slang->sl_sugbuf == NULL)
10942 goto someerror;
10943 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010944 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010945 if (wcount < 0)
10946 goto someerror;
10947
10948 /* Read all the wordnr lists into the buffer, one NUL terminated
10949 * list per line. */
10950 ga_init2(&ga, 1, 100);
10951 for (wordnr = 0; wordnr < wcount; ++wordnr)
10952 {
10953 ga.ga_len = 0;
10954 for (;;)
10955 {
10956 c = getc(fd); /* <sugline> */
10957 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10958 goto someerror;
10959 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10960 if (c == NUL)
10961 break;
10962 }
10963 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10964 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10965 goto someerror;
10966 }
10967 ga_clear(&ga);
10968
10969 /*
10970 * Need to put word counts in the word tries, so that we can find
10971 * a word by its number.
10972 */
10973 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10974 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10975
10976nextone:
10977 if (fd != NULL)
10978 fclose(fd);
10979 STRCPY(dotp, ".spl");
10980 }
10981 }
10982}
10983
10984
10985/*
10986 * Fill in the wordcount fields for a trie.
10987 * Returns the total number of words.
10988 */
10989 static void
10990tree_count_words(byts, idxs)
10991 char_u *byts;
10992 idx_T *idxs;
10993{
10994 int depth;
10995 idx_T arridx[MAXWLEN];
10996 int curi[MAXWLEN];
10997 int c;
10998 idx_T n;
10999 int wordcount[MAXWLEN];
11000
11001 arridx[0] = 0;
11002 curi[0] = 1;
11003 wordcount[0] = 0;
11004 depth = 0;
11005 while (depth >= 0 && !got_int)
11006 {
11007 if (curi[depth] > byts[arridx[depth]])
11008 {
11009 /* Done all bytes at this node, go up one level. */
11010 idxs[arridx[depth]] = wordcount[depth];
11011 if (depth > 0)
11012 wordcount[depth - 1] += wordcount[depth];
11013
11014 --depth;
11015 fast_breakcheck();
11016 }
11017 else
11018 {
11019 /* Do one more byte at this node. */
11020 n = arridx[depth] + curi[depth];
11021 ++curi[depth];
11022
11023 c = byts[n];
11024 if (c == 0)
11025 {
11026 /* End of word, count it. */
11027 ++wordcount[depth];
11028
11029 /* Skip over any other NUL bytes (same word with different
11030 * flags). */
11031 while (byts[n + 1] == 0)
11032 {
11033 ++n;
11034 ++curi[depth];
11035 }
11036 }
11037 else
11038 {
11039 /* Normal char, go one level deeper to count the words. */
11040 ++depth;
11041 arridx[depth] = idxs[n];
11042 curi[depth] = 1;
11043 wordcount[depth] = 0;
11044 }
11045 }
11046 }
11047}
11048
11049/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011050 * Free the info put in "*su" by spell_find_suggest().
11051 */
11052 static void
11053spell_find_cleanup(su)
11054 suginfo_T *su;
11055{
11056 int i;
11057
11058 /* Free the suggestions. */
11059 for (i = 0; i < su->su_ga.ga_len; ++i)
11060 vim_free(SUG(su->su_ga, i).st_word);
11061 ga_clear(&su->su_ga);
11062 for (i = 0; i < su->su_sga.ga_len; ++i)
11063 vim_free(SUG(su->su_sga, i).st_word);
11064 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065
11066 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011067 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068}
11069
11070/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011071 * Make a copy of "word", with the first letter upper or lower cased, to
11072 * "wcopy[MAXWLEN]". "word" must not be empty.
11073 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 */
11075 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011076onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 char_u *wcopy;
11079 int upper; /* TRUE: first letter made upper case */
11080{
11081 char_u *p;
11082 int c;
11083 int l;
11084
11085 p = word;
11086#ifdef FEAT_MBYTE
11087 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011088 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 else
11090#endif
11091 c = *p++;
11092 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011093 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011095 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096#ifdef FEAT_MBYTE
11097 if (has_mbyte)
11098 l = mb_char2bytes(c, wcopy);
11099 else
11100#endif
11101 {
11102 l = 1;
11103 wcopy[0] = c;
11104 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011105 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106}
11107
11108/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011109 * Make a copy of "word" with all the letters upper cased into
11110 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 */
11112 static void
11113allcap_copy(word, wcopy)
11114 char_u *word;
11115 char_u *wcopy;
11116{
11117 char_u *s;
11118 char_u *d;
11119 int c;
11120
11121 d = wcopy;
11122 for (s = word; *s != NUL; )
11123 {
11124#ifdef FEAT_MBYTE
11125 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011126 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 else
11128#endif
11129 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011130
11131#ifdef FEAT_MBYTE
11132 /* We only change ß to SS when we are certain latin1 is used. It
11133 * would cause weird errors in other 8-bit encodings. */
11134 if (enc_latin1like && c == 0xdf)
11135 {
11136 c = 'S';
11137 if (d - wcopy >= MAXWLEN - 1)
11138 break;
11139 *d++ = c;
11140 }
11141 else
11142#endif
11143 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011144
11145#ifdef FEAT_MBYTE
11146 if (has_mbyte)
11147 {
11148 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11149 break;
11150 d += mb_char2bytes(c, d);
11151 }
11152 else
11153#endif
11154 {
11155 if (d - wcopy >= MAXWLEN - 1)
11156 break;
11157 *d++ = c;
11158 }
11159 }
11160 *d = NUL;
11161}
11162
11163/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011164 * Try finding suggestions by recognizing specific situations.
11165 */
11166 static void
11167suggest_try_special(su)
11168 suginfo_T *su;
11169{
11170 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011171 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011172 int c;
11173 char_u word[MAXWLEN];
11174
11175 /*
11176 * Recognize a word that is repeated: "the the".
11177 */
11178 p = skiptowhite(su->su_fbadword);
11179 len = p - su->su_fbadword;
11180 p = skipwhite(p);
11181 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11182 {
11183 /* Include badflags: if the badword is onecap or allcap
11184 * use that for the goodword too: "The the" -> "The". */
11185 c = su->su_fbadword[len];
11186 su->su_fbadword[len] = NUL;
11187 make_case_word(su->su_fbadword, word, su->su_badflags);
11188 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011189
11190 /* Give a soundalike score of 0, compute the score as if deleting one
11191 * character. */
11192 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011193 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011194 }
11195}
11196
11197/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 * Try finding suggestions by adding/removing/swapping letters.
11199 */
11200 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011201suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 suginfo_T *su;
11203{
11204 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011205 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011207 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011208 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209
11210 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011211 * to find matches (esp. REP items). Append some more text, changing
11212 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011214 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011215 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011216 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217
Bram Moolenaar860cae12010-06-05 23:22:07 +020011218 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011219 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011220 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011221
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011222 /* If reloading a spell file fails it's still in the list but
11223 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011224 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011225 continue;
11226
Bram Moolenaar4770d092006-01-12 23:22:24 +000011227 /* Try it for this language. Will add possible suggestions. */
11228 suggest_trie_walk(su, lp, fword, FALSE);
11229 }
11230}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231
Bram Moolenaar4770d092006-01-12 23:22:24 +000011232/* Check the maximum score, if we go over it we won't try this change. */
11233#define TRY_DEEPER(su, stack, depth, add) \
11234 (stack[depth].ts_score + (add) < su->su_maxscore)
11235
11236/*
11237 * Try finding suggestions by adding/removing/swapping letters.
11238 *
11239 * This uses a state machine. At each node in the tree we try various
11240 * operations. When trying if an operation works "depth" is increased and the
11241 * stack[] is used to store info. This allows combinations, thus insert one
11242 * character, replace one and delete another. The number of changes is
11243 * limited by su->su_maxscore.
11244 *
11245 * After implementing this I noticed an article by Kemal Oflazer that
11246 * describes something similar: "Error-tolerant Finite State Recognition with
11247 * Applications to Morphological Analysis and Spelling Correction" (1996).
11248 * The implementation in the article is simplified and requires a stack of
11249 * unknown depth. The implementation here only needs a stack depth equal to
11250 * the length of the word.
11251 *
11252 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11253 * The mechanism is the same, but we find a match with a sound-folded word
11254 * that comes from one or more original words. Each of these words may be
11255 * added, this is done by add_sound_suggest().
11256 * Don't use:
11257 * the prefix tree or the keep-case tree
11258 * "su->su_badlen"
11259 * anything to do with upper and lower case
11260 * anything to do with word or non-word characters ("spell_iswordp()")
11261 * banned words
11262 * word flags (rare, region, compounding)
11263 * word splitting for now
11264 * "similar_chars()"
11265 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11266 */
11267 static void
11268suggest_trie_walk(su, lp, fword, soundfold)
11269 suginfo_T *su;
11270 langp_T *lp;
11271 char_u *fword;
11272 int soundfold;
11273{
11274 char_u tword[MAXWLEN]; /* good word collected so far */
11275 trystate_T stack[MAXWLEN];
11276 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011277 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011278 * words and split word. NUL terminated
11279 * when going deeper but not when coming
11280 * back. */
11281 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11282 trystate_T *sp;
11283 int newscore;
11284 int score;
11285 char_u *byts, *fbyts, *pbyts;
11286 idx_T *idxs, *fidxs, *pidxs;
11287 int depth;
11288 int c, c2, c3;
11289 int n = 0;
11290 int flags;
11291 garray_T *gap;
11292 idx_T arridx;
11293 int len;
11294 char_u *p;
11295 fromto_T *ftp;
11296 int fl = 0, tl;
11297 int repextra = 0; /* extra bytes in fword[] from REP item */
11298 slang_T *slang = lp->lp_slang;
11299 int fword_ends;
11300 int goodword_ends;
11301#ifdef DEBUG_TRIEWALK
11302 /* Stores the name of the change made at each level. */
11303 char_u changename[MAXWLEN][80];
11304#endif
11305 int breakcheckcount = 1000;
11306 int compound_ok;
11307
11308 /*
11309 * Go through the whole case-fold tree, try changes at each node.
11310 * "tword[]" contains the word collected from nodes in the tree.
11311 * "fword[]" the word we are trying to match with (initially the bad
11312 * word).
11313 */
11314 depth = 0;
11315 sp = &stack[0];
11316 vim_memset(sp, 0, sizeof(trystate_T));
11317 sp->ts_curi = 1;
11318
11319 if (soundfold)
11320 {
11321 /* Going through the soundfold tree. */
11322 byts = fbyts = slang->sl_sbyts;
11323 idxs = fidxs = slang->sl_sidxs;
11324 pbyts = NULL;
11325 pidxs = NULL;
11326 sp->ts_prefixdepth = PFD_NOPREFIX;
11327 sp->ts_state = STATE_START;
11328 }
11329 else
11330 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011331 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011332 * When there are postponed prefixes we need to use these first. At
11333 * the end of the prefix we continue in the case-fold tree.
11334 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011335 fbyts = slang->sl_fbyts;
11336 fidxs = slang->sl_fidxs;
11337 pbyts = slang->sl_pbyts;
11338 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011339 if (pbyts != NULL)
11340 {
11341 byts = pbyts;
11342 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011343 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011344 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11345 }
11346 else
11347 {
11348 byts = fbyts;
11349 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011350 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011351 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011352 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011353 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011354
Bram Moolenaar4770d092006-01-12 23:22:24 +000011355 /*
11356 * Loop to find all suggestions. At each round we either:
11357 * - For the current state try one operation, advance "ts_curi",
11358 * increase "depth".
11359 * - When a state is done go to the next, set "ts_state".
11360 * - When all states are tried decrease "depth".
11361 */
11362 while (depth >= 0 && !got_int)
11363 {
11364 sp = &stack[depth];
11365 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011367 case STATE_START:
11368 case STATE_NOPREFIX:
11369 /*
11370 * Start of node: Deal with NUL bytes, which means
11371 * tword[] may end here.
11372 */
11373 arridx = sp->ts_arridx; /* current node in the tree */
11374 len = byts[arridx]; /* bytes in this node */
11375 arridx += sp->ts_curi; /* index of current byte */
11376
11377 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 /* Skip over the NUL bytes, we use them later. */
11380 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11381 ;
11382 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383
Bram Moolenaar4770d092006-01-12 23:22:24 +000011384 /* Always past NUL bytes now. */
11385 n = (int)sp->ts_state;
11386 sp->ts_state = STATE_ENDNUL;
11387 sp->ts_save_badflags = su->su_badflags;
11388
11389 /* At end of a prefix or at start of prefixtree: check for
11390 * following word. */
11391 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011392 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011393 /* Set su->su_badflags to the caps type at this position.
11394 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011395#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011396 if (has_mbyte)
11397 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11398 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011399#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011400 n = sp->ts_fidx;
11401 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11402 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011403 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011404#ifdef DEBUG_TRIEWALK
11405 sprintf(changename[depth], "prefix");
11406#endif
11407 go_deeper(stack, depth, 0);
11408 ++depth;
11409 sp = &stack[depth];
11410 sp->ts_prefixdepth = depth - 1;
11411 byts = fbyts;
11412 idxs = fidxs;
11413 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011414
Bram Moolenaar4770d092006-01-12 23:22:24 +000011415 /* Move the prefix to preword[] with the right case
11416 * and make find_keepcap_word() works. */
11417 tword[sp->ts_twordlen] = NUL;
11418 make_case_word(tword + sp->ts_splitoff,
11419 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011420 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011421 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011422 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011423 break;
11424 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011425
Bram Moolenaar4770d092006-01-12 23:22:24 +000011426 if (sp->ts_curi > len || byts[arridx] != 0)
11427 {
11428 /* Past bytes in node and/or past NUL bytes. */
11429 sp->ts_state = STATE_ENDNUL;
11430 sp->ts_save_badflags = su->su_badflags;
11431 break;
11432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433
Bram Moolenaar4770d092006-01-12 23:22:24 +000011434 /*
11435 * End of word in tree.
11436 */
11437 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438
Bram Moolenaar4770d092006-01-12 23:22:24 +000011439 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011440
11441 /* Skip words with the NOSUGGEST flag. */
11442 if (flags & WF_NOSUGGEST)
11443 break;
11444
Bram Moolenaar4770d092006-01-12 23:22:24 +000011445 fword_ends = (fword[sp->ts_fidx] == NUL
11446 || (soundfold
11447 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011448 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011449 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450
Bram Moolenaar4770d092006-01-12 23:22:24 +000011451 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011452 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011453 {
11454 /* There was a prefix before the word. Check that the prefix
11455 * can be used with this word. */
11456 /* Count the length of the NULs in the prefix. If there are
11457 * none this must be the first try without a prefix. */
11458 n = stack[sp->ts_prefixdepth].ts_arridx;
11459 len = pbyts[n++];
11460 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11461 ;
11462 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011463 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011464 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011465 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011466 if (c == 0)
11467 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011468
Bram Moolenaar4770d092006-01-12 23:22:24 +000011469 /* Use the WF_RARE flag for a rare prefix. */
11470 if (c & WF_RAREPFX)
11471 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011472
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473 /* Tricky: when checking for both prefix and compounding
11474 * we run into the prefix flag first.
11475 * Remember that it's OK, so that we accept the prefix
11476 * when arriving at a compound flag. */
11477 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011478 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011479 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011480
Bram Moolenaar4770d092006-01-12 23:22:24 +000011481 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11482 * appending another compound word below. */
11483 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011484 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011485 goodword_ends = FALSE;
11486 else
11487 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011488
Bram Moolenaar4770d092006-01-12 23:22:24 +000011489 p = NULL;
11490 compound_ok = TRUE;
11491 if (sp->ts_complen > sp->ts_compsplit)
11492 {
11493 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011494 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011495 /* There was a word before this word. When there was no
11496 * change in this word (it was correct) add the first word
11497 * as a suggestion. If this word was corrected too, we
11498 * need to check if a correct word follows. */
11499 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011500 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011501 && STRNCMP(fword + sp->ts_splitfidx,
11502 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011503 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 {
11505 preword[sp->ts_prewordlen] = NUL;
11506 newscore = score_wordcount_adj(slang, sp->ts_score,
11507 preword + sp->ts_prewordlen,
11508 sp->ts_prewordlen > 0);
11509 /* Add the suggestion if the score isn't too bad. */
11510 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011511 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011512 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011513 newscore, 0, FALSE,
11514 lp->lp_sallang, FALSE);
11515 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011516 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011517 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011518 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011519 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011520 /* There was a compound word before this word. If this
11521 * word does not support compounding then give up
11522 * (splitting is tried for the word without compound
11523 * flag). */
11524 if (((unsigned)flags >> 24) == 0
11525 || sp->ts_twordlen - sp->ts_splitoff
11526 < slang->sl_compminlen)
11527 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011528#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011529 /* For multi-byte chars check character length against
11530 * COMPOUNDMIN. */
11531 if (has_mbyte
11532 && slang->sl_compminlen > 0
11533 && mb_charlen(tword + sp->ts_splitoff)
11534 < slang->sl_compminlen)
11535 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011536#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011537
Bram Moolenaar4770d092006-01-12 23:22:24 +000011538 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11539 compflags[sp->ts_complen + 1] = NUL;
11540 vim_strncpy(preword + sp->ts_prewordlen,
11541 tword + sp->ts_splitoff,
11542 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011543
11544 /* Verify CHECKCOMPOUNDPATTERN rules. */
11545 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11546 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011547 compound_ok = FALSE;
11548
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011549 if (compound_ok)
11550 {
11551 p = preword;
11552 while (*skiptowhite(p) != NUL)
11553 p = skipwhite(skiptowhite(p));
11554 if (fword_ends && !can_compound(slang, p,
11555 compflags + sp->ts_compsplit))
11556 /* Compound is not allowed. But it may still be
11557 * possible if we add another (short) word. */
11558 compound_ok = FALSE;
11559 }
11560
Bram Moolenaar4770d092006-01-12 23:22:24 +000011561 /* Get pointer to last char of previous word. */
11562 p = preword + sp->ts_prewordlen;
11563 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011564 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011565 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566
Bram Moolenaar4770d092006-01-12 23:22:24 +000011567 /*
11568 * Form the word with proper case in preword.
11569 * If there is a word from a previous split, append.
11570 * For the soundfold tree don't change the case, simply append.
11571 */
11572 if (soundfold)
11573 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11574 else if (flags & WF_KEEPCAP)
11575 /* Must find the word in the keep-case tree. */
11576 find_keepcap_word(slang, tword + sp->ts_splitoff,
11577 preword + sp->ts_prewordlen);
11578 else
11579 {
11580 /* Include badflags: If the badword is onecap or allcap
11581 * use that for the goodword too. But if the badword is
11582 * allcap and it's only one char long use onecap. */
11583 c = su->su_badflags;
11584 if ((c & WF_ALLCAP)
11585#ifdef FEAT_MBYTE
11586 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11587#else
11588 && su->su_badlen == 1
11589#endif
11590 )
11591 c = WF_ONECAP;
11592 c |= flags;
11593
11594 /* When appending a compound word after a word character don't
11595 * use Onecap. */
11596 if (p != NULL && spell_iswordp_nmw(p))
11597 c &= ~WF_ONECAP;
11598 make_case_word(tword + sp->ts_splitoff,
11599 preword + sp->ts_prewordlen, c);
11600 }
11601
11602 if (!soundfold)
11603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 /* Don't use a banned word. It may appear again as a good
11605 * word, thus remember it. */
11606 if (flags & WF_BANNED)
11607 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011608 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 break;
11610 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011611 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011612 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11613 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011614 {
11615 if (slang->sl_compprog == NULL)
11616 break;
11617 /* the word so far was banned but we may try compounding */
11618 goodword_ends = FALSE;
11619 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621
Bram Moolenaar4770d092006-01-12 23:22:24 +000011622 newscore = 0;
11623 if (!soundfold) /* soundfold words don't have flags */
11624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011626 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011627 newscore += SCORE_REGION;
11628 if (flags & WF_RARE)
11629 newscore += SCORE_RARE;
11630
Bram Moolenaar0c405862005-06-22 22:26:26 +000011631 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011632 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635
Bram Moolenaar4770d092006-01-12 23:22:24 +000011636 /* TODO: how about splitting in the soundfold tree? */
11637 if (fword_ends
11638 && goodword_ends
11639 && sp->ts_fidx >= sp->ts_fidxtry
11640 && compound_ok)
11641 {
11642 /* The badword also ends: add suggestions. */
11643#ifdef DEBUG_TRIEWALK
11644 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011645 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011646 int j;
11647
11648 /* print the stack of changes that brought us here */
11649 smsg("------ %s -------", fword);
11650 for (j = 0; j < depth; ++j)
11651 smsg("%s", changename[j]);
11652 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011653#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011654 if (soundfold)
11655 {
11656 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011657 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011658 add_sound_suggest(su, preword, sp->ts_score, lp);
11659 }
11660 else
11661 {
11662 /* Give a penalty when changing non-word char to word
11663 * char, e.g., "thes," -> "these". */
11664 p = fword + sp->ts_fidx;
11665 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011666 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011667 {
11668 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011669 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011670 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011671 newscore += SCORE_NONWORD;
11672 }
11673
Bram Moolenaar4770d092006-01-12 23:22:24 +000011674 /* Give a bonus to words seen before. */
11675 score = score_wordcount_adj(slang,
11676 sp->ts_score + newscore,
11677 preword + sp->ts_prewordlen,
11678 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011679
Bram Moolenaar4770d092006-01-12 23:22:24 +000011680 /* Add the suggestion if the score isn't too bad. */
11681 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011682 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011683 add_suggestion(su, &su->su_ga, preword,
11684 sp->ts_fidx - repextra,
11685 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011686
11687 if (su->su_badflags & WF_MIXCAP)
11688 {
11689 /* We really don't know if the word should be
11690 * upper or lower case, add both. */
11691 c = captype(preword, NULL);
11692 if (c == 0 || c == WF_ALLCAP)
11693 {
11694 make_case_word(tword + sp->ts_splitoff,
11695 preword + sp->ts_prewordlen,
11696 c == 0 ? WF_ALLCAP : 0);
11697
11698 add_suggestion(su, &su->su_ga, preword,
11699 sp->ts_fidx - repextra,
11700 score + SCORE_ICASE, 0, FALSE,
11701 lp->lp_sallang, FALSE);
11702 }
11703 }
11704 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011706 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011707
Bram Moolenaar4770d092006-01-12 23:22:24 +000011708 /*
11709 * Try word split and/or compounding.
11710 */
11711 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011712#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011713 /* Don't split halfway a character. */
11714 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011715#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011716 )
11717 {
11718 int try_compound;
11719 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011720
Bram Moolenaar4770d092006-01-12 23:22:24 +000011721 /* If past the end of the bad word don't try a split.
11722 * Otherwise try changing the next word. E.g., find
11723 * suggestions for "the the" where the second "the" is
11724 * different. It's done like a split.
11725 * TODO: word split for soundfold words */
11726 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11727 && !soundfold;
11728
11729 /* Get here in several situations:
11730 * 1. The word in the tree ends:
11731 * If the word allows compounding try that. Otherwise try
11732 * a split by inserting a space. For both check that a
11733 * valid words starts at fword[sp->ts_fidx].
11734 * For NOBREAK do like compounding to be able to check if
11735 * the next word is valid.
11736 * 2. The badword does end, but it was due to a change (e.g.,
11737 * a swap). No need to split, but do check that the
11738 * following word is valid.
11739 * 3. The badword and the word in the tree end. It may still
11740 * be possible to compound another (short) word.
11741 */
11742 try_compound = FALSE;
11743 if (!soundfold
11744 && slang->sl_compprog != NULL
11745 && ((unsigned)flags >> 24) != 0
11746 && sp->ts_twordlen - sp->ts_splitoff
11747 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011748#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011749 && (!has_mbyte
11750 || slang->sl_compminlen == 0
11751 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011752 >= slang->sl_compminlen)
11753#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011754 && (slang->sl_compsylmax < MAXWLEN
11755 || sp->ts_complen + 1 - sp->ts_compsplit
11756 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011757 && (can_be_compound(sp, slang,
11758 compflags, ((unsigned)flags >> 24))))
11759
Bram Moolenaar4770d092006-01-12 23:22:24 +000011760 {
11761 try_compound = TRUE;
11762 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11763 compflags[sp->ts_complen + 1] = NUL;
11764 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011765
Bram Moolenaar4770d092006-01-12 23:22:24 +000011766 /* For NOBREAK we never try splitting, it won't make any word
11767 * valid. */
11768 if (slang->sl_nobreak)
11769 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011770
Bram Moolenaar4770d092006-01-12 23:22:24 +000011771 /* If we could add a compound word, and it's also possible to
11772 * split at this point, do the split first and set
11773 * TSF_DIDSPLIT to avoid doing it again. */
11774 else if (!fword_ends
11775 && try_compound
11776 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11777 {
11778 try_compound = FALSE;
11779 sp->ts_flags |= TSF_DIDSPLIT;
11780 --sp->ts_curi; /* do the same NUL again */
11781 compflags[sp->ts_complen] = NUL;
11782 }
11783 else
11784 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011785
Bram Moolenaar4770d092006-01-12 23:22:24 +000011786 if (try_split || try_compound)
11787 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011788 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011789 {
11790 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011791 * words so far are valid for compounding. If there
11792 * is only one word it must not have the NEEDCOMPOUND
11793 * flag. */
11794 if (sp->ts_complen == sp->ts_compsplit
11795 && (flags & WF_NEEDCOMP))
11796 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011797 p = preword;
11798 while (*skiptowhite(p) != NUL)
11799 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011800 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011801 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011802 compflags + sp->ts_compsplit))
11803 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011804
11805 if (slang->sl_nosplitsugs)
11806 newscore += SCORE_SPLIT_NO;
11807 else
11808 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011809
11810 /* Give a bonus to words seen before. */
11811 newscore = score_wordcount_adj(slang, newscore,
11812 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011813 }
11814
Bram Moolenaar4770d092006-01-12 23:22:24 +000011815 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011817 go_deeper(stack, depth, newscore);
11818#ifdef DEBUG_TRIEWALK
11819 if (!try_compound && !fword_ends)
11820 sprintf(changename[depth], "%.*s-%s: split",
11821 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11822 else
11823 sprintf(changename[depth], "%.*s-%s: compound",
11824 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011827 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011828 sp->ts_state = STATE_SPLITUNDO;
11829
11830 ++depth;
11831 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011833 /* Append a space to preword when splitting. */
11834 if (!try_compound && !fword_ends)
11835 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011836 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011837 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011838 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011839
11840 /* If the badword has a non-word character at this
11841 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011842 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011843 * character when the word ends. But only when the
11844 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011845 if (((!try_compound && !spell_iswordp_nmw(fword
11846 + sp->ts_fidx))
11847 || fword_ends)
11848 && fword[sp->ts_fidx] != NUL
11849 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011850 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011851 int l;
11852
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011853#ifdef FEAT_MBYTE
11854 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011855 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011856 else
11857#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011858 l = 1;
11859 if (fword_ends)
11860 {
11861 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011862 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011863 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011864 sp->ts_prewordlen += l;
11865 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011866 }
11867 else
11868 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11869 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011870 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011871
Bram Moolenaard12a1322005-08-21 22:08:24 +000011872 /* When compounding include compound flag in
11873 * compflags[] (already set above). When splitting we
11874 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011875 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011876 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011877 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011878 sp->ts_compsplit = sp->ts_complen;
11879 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011880
Bram Moolenaar53805d12005-08-01 07:08:33 +000011881 /* set su->su_badflags to the caps type at this
11882 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011883#ifdef FEAT_MBYTE
11884 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011885 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011886 else
11887#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011888 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011889 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011890 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011893 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011894
11895 /* If there are postponed prefixes, try these too. */
11896 if (pbyts != NULL)
11897 {
11898 byts = pbyts;
11899 idxs = pidxs;
11900 sp->ts_prefixdepth = PFD_PREFIXTREE;
11901 sp->ts_state = STATE_NOPREFIX;
11902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 }
11904 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011905 }
11906 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907
Bram Moolenaar4770d092006-01-12 23:22:24 +000011908 case STATE_SPLITUNDO:
11909 /* Undo the changes done for word split or compound word. */
11910 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011911
Bram Moolenaar4770d092006-01-12 23:22:24 +000011912 /* Continue looking for NUL bytes. */
11913 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011914
Bram Moolenaar4770d092006-01-12 23:22:24 +000011915 /* In case we went into the prefix tree. */
11916 byts = fbyts;
11917 idxs = fidxs;
11918 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011919
Bram Moolenaar4770d092006-01-12 23:22:24 +000011920 case STATE_ENDNUL:
11921 /* Past the NUL bytes in the node. */
11922 su->su_badflags = sp->ts_save_badflags;
11923 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011924#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011925 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011926#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011927 )
11928 {
11929 /* The badword ends, can't use STATE_PLAIN. */
11930 sp->ts_state = STATE_DEL;
11931 break;
11932 }
11933 sp->ts_state = STATE_PLAIN;
11934 /*FALLTHROUGH*/
11935
11936 case STATE_PLAIN:
11937 /*
11938 * Go over all possible bytes at this node, add each to tword[]
11939 * and use child node. "ts_curi" is the index.
11940 */
11941 arridx = sp->ts_arridx;
11942 if (sp->ts_curi > byts[arridx])
11943 {
11944 /* Done all bytes at this node, do next state. When still at
11945 * already changed bytes skip the other tricks. */
11946 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011949 sp->ts_state = STATE_FINAL;
11950 }
11951 else
11952 {
11953 arridx += sp->ts_curi++;
11954 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955
Bram Moolenaar4770d092006-01-12 23:22:24 +000011956 /* Normal byte, go one level deeper. If it's not equal to the
11957 * byte in the bad word adjust the score. But don't even try
11958 * when the byte was already changed. And don't try when we
11959 * just deleted this byte, accepting it is always cheaper then
11960 * delete + substitute. */
11961 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011962#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011963 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011964#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011965 )
11966 newscore = 0;
11967 else
11968 newscore = SCORE_SUBST;
11969 if ((newscore == 0
11970 || (sp->ts_fidx >= sp->ts_fidxtry
11971 && ((sp->ts_flags & TSF_DIDDEL) == 0
11972 || c != fword[sp->ts_delidx])))
11973 && TRY_DEEPER(su, stack, depth, newscore))
11974 {
11975 go_deeper(stack, depth, newscore);
11976#ifdef DEBUG_TRIEWALK
11977 if (newscore > 0)
11978 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11979 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11980 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011982 sprintf(changename[depth], "%.*s-%s: accept %c",
11983 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11984 fword[sp->ts_fidx]);
11985#endif
11986 ++depth;
11987 sp = &stack[depth];
11988 ++sp->ts_fidx;
11989 tword[sp->ts_twordlen++] = c;
11990 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011991#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011992 if (newscore == SCORE_SUBST)
11993 sp->ts_isdiff = DIFF_YES;
11994 if (has_mbyte)
11995 {
11996 /* Multi-byte characters are a bit complicated to
11997 * handle: They differ when any of the bytes differ
11998 * and then their length may also differ. */
11999 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012000 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012001 /* First byte. */
12002 sp->ts_tcharidx = 0;
12003 sp->ts_tcharlen = MB_BYTE2LEN(c);
12004 sp->ts_fcharstart = sp->ts_fidx - 1;
12005 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012006 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012007 }
12008 else if (sp->ts_isdiff == DIFF_INSERT)
12009 /* When inserting trail bytes don't advance in the
12010 * bad word. */
12011 --sp->ts_fidx;
12012 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12013 {
12014 /* Last byte of character. */
12015 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012016 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012017 /* Correct ts_fidx for the byte length of the
12018 * character (we didn't check that before). */
12019 sp->ts_fidx = sp->ts_fcharstart
12020 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012021 fword[sp->ts_fcharstart]);
12022
Bram Moolenaar4770d092006-01-12 23:22:24 +000012023 /* For changing a composing character adjust
12024 * the score from SCORE_SUBST to
12025 * SCORE_SUBCOMP. */
12026 if (enc_utf8
12027 && utf_iscomposing(
12028 mb_ptr2char(tword
12029 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012030 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012031 && utf_iscomposing(
12032 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012033 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012034 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012035 SCORE_SUBST - SCORE_SUBCOMP;
12036
Bram Moolenaar4770d092006-01-12 23:22:24 +000012037 /* For a similar character adjust score from
12038 * SCORE_SUBST to SCORE_SIMILAR. */
12039 else if (!soundfold
12040 && slang->sl_has_map
12041 && similar_chars(slang,
12042 mb_ptr2char(tword
12043 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012044 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012045 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012046 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012047 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012048 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012049 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012050 else if (sp->ts_isdiff == DIFF_INSERT
12051 && sp->ts_twordlen > sp->ts_tcharlen)
12052 {
12053 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12054 c = mb_ptr2char(p);
12055 if (enc_utf8 && utf_iscomposing(c))
12056 {
12057 /* Inserting a composing char doesn't
12058 * count that much. */
12059 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12060 }
12061 else
12062 {
12063 /* If the previous character was the same,
12064 * thus doubling a character, give a bonus
12065 * to the score. Also for the soundfold
12066 * tree (might seem illogical but does
12067 * give better scores). */
12068 mb_ptr_back(tword, p);
12069 if (c == mb_ptr2char(p))
12070 sp->ts_score -= SCORE_INS
12071 - SCORE_INSDUP;
12072 }
12073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074
Bram Moolenaar4770d092006-01-12 23:22:24 +000012075 /* Starting a new char, reset the length. */
12076 sp->ts_tcharlen = 0;
12077 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012078 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012079 else
12080#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012081 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012082 /* If we found a similar char adjust the score.
12083 * We do this after calling go_deeper() because
12084 * it's slow. */
12085 if (newscore != 0
12086 && !soundfold
12087 && slang->sl_has_map
12088 && similar_chars(slang,
12089 c, fword[sp->ts_fidx - 1]))
12090 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012092 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012093 }
12094 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095
Bram Moolenaar4770d092006-01-12 23:22:24 +000012096 case STATE_DEL:
12097#ifdef FEAT_MBYTE
12098 /* When past the first byte of a multi-byte char don't try
12099 * delete/insert/swap a character. */
12100 if (has_mbyte && sp->ts_tcharlen > 0)
12101 {
12102 sp->ts_state = STATE_FINAL;
12103 break;
12104 }
12105#endif
12106 /*
12107 * Try skipping one character in the bad word (delete it).
12108 */
12109 sp->ts_state = STATE_INS_PREP;
12110 sp->ts_curi = 1;
12111 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12112 /* Deleting a vowel at the start of a word counts less, see
12113 * soundalike_score(). */
12114 newscore = 2 * SCORE_DEL / 3;
12115 else
12116 newscore = SCORE_DEL;
12117 if (fword[sp->ts_fidx] != NUL
12118 && TRY_DEEPER(su, stack, depth, newscore))
12119 {
12120 go_deeper(stack, depth, newscore);
12121#ifdef DEBUG_TRIEWALK
12122 sprintf(changename[depth], "%.*s-%s: delete %c",
12123 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12124 fword[sp->ts_fidx]);
12125#endif
12126 ++depth;
12127
12128 /* Remember what character we deleted, so that we can avoid
12129 * inserting it again. */
12130 stack[depth].ts_flags |= TSF_DIDDEL;
12131 stack[depth].ts_delidx = sp->ts_fidx;
12132
12133 /* Advance over the character in fword[]. Give a bonus to the
12134 * score if the same character is following "nn" -> "n". It's
12135 * a bit illogical for soundfold tree but it does give better
12136 * results. */
12137#ifdef FEAT_MBYTE
12138 if (has_mbyte)
12139 {
12140 c = mb_ptr2char(fword + sp->ts_fidx);
12141 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12142 if (enc_utf8 && utf_iscomposing(c))
12143 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12144 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12145 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12146 }
12147 else
12148#endif
12149 {
12150 ++stack[depth].ts_fidx;
12151 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12152 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12153 }
12154 break;
12155 }
12156 /*FALLTHROUGH*/
12157
12158 case STATE_INS_PREP:
12159 if (sp->ts_flags & TSF_DIDDEL)
12160 {
12161 /* If we just deleted a byte then inserting won't make sense,
12162 * a substitute is always cheaper. */
12163 sp->ts_state = STATE_SWAP;
12164 break;
12165 }
12166
12167 /* skip over NUL bytes */
12168 n = sp->ts_arridx;
12169 for (;;)
12170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012171 if (sp->ts_curi > byts[n])
12172 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012173 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012175 break;
12176 }
12177 if (byts[n + sp->ts_curi] != NUL)
12178 {
12179 /* Found a byte to insert. */
12180 sp->ts_state = STATE_INS;
12181 break;
12182 }
12183 ++sp->ts_curi;
12184 }
12185 break;
12186
12187 /*FALLTHROUGH*/
12188
12189 case STATE_INS:
12190 /* Insert one byte. Repeat this for each possible byte at this
12191 * node. */
12192 n = sp->ts_arridx;
12193 if (sp->ts_curi > byts[n])
12194 {
12195 /* Done all bytes at this node, go to next state. */
12196 sp->ts_state = STATE_SWAP;
12197 break;
12198 }
12199
12200 /* Do one more byte at this node, but:
12201 * - Skip NUL bytes.
12202 * - Skip the byte if it's equal to the byte in the word,
12203 * accepting that byte is always better.
12204 */
12205 n += sp->ts_curi++;
12206 c = byts[n];
12207 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12208 /* Inserting a vowel at the start of a word counts less,
12209 * see soundalike_score(). */
12210 newscore = 2 * SCORE_INS / 3;
12211 else
12212 newscore = SCORE_INS;
12213 if (c != fword[sp->ts_fidx]
12214 && TRY_DEEPER(su, stack, depth, newscore))
12215 {
12216 go_deeper(stack, depth, newscore);
12217#ifdef DEBUG_TRIEWALK
12218 sprintf(changename[depth], "%.*s-%s: insert %c",
12219 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12220 c);
12221#endif
12222 ++depth;
12223 sp = &stack[depth];
12224 tword[sp->ts_twordlen++] = c;
12225 sp->ts_arridx = idxs[n];
12226#ifdef FEAT_MBYTE
12227 if (has_mbyte)
12228 {
12229 fl = MB_BYTE2LEN(c);
12230 if (fl > 1)
12231 {
12232 /* There are following bytes for the same character.
12233 * We must find all bytes before trying
12234 * delete/insert/swap/etc. */
12235 sp->ts_tcharlen = fl;
12236 sp->ts_tcharidx = 1;
12237 sp->ts_isdiff = DIFF_INSERT;
12238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 }
12240 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012241 fl = 1;
12242 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012243#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012244 {
12245 /* If the previous character was the same, thus doubling a
12246 * character, give a bonus to the score. Also for
12247 * soundfold words (illogical but does give a better
12248 * score). */
12249 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012250 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012251 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012252 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012253 }
12254 break;
12255
12256 case STATE_SWAP:
12257 /*
12258 * Swap two bytes in the bad word: "12" -> "21".
12259 * We change "fword" here, it's changed back afterwards at
12260 * STATE_UNSWAP.
12261 */
12262 p = fword + sp->ts_fidx;
12263 c = *p;
12264 if (c == NUL)
12265 {
12266 /* End of word, can't swap or replace. */
12267 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012270
Bram Moolenaar4770d092006-01-12 23:22:24 +000012271 /* Don't swap if the first character is not a word character.
12272 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012273 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012274 {
12275 sp->ts_state = STATE_REP_INI;
12276 break;
12277 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012278
Bram Moolenaar4770d092006-01-12 23:22:24 +000012279#ifdef FEAT_MBYTE
12280 if (has_mbyte)
12281 {
12282 n = mb_cptr2len(p);
12283 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012284 if (p[n] == NUL)
12285 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012286 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012287 c2 = c; /* don't swap non-word char */
12288 else
12289 c2 = mb_ptr2char(p + n);
12290 }
12291 else
12292#endif
12293 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012294 if (p[1] == NUL)
12295 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012296 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012297 c2 = c; /* don't swap non-word char */
12298 else
12299 c2 = p[1];
12300 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012301
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012302 /* When the second character is NUL we can't swap. */
12303 if (c2 == NUL)
12304 {
12305 sp->ts_state = STATE_REP_INI;
12306 break;
12307 }
12308
Bram Moolenaar4770d092006-01-12 23:22:24 +000012309 /* When characters are identical, swap won't do anything.
12310 * Also get here if the second char is not a word character. */
12311 if (c == c2)
12312 {
12313 sp->ts_state = STATE_SWAP3;
12314 break;
12315 }
12316 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12317 {
12318 go_deeper(stack, depth, SCORE_SWAP);
12319#ifdef DEBUG_TRIEWALK
12320 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12321 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12322 c, c2);
12323#endif
12324 sp->ts_state = STATE_UNSWAP;
12325 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012326#ifdef FEAT_MBYTE
12327 if (has_mbyte)
12328 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012329 fl = mb_char2len(c2);
12330 mch_memmove(p, p + n, fl);
12331 mb_char2bytes(c, p + fl);
12332 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012333 }
12334 else
12335#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012336 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012337 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012338 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012339 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012340 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012341 }
12342 else
12343 /* If this swap doesn't work then SWAP3 won't either. */
12344 sp->ts_state = STATE_REP_INI;
12345 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012346
Bram Moolenaar4770d092006-01-12 23:22:24 +000012347 case STATE_UNSWAP:
12348 /* Undo the STATE_SWAP swap: "21" -> "12". */
12349 p = fword + sp->ts_fidx;
12350#ifdef FEAT_MBYTE
12351 if (has_mbyte)
12352 {
12353 n = MB_BYTE2LEN(*p);
12354 c = mb_ptr2char(p + n);
12355 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12356 mb_char2bytes(c, p);
12357 }
12358 else
12359#endif
12360 {
12361 c = *p;
12362 *p = p[1];
12363 p[1] = c;
12364 }
12365 /*FALLTHROUGH*/
12366
12367 case STATE_SWAP3:
12368 /* Swap two bytes, skipping one: "123" -> "321". We change
12369 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12370 p = fword + sp->ts_fidx;
12371#ifdef FEAT_MBYTE
12372 if (has_mbyte)
12373 {
12374 n = mb_cptr2len(p);
12375 c = mb_ptr2char(p);
12376 fl = mb_cptr2len(p + n);
12377 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012378 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012379 c3 = c; /* don't swap non-word char */
12380 else
12381 c3 = mb_ptr2char(p + n + fl);
12382 }
12383 else
12384#endif
12385 {
12386 c = *p;
12387 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012388 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012389 c3 = c; /* don't swap non-word char */
12390 else
12391 c3 = p[2];
12392 }
12393
12394 /* When characters are identical: "121" then SWAP3 result is
12395 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12396 * same as SWAP on next char: "112". Thus skip all swapping.
12397 * Also skip when c3 is NUL.
12398 * Also get here when the third character is not a word character.
12399 * Second character may any char: "a.b" -> "b.a" */
12400 if (c == c3 || c3 == NUL)
12401 {
12402 sp->ts_state = STATE_REP_INI;
12403 break;
12404 }
12405 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12406 {
12407 go_deeper(stack, depth, SCORE_SWAP3);
12408#ifdef DEBUG_TRIEWALK
12409 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12410 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12411 c, c3);
12412#endif
12413 sp->ts_state = STATE_UNSWAP3;
12414 ++depth;
12415#ifdef FEAT_MBYTE
12416 if (has_mbyte)
12417 {
12418 tl = mb_char2len(c3);
12419 mch_memmove(p, p + n + fl, tl);
12420 mb_char2bytes(c2, p + tl);
12421 mb_char2bytes(c, p + fl + tl);
12422 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12423 }
12424 else
12425#endif
12426 {
12427 p[0] = p[2];
12428 p[2] = c;
12429 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12430 }
12431 }
12432 else
12433 sp->ts_state = STATE_REP_INI;
12434 break;
12435
12436 case STATE_UNSWAP3:
12437 /* Undo STATE_SWAP3: "321" -> "123" */
12438 p = fword + sp->ts_fidx;
12439#ifdef FEAT_MBYTE
12440 if (has_mbyte)
12441 {
12442 n = MB_BYTE2LEN(*p);
12443 c2 = mb_ptr2char(p + n);
12444 fl = MB_BYTE2LEN(p[n]);
12445 c = mb_ptr2char(p + n + fl);
12446 tl = MB_BYTE2LEN(p[n + fl]);
12447 mch_memmove(p + fl + tl, p, n);
12448 mb_char2bytes(c, p);
12449 mb_char2bytes(c2, p + tl);
12450 p = p + tl;
12451 }
12452 else
12453#endif
12454 {
12455 c = *p;
12456 *p = p[2];
12457 p[2] = c;
12458 ++p;
12459 }
12460
Bram Moolenaar860cae12010-06-05 23:22:07 +020012461 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012462 {
12463 /* Middle char is not a word char, skip the rotate. First and
12464 * third char were already checked at swap and swap3. */
12465 sp->ts_state = STATE_REP_INI;
12466 break;
12467 }
12468
12469 /* Rotate three characters left: "123" -> "231". We change
12470 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12471 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12472 {
12473 go_deeper(stack, depth, SCORE_SWAP3);
12474#ifdef DEBUG_TRIEWALK
12475 p = fword + sp->ts_fidx;
12476 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12477 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12478 p[0], p[1], p[2]);
12479#endif
12480 sp->ts_state = STATE_UNROT3L;
12481 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012482 p = fword + sp->ts_fidx;
12483#ifdef FEAT_MBYTE
12484 if (has_mbyte)
12485 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012486 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012487 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012488 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012489 fl += mb_cptr2len(p + n + fl);
12490 mch_memmove(p, p + n, fl);
12491 mb_char2bytes(c, p + fl);
12492 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012493 }
12494 else
12495#endif
12496 {
12497 c = *p;
12498 *p = p[1];
12499 p[1] = p[2];
12500 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012501 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012502 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012503 }
12504 else
12505 sp->ts_state = STATE_REP_INI;
12506 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507
Bram Moolenaar4770d092006-01-12 23:22:24 +000012508 case STATE_UNROT3L:
12509 /* Undo ROT3L: "231" -> "123" */
12510 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012511#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012512 if (has_mbyte)
12513 {
12514 n = MB_BYTE2LEN(*p);
12515 n += MB_BYTE2LEN(p[n]);
12516 c = mb_ptr2char(p + n);
12517 tl = MB_BYTE2LEN(p[n]);
12518 mch_memmove(p + tl, p, n);
12519 mb_char2bytes(c, p);
12520 }
12521 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012522#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012523 {
12524 c = p[2];
12525 p[2] = p[1];
12526 p[1] = *p;
12527 *p = c;
12528 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012529
Bram Moolenaar4770d092006-01-12 23:22:24 +000012530 /* Rotate three bytes right: "123" -> "312". We change "fword"
12531 * here, it's changed back afterwards at STATE_UNROT3R. */
12532 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12533 {
12534 go_deeper(stack, depth, SCORE_SWAP3);
12535#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012537 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12538 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12539 p[0], p[1], p[2]);
12540#endif
12541 sp->ts_state = STATE_UNROT3R;
12542 ++depth;
12543 p = fword + sp->ts_fidx;
12544#ifdef FEAT_MBYTE
12545 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012546 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012547 n = mb_cptr2len(p);
12548 n += mb_cptr2len(p + n);
12549 c = mb_ptr2char(p + n);
12550 tl = mb_cptr2len(p + n);
12551 mch_memmove(p + tl, p, n);
12552 mb_char2bytes(c, p);
12553 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012554 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012555 else
12556#endif
12557 {
12558 c = p[2];
12559 p[2] = p[1];
12560 p[1] = *p;
12561 *p = c;
12562 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12563 }
12564 }
12565 else
12566 sp->ts_state = STATE_REP_INI;
12567 break;
12568
12569 case STATE_UNROT3R:
12570 /* Undo ROT3R: "312" -> "123" */
12571 p = fword + sp->ts_fidx;
12572#ifdef FEAT_MBYTE
12573 if (has_mbyte)
12574 {
12575 c = mb_ptr2char(p);
12576 tl = MB_BYTE2LEN(*p);
12577 n = MB_BYTE2LEN(p[tl]);
12578 n += MB_BYTE2LEN(p[tl + n]);
12579 mch_memmove(p, p + tl, n);
12580 mb_char2bytes(c, p + n);
12581 }
12582 else
12583#endif
12584 {
12585 c = *p;
12586 *p = p[1];
12587 p[1] = p[2];
12588 p[2] = c;
12589 }
12590 /*FALLTHROUGH*/
12591
12592 case STATE_REP_INI:
12593 /* Check if matching with REP items from the .aff file would work.
12594 * Quickly skip if:
12595 * - there are no REP items and we are not in the soundfold trie
12596 * - the score is going to be too high anyway
12597 * - already applied a REP item or swapped here */
12598 if ((lp->lp_replang == NULL && !soundfold)
12599 || sp->ts_score + SCORE_REP >= su->su_maxscore
12600 || sp->ts_fidx < sp->ts_fidxtry)
12601 {
12602 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012603 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605
Bram Moolenaar4770d092006-01-12 23:22:24 +000012606 /* Use the first byte to quickly find the first entry that may
12607 * match. If the index is -1 there is none. */
12608 if (soundfold)
12609 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12610 else
12611 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012612
Bram Moolenaar4770d092006-01-12 23:22:24 +000012613 if (sp->ts_curi < 0)
12614 {
12615 sp->ts_state = STATE_FINAL;
12616 break;
12617 }
12618
12619 sp->ts_state = STATE_REP;
12620 /*FALLTHROUGH*/
12621
12622 case STATE_REP:
12623 /* Try matching with REP items from the .aff file. For each match
12624 * replace the characters and check if the resulting word is
12625 * valid. */
12626 p = fword + sp->ts_fidx;
12627
12628 if (soundfold)
12629 gap = &slang->sl_repsal;
12630 else
12631 gap = &lp->lp_replang->sl_rep;
12632 while (sp->ts_curi < gap->ga_len)
12633 {
12634 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12635 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012636 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012637 /* past possible matching entries */
12638 sp->ts_curi = gap->ga_len;
12639 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012640 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012641 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12642 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12643 {
12644 go_deeper(stack, depth, SCORE_REP);
12645#ifdef DEBUG_TRIEWALK
12646 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12647 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12648 ftp->ft_from, ftp->ft_to);
12649#endif
12650 /* Need to undo this afterwards. */
12651 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012652
Bram Moolenaar4770d092006-01-12 23:22:24 +000012653 /* Change the "from" to the "to" string. */
12654 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012655 fl = (int)STRLEN(ftp->ft_from);
12656 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012657 if (fl != tl)
12658 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012659 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012660 repextra += tl - fl;
12661 }
12662 mch_memmove(p, ftp->ft_to, tl);
12663 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12664#ifdef FEAT_MBYTE
12665 stack[depth].ts_tcharlen = 0;
12666#endif
12667 break;
12668 }
12669 }
12670
12671 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12672 /* No (more) matches. */
12673 sp->ts_state = STATE_FINAL;
12674
12675 break;
12676
12677 case STATE_REP_UNDO:
12678 /* Undo a REP replacement and continue with the next one. */
12679 if (soundfold)
12680 gap = &slang->sl_repsal;
12681 else
12682 gap = &lp->lp_replang->sl_rep;
12683 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012684 fl = (int)STRLEN(ftp->ft_from);
12685 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012686 p = fword + sp->ts_fidx;
12687 if (fl != tl)
12688 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012689 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012690 repextra -= tl - fl;
12691 }
12692 mch_memmove(p, ftp->ft_from, fl);
12693 sp->ts_state = STATE_REP;
12694 break;
12695
12696 default:
12697 /* Did all possible states at this level, go up one level. */
12698 --depth;
12699
12700 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12701 {
12702 /* Continue in or go back to the prefix tree. */
12703 byts = pbyts;
12704 idxs = pidxs;
12705 }
12706
12707 /* Don't check for CTRL-C too often, it takes time. */
12708 if (--breakcheckcount == 0)
12709 {
12710 ui_breakcheck();
12711 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012712 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 }
12714 }
12715}
12716
Bram Moolenaar4770d092006-01-12 23:22:24 +000012717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012719 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012721 static void
12722go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 trystate_T *stack;
12724 int depth;
12725 int score_add;
12726{
Bram Moolenaarea424162005-06-16 21:51:00 +000012727 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012729 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012730 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012731 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012732}
12733
Bram Moolenaar53805d12005-08-01 07:08:33 +000012734#ifdef FEAT_MBYTE
12735/*
12736 * Case-folding may change the number of bytes: Count nr of chars in
12737 * fword[flen] and return the byte length of that many chars in "word".
12738 */
12739 static int
12740nofold_len(fword, flen, word)
12741 char_u *fword;
12742 int flen;
12743 char_u *word;
12744{
12745 char_u *p;
12746 int i = 0;
12747
12748 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12749 ++i;
12750 for (p = word; i > 0; mb_ptr_adv(p))
12751 --i;
12752 return (int)(p - word);
12753}
12754#endif
12755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012756/*
12757 * "fword" is a good word with case folded. Find the matching keep-case
12758 * words and put it in "kword".
12759 * Theoretically there could be several keep-case words that result in the
12760 * same case-folded word, but we only find one...
12761 */
12762 static void
12763find_keepcap_word(slang, fword, kword)
12764 slang_T *slang;
12765 char_u *fword;
12766 char_u *kword;
12767{
12768 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12769 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012770 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771
12772 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012773 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 int round[MAXWLEN];
12775 int fwordidx[MAXWLEN];
12776 int uwordidx[MAXWLEN];
12777 int kwordlen[MAXWLEN];
12778
12779 int flen, ulen;
12780 int l;
12781 int len;
12782 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012783 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784 char_u *p;
12785 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012786 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787
12788 if (byts == NULL)
12789 {
12790 /* array is empty: "cannot happen" */
12791 *kword = NUL;
12792 return;
12793 }
12794
12795 /* Make an all-cap version of "fword". */
12796 allcap_copy(fword, uword);
12797
12798 /*
12799 * Each character needs to be tried both case-folded and upper-case.
12800 * All this gets very complicated if we keep in mind that changing case
12801 * may change the byte length of a multi-byte character...
12802 */
12803 depth = 0;
12804 arridx[0] = 0;
12805 round[0] = 0;
12806 fwordidx[0] = 0;
12807 uwordidx[0] = 0;
12808 kwordlen[0] = 0;
12809 while (depth >= 0)
12810 {
12811 if (fword[fwordidx[depth]] == NUL)
12812 {
12813 /* We are at the end of "fword". If the tree allows a word to end
12814 * here we have found a match. */
12815 if (byts[arridx[depth] + 1] == 0)
12816 {
12817 kword[kwordlen[depth]] = NUL;
12818 return;
12819 }
12820
12821 /* kword is getting too long, continue one level up */
12822 --depth;
12823 }
12824 else if (++round[depth] > 2)
12825 {
12826 /* tried both fold-case and upper-case character, continue one
12827 * level up */
12828 --depth;
12829 }
12830 else
12831 {
12832 /*
12833 * round[depth] == 1: Try using the folded-case character.
12834 * round[depth] == 2: Try using the upper-case character.
12835 */
12836#ifdef FEAT_MBYTE
12837 if (has_mbyte)
12838 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012839 flen = mb_cptr2len(fword + fwordidx[depth]);
12840 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841 }
12842 else
12843#endif
12844 ulen = flen = 1;
12845 if (round[depth] == 1)
12846 {
12847 p = fword + fwordidx[depth];
12848 l = flen;
12849 }
12850 else
12851 {
12852 p = uword + uwordidx[depth];
12853 l = ulen;
12854 }
12855
12856 for (tryidx = arridx[depth]; l > 0; --l)
12857 {
12858 /* Perform a binary search in the list of accepted bytes. */
12859 len = byts[tryidx++];
12860 c = *p++;
12861 lo = tryidx;
12862 hi = tryidx + len - 1;
12863 while (lo < hi)
12864 {
12865 m = (lo + hi) / 2;
12866 if (byts[m] > c)
12867 hi = m - 1;
12868 else if (byts[m] < c)
12869 lo = m + 1;
12870 else
12871 {
12872 lo = hi = m;
12873 break;
12874 }
12875 }
12876
12877 /* Stop if there is no matching byte. */
12878 if (hi < lo || byts[lo] != c)
12879 break;
12880
12881 /* Continue at the child (if there is one). */
12882 tryidx = idxs[lo];
12883 }
12884
12885 if (l == 0)
12886 {
12887 /*
12888 * Found the matching char. Copy it to "kword" and go a
12889 * level deeper.
12890 */
12891 if (round[depth] == 1)
12892 {
12893 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12894 flen);
12895 kwordlen[depth + 1] = kwordlen[depth] + flen;
12896 }
12897 else
12898 {
12899 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12900 ulen);
12901 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12902 }
12903 fwordidx[depth + 1] = fwordidx[depth] + flen;
12904 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12905
12906 ++depth;
12907 arridx[depth] = tryidx;
12908 round[depth] = 0;
12909 }
12910 }
12911 }
12912
12913 /* Didn't find it: "cannot happen". */
12914 *kword = NUL;
12915}
12916
12917/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012918 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12919 * su->su_sga.
12920 */
12921 static void
12922score_comp_sal(su)
12923 suginfo_T *su;
12924{
12925 langp_T *lp;
12926 char_u badsound[MAXWLEN];
12927 int i;
12928 suggest_T *stp;
12929 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012930 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012931 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012932
12933 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12934 return;
12935
12936 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012937 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012938 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012939 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012940 if (lp->lp_slang->sl_sal.ga_len > 0)
12941 {
12942 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012943 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012944
12945 for (i = 0; i < su->su_ga.ga_len; ++i)
12946 {
12947 stp = &SUG(su->su_ga, i);
12948
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012949 /* Case-fold the suggested word, sound-fold it and compute the
12950 * sound-a-like score. */
12951 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012952 if (score < SCORE_MAXMAX)
12953 {
12954 /* Add the suggestion. */
12955 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12956 sstp->st_word = vim_strsave(stp->st_word);
12957 if (sstp->st_word != NULL)
12958 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012959 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012960 sstp->st_score = score;
12961 sstp->st_altscore = 0;
12962 sstp->st_orglen = stp->st_orglen;
12963 ++su->su_sga.ga_len;
12964 }
12965 }
12966 }
12967 break;
12968 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012969 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012970}
12971
12972/*
12973 * Combine the list of suggestions in su->su_ga and su->su_sga.
12974 * They are intwined.
12975 */
12976 static void
12977score_combine(su)
12978 suginfo_T *su;
12979{
12980 int i;
12981 int j;
12982 garray_T ga;
12983 garray_T *gap;
12984 langp_T *lp;
12985 suggest_T *stp;
12986 char_u *p;
12987 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012988 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012989 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012990 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012991
12992 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012993 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012994 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012995 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012996 if (lp->lp_slang->sl_sal.ga_len > 0)
12997 {
12998 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012999 slang = lp->lp_slang;
13000 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013001
13002 for (i = 0; i < su->su_ga.ga_len; ++i)
13003 {
13004 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013005 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013006 if (stp->st_altscore == SCORE_MAXMAX)
13007 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13008 else
13009 stp->st_score = (stp->st_score * 3
13010 + stp->st_altscore) / 4;
13011 stp->st_salscore = FALSE;
13012 }
13013 break;
13014 }
13015 }
13016
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013017 if (slang == NULL) /* Using "double" without sound folding. */
13018 {
13019 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13020 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013021 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013022 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013023
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013024 /* Add the alternate score to su_sga. */
13025 for (i = 0; i < su->su_sga.ga_len; ++i)
13026 {
13027 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013028 stp->st_altscore = spell_edit_score(slang,
13029 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013030 if (stp->st_score == SCORE_MAXMAX)
13031 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13032 else
13033 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13034 stp->st_salscore = TRUE;
13035 }
13036
Bram Moolenaar4770d092006-01-12 23:22:24 +000013037 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13038 * for both lists. */
13039 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013040 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013041 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013042 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13043
13044 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13045 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13046 return;
13047
13048 stp = &SUG(ga, 0);
13049 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13050 {
13051 /* round 1: get a suggestion from su_ga
13052 * round 2: get a suggestion from su_sga */
13053 for (round = 1; round <= 2; ++round)
13054 {
13055 gap = round == 1 ? &su->su_ga : &su->su_sga;
13056 if (i < gap->ga_len)
13057 {
13058 /* Don't add a word if it's already there. */
13059 p = SUG(*gap, i).st_word;
13060 for (j = 0; j < ga.ga_len; ++j)
13061 if (STRCMP(stp[j].st_word, p) == 0)
13062 break;
13063 if (j == ga.ga_len)
13064 stp[ga.ga_len++] = SUG(*gap, i);
13065 else
13066 vim_free(p);
13067 }
13068 }
13069 }
13070
13071 ga_clear(&su->su_ga);
13072 ga_clear(&su->su_sga);
13073
13074 /* Truncate the list to the number of suggestions that will be displayed. */
13075 if (ga.ga_len > su->su_maxcount)
13076 {
13077 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13078 vim_free(stp[i].st_word);
13079 ga.ga_len = su->su_maxcount;
13080 }
13081
13082 su->su_ga = ga;
13083}
13084
13085/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013086 * For the goodword in "stp" compute the soundalike score compared to the
13087 * badword.
13088 */
13089 static int
13090stp_sal_score(stp, su, slang, badsound)
13091 suggest_T *stp;
13092 suginfo_T *su;
13093 slang_T *slang;
13094 char_u *badsound; /* sound-folded badword */
13095{
13096 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013097 char_u *pbad;
13098 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013099 char_u badsound2[MAXWLEN];
13100 char_u fword[MAXWLEN];
13101 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013102 char_u goodword[MAXWLEN];
13103 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013104
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013105 lendiff = (int)(su->su_badlen - stp->st_orglen);
13106 if (lendiff >= 0)
13107 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013108 else
13109 {
13110 /* soundfold the bad word with more characters following */
13111 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13112
13113 /* When joining two words the sound often changes a lot. E.g., "t he"
13114 * sounds like "t h" while "the" sounds like "@". Avoid that by
13115 * removing the space. Don't do it when the good word also contains a
13116 * space. */
13117 if (vim_iswhite(su->su_badptr[su->su_badlen])
13118 && *skiptowhite(stp->st_word) == NUL)
13119 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013120 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013121
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013122 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013123 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013124 }
13125
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013126 if (lendiff > 0)
13127 {
13128 /* Add part of the bad word to the good word, so that we soundfold
13129 * what replaces the bad word. */
13130 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013131 vim_strncpy(goodword + stp->st_wordlen,
13132 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013133 pgood = goodword;
13134 }
13135 else
13136 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013137
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013138 /* Sound-fold the word and compute the score for the difference. */
13139 spell_soundfold(slang, pgood, FALSE, goodsound);
13140
13141 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013142}
13143
Bram Moolenaar4770d092006-01-12 23:22:24 +000013144/* structure used to store soundfolded words that add_sound_suggest() has
13145 * handled already. */
13146typedef struct
13147{
13148 short sft_score; /* lowest score used */
13149 char_u sft_word[1]; /* soundfolded word, actually longer */
13150} sftword_T;
13151
13152static sftword_T dumsft;
13153#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13154#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13155
13156/*
13157 * Prepare for calling suggest_try_soundalike().
13158 */
13159 static void
13160suggest_try_soundalike_prep()
13161{
13162 langp_T *lp;
13163 int lpi;
13164 slang_T *slang;
13165
13166 /* Do this for all languages that support sound folding and for which a
13167 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013168 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013169 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013170 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013171 slang = lp->lp_slang;
13172 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13173 /* prepare the hashtable used by add_sound_suggest() */
13174 hash_init(&slang->sl_sounddone);
13175 }
13176}
13177
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013178/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013180 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013181 */
13182 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013183suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 suginfo_T *su;
13185{
13186 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013187 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013188 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013189 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013190
Bram Moolenaar4770d092006-01-12 23:22:24 +000013191 /* Do this for all languages that support sound folding and for which a
13192 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013193 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013195 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013196 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013197 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013198 {
13199 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013200 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201
Bram Moolenaar4770d092006-01-12 23:22:24 +000013202 /* try all kinds of inserts/deletes/swaps/etc. */
13203 /* TODO: also soundfold the next words, so that we can try joining
13204 * and splitting */
13205 suggest_trie_walk(su, lp, salword, TRUE);
13206 }
13207 }
13208}
13209
13210/*
13211 * Finish up after calling suggest_try_soundalike().
13212 */
13213 static void
13214suggest_try_soundalike_finish()
13215{
13216 langp_T *lp;
13217 int lpi;
13218 slang_T *slang;
13219 int todo;
13220 hashitem_T *hi;
13221
13222 /* Do this for all languages that support sound folding and for which a
13223 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013224 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013225 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013226 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013227 slang = lp->lp_slang;
13228 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13229 {
13230 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013231 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013232 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13233 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013235 vim_free(HI2SFT(hi));
13236 --todo;
13237 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013238
13239 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013240 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013241 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013242 }
13243 }
13244}
13245
13246/*
13247 * A match with a soundfolded word is found. Add the good word(s) that
13248 * produce this soundfolded word.
13249 */
13250 static void
13251add_sound_suggest(su, goodword, score, lp)
13252 suginfo_T *su;
13253 char_u *goodword;
13254 int score; /* soundfold score */
13255 langp_T *lp;
13256{
13257 slang_T *slang = lp->lp_slang; /* language for sound folding */
13258 int sfwordnr;
13259 char_u *nrline;
13260 int orgnr;
13261 char_u theword[MAXWLEN];
13262 int i;
13263 int wlen;
13264 char_u *byts;
13265 idx_T *idxs;
13266 int n;
13267 int wordcount;
13268 int wc;
13269 int goodscore;
13270 hash_T hash;
13271 hashitem_T *hi;
13272 sftword_T *sft;
13273 int bc, gc;
13274 int limit;
13275
13276 /*
13277 * It's very well possible that the same soundfold word is found several
13278 * times with different scores. Since the following is quite slow only do
13279 * the words that have a better score than before. Use a hashtable to
13280 * remember the words that have been done.
13281 */
13282 hash = hash_hash(goodword);
13283 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13284 if (HASHITEM_EMPTY(hi))
13285 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013286 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13287 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013288 if (sft != NULL)
13289 {
13290 sft->sft_score = score;
13291 STRCPY(sft->sft_word, goodword);
13292 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13293 }
13294 }
13295 else
13296 {
13297 sft = HI2SFT(hi);
13298 if (score >= sft->sft_score)
13299 return;
13300 sft->sft_score = score;
13301 }
13302
13303 /*
13304 * Find the word nr in the soundfold tree.
13305 */
13306 sfwordnr = soundfold_find(slang, goodword);
13307 if (sfwordnr < 0)
13308 {
13309 EMSG2(_(e_intern2), "add_sound_suggest()");
13310 return;
13311 }
13312
13313 /*
13314 * go over the list of good words that produce this soundfold word
13315 */
13316 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13317 orgnr = 0;
13318 while (*nrline != NUL)
13319 {
13320 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13321 * previous wordnr. */
13322 orgnr += bytes2offset(&nrline);
13323
13324 byts = slang->sl_fbyts;
13325 idxs = slang->sl_fidxs;
13326
13327 /* Lookup the word "orgnr" one of the two tries. */
13328 n = 0;
13329 wlen = 0;
13330 wordcount = 0;
13331 for (;;)
13332 {
13333 i = 1;
13334 if (wordcount == orgnr && byts[n + 1] == NUL)
13335 break; /* found end of word */
13336
13337 if (byts[n + 1] == NUL)
13338 ++wordcount;
13339
13340 /* skip over the NUL bytes */
13341 for ( ; byts[n + i] == NUL; ++i)
13342 if (i > byts[n]) /* safety check */
13343 {
13344 STRCPY(theword + wlen, "BAD");
13345 goto badword;
13346 }
13347
13348 /* One of the siblings must have the word. */
13349 for ( ; i < byts[n]; ++i)
13350 {
13351 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13352 if (wordcount + wc > orgnr)
13353 break;
13354 wordcount += wc;
13355 }
13356
13357 theword[wlen++] = byts[n + i];
13358 n = idxs[n + i];
13359 }
13360badword:
13361 theword[wlen] = NUL;
13362
13363 /* Go over the possible flags and regions. */
13364 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13365 {
13366 char_u cword[MAXWLEN];
13367 char_u *p;
13368 int flags = (int)idxs[n + i];
13369
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013370 /* Skip words with the NOSUGGEST flag */
13371 if (flags & WF_NOSUGGEST)
13372 continue;
13373
Bram Moolenaar4770d092006-01-12 23:22:24 +000013374 if (flags & WF_KEEPCAP)
13375 {
13376 /* Must find the word in the keep-case tree. */
13377 find_keepcap_word(slang, theword, cword);
13378 p = cword;
13379 }
13380 else
13381 {
13382 flags |= su->su_badflags;
13383 if ((flags & WF_CAPMASK) != 0)
13384 {
13385 /* Need to fix case according to "flags". */
13386 make_case_word(theword, cword, flags);
13387 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 }
13389 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013390 p = theword;
13391 }
13392
13393 /* Add the suggestion. */
13394 if (sps_flags & SPS_DOUBLE)
13395 {
13396 /* Add the suggestion if the score isn't too bad. */
13397 if (score <= su->su_maxscore)
13398 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13399 score, 0, FALSE, slang, FALSE);
13400 }
13401 else
13402 {
13403 /* Add a penalty for words in another region. */
13404 if ((flags & WF_REGION)
13405 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13406 goodscore = SCORE_REGION;
13407 else
13408 goodscore = 0;
13409
13410 /* Add a small penalty for changing the first letter from
13411 * lower to upper case. Helps for "tath" -> "Kath", which is
13412 * less common thatn "tath" -> "path". Don't do it when the
13413 * letter is the same, that has already been counted. */
13414 gc = PTR2CHAR(p);
13415 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013416 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013417 bc = PTR2CHAR(su->su_badword);
13418 if (!SPELL_ISUPPER(bc)
13419 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13420 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 }
13422
Bram Moolenaar4770d092006-01-12 23:22:24 +000013423 /* Compute the score for the good word. This only does letter
13424 * insert/delete/swap/replace. REP items are not considered,
13425 * which may make the score a bit higher.
13426 * Use a limit for the score to make it work faster. Use
13427 * MAXSCORE(), because RESCORE() will change the score.
13428 * If the limit is very high then the iterative method is
13429 * inefficient, using an array is quicker. */
13430 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13431 if (limit > SCORE_LIMITMAX)
13432 goodscore += spell_edit_score(slang, su->su_badword, p);
13433 else
13434 goodscore += spell_edit_score_limit(slang, su->su_badword,
13435 p, limit);
13436
13437 /* When going over the limit don't bother to do the rest. */
13438 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013440 /* Give a bonus to words seen before. */
13441 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013442
Bram Moolenaar4770d092006-01-12 23:22:24 +000013443 /* Add the suggestion if the score isn't too bad. */
13444 goodscore = RESCORE(goodscore, score);
13445 if (goodscore <= su->su_sfmaxscore)
13446 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13447 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 }
13450 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013451 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013452 }
13453}
13454
13455/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013456 * Find word "word" in fold-case tree for "slang" and return the word number.
13457 */
13458 static int
13459soundfold_find(slang, word)
13460 slang_T *slang;
13461 char_u *word;
13462{
13463 idx_T arridx = 0;
13464 int len;
13465 int wlen = 0;
13466 int c;
13467 char_u *ptr = word;
13468 char_u *byts;
13469 idx_T *idxs;
13470 int wordnr = 0;
13471
13472 byts = slang->sl_sbyts;
13473 idxs = slang->sl_sidxs;
13474
13475 for (;;)
13476 {
13477 /* First byte is the number of possible bytes. */
13478 len = byts[arridx++];
13479
13480 /* If the first possible byte is a zero the word could end here.
13481 * If the word ends we found the word. If not skip the NUL bytes. */
13482 c = ptr[wlen];
13483 if (byts[arridx] == NUL)
13484 {
13485 if (c == NUL)
13486 break;
13487
13488 /* Skip over the zeros, there can be several. */
13489 while (len > 0 && byts[arridx] == NUL)
13490 {
13491 ++arridx;
13492 --len;
13493 }
13494 if (len == 0)
13495 return -1; /* no children, word should have ended here */
13496 ++wordnr;
13497 }
13498
13499 /* If the word ends we didn't find it. */
13500 if (c == NUL)
13501 return -1;
13502
13503 /* Perform a binary search in the list of accepted bytes. */
13504 if (c == TAB) /* <Tab> is handled like <Space> */
13505 c = ' ';
13506 while (byts[arridx] < c)
13507 {
13508 /* The word count is in the first idxs[] entry of the child. */
13509 wordnr += idxs[idxs[arridx]];
13510 ++arridx;
13511 if (--len == 0) /* end of the bytes, didn't find it */
13512 return -1;
13513 }
13514 if (byts[arridx] != c) /* didn't find the byte */
13515 return -1;
13516
13517 /* Continue at the child (if there is one). */
13518 arridx = idxs[arridx];
13519 ++wlen;
13520
13521 /* One space in the good word may stand for several spaces in the
13522 * checked word. */
13523 if (c == ' ')
13524 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13525 ++wlen;
13526 }
13527
13528 return wordnr;
13529}
13530
13531/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013532 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013533 */
13534 static void
13535make_case_word(fword, cword, flags)
13536 char_u *fword;
13537 char_u *cword;
13538 int flags;
13539{
13540 if (flags & WF_ALLCAP)
13541 /* Make it all upper-case */
13542 allcap_copy(fword, cword);
13543 else if (flags & WF_ONECAP)
13544 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013545 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 else
13547 /* Use goodword as-is. */
13548 STRCPY(cword, fword);
13549}
13550
Bram Moolenaarea424162005-06-16 21:51:00 +000013551/*
13552 * Use map string "map" for languages "lp".
13553 */
13554 static void
13555set_map_str(lp, map)
13556 slang_T *lp;
13557 char_u *map;
13558{
13559 char_u *p;
13560 int headc = 0;
13561 int c;
13562 int i;
13563
13564 if (*map == NUL)
13565 {
13566 lp->sl_has_map = FALSE;
13567 return;
13568 }
13569 lp->sl_has_map = TRUE;
13570
Bram Moolenaar4770d092006-01-12 23:22:24 +000013571 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013572 for (i = 0; i < 256; ++i)
13573 lp->sl_map_array[i] = 0;
13574#ifdef FEAT_MBYTE
13575 hash_init(&lp->sl_map_hash);
13576#endif
13577
13578 /*
13579 * The similar characters are stored separated with slashes:
13580 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13581 * before the same slash. For characters above 255 sl_map_hash is used.
13582 */
13583 for (p = map; *p != NUL; )
13584 {
13585#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013586 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013587#else
13588 c = *p++;
13589#endif
13590 if (c == '/')
13591 headc = 0;
13592 else
13593 {
13594 if (headc == 0)
13595 headc = c;
13596
13597#ifdef FEAT_MBYTE
13598 /* Characters above 255 don't fit in sl_map_array[], put them in
13599 * the hash table. Each entry is the char, a NUL the headchar and
13600 * a NUL. */
13601 if (c >= 256)
13602 {
13603 int cl = mb_char2len(c);
13604 int headcl = mb_char2len(headc);
13605 char_u *b;
13606 hash_T hash;
13607 hashitem_T *hi;
13608
13609 b = alloc((unsigned)(cl + headcl + 2));
13610 if (b == NULL)
13611 return;
13612 mb_char2bytes(c, b);
13613 b[cl] = NUL;
13614 mb_char2bytes(headc, b + cl + 1);
13615 b[cl + 1 + headcl] = NUL;
13616 hash = hash_hash(b);
13617 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13618 if (HASHITEM_EMPTY(hi))
13619 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13620 else
13621 {
13622 /* This should have been checked when generating the .spl
13623 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013624 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013625 vim_free(b);
13626 }
13627 }
13628 else
13629#endif
13630 lp->sl_map_array[c] = headc;
13631 }
13632 }
13633}
13634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635/*
13636 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13637 * lines in the .aff file.
13638 */
13639 static int
13640similar_chars(slang, c1, c2)
13641 slang_T *slang;
13642 int c1;
13643 int c2;
13644{
Bram Moolenaarea424162005-06-16 21:51:00 +000013645 int m1, m2;
13646#ifdef FEAT_MBYTE
13647 char_u buf[MB_MAXBYTES];
13648 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013649
Bram Moolenaarea424162005-06-16 21:51:00 +000013650 if (c1 >= 256)
13651 {
13652 buf[mb_char2bytes(c1, buf)] = 0;
13653 hi = hash_find(&slang->sl_map_hash, buf);
13654 if (HASHITEM_EMPTY(hi))
13655 m1 = 0;
13656 else
13657 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13658 }
13659 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013660#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013661 m1 = slang->sl_map_array[c1];
13662 if (m1 == 0)
13663 return FALSE;
13664
13665
13666#ifdef FEAT_MBYTE
13667 if (c2 >= 256)
13668 {
13669 buf[mb_char2bytes(c2, buf)] = 0;
13670 hi = hash_find(&slang->sl_map_hash, buf);
13671 if (HASHITEM_EMPTY(hi))
13672 m2 = 0;
13673 else
13674 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13675 }
13676 else
13677#endif
13678 m2 = slang->sl_map_array[c2];
13679
13680 return m1 == m2;
13681}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013682
13683/*
13684 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013685 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 */
13687 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013688add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13689 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013691 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013692 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013693 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013694 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013695 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013696 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013697 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013698 int maxsf; /* su_maxscore applies to soundfold score,
13699 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013701 int goodlen; /* len of goodword changed */
13702 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013704 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013705 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013706 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013708 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13709 * "thee the" is added next to changing the first "the" the "thee". */
13710 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013711 pbad = su->su_badptr + badlenarg;
13712 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013713 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013714 goodlen = (int)(pgood - goodword);
13715 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013716 if (goodlen <= 0 || badlen <= 0)
13717 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013718 mb_ptr_back(goodword, pgood);
13719 mb_ptr_back(su->su_badptr, pbad);
13720#ifdef FEAT_MBYTE
13721 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013722 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013723 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13724 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013725 }
13726 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013727#endif
13728 if (*pgood != *pbad)
13729 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013730 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013731
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013732 if (badlen == 0 && goodlen == 0)
13733 /* goodword doesn't change anything; may happen for "the the" changing
13734 * the first "the" to itself. */
13735 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013736
Bram Moolenaar89d40322006-08-29 15:30:07 +000013737 if (gap->ga_len == 0)
13738 i = -1;
13739 else
13740 {
13741 /* Check if the word is already there. Also check the length that is
13742 * being replaced "thes," -> "these" is a different suggestion from
13743 * "thes" -> "these". */
13744 stp = &SUG(*gap, 0);
13745 for (i = gap->ga_len; --i >= 0; ++stp)
13746 if (stp->st_wordlen == goodlen
13747 && stp->st_orglen == badlen
13748 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013750 /*
13751 * Found it. Remember the word with the lowest score.
13752 */
13753 if (stp->st_slang == NULL)
13754 stp->st_slang = slang;
13755
13756 new_sug.st_score = score;
13757 new_sug.st_altscore = altscore;
13758 new_sug.st_had_bonus = had_bonus;
13759
13760 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013761 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013762 /* Only one of the two had the soundalike score computed.
13763 * Need to do that for the other one now, otherwise the
13764 * scores can't be compared. This happens because
13765 * suggest_try_change() doesn't compute the soundalike
13766 * word to keep it fast, while some special methods set
13767 * the soundalike score to zero. */
13768 if (had_bonus)
13769 rescore_one(su, stp);
13770 else
13771 {
13772 new_sug.st_word = stp->st_word;
13773 new_sug.st_wordlen = stp->st_wordlen;
13774 new_sug.st_slang = stp->st_slang;
13775 new_sug.st_orglen = badlen;
13776 rescore_one(su, &new_sug);
13777 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779
Bram Moolenaar89d40322006-08-29 15:30:07 +000013780 if (stp->st_score > new_sug.st_score)
13781 {
13782 stp->st_score = new_sug.st_score;
13783 stp->st_altscore = new_sug.st_altscore;
13784 stp->st_had_bonus = new_sug.st_had_bonus;
13785 }
13786 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013787 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013788 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789
Bram Moolenaar4770d092006-01-12 23:22:24 +000013790 if (i < 0 && ga_grow(gap, 1) == OK)
13791 {
13792 /* Add a suggestion. */
13793 stp = &SUG(*gap, gap->ga_len);
13794 stp->st_word = vim_strnsave(goodword, goodlen);
13795 if (stp->st_word != NULL)
13796 {
13797 stp->st_wordlen = goodlen;
13798 stp->st_score = score;
13799 stp->st_altscore = altscore;
13800 stp->st_had_bonus = had_bonus;
13801 stp->st_orglen = badlen;
13802 stp->st_slang = slang;
13803 ++gap->ga_len;
13804
13805 /* If we have too many suggestions now, sort the list and keep
13806 * the best suggestions. */
13807 if (gap->ga_len > SUG_MAX_COUNT(su))
13808 {
13809 if (maxsf)
13810 su->su_sfmaxscore = cleanup_suggestions(gap,
13811 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13812 else
13813 {
13814 i = su->su_maxscore;
13815 su->su_maxscore = cleanup_suggestions(gap,
13816 su->su_maxscore, SUG_CLEAN_COUNT(su));
13817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 }
13819 }
13820 }
13821}
13822
13823/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013824 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13825 * for split words, such as "the the". Remove these from the list here.
13826 */
13827 static void
13828check_suggestions(su, gap)
13829 suginfo_T *su;
13830 garray_T *gap; /* either su_ga or su_sga */
13831{
13832 suggest_T *stp;
13833 int i;
13834 char_u longword[MAXWLEN + 1];
13835 int len;
13836 hlf_T attr;
13837
13838 stp = &SUG(*gap, 0);
13839 for (i = gap->ga_len - 1; i >= 0; --i)
13840 {
13841 /* Need to append what follows to check for "the the". */
13842 STRCPY(longword, stp[i].st_word);
13843 len = stp[i].st_wordlen;
13844 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13845 MAXWLEN - len);
13846 attr = HLF_COUNT;
13847 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13848 if (attr != HLF_COUNT)
13849 {
13850 /* Remove this entry. */
13851 vim_free(stp[i].st_word);
13852 --gap->ga_len;
13853 if (i < gap->ga_len)
13854 mch_memmove(stp + i, stp + i + 1,
13855 sizeof(suggest_T) * (gap->ga_len - i));
13856 }
13857 }
13858}
13859
13860
13861/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 * Add a word to be banned.
13863 */
13864 static void
13865add_banned(su, word)
13866 suginfo_T *su;
13867 char_u *word;
13868{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013869 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013870 hash_T hash;
13871 hashitem_T *hi;
13872
Bram Moolenaar4770d092006-01-12 23:22:24 +000013873 hash = hash_hash(word);
13874 hi = hash_lookup(&su->su_banned, word, hash);
13875 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013877 s = vim_strsave(word);
13878 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013879 hash_add_item(&su->su_banned, hi, s, hash);
13880 }
13881}
13882
13883/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013884 * Recompute the score for all suggestions if sound-folding is possible. This
13885 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013886 */
13887 static void
13888rescore_suggestions(su)
13889 suginfo_T *su;
13890{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013891 int i;
13892
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013893 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013894 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013895 rescore_one(su, &SUG(su->su_ga, i));
13896}
13897
13898/*
13899 * Recompute the score for one suggestion if sound-folding is possible.
13900 */
13901 static void
13902rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013903 suginfo_T *su;
13904 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013905{
13906 slang_T *slang = stp->st_slang;
13907 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013908 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013909
13910 /* Only rescore suggestions that have no sal score yet and do have a
13911 * language. */
13912 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13913 {
13914 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013915 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013916 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013917 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013918 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013919 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013920 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013921
13922 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013923 if (stp->st_altscore == SCORE_MAXMAX)
13924 stp->st_altscore = SCORE_BIG;
13925 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13926 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013927 }
13928}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013930static int
13931#ifdef __BORLANDC__
13932_RTLENTRYF
13933#endif
13934sug_compare __ARGS((const void *s1, const void *s2));
13935
13936/*
13937 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013938 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939 */
13940 static int
13941#ifdef __BORLANDC__
13942_RTLENTRYF
13943#endif
13944sug_compare(s1, s2)
13945 const void *s1;
13946 const void *s2;
13947{
13948 suggest_T *p1 = (suggest_T *)s1;
13949 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013950 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013951
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013952 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013953 {
13954 n = p1->st_altscore - p2->st_altscore;
13955 if (n == 0)
13956 n = STRICMP(p1->st_word, p2->st_word);
13957 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013958 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013959}
13960
13961/*
13962 * Cleanup the suggestions:
13963 * - Sort on score.
13964 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013965 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013966 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013967 static int
13968cleanup_suggestions(gap, maxscore, keep)
13969 garray_T *gap;
13970 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013971 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013973 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 int i;
13975
13976 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013977 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978
13979 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013980 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013982 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013983 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013984 gap->ga_len = keep;
13985 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013987 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988}
13989
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013990#if defined(FEAT_EVAL) || defined(PROTO)
13991/*
13992 * Soundfold a string, for soundfold().
13993 * Result is in allocated memory, NULL for an error.
13994 */
13995 char_u *
13996eval_soundfold(word)
13997 char_u *word;
13998{
13999 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014000 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014001 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014002
Bram Moolenaar860cae12010-06-05 23:22:07 +020014003 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014004 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014005 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014006 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014007 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014008 if (lp->lp_slang->sl_sal.ga_len > 0)
14009 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014010 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014011 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014012 return vim_strsave(sound);
14013 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014014 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014015
14016 /* No language with sound folding, return word as-is. */
14017 return vim_strsave(word);
14018}
14019#endif
14020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021/*
14022 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014023 *
14024 * There are many ways to turn a word into a sound-a-like representation. The
14025 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14026 * swedish name matching - survey and test of different algorithms" by Klas
14027 * Erikson.
14028 *
14029 * We support two methods:
14030 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14031 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 */
14033 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014034spell_soundfold(slang, inword, folded, res)
14035 slang_T *slang;
14036 char_u *inword;
14037 int folded; /* "inword" is already case-folded */
14038 char_u *res;
14039{
14040 char_u fword[MAXWLEN];
14041 char_u *word;
14042
14043 if (slang->sl_sofo)
14044 /* SOFOFROM and SOFOTO used */
14045 spell_soundfold_sofo(slang, inword, res);
14046 else
14047 {
14048 /* SAL items used. Requires the word to be case-folded. */
14049 if (folded)
14050 word = inword;
14051 else
14052 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014053 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014054 word = fword;
14055 }
14056
14057#ifdef FEAT_MBYTE
14058 if (has_mbyte)
14059 spell_soundfold_wsal(slang, word, res);
14060 else
14061#endif
14062 spell_soundfold_sal(slang, word, res);
14063 }
14064}
14065
14066/*
14067 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14068 * SOFOTO lines.
14069 */
14070 static void
14071spell_soundfold_sofo(slang, inword, res)
14072 slang_T *slang;
14073 char_u *inword;
14074 char_u *res;
14075{
14076 char_u *s;
14077 int ri = 0;
14078 int c;
14079
14080#ifdef FEAT_MBYTE
14081 if (has_mbyte)
14082 {
14083 int prevc = 0;
14084 int *ip;
14085
14086 /* The sl_sal_first[] table contains the translation for chars up to
14087 * 255, sl_sal the rest. */
14088 for (s = inword; *s != NUL; )
14089 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014090 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014091 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14092 c = ' ';
14093 else if (c < 256)
14094 c = slang->sl_sal_first[c];
14095 else
14096 {
14097 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14098 if (ip == NULL) /* empty list, can't match */
14099 c = NUL;
14100 else
14101 for (;;) /* find "c" in the list */
14102 {
14103 if (*ip == 0) /* not found */
14104 {
14105 c = NUL;
14106 break;
14107 }
14108 if (*ip == c) /* match! */
14109 {
14110 c = ip[1];
14111 break;
14112 }
14113 ip += 2;
14114 }
14115 }
14116
14117 if (c != NUL && c != prevc)
14118 {
14119 ri += mb_char2bytes(c, res + ri);
14120 if (ri + MB_MAXBYTES > MAXWLEN)
14121 break;
14122 prevc = c;
14123 }
14124 }
14125 }
14126 else
14127#endif
14128 {
14129 /* The sl_sal_first[] table contains the translation. */
14130 for (s = inword; (c = *s) != NUL; ++s)
14131 {
14132 if (vim_iswhite(c))
14133 c = ' ';
14134 else
14135 c = slang->sl_sal_first[c];
14136 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14137 res[ri++] = c;
14138 }
14139 }
14140
14141 res[ri] = NUL;
14142}
14143
14144 static void
14145spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 slang_T *slang;
14147 char_u *inword;
14148 char_u *res;
14149{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014150 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014152 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014154 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014156 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014157 int n, k = 0;
14158 int z0;
14159 int k0;
14160 int n0;
14161 int c;
14162 int pri;
14163 int p0 = -333;
14164 int c0;
14165
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014166 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014167 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 if (slang->sl_rem_accents)
14169 {
14170 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014171 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014173 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014174 {
14175 *t++ = ' ';
14176 s = skipwhite(s);
14177 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014178 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014180 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014181 *t++ = *s;
14182 ++s;
14183 }
14184 }
14185 *t = NUL;
14186 }
14187 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014188 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014190 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191
14192 /*
14193 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014194 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014196 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 while ((c = word[i]) != NUL)
14198 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014199 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 n = slang->sl_sal_first[c];
14201 z0 = 0;
14202
14203 if (n >= 0)
14204 {
14205 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014206 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014208 /* Quickly skip entries that don't match the word. Most
14209 * entries are less then three chars, optimize for that. */
14210 k = smp[n].sm_leadlen;
14211 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014213 if (word[i + 1] != s[1])
14214 continue;
14215 if (k > 2)
14216 {
14217 for (j = 2; j < k; ++j)
14218 if (word[i + j] != s[j])
14219 break;
14220 if (j < k)
14221 continue;
14222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 }
14224
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014225 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014227 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228 while (*pf != NUL && *pf != word[i + k])
14229 ++pf;
14230 if (*pf == NUL)
14231 continue;
14232 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014234 s = smp[n].sm_rules;
14235 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236
14237 p0 = *s;
14238 k0 = k;
14239 while (*s == '-' && k > 1)
14240 {
14241 k--;
14242 s++;
14243 }
14244 if (*s == '<')
14245 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014246 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 {
14248 /* determine priority */
14249 pri = *s - '0';
14250 s++;
14251 }
14252 if (*s == '^' && *(s + 1) == '^')
14253 s++;
14254
14255 if (*s == NUL
14256 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014257 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014258 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014260 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014262 && spell_iswordp(word + i - 1, curwin)
14263 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 {
14265 /* search for followup rules, if: */
14266 /* followup and k > 1 and NO '-' in searchstring */
14267 c0 = word[i + k - 1];
14268 n0 = slang->sl_sal_first[c0];
14269
14270 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014271 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272 {
14273 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014274 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014275 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014276 /* Quickly skip entries that don't match the word.
14277 * */
14278 k0 = smp[n0].sm_leadlen;
14279 if (k0 > 1)
14280 {
14281 if (word[i + k] != s[1])
14282 continue;
14283 if (k0 > 2)
14284 {
14285 pf = word + i + k + 1;
14286 for (j = 2; j < k0; ++j)
14287 if (*pf++ != s[j])
14288 break;
14289 if (j < k0)
14290 continue;
14291 }
14292 }
14293 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014295 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014296 {
14297 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014298 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014299 while (*pf != NUL && *pf != word[i + k0])
14300 ++pf;
14301 if (*pf == NUL)
14302 continue;
14303 ++k0;
14304 }
14305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014307 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 while (*s == '-')
14309 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014310 /* "k0" gets NOT reduced because
14311 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 s++;
14313 }
14314 if (*s == '<')
14315 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014316 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 {
14318 p0 = *s - '0';
14319 s++;
14320 }
14321
14322 if (*s == NUL
14323 /* *s == '^' cuts */
14324 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014325 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014326 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014327 {
14328 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331
14332 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014334 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 /* rule fits; stop search */
14336 break;
14337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 }
14339
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014340 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 }
14343
14344 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014345 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014346 if (s == NULL)
14347 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014348 pf = smp[n].sm_rules;
14349 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 if (p0 == 1 && z == 0)
14351 {
14352 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014353 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14354 || res[reslen - 1] == *s))
14355 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 z0 = 1;
14357 z = 1;
14358 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014359 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 {
14361 word[i + k0] = *s;
14362 k0++;
14363 s++;
14364 }
14365 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014366 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367
14368 /* new "actual letter" */
14369 c = word[i];
14370 }
14371 else
14372 {
14373 /* no '<' rule used */
14374 i += k - 1;
14375 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014376 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014378 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014379 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 s++;
14381 }
14382 /* new "actual letter" */
14383 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014384 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 {
14386 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014387 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014388 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 i = 0;
14390 z0 = 1;
14391 }
14392 }
14393 break;
14394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 }
14396 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014397 else if (vim_iswhite(c))
14398 {
14399 c = ' ';
14400 k = 1;
14401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402
14403 if (z0 == 0)
14404 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014405 if (k && !p0 && reslen < MAXWLEN && c != NUL
14406 && (!slang->sl_collapse || reslen == 0
14407 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014409 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410
14411 i++;
14412 z = 0;
14413 k = 0;
14414 }
14415 }
14416
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014417 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418}
14419
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014420#ifdef FEAT_MBYTE
14421/*
14422 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14423 * Multi-byte version of spell_soundfold().
14424 */
14425 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014426spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014427 slang_T *slang;
14428 char_u *inword;
14429 char_u *res;
14430{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014431 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014432 int word[MAXWLEN];
14433 int wres[MAXWLEN];
14434 int l;
14435 char_u *s;
14436 int *ws;
14437 char_u *t;
14438 int *pf;
14439 int i, j, z;
14440 int reslen;
14441 int n, k = 0;
14442 int z0;
14443 int k0;
14444 int n0;
14445 int c;
14446 int pri;
14447 int p0 = -333;
14448 int c0;
14449 int did_white = FALSE;
14450
14451 /*
14452 * Convert the multi-byte string to a wide-character string.
14453 * Remove accents, if wanted. We actually remove all non-word characters.
14454 * But keep white space.
14455 */
14456 n = 0;
14457 for (s = inword; *s != NUL; )
14458 {
14459 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014460 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014461 if (slang->sl_rem_accents)
14462 {
14463 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14464 {
14465 if (did_white)
14466 continue;
14467 c = ' ';
14468 did_white = TRUE;
14469 }
14470 else
14471 {
14472 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014473 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014474 continue;
14475 }
14476 }
14477 word[n++] = c;
14478 }
14479 word[n] = NUL;
14480
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014481 /*
14482 * This comes from Aspell phonet.cpp.
14483 * Converted from C++ to C. Added support for multi-byte chars.
14484 * Changed to keep spaces.
14485 */
14486 i = reslen = z = 0;
14487 while ((c = word[i]) != NUL)
14488 {
14489 /* Start with the first rule that has the character in the word. */
14490 n = slang->sl_sal_first[c & 0xff];
14491 z0 = 0;
14492
14493 if (n >= 0)
14494 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014495 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014496 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14497 {
14498 /* Quickly skip entries that don't match the word. Most
14499 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014500 if (c != ws[0])
14501 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014502 k = smp[n].sm_leadlen;
14503 if (k > 1)
14504 {
14505 if (word[i + 1] != ws[1])
14506 continue;
14507 if (k > 2)
14508 {
14509 for (j = 2; j < k; ++j)
14510 if (word[i + j] != ws[j])
14511 break;
14512 if (j < k)
14513 continue;
14514 }
14515 }
14516
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014517 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014518 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014519 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014520 while (*pf != NUL && *pf != word[i + k])
14521 ++pf;
14522 if (*pf == NUL)
14523 continue;
14524 ++k;
14525 }
14526 s = smp[n].sm_rules;
14527 pri = 5; /* default priority */
14528
14529 p0 = *s;
14530 k0 = k;
14531 while (*s == '-' && k > 1)
14532 {
14533 k--;
14534 s++;
14535 }
14536 if (*s == '<')
14537 s++;
14538 if (VIM_ISDIGIT(*s))
14539 {
14540 /* determine priority */
14541 pri = *s - '0';
14542 s++;
14543 }
14544 if (*s == '^' && *(s + 1) == '^')
14545 s++;
14546
14547 if (*s == NUL
14548 || (*s == '^'
14549 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014550 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014551 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014552 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014553 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014554 && spell_iswordp_w(word + i - 1, curwin)
14555 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014556 {
14557 /* search for followup rules, if: */
14558 /* followup and k > 1 and NO '-' in searchstring */
14559 c0 = word[i + k - 1];
14560 n0 = slang->sl_sal_first[c0 & 0xff];
14561
14562 if (slang->sl_followup && k > 1 && n0 >= 0
14563 && p0 != '-' && word[i + k] != NUL)
14564 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014565 /* Test follow-up rule for "word[i + k]"; loop over
14566 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014567 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14568 == (c0 & 0xff); ++n0)
14569 {
14570 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014571 */
14572 if (c0 != ws[0])
14573 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014574 k0 = smp[n0].sm_leadlen;
14575 if (k0 > 1)
14576 {
14577 if (word[i + k] != ws[1])
14578 continue;
14579 if (k0 > 2)
14580 {
14581 pf = word + i + k + 1;
14582 for (j = 2; j < k0; ++j)
14583 if (*pf++ != ws[j])
14584 break;
14585 if (j < k0)
14586 continue;
14587 }
14588 }
14589 k0 += k - 1;
14590
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014591 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014592 {
14593 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014594 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014595 while (*pf != NUL && *pf != word[i + k0])
14596 ++pf;
14597 if (*pf == NUL)
14598 continue;
14599 ++k0;
14600 }
14601
14602 p0 = 5;
14603 s = smp[n0].sm_rules;
14604 while (*s == '-')
14605 {
14606 /* "k0" gets NOT reduced because
14607 * "if (k0 == k)" */
14608 s++;
14609 }
14610 if (*s == '<')
14611 s++;
14612 if (VIM_ISDIGIT(*s))
14613 {
14614 p0 = *s - '0';
14615 s++;
14616 }
14617
14618 if (*s == NUL
14619 /* *s == '^' cuts */
14620 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014621 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014622 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014623 {
14624 if (k0 == k)
14625 /* this is just a piece of the string */
14626 continue;
14627
14628 if (p0 < pri)
14629 /* priority too low */
14630 continue;
14631 /* rule fits; stop search */
14632 break;
14633 }
14634 }
14635
14636 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14637 == (c0 & 0xff))
14638 continue;
14639 }
14640
14641 /* replace string */
14642 ws = smp[n].sm_to_w;
14643 s = smp[n].sm_rules;
14644 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14645 if (p0 == 1 && z == 0)
14646 {
14647 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014648 if (reslen > 0 && ws != NULL && *ws != NUL
14649 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014650 || wres[reslen - 1] == *ws))
14651 reslen--;
14652 z0 = 1;
14653 z = 1;
14654 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014655 if (ws != NULL)
14656 while (*ws != NUL && word[i + k0] != NUL)
14657 {
14658 word[i + k0] = *ws;
14659 k0++;
14660 ws++;
14661 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014662 if (k > k0)
14663 mch_memmove(word + i + k0, word + i + k,
14664 sizeof(int) * (STRLEN(word + i + k) + 1));
14665
14666 /* new "actual letter" */
14667 c = word[i];
14668 }
14669 else
14670 {
14671 /* no '<' rule used */
14672 i += k - 1;
14673 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014674 if (ws != NULL)
14675 while (*ws != NUL && ws[1] != NUL
14676 && reslen < MAXWLEN)
14677 {
14678 if (reslen == 0 || wres[reslen - 1] != *ws)
14679 wres[reslen++] = *ws;
14680 ws++;
14681 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014682 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014683 if (ws == NULL)
14684 c = NUL;
14685 else
14686 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014687 if (strstr((char *)s, "^^") != NULL)
14688 {
14689 if (c != NUL)
14690 wres[reslen++] = c;
14691 mch_memmove(word, word + i + 1,
14692 sizeof(int) * (STRLEN(word + i + 1) + 1));
14693 i = 0;
14694 z0 = 1;
14695 }
14696 }
14697 break;
14698 }
14699 }
14700 }
14701 else if (vim_iswhite(c))
14702 {
14703 c = ' ';
14704 k = 1;
14705 }
14706
14707 if (z0 == 0)
14708 {
14709 if (k && !p0 && reslen < MAXWLEN && c != NUL
14710 && (!slang->sl_collapse || reslen == 0
14711 || wres[reslen - 1] != c))
14712 /* condense only double letters */
14713 wres[reslen++] = c;
14714
14715 i++;
14716 z = 0;
14717 k = 0;
14718 }
14719 }
14720
14721 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14722 l = 0;
14723 for (n = 0; n < reslen; ++n)
14724 {
14725 l += mb_char2bytes(wres[n], res + l);
14726 if (l + MB_MAXBYTES > MAXWLEN)
14727 break;
14728 }
14729 res[l] = NUL;
14730}
14731#endif
14732
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014733/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014734 * Compute a score for two sound-a-like words.
14735 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14736 * Instead of a generic loop we write out the code. That keeps it fast by
14737 * avoiding checks that will not be possible.
14738 */
14739 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014740soundalike_score(goodstart, badstart)
14741 char_u *goodstart; /* sound-folded good word */
14742 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014743{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014744 char_u *goodsound = goodstart;
14745 char_u *badsound = badstart;
14746 int goodlen;
14747 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014748 int n;
14749 char_u *pl, *ps;
14750 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014751 int score = 0;
14752
14753 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14754 * counted so much, vowels halfway the word aren't counted at all. */
14755 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14756 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014757 if (badsound[1] == goodsound[1]
14758 || (badsound[1] != NUL
14759 && goodsound[1] != NUL
14760 && badsound[2] == goodsound[2]))
14761 {
14762 /* handle like a substitute */
14763 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014764 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014765 {
14766 score = 2 * SCORE_DEL / 3;
14767 if (*badsound == '*')
14768 ++badsound;
14769 else
14770 ++goodsound;
14771 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014772 }
14773
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014774 goodlen = (int)STRLEN(goodsound);
14775 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014776
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014777 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014778 * changes. */
14779 n = goodlen - badlen;
14780 if (n < -2 || n > 2)
14781 return SCORE_MAXMAX;
14782
14783 if (n > 0)
14784 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014785 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014786 ps = badsound;
14787 }
14788 else
14789 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014790 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014791 ps = goodsound;
14792 }
14793
14794 /* Skip over the identical part. */
14795 while (*pl == *ps && *pl != NUL)
14796 {
14797 ++pl;
14798 ++ps;
14799 }
14800
14801 switch (n)
14802 {
14803 case -2:
14804 case 2:
14805 /*
14806 * Must delete two characters from "pl".
14807 */
14808 ++pl; /* first delete */
14809 while (*pl == *ps)
14810 {
14811 ++pl;
14812 ++ps;
14813 }
14814 /* strings must be equal after second delete */
14815 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014816 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014817
14818 /* Failed to compare. */
14819 break;
14820
14821 case -1:
14822 case 1:
14823 /*
14824 * Minimal one delete from "pl" required.
14825 */
14826
14827 /* 1: delete */
14828 pl2 = pl + 1;
14829 ps2 = ps;
14830 while (*pl2 == *ps2)
14831 {
14832 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014833 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014834 ++pl2;
14835 ++ps2;
14836 }
14837
14838 /* 2: delete then swap, then rest must be equal */
14839 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14840 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014841 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014842
14843 /* 3: delete then substitute, then the rest must be equal */
14844 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014845 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014846
14847 /* 4: first swap then delete */
14848 if (pl[0] == ps[1] && pl[1] == ps[0])
14849 {
14850 pl2 = pl + 2; /* swap, skip two chars */
14851 ps2 = ps + 2;
14852 while (*pl2 == *ps2)
14853 {
14854 ++pl2;
14855 ++ps2;
14856 }
14857 /* delete a char and then strings must be equal */
14858 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014859 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014860 }
14861
14862 /* 5: first substitute then delete */
14863 pl2 = pl + 1; /* substitute, skip one char */
14864 ps2 = ps + 1;
14865 while (*pl2 == *ps2)
14866 {
14867 ++pl2;
14868 ++ps2;
14869 }
14870 /* delete a char and then strings must be equal */
14871 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014872 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014873
14874 /* Failed to compare. */
14875 break;
14876
14877 case 0:
14878 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014879 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014880 * insert is only possible in combination with a delete.
14881 * 1: check if for identical strings
14882 */
14883 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014884 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014885
14886 /* 2: swap */
14887 if (pl[0] == ps[1] && pl[1] == ps[0])
14888 {
14889 pl2 = pl + 2; /* swap, skip two chars */
14890 ps2 = ps + 2;
14891 while (*pl2 == *ps2)
14892 {
14893 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014894 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014895 ++pl2;
14896 ++ps2;
14897 }
14898 /* 3: swap and swap again */
14899 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14900 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014901 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014902
14903 /* 4: swap and substitute */
14904 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014905 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014906 }
14907
14908 /* 5: substitute */
14909 pl2 = pl + 1;
14910 ps2 = ps + 1;
14911 while (*pl2 == *ps2)
14912 {
14913 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014914 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014915 ++pl2;
14916 ++ps2;
14917 }
14918
14919 /* 6: substitute and swap */
14920 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14921 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014922 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014923
14924 /* 7: substitute and substitute */
14925 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014926 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014927
14928 /* 8: insert then delete */
14929 pl2 = pl;
14930 ps2 = ps + 1;
14931 while (*pl2 == *ps2)
14932 {
14933 ++pl2;
14934 ++ps2;
14935 }
14936 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014937 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014938
14939 /* 9: delete then insert */
14940 pl2 = pl + 1;
14941 ps2 = ps;
14942 while (*pl2 == *ps2)
14943 {
14944 ++pl2;
14945 ++ps2;
14946 }
14947 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014948 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014949
14950 /* Failed to compare. */
14951 break;
14952 }
14953
14954 return SCORE_MAXMAX;
14955}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014957/*
14958 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014959 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014960 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014961 * The algorithm is described by Du and Chang, 1992.
14962 * The implementation of the algorithm comes from Aspell editdist.cpp,
14963 * edit_distance(). It has been converted from C++ to C and modified to
14964 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014965 */
14966 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014967spell_edit_score(slang, badword, goodword)
14968 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014969 char_u *badword;
14970 char_u *goodword;
14971{
14972 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014973 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 int j, i;
14975 int t;
14976 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014977 int pbc, pgc;
14978#ifdef FEAT_MBYTE
14979 char_u *p;
14980 int wbadword[MAXWLEN];
14981 int wgoodword[MAXWLEN];
14982
14983 if (has_mbyte)
14984 {
14985 /* Get the characters from the multi-byte strings and put them in an
14986 * int array for easy access. */
14987 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014988 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014989 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014990 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014991 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014992 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014993 }
14994 else
14995#endif
14996 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014997 badlen = (int)STRLEN(badword) + 1;
14998 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014999 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015000
15001 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15002#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015003 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15004 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015005 if (cnt == NULL)
15006 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015007
15008 CNT(0, 0) = 0;
15009 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015010 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015011
15012 for (i = 1; i <= badlen; ++i)
15013 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015014 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 for (j = 1; j <= goodlen; ++j)
15016 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015017#ifdef FEAT_MBYTE
15018 if (has_mbyte)
15019 {
15020 bc = wbadword[i - 1];
15021 gc = wgoodword[j - 1];
15022 }
15023 else
15024#endif
15025 {
15026 bc = badword[i - 1];
15027 gc = goodword[j - 1];
15028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015029 if (bc == gc)
15030 CNT(i, j) = CNT(i - 1, j - 1);
15031 else
15032 {
15033 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015034 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015035 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15036 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015037 {
15038 /* For a similar character use SCORE_SIMILAR. */
15039 if (slang != NULL
15040 && slang->sl_has_map
15041 && similar_chars(slang, gc, bc))
15042 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15043 else
15044 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015047 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015049#ifdef FEAT_MBYTE
15050 if (has_mbyte)
15051 {
15052 pbc = wbadword[i - 2];
15053 pgc = wgoodword[j - 2];
15054 }
15055 else
15056#endif
15057 {
15058 pbc = badword[i - 2];
15059 pgc = goodword[j - 2];
15060 }
15061 if (bc == pgc && pbc == gc)
15062 {
15063 t = SCORE_SWAP + CNT(i - 2, j - 2);
15064 if (t < CNT(i, j))
15065 CNT(i, j) = t;
15066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015067 }
15068 t = SCORE_DEL + CNT(i - 1, j);
15069 if (t < CNT(i, j))
15070 CNT(i, j) = t;
15071 t = SCORE_INS + CNT(i, j - 1);
15072 if (t < CNT(i, j))
15073 CNT(i, j) = t;
15074 }
15075 }
15076 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015077
15078 i = CNT(badlen - 1, goodlen - 1);
15079 vim_free(cnt);
15080 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015081}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015082
Bram Moolenaar4770d092006-01-12 23:22:24 +000015083typedef struct
15084{
15085 int badi;
15086 int goodi;
15087 int score;
15088} limitscore_T;
15089
15090/*
15091 * Like spell_edit_score(), but with a limit on the score to make it faster.
15092 * May return SCORE_MAXMAX when the score is higher than "limit".
15093 *
15094 * This uses a stack for the edits still to be tried.
15095 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15096 * for multi-byte characters.
15097 */
15098 static int
15099spell_edit_score_limit(slang, badword, goodword, limit)
15100 slang_T *slang;
15101 char_u *badword;
15102 char_u *goodword;
15103 int limit;
15104{
15105 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15106 int stackidx;
15107 int bi, gi;
15108 int bi2, gi2;
15109 int bc, gc;
15110 int score;
15111 int score_off;
15112 int minscore;
15113 int round;
15114
15115#ifdef FEAT_MBYTE
15116 /* Multi-byte characters require a bit more work, use a different function
15117 * to avoid testing "has_mbyte" quite often. */
15118 if (has_mbyte)
15119 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15120#endif
15121
15122 /*
15123 * The idea is to go from start to end over the words. So long as
15124 * characters are equal just continue, this always gives the lowest score.
15125 * When there is a difference try several alternatives. Each alternative
15126 * increases "score" for the edit distance. Some of the alternatives are
15127 * pushed unto a stack and tried later, some are tried right away. At the
15128 * end of the word the score for one alternative is known. The lowest
15129 * possible score is stored in "minscore".
15130 */
15131 stackidx = 0;
15132 bi = 0;
15133 gi = 0;
15134 score = 0;
15135 minscore = limit + 1;
15136
15137 for (;;)
15138 {
15139 /* Skip over an equal part, score remains the same. */
15140 for (;;)
15141 {
15142 bc = badword[bi];
15143 gc = goodword[gi];
15144 if (bc != gc) /* stop at a char that's different */
15145 break;
15146 if (bc == NUL) /* both words end */
15147 {
15148 if (score < minscore)
15149 minscore = score;
15150 goto pop; /* do next alternative */
15151 }
15152 ++bi;
15153 ++gi;
15154 }
15155
15156 if (gc == NUL) /* goodword ends, delete badword chars */
15157 {
15158 do
15159 {
15160 if ((score += SCORE_DEL) >= minscore)
15161 goto pop; /* do next alternative */
15162 } while (badword[++bi] != NUL);
15163 minscore = score;
15164 }
15165 else if (bc == NUL) /* badword ends, insert badword chars */
15166 {
15167 do
15168 {
15169 if ((score += SCORE_INS) >= minscore)
15170 goto pop; /* do next alternative */
15171 } while (goodword[++gi] != NUL);
15172 minscore = score;
15173 }
15174 else /* both words continue */
15175 {
15176 /* If not close to the limit, perform a change. Only try changes
15177 * that may lead to a lower score than "minscore".
15178 * round 0: try deleting a char from badword
15179 * round 1: try inserting a char in badword */
15180 for (round = 0; round <= 1; ++round)
15181 {
15182 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15183 if (score_off < minscore)
15184 {
15185 if (score_off + SCORE_EDIT_MIN >= minscore)
15186 {
15187 /* Near the limit, rest of the words must match. We
15188 * can check that right now, no need to push an item
15189 * onto the stack. */
15190 bi2 = bi + 1 - round;
15191 gi2 = gi + round;
15192 while (goodword[gi2] == badword[bi2])
15193 {
15194 if (goodword[gi2] == NUL)
15195 {
15196 minscore = score_off;
15197 break;
15198 }
15199 ++bi2;
15200 ++gi2;
15201 }
15202 }
15203 else
15204 {
15205 /* try deleting/inserting a character later */
15206 stack[stackidx].badi = bi + 1 - round;
15207 stack[stackidx].goodi = gi + round;
15208 stack[stackidx].score = score_off;
15209 ++stackidx;
15210 }
15211 }
15212 }
15213
15214 if (score + SCORE_SWAP < minscore)
15215 {
15216 /* If swapping two characters makes a match then the
15217 * substitution is more expensive, thus there is no need to
15218 * try both. */
15219 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15220 {
15221 /* Swap two characters, that is: skip them. */
15222 gi += 2;
15223 bi += 2;
15224 score += SCORE_SWAP;
15225 continue;
15226 }
15227 }
15228
15229 /* Substitute one character for another which is the same
15230 * thing as deleting a character from both goodword and badword.
15231 * Use a better score when there is only a case difference. */
15232 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15233 score += SCORE_ICASE;
15234 else
15235 {
15236 /* For a similar character use SCORE_SIMILAR. */
15237 if (slang != NULL
15238 && slang->sl_has_map
15239 && similar_chars(slang, gc, bc))
15240 score += SCORE_SIMILAR;
15241 else
15242 score += SCORE_SUBST;
15243 }
15244
15245 if (score < minscore)
15246 {
15247 /* Do the substitution. */
15248 ++gi;
15249 ++bi;
15250 continue;
15251 }
15252 }
15253pop:
15254 /*
15255 * Get here to try the next alternative, pop it from the stack.
15256 */
15257 if (stackidx == 0) /* stack is empty, finished */
15258 break;
15259
15260 /* pop an item from the stack */
15261 --stackidx;
15262 gi = stack[stackidx].goodi;
15263 bi = stack[stackidx].badi;
15264 score = stack[stackidx].score;
15265 }
15266
15267 /* When the score goes over "limit" it may actually be much higher.
15268 * Return a very large number to avoid going below the limit when giving a
15269 * bonus. */
15270 if (minscore > limit)
15271 return SCORE_MAXMAX;
15272 return minscore;
15273}
15274
15275#ifdef FEAT_MBYTE
15276/*
15277 * Multi-byte version of spell_edit_score_limit().
15278 * Keep it in sync with the above!
15279 */
15280 static int
15281spell_edit_score_limit_w(slang, badword, goodword, limit)
15282 slang_T *slang;
15283 char_u *badword;
15284 char_u *goodword;
15285 int limit;
15286{
15287 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15288 int stackidx;
15289 int bi, gi;
15290 int bi2, gi2;
15291 int bc, gc;
15292 int score;
15293 int score_off;
15294 int minscore;
15295 int round;
15296 char_u *p;
15297 int wbadword[MAXWLEN];
15298 int wgoodword[MAXWLEN];
15299
15300 /* Get the characters from the multi-byte strings and put them in an
15301 * int array for easy access. */
15302 bi = 0;
15303 for (p = badword; *p != NUL; )
15304 wbadword[bi++] = mb_cptr2char_adv(&p);
15305 wbadword[bi++] = 0;
15306 gi = 0;
15307 for (p = goodword; *p != NUL; )
15308 wgoodword[gi++] = mb_cptr2char_adv(&p);
15309 wgoodword[gi++] = 0;
15310
15311 /*
15312 * The idea is to go from start to end over the words. So long as
15313 * characters are equal just continue, this always gives the lowest score.
15314 * When there is a difference try several alternatives. Each alternative
15315 * increases "score" for the edit distance. Some of the alternatives are
15316 * pushed unto a stack and tried later, some are tried right away. At the
15317 * end of the word the score for one alternative is known. The lowest
15318 * possible score is stored in "minscore".
15319 */
15320 stackidx = 0;
15321 bi = 0;
15322 gi = 0;
15323 score = 0;
15324 minscore = limit + 1;
15325
15326 for (;;)
15327 {
15328 /* Skip over an equal part, score remains the same. */
15329 for (;;)
15330 {
15331 bc = wbadword[bi];
15332 gc = wgoodword[gi];
15333
15334 if (bc != gc) /* stop at a char that's different */
15335 break;
15336 if (bc == NUL) /* both words end */
15337 {
15338 if (score < minscore)
15339 minscore = score;
15340 goto pop; /* do next alternative */
15341 }
15342 ++bi;
15343 ++gi;
15344 }
15345
15346 if (gc == NUL) /* goodword ends, delete badword chars */
15347 {
15348 do
15349 {
15350 if ((score += SCORE_DEL) >= minscore)
15351 goto pop; /* do next alternative */
15352 } while (wbadword[++bi] != NUL);
15353 minscore = score;
15354 }
15355 else if (bc == NUL) /* badword ends, insert badword chars */
15356 {
15357 do
15358 {
15359 if ((score += SCORE_INS) >= minscore)
15360 goto pop; /* do next alternative */
15361 } while (wgoodword[++gi] != NUL);
15362 minscore = score;
15363 }
15364 else /* both words continue */
15365 {
15366 /* If not close to the limit, perform a change. Only try changes
15367 * that may lead to a lower score than "minscore".
15368 * round 0: try deleting a char from badword
15369 * round 1: try inserting a char in badword */
15370 for (round = 0; round <= 1; ++round)
15371 {
15372 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15373 if (score_off < minscore)
15374 {
15375 if (score_off + SCORE_EDIT_MIN >= minscore)
15376 {
15377 /* Near the limit, rest of the words must match. We
15378 * can check that right now, no need to push an item
15379 * onto the stack. */
15380 bi2 = bi + 1 - round;
15381 gi2 = gi + round;
15382 while (wgoodword[gi2] == wbadword[bi2])
15383 {
15384 if (wgoodword[gi2] == NUL)
15385 {
15386 minscore = score_off;
15387 break;
15388 }
15389 ++bi2;
15390 ++gi2;
15391 }
15392 }
15393 else
15394 {
15395 /* try deleting a character from badword later */
15396 stack[stackidx].badi = bi + 1 - round;
15397 stack[stackidx].goodi = gi + round;
15398 stack[stackidx].score = score_off;
15399 ++stackidx;
15400 }
15401 }
15402 }
15403
15404 if (score + SCORE_SWAP < minscore)
15405 {
15406 /* If swapping two characters makes a match then the
15407 * substitution is more expensive, thus there is no need to
15408 * try both. */
15409 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15410 {
15411 /* Swap two characters, that is: skip them. */
15412 gi += 2;
15413 bi += 2;
15414 score += SCORE_SWAP;
15415 continue;
15416 }
15417 }
15418
15419 /* Substitute one character for another which is the same
15420 * thing as deleting a character from both goodword and badword.
15421 * Use a better score when there is only a case difference. */
15422 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15423 score += SCORE_ICASE;
15424 else
15425 {
15426 /* For a similar character use SCORE_SIMILAR. */
15427 if (slang != NULL
15428 && slang->sl_has_map
15429 && similar_chars(slang, gc, bc))
15430 score += SCORE_SIMILAR;
15431 else
15432 score += SCORE_SUBST;
15433 }
15434
15435 if (score < minscore)
15436 {
15437 /* Do the substitution. */
15438 ++gi;
15439 ++bi;
15440 continue;
15441 }
15442 }
15443pop:
15444 /*
15445 * Get here to try the next alternative, pop it from the stack.
15446 */
15447 if (stackidx == 0) /* stack is empty, finished */
15448 break;
15449
15450 /* pop an item from the stack */
15451 --stackidx;
15452 gi = stack[stackidx].goodi;
15453 bi = stack[stackidx].badi;
15454 score = stack[stackidx].score;
15455 }
15456
15457 /* When the score goes over "limit" it may actually be much higher.
15458 * Return a very large number to avoid going below the limit when giving a
15459 * bonus. */
15460 if (minscore > limit)
15461 return SCORE_MAXMAX;
15462 return minscore;
15463}
15464#endif
15465
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015466/*
15467 * ":spellinfo"
15468 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015469 void
15470ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015471 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015472{
15473 int lpi;
15474 langp_T *lp;
15475 char_u *p;
15476
15477 if (no_spell_checking(curwin))
15478 return;
15479
15480 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015481 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015482 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015483 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015484 msg_puts((char_u *)"file: ");
15485 msg_puts(lp->lp_slang->sl_fname);
15486 msg_putchar('\n');
15487 p = lp->lp_slang->sl_info;
15488 if (p != NULL)
15489 {
15490 msg_puts(p);
15491 msg_putchar('\n');
15492 }
15493 }
15494 msg_end();
15495}
15496
Bram Moolenaar4770d092006-01-12 23:22:24 +000015497#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15498#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015499#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015500#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15501#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015502
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015503/*
15504 * ":spelldump"
15505 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015506 void
15507ex_spelldump(eap)
15508 exarg_T *eap;
15509{
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015510 if (no_spell_checking(curwin))
15511 return;
15512
15513 /* Create a new empty buffer by splitting the window. */
15514 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar860cae12010-06-05 23:22:07 +020015515 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015516 return;
15517
Bram Moolenaar860cae12010-06-05 23:22:07 +020015518 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015519
15520 /* Delete the empty line that we started with. */
15521 if (curbuf->b_ml.ml_line_count > 1)
15522 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15523
15524 redraw_later(NOT_VALID);
15525}
15526
15527/*
15528 * Go through all possible words and:
15529 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15530 * "ic" and "dir" are not used.
15531 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15532 */
15533 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015534spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015535 char_u *pat; /* leading part of the word */
15536 int ic; /* ignore case */
15537 int *dir; /* direction for adding matches */
15538 int dumpflags_arg; /* DUMPFLAG_* */
15539{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015540 langp_T *lp;
15541 slang_T *slang;
15542 idx_T arridx[MAXWLEN];
15543 int curi[MAXWLEN];
15544 char_u word[MAXWLEN];
15545 int c;
15546 char_u *byts;
15547 idx_T *idxs;
15548 linenr_T lnum = 0;
15549 int round;
15550 int depth;
15551 int n;
15552 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015553 char_u *region_names = NULL; /* region names being used */
15554 int do_region = TRUE; /* dump region names and numbers */
15555 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015556 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015557 int dumpflags = dumpflags_arg;
15558 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015559
Bram Moolenaard0131a82006-03-04 21:46:13 +000015560 /* When ignoring case or when the pattern starts with capital pass this on
15561 * to dump_word(). */
15562 if (pat != NULL)
15563 {
15564 if (ic)
15565 dumpflags |= DUMPFLAG_ICASE;
15566 else
15567 {
15568 n = captype(pat, NULL);
15569 if (n == WF_ONECAP)
15570 dumpflags |= DUMPFLAG_ONECAP;
15571 else if (n == WF_ALLCAP
15572#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015573 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015574#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015575 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015576#endif
15577 )
15578 dumpflags |= DUMPFLAG_ALLCAP;
15579 }
15580 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015581
Bram Moolenaar7887d882005-07-01 22:33:52 +000015582 /* Find out if we can support regions: All languages must support the same
15583 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015584 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015585 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015586 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015587 p = lp->lp_slang->sl_regions;
15588 if (p[0] != 0)
15589 {
15590 if (region_names == NULL) /* first language with regions */
15591 region_names = p;
15592 else if (STRCMP(region_names, p) != 0)
15593 {
15594 do_region = FALSE; /* region names are different */
15595 break;
15596 }
15597 }
15598 }
15599
15600 if (do_region && region_names != NULL)
15601 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015602 if (pat == NULL)
15603 {
15604 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15605 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15606 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015607 }
15608 else
15609 do_region = FALSE;
15610
15611 /*
15612 * Loop over all files loaded for the entries in 'spelllang'.
15613 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015614 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015615 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015616 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015617 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015618 if (slang->sl_fbyts == NULL) /* reloading failed */
15619 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015620
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015621 if (pat == NULL)
15622 {
15623 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15624 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15625 }
15626
15627 /* When matching with a pattern and there are no prefixes only use
15628 * parts of the tree that match "pat". */
15629 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015630 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015631 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015632 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015633
15634 /* round 1: case-folded tree
15635 * round 2: keep-case tree */
15636 for (round = 1; round <= 2; ++round)
15637 {
15638 if (round == 1)
15639 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015640 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015641 byts = slang->sl_fbyts;
15642 idxs = slang->sl_fidxs;
15643 }
15644 else
15645 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015646 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015647 byts = slang->sl_kbyts;
15648 idxs = slang->sl_kidxs;
15649 }
15650 if (byts == NULL)
15651 continue; /* array is empty */
15652
15653 depth = 0;
15654 arridx[0] = 0;
15655 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015656 while (depth >= 0 && !got_int
15657 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015658 {
15659 if (curi[depth] > byts[arridx[depth]])
15660 {
15661 /* Done all bytes at this node, go up one level. */
15662 --depth;
15663 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015664 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015665 }
15666 else
15667 {
15668 /* Do one more byte at this node. */
15669 n = arridx[depth] + curi[depth];
15670 ++curi[depth];
15671 c = byts[n];
15672 if (c == 0)
15673 {
15674 /* End of word, deal with the word.
15675 * Don't use keep-case words in the fold-case tree,
15676 * they will appear in the keep-case tree.
15677 * Only use the word when the region matches. */
15678 flags = (int)idxs[n];
15679 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015680 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015681 && (do_region
15682 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015683 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015684 & lp->lp_region) != 0))
15685 {
15686 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015687 if (!do_region)
15688 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015689
15690 /* Dump the basic word if there is no prefix or
15691 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015692 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015693 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015694 {
15695 dump_word(slang, word, pat, dir,
15696 dumpflags, flags, lnum);
15697 if (pat == NULL)
15698 ++lnum;
15699 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015700
15701 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015702 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015703 lnum = dump_prefixes(slang, word, pat, dir,
15704 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015705 }
15706 }
15707 else
15708 {
15709 /* Normal char, go one level deeper. */
15710 word[depth++] = c;
15711 arridx[depth] = idxs[n];
15712 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015713
15714 /* Check if this characters matches with the pattern.
15715 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015716 * Always ignore case here, dump_word() will check
15717 * proper case later. This isn't exactly right when
15718 * length changes for multi-byte characters with
15719 * ignore case... */
15720 if (depth <= patlen
15721 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015722 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015723 }
15724 }
15725 }
15726 }
15727 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015728}
15729
15730/*
15731 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015732 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015733 */
15734 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015735dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015736 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015737 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015738 char_u *pat;
15739 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015740 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015741 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015742 linenr_T lnum;
15743{
15744 int keepcap = FALSE;
15745 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015746 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015747 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015748 char_u badword[MAXWLEN + 10];
15749 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015750 int flags = wordflags;
15751
15752 if (dumpflags & DUMPFLAG_ONECAP)
15753 flags |= WF_ONECAP;
15754 if (dumpflags & DUMPFLAG_ALLCAP)
15755 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015756
Bram Moolenaar4770d092006-01-12 23:22:24 +000015757 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015758 {
15759 /* Need to fix case according to "flags". */
15760 make_case_word(word, cword, flags);
15761 p = cword;
15762 }
15763 else
15764 {
15765 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015766 if ((dumpflags & DUMPFLAG_KEEPCASE)
15767 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015768 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015769 keepcap = TRUE;
15770 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015771 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015772
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015773 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015774 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015775 /* Add flags and regions after a slash. */
15776 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015777 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015778 STRCPY(badword, p);
15779 STRCAT(badword, "/");
15780 if (keepcap)
15781 STRCAT(badword, "=");
15782 if (flags & WF_BANNED)
15783 STRCAT(badword, "!");
15784 else if (flags & WF_RARE)
15785 STRCAT(badword, "?");
15786 if (flags & WF_REGION)
15787 for (i = 0; i < 7; ++i)
15788 if (flags & (0x10000 << i))
15789 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15790 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015791 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015792
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015793 if (dumpflags & DUMPFLAG_COUNT)
15794 {
15795 hashitem_T *hi;
15796
15797 /* Include the word count for ":spelldump!". */
15798 hi = hash_find(&slang->sl_wordcount, tw);
15799 if (!HASHITEM_EMPTY(hi))
15800 {
15801 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15802 tw, HI2WC(hi)->wc_count);
15803 p = IObuff;
15804 }
15805 }
15806
15807 ml_append(lnum, p, (colnr_T)0, FALSE);
15808 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015809 else if (((dumpflags & DUMPFLAG_ICASE)
15810 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15811 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015812 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015813 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015814 /* if dir was BACKWARD then honor it just once */
15815 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015816}
15817
15818/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015819 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15820 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015821 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015822 * Return the updated line number.
15823 */
15824 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015825dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015826 slang_T *slang;
15827 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015828 char_u *pat;
15829 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015830 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015831 int flags; /* flags with prefix ID */
15832 linenr_T startlnum;
15833{
15834 idx_T arridx[MAXWLEN];
15835 int curi[MAXWLEN];
15836 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015837 char_u word_up[MAXWLEN];
15838 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015839 int c;
15840 char_u *byts;
15841 idx_T *idxs;
15842 linenr_T lnum = startlnum;
15843 int depth;
15844 int n;
15845 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015846 int i;
15847
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015848 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015849 * upper-case letter in word_up[]. */
15850 c = PTR2CHAR(word);
15851 if (SPELL_TOUPPER(c) != c)
15852 {
15853 onecap_copy(word, word_up, TRUE);
15854 has_word_up = TRUE;
15855 }
15856
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015857 byts = slang->sl_pbyts;
15858 idxs = slang->sl_pidxs;
15859 if (byts != NULL) /* array not is empty */
15860 {
15861 /*
15862 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015863 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015864 */
15865 depth = 0;
15866 arridx[0] = 0;
15867 curi[0] = 1;
15868 while (depth >= 0 && !got_int)
15869 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015870 n = arridx[depth];
15871 len = byts[n];
15872 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015873 {
15874 /* Done all bytes at this node, go up one level. */
15875 --depth;
15876 line_breakcheck();
15877 }
15878 else
15879 {
15880 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015881 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015882 ++curi[depth];
15883 c = byts[n];
15884 if (c == 0)
15885 {
15886 /* End of prefix, find out how many IDs there are. */
15887 for (i = 1; i < len; ++i)
15888 if (byts[n + i] != 0)
15889 break;
15890 curi[depth] += i - 1;
15891
Bram Moolenaar53805d12005-08-01 07:08:33 +000015892 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15893 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015894 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015895 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015896 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015897 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015898 : flags, lnum);
15899 if (lnum != 0)
15900 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015901 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015902
15903 /* Check for prefix that matches the word when the
15904 * first letter is upper-case, but only if the prefix has
15905 * a condition. */
15906 if (has_word_up)
15907 {
15908 c = valid_word_prefix(i, n, flags, word_up, slang,
15909 TRUE);
15910 if (c != 0)
15911 {
15912 vim_strncpy(prefix + depth, word_up,
15913 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015914 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015915 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015916 : flags, lnum);
15917 if (lnum != 0)
15918 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015919 }
15920 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015921 }
15922 else
15923 {
15924 /* Normal char, go one level deeper. */
15925 prefix[depth++] = c;
15926 arridx[depth] = idxs[n];
15927 curi[depth] = 1;
15928 }
15929 }
15930 }
15931 }
15932
15933 return lnum;
15934}
15935
Bram Moolenaar95529562005-08-25 21:21:38 +000015936/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015937 * Move "p" to the end of word "start".
15938 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015939 */
15940 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020015941spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015942 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015943 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000015944{
15945 char_u *p = start;
15946
Bram Moolenaar860cae12010-06-05 23:22:07 +020015947 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000015948 mb_ptr_adv(p);
15949 return p;
15950}
15951
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015952#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015953/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015954 * For Insert mode completion CTRL-X s:
15955 * Find start of the word in front of column "startcol".
15956 * We don't check if it is badly spelled, with completion we can only change
15957 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015958 * Returns the column number of the word.
15959 */
15960 int
15961spell_word_start(startcol)
15962 int startcol;
15963{
15964 char_u *line;
15965 char_u *p;
15966 int col = 0;
15967
Bram Moolenaar95529562005-08-25 21:21:38 +000015968 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015969 return startcol;
15970
15971 /* Find a word character before "startcol". */
15972 line = ml_get_curline();
15973 for (p = line + startcol; p > line; )
15974 {
15975 mb_ptr_back(line, p);
15976 if (spell_iswordp_nmw(p))
15977 break;
15978 }
15979
15980 /* Go back to start of the word. */
15981 while (p > line)
15982 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015983 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015984 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020015985 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015986 break;
15987 col = 0;
15988 }
15989
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015990 return col;
15991}
15992
15993/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015994 * Need to check for 'spellcapcheck' now, the word is removed before
15995 * expand_spelling() is called. Therefore the ugly global variable.
15996 */
15997static int spell_expand_need_cap;
15998
15999 void
16000spell_expand_check_cap(col)
16001 colnr_T col;
16002{
16003 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16004}
16005
16006/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016007 * Get list of spelling suggestions.
16008 * Used for Insert mode completion CTRL-X ?.
16009 * Returns the number of matches. The matches are in "matchp[]", array of
16010 * allocated strings.
16011 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016012 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016013expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016014 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016015 char_u *pat;
16016 char_u ***matchp;
16017{
16018 garray_T ga;
16019
Bram Moolenaar4770d092006-01-12 23:22:24 +000016020 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016021 *matchp = ga.ga_data;
16022 return ga.ga_len;
16023}
16024#endif
16025
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016026#endif /* FEAT_SPELL */