blob: 30510ad27176cc23388f8965852dc4607642f2a3 [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 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009442 if (fd != NULL)
9443 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009444 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009445 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009446
9447 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009448 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009449 fd = mch_fopen((char *)fname, "a");
9450 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009451 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009452 char_u *p;
9453
Bram Moolenaard0131a82006-03-04 21:46:13 +00009454 /* We just initialized the 'spellfile' option and can't open the
9455 * file. We may need to create the "spell" directory first. We
9456 * already checked the runtime directory is writable in
9457 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009458 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009459 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009460 int c = *p;
9461
Bram Moolenaard0131a82006-03-04 21:46:13 +00009462 /* The directory doesn't exist. Try creating it and opening
9463 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009464 *p = NUL;
9465 vim_mkdir(fname, 0755);
9466 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009467 fd = mch_fopen((char *)fname, "a");
9468 }
9469 }
9470
9471 if (fd == NULL)
9472 EMSG2(_(e_notopen), fname);
9473 else
9474 {
9475 if (bad)
9476 fprintf(fd, "%.*s/!\n", len, word);
9477 else
9478 fprintf(fd, "%.*s\n", len, word);
9479 fclose(fd);
9480
9481 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9482 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009483 }
9484 }
9485
Bram Moolenaard0131a82006-03-04 21:46:13 +00009486 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009487 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009488 /* Update the .add.spl file. */
9489 mkspell(1, &fname, FALSE, TRUE, TRUE);
9490
9491 /* If the .add file is edited somewhere, reload it. */
9492 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009493 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009494
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009495 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009496 }
9497}
9498
9499/*
9500 * Initialize 'spellfile' for the current buffer.
9501 */
9502 static void
9503init_spellfile()
9504{
9505 char_u buf[MAXPATHL];
9506 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009507 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009508 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009509 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009510 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009511 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009512
Bram Moolenaar860cae12010-06-05 23:22:07 +02009513 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009514 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009515 /* Find the end of the language name. Exclude the region. If there
9516 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009517 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009518 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009519 if (vim_ispathsep(*lend))
9520 {
9521 aspath = TRUE;
9522 lstart = lend + 1;
9523 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009524
9525 /* Loop over all entries in 'runtimepath'. Use the first one where we
9526 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009527 rtp = p_rtp;
9528 while (*rtp != NUL)
9529 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009530 if (aspath)
9531 /* Use directory of an entry with path, e.g., for
9532 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009533 vim_strncpy(buf, curbuf->b_s.b_p_spl, lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009534 else
9535 /* Copy the path from 'runtimepath' to buf[]. */
9536 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009537 if (filewritable(buf) == 2)
9538 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009539 /* Use the first language name from 'spelllang' and the
9540 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009541 if (aspath)
Bram Moolenaar860cae12010-06-05 23:22:07 +02009542 vim_strncpy(buf, curbuf->b_s.b_p_spl, lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009543 else
9544 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009545 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009546 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009547 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9548 if (!filewritable(buf) != 2)
9549 vim_mkdir(buf, 0755);
9550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009551 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009552 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009553 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009554 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009555 l = (int)STRLEN(buf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009556 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009557 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9558 fname != NULL
9559 && strstr((char *)gettail(fname), ".ascii.") != NULL
9560 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009561 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9562 break;
9563 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009564 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009565 }
9566 }
9567}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009568
Bram Moolenaar51485f02005-06-04 21:55:20 +00009569
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009570/*
9571 * Init the chartab used for spelling for ASCII.
9572 * EBCDIC is not supported!
9573 */
9574 static void
9575clear_spell_chartab(sp)
9576 spelltab_T *sp;
9577{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009578 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009579
9580 /* Init everything to FALSE. */
9581 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9582 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9583 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009584 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009585 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009586 sp->st_upper[i] = i;
9587 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009588
9589 /* We include digits. A word shouldn't start with a digit, but handling
9590 * that is done separately. */
9591 for (i = '0'; i <= '9'; ++i)
9592 sp->st_isw[i] = TRUE;
9593 for (i = 'A'; i <= 'Z'; ++i)
9594 {
9595 sp->st_isw[i] = TRUE;
9596 sp->st_isu[i] = TRUE;
9597 sp->st_fold[i] = i + 0x20;
9598 }
9599 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009600 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009601 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009602 sp->st_upper[i] = i - 0x20;
9603 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009604}
9605
9606/*
9607 * Init the chartab used for spelling. Only depends on 'encoding'.
9608 * Called once while starting up and when 'encoding' changes.
9609 * The default is to use isalpha(), but the spell file should define the word
9610 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009611 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009612 */
9613 void
9614init_spell_chartab()
9615{
9616 int i;
9617
9618 did_set_spelltab = FALSE;
9619 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009620#ifdef FEAT_MBYTE
9621 if (enc_dbcs)
9622 {
9623 /* DBCS: assume double-wide characters are word characters. */
9624 for (i = 128; i <= 255; ++i)
9625 if (MB_BYTE2LEN(i) == 2)
9626 spelltab.st_isw[i] = TRUE;
9627 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009628 else if (enc_utf8)
9629 {
9630 for (i = 128; i < 256; ++i)
9631 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009632 int f = utf_fold(i);
9633 int u = utf_toupper(i);
9634
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009635 spelltab.st_isu[i] = utf_isupper(i);
9636 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009637 /* The folded/upper-cased value is different between latin1 and
9638 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9639 * value for utf-8 to avoid this. */
9640 spelltab.st_fold[i] = (f < 256) ? f : i;
9641 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009642 }
9643 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009644 else
9645#endif
9646 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009647 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009648 for (i = 128; i < 256; ++i)
9649 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009650 if (MB_ISUPPER(i))
9651 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009652 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009653 spelltab.st_isu[i] = TRUE;
9654 spelltab.st_fold[i] = MB_TOLOWER(i);
9655 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009656 else if (MB_ISLOWER(i))
9657 {
9658 spelltab.st_isw[i] = TRUE;
9659 spelltab.st_upper[i] = MB_TOUPPER(i);
9660 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009661 }
9662 }
9663}
9664
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009665/*
9666 * Set the spell character tables from strings in the affix file.
9667 */
9668 static int
9669set_spell_chartab(fol, low, upp)
9670 char_u *fol;
9671 char_u *low;
9672 char_u *upp;
9673{
9674 /* We build the new tables here first, so that we can compare with the
9675 * previous one. */
9676 spelltab_T new_st;
9677 char_u *pf = fol, *pl = low, *pu = upp;
9678 int f, l, u;
9679
9680 clear_spell_chartab(&new_st);
9681
9682 while (*pf != NUL)
9683 {
9684 if (*pl == NUL || *pu == NUL)
9685 {
9686 EMSG(_(e_affform));
9687 return FAIL;
9688 }
9689#ifdef FEAT_MBYTE
9690 f = mb_ptr2char_adv(&pf);
9691 l = mb_ptr2char_adv(&pl);
9692 u = mb_ptr2char_adv(&pu);
9693#else
9694 f = *pf++;
9695 l = *pl++;
9696 u = *pu++;
9697#endif
9698 /* Every character that appears is a word character. */
9699 if (f < 256)
9700 new_st.st_isw[f] = TRUE;
9701 if (l < 256)
9702 new_st.st_isw[l] = TRUE;
9703 if (u < 256)
9704 new_st.st_isw[u] = TRUE;
9705
9706 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9707 * case-folding */
9708 if (l < 256 && l != f)
9709 {
9710 if (f >= 256)
9711 {
9712 EMSG(_(e_affrange));
9713 return FAIL;
9714 }
9715 new_st.st_fold[l] = f;
9716 }
9717
9718 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009719 * case-folding, it's upper case and the "UPP" is the upper case of
9720 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009721 if (u < 256 && u != f)
9722 {
9723 if (f >= 256)
9724 {
9725 EMSG(_(e_affrange));
9726 return FAIL;
9727 }
9728 new_st.st_fold[u] = f;
9729 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009730 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009731 }
9732 }
9733
9734 if (*pl != NUL || *pu != NUL)
9735 {
9736 EMSG(_(e_affform));
9737 return FAIL;
9738 }
9739
9740 return set_spell_finish(&new_st);
9741}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009742
9743/*
9744 * Set the spell character tables from strings in the .spl file.
9745 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009746 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009747set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009748 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009749 int cnt; /* length of "flags" */
9750 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009751{
9752 /* We build the new tables here first, so that we can compare with the
9753 * previous one. */
9754 spelltab_T new_st;
9755 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009756 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009757 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009758
9759 clear_spell_chartab(&new_st);
9760
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009761 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009762 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009763 if (i < cnt)
9764 {
9765 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9766 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9767 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009768
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009769 if (*p != NUL)
9770 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009771#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009772 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009773#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009774 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009775#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009776 new_st.st_fold[i + 128] = c;
9777 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9778 new_st.st_upper[c] = i + 128;
9779 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009780 }
9781
Bram Moolenaar5195e452005-08-19 20:32:47 +00009782 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009783}
9784
9785 static int
9786set_spell_finish(new_st)
9787 spelltab_T *new_st;
9788{
9789 int i;
9790
9791 if (did_set_spelltab)
9792 {
9793 /* check that it's the same table */
9794 for (i = 0; i < 256; ++i)
9795 {
9796 if (spelltab.st_isw[i] != new_st->st_isw[i]
9797 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009798 || spelltab.st_fold[i] != new_st->st_fold[i]
9799 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009800 {
9801 EMSG(_("E763: Word characters differ between spell files"));
9802 return FAIL;
9803 }
9804 }
9805 }
9806 else
9807 {
9808 /* copy the new spelltab into the one being used */
9809 spelltab = *new_st;
9810 did_set_spelltab = TRUE;
9811 }
9812
9813 return OK;
9814}
9815
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009816/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009817 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009818 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009819 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009820 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009821 */
9822 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009823spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009824 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009825 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009826{
Bram Moolenaarea408852005-06-25 22:49:46 +00009827#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009828 char_u *s;
9829 int l;
9830 int c;
9831
9832 if (has_mbyte)
9833 {
9834 l = MB_BYTE2LEN(*p);
9835 s = p;
9836 if (l == 1)
9837 {
9838 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009839 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009840 {
9841 s = p + 1; /* skip a mid-word character */
9842 l = MB_BYTE2LEN(*s);
9843 }
9844 }
9845 else
9846 {
9847 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009848 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9849 : (wp->w_s->b_spell_ismw_mb != NULL
9850 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009851 {
9852 s = p + l;
9853 l = MB_BYTE2LEN(*s);
9854 }
9855 }
9856
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009857 c = mb_ptr2char(s);
9858 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009859 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009860 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009861 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009862#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009863
Bram Moolenaar860cae12010-06-05 23:22:07 +02009864 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009865}
9866
9867/*
9868 * Return TRUE if "p" points to a word character.
9869 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9870 */
9871 static int
9872spell_iswordp_nmw(p)
9873 char_u *p;
9874{
9875#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009876 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009877
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009878 if (has_mbyte)
9879 {
9880 c = mb_ptr2char(p);
9881 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009882 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009883 return spelltab.st_isw[c];
9884 }
9885#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009886 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009887}
9888
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009889#ifdef FEAT_MBYTE
9890/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009891 * Return TRUE if word class indicates a word character.
9892 * Only for characters above 255.
9893 * Unicode subscript and superscript are not considered word characters.
9894 */
9895 static int
9896spell_mb_isword_class(cl)
9897 int cl;
9898{
9899 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9900}
9901
9902/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009903 * Return TRUE if "p" points to a word character.
9904 * Wide version of spell_iswordp().
9905 */
9906 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009907spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009908 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009909 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009910{
9911 int *s;
9912
Bram Moolenaar860cae12010-06-05 23:22:07 +02009913 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9914 : (wp->w_s->b_spell_ismw_mb != NULL
9915 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009916 s = p + 1;
9917 else
9918 s = p;
9919
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009920 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009921 {
9922 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009923 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009924 if (enc_dbcs)
9925 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9926 return 0;
9927 }
9928 return spelltab.st_isw[*s];
9929}
9930#endif
9931
Bram Moolenaarea408852005-06-25 22:49:46 +00009932/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009933 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009934 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009935 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009936 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009937write_spell_prefcond(fd, gap)
9938 FILE *fd;
9939 garray_T *gap;
9940{
9941 int i;
9942 char_u *p;
9943 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009944 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009945 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009946
Bram Moolenaar5195e452005-08-19 20:32:47 +00009947 if (fd != NULL)
9948 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9949
9950 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009951
9952 for (i = 0; i < gap->ga_len; ++i)
9953 {
9954 /* <prefcond> : <condlen> <condstr> */
9955 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009956 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009957 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009958 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009959 if (fd != NULL)
9960 {
9961 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009962 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009963 }
9964 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009965 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009966 else if (fd != NULL)
9967 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009968 }
9969
Bram Moolenaar5195e452005-08-19 20:32:47 +00009970 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009971}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009972
9973/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009974 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9975 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009976 * When using a multi-byte 'encoding' the length may change!
9977 * Returns FAIL when something wrong.
9978 */
9979 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009980spell_casefold(str, len, buf, buflen)
9981 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009982 int len;
9983 char_u *buf;
9984 int buflen;
9985{
9986 int i;
9987
9988 if (len >= buflen)
9989 {
9990 buf[0] = NUL;
9991 return FAIL; /* result will not fit */
9992 }
9993
9994#ifdef FEAT_MBYTE
9995 if (has_mbyte)
9996 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009997 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009998 char_u *p;
9999 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010000
10001 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010002 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010003 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010004 if (outi + MB_MAXBYTES > buflen)
10005 {
10006 buf[outi] = NUL;
10007 return FAIL;
10008 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010009 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010010 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010011 }
10012 buf[outi] = NUL;
10013 }
10014 else
10015#endif
10016 {
10017 /* Be quick for non-multibyte encodings. */
10018 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010019 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010020 buf[i] = NUL;
10021 }
10022
10023 return OK;
10024}
10025
Bram Moolenaar4770d092006-01-12 23:22:24 +000010026/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010027#define SPS_BEST 1
10028#define SPS_FAST 2
10029#define SPS_DOUBLE 4
10030
Bram Moolenaar4770d092006-01-12 23:22:24 +000010031static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10032static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010033
10034/*
10035 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010036 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010037 */
10038 int
10039spell_check_sps()
10040{
10041 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010042 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010043 char_u buf[MAXPATHL];
10044 int f;
10045
10046 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010047 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010048
10049 for (p = p_sps; *p != NUL; )
10050 {
10051 copy_option_part(&p, buf, MAXPATHL, ",");
10052
10053 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010054 if (VIM_ISDIGIT(*buf))
10055 {
10056 s = buf;
10057 sps_limit = getdigits(&s);
10058 if (*s != NUL && !VIM_ISDIGIT(*s))
10059 f = -1;
10060 }
10061 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010062 f = SPS_BEST;
10063 else if (STRCMP(buf, "fast") == 0)
10064 f = SPS_FAST;
10065 else if (STRCMP(buf, "double") == 0)
10066 f = SPS_DOUBLE;
10067 else if (STRNCMP(buf, "expr:", 5) != 0
10068 && STRNCMP(buf, "file:", 5) != 0)
10069 f = -1;
10070
10071 if (f == -1 || (sps_flags != 0 && f != 0))
10072 {
10073 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010074 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010075 return FAIL;
10076 }
10077 if (f != 0)
10078 sps_flags = f;
10079 }
10080
10081 if (sps_flags == 0)
10082 sps_flags = SPS_BEST;
10083
10084 return OK;
10085}
10086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010087/*
10088 * "z?": Find badly spelled word under or after the cursor.
10089 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010090 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010091 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010092 */
10093 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010094spell_suggest(count)
10095 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096{
10097 char_u *line;
10098 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010099 char_u wcopy[MAXWLEN + 2];
10100 char_u *p;
10101 int i;
10102 int c;
10103 suginfo_T sug;
10104 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010105 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010106 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010107 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010108 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010109 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010110 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010112 if (no_spell_checking(curwin))
10113 return;
10114
10115#ifdef FEAT_VISUAL
10116 if (VIsual_active)
10117 {
10118 /* Use the Visually selected text as the bad word. But reject
10119 * a multi-line selection. */
10120 if (curwin->w_cursor.lnum != VIsual.lnum)
10121 {
10122 vim_beep();
10123 return;
10124 }
10125 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10126 if (badlen < 0)
10127 badlen = -badlen;
10128 else
10129 curwin->w_cursor.col = VIsual.col;
10130 ++badlen;
10131 end_visual_mode();
10132 }
10133 else
10134#endif
10135 /* Find the start of the badly spelled word. */
10136 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010137 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010139 /* No bad word or it starts after the cursor: use the word under the
10140 * cursor. */
10141 curwin->w_cursor = prev_cursor;
10142 line = ml_get_curline();
10143 p = line + curwin->w_cursor.col;
10144 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010145 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010146 mb_ptr_back(line, p);
10147 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010148 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010149 mb_ptr_adv(p);
10150
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010151 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010152 {
10153 beep_flush();
10154 return;
10155 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010156 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 }
10158
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010159 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010161 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010162 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010163
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010164 /* Make a copy of current line since autocommands may free the line. */
10165 line = vim_strsave(ml_get_curline());
10166 if (line == NULL)
10167 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010168
Bram Moolenaar5195e452005-08-19 20:32:47 +000010169 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10170 * 'spellsuggest', whatever is smaller. */
10171 if (sps_limit > (int)Rows - 2)
10172 limit = (int)Rows - 2;
10173 else
10174 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010175 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010176 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177
10178 if (sug.su_ga.ga_len == 0)
10179 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010180 else if (count > 0)
10181 {
10182 if (count > sug.su_ga.ga_len)
10183 smsg((char_u *)_("Sorry, only %ld suggestions"),
10184 (long)sug.su_ga.ga_len);
10185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 else
10187 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010188 vim_free(repl_from);
10189 repl_from = NULL;
10190 vim_free(repl_to);
10191 repl_to = NULL;
10192
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010193#ifdef FEAT_RIGHTLEFT
10194 /* When 'rightleft' is set the list is drawn right-left. */
10195 cmdmsg_rl = curwin->w_p_rl;
10196 if (cmdmsg_rl)
10197 msg_col = Columns - 1;
10198#endif
10199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 /* List the suggestions. */
10201 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010202 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010203 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010204 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10205 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010206#ifdef FEAT_RIGHTLEFT
10207 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10208 {
10209 /* And now the rabbit from the high hat: Avoid showing the
10210 * untranslated message rightleft. */
10211 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10212 sug.su_badlen, sug.su_badptr);
10213 }
10214#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 msg_puts(IObuff);
10216 msg_clr_eos();
10217 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 msg_scroll = TRUE;
10220 for (i = 0; i < sug.su_ga.ga_len; ++i)
10221 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010222 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223
10224 /* The suggested word may replace only part of the bad word, add
10225 * the not replaced part. */
10226 STRCPY(wcopy, stp->st_word);
10227 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010228 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 sug.su_badptr + stp->st_orglen,
10230 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010231 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10232#ifdef FEAT_RIGHTLEFT
10233 if (cmdmsg_rl)
10234 rl_mirror(IObuff);
10235#endif
10236 msg_puts(IObuff);
10237
10238 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010239 msg_puts(IObuff);
10240
10241 /* The word may replace more than "su_badlen". */
10242 if (sug.su_badlen < stp->st_orglen)
10243 {
10244 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10245 stp->st_orglen, sug.su_badptr);
10246 msg_puts(IObuff);
10247 }
10248
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010249 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010250 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010251 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010252 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010253 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010254 stp->st_salscore ? "s " : "",
10255 stp->st_score, stp->st_altscore);
10256 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010257 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010258 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010259#ifdef FEAT_RIGHTLEFT
10260 if (cmdmsg_rl)
10261 /* Mirror the numbers, but keep the leading space. */
10262 rl_mirror(IObuff + 1);
10263#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010264 msg_advance(30);
10265 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 msg_putchar('\n');
10268 }
10269
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010270#ifdef FEAT_RIGHTLEFT
10271 cmdmsg_rl = FALSE;
10272 msg_col = 0;
10273#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010275 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010276 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010277 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010278 lines_left = Rows; /* avoid more prompt */
10279 /* don't delay for 'smd' in normal_cmd() */
10280 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 }
10282
Bram Moolenaard12a1322005-08-21 22:08:24 +000010283 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10284 {
10285 /* Save the from and to text for :spellrepall. */
10286 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010287 if (sug.su_badlen > stp->st_orglen)
10288 {
10289 /* Replacing less than "su_badlen", append the remainder to
10290 * repl_to. */
10291 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10292 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10293 sug.su_badlen - stp->st_orglen,
10294 sug.su_badptr + stp->st_orglen);
10295 repl_to = vim_strsave(IObuff);
10296 }
10297 else
10298 {
10299 /* Replacing su_badlen or more, use the whole word. */
10300 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10301 repl_to = vim_strsave(stp->st_word);
10302 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010303
10304 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010305 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10306 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010307 if (p != NULL)
10308 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010309 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010310 mch_memmove(p, line, c);
10311 STRCPY(p + c, stp->st_word);
10312 STRCAT(p, sug.su_badptr + stp->st_orglen);
10313 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10314 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010315
10316 /* For redo we use a change-word command. */
10317 ResetRedobuff();
10318 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010319 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010320 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010321 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010322
10323 /* After this "p" may be invalid. */
10324 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010325 }
10326 }
10327 else
10328 curwin->w_cursor = prev_cursor;
10329
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010330 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010331skip:
10332 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010333}
10334
10335/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010336 * Check if the word at line "lnum" column "col" is required to start with a
10337 * capital. This uses 'spellcapcheck' of the current buffer.
10338 */
10339 static int
10340check_need_cap(lnum, col)
10341 linenr_T lnum;
10342 colnr_T col;
10343{
10344 int need_cap = FALSE;
10345 char_u *line;
10346 char_u *line_copy = NULL;
10347 char_u *p;
10348 colnr_T endcol;
10349 regmatch_T regmatch;
10350
Bram Moolenaar860cae12010-06-05 23:22:07 +020010351 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010352 return FALSE;
10353
10354 line = ml_get_curline();
10355 endcol = 0;
10356 if ((int)(skipwhite(line) - line) >= (int)col)
10357 {
10358 /* At start of line, check if previous line is empty or sentence
10359 * ends there. */
10360 if (lnum == 1)
10361 need_cap = TRUE;
10362 else
10363 {
10364 line = ml_get(lnum - 1);
10365 if (*skipwhite(line) == NUL)
10366 need_cap = TRUE;
10367 else
10368 {
10369 /* Append a space in place of the line break. */
10370 line_copy = concat_str(line, (char_u *)" ");
10371 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010372 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010373 }
10374 }
10375 }
10376 else
10377 endcol = col;
10378
10379 if (endcol > 0)
10380 {
10381 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010382 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010383 regmatch.rm_ic = FALSE;
10384 p = line + endcol;
10385 for (;;)
10386 {
10387 mb_ptr_back(line, p);
10388 if (p == line || spell_iswordp_nmw(p))
10389 break;
10390 if (vim_regexec(&regmatch, p, 0)
10391 && regmatch.endp[0] == line + endcol)
10392 {
10393 need_cap = TRUE;
10394 break;
10395 }
10396 }
10397 }
10398
10399 vim_free(line_copy);
10400
10401 return need_cap;
10402}
10403
10404
10405/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010406 * ":spellrepall"
10407 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010408 void
10409ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010410 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010411{
10412 pos_T pos = curwin->w_cursor;
10413 char_u *frompat;
10414 int addlen;
10415 char_u *line;
10416 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010417 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010418 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010419
10420 if (repl_from == NULL || repl_to == NULL)
10421 {
10422 EMSG(_("E752: No previous spell replacement"));
10423 return;
10424 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010425 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010426
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010427 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010428 if (frompat == NULL)
10429 return;
10430 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10431 p_ws = FALSE;
10432
Bram Moolenaar5195e452005-08-19 20:32:47 +000010433 sub_nsubs = 0;
10434 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010435 curwin->w_cursor.lnum = 0;
10436 while (!got_int)
10437 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010438 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010439 || u_save_cursor() == FAIL)
10440 break;
10441
10442 /* Only replace when the right word isn't there yet. This happens
10443 * when changing "etc" to "etc.". */
10444 line = ml_get_curline();
10445 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10446 repl_to, STRLEN(repl_to)) != 0)
10447 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010448 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010449 if (p == NULL)
10450 break;
10451 mch_memmove(p, line, curwin->w_cursor.col);
10452 STRCPY(p + curwin->w_cursor.col, repl_to);
10453 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10454 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10455 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010456
10457 if (curwin->w_cursor.lnum != prev_lnum)
10458 {
10459 ++sub_nlines;
10460 prev_lnum = curwin->w_cursor.lnum;
10461 }
10462 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010463 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010464 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010465 }
10466
10467 p_ws = save_ws;
10468 curwin->w_cursor = pos;
10469 vim_free(frompat);
10470
Bram Moolenaar5195e452005-08-19 20:32:47 +000010471 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010472 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010473 else
10474 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010475}
10476
10477/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010478 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10479 * a list of allocated strings.
10480 */
10481 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010482spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010483 garray_T *gap;
10484 char_u *word;
10485 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010486 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010487 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010488{
10489 suginfo_T sug;
10490 int i;
10491 suggest_T *stp;
10492 char_u *wcopy;
10493
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010494 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010495
10496 /* Make room in "gap". */
10497 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010498 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010499 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010500 for (i = 0; i < sug.su_ga.ga_len; ++i)
10501 {
10502 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010503
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010504 /* The suggested word may replace only part of "word", add the not
10505 * replaced part. */
10506 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010507 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010508 if (wcopy == NULL)
10509 break;
10510 STRCPY(wcopy, stp->st_word);
10511 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10512 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10513 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010514 }
10515
10516 spell_find_cleanup(&sug);
10517}
10518
10519/*
10520 * Find spell suggestions for the word at the start of "badptr".
10521 * Return the suggestions in "su->su_ga".
10522 * The maximum number of suggestions is "maxcount".
10523 * Note: does use info for the current window.
10524 * This is based on the mechanisms of Aspell, but completely reimplemented.
10525 */
10526 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010527spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010528 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010529 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010530 suginfo_T *su;
10531 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010532 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010533 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010534 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010535{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010536 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010537 char_u buf[MAXPATHL];
10538 char_u *p;
10539 int do_combine = FALSE;
10540 char_u *sps_copy;
10541#ifdef FEAT_EVAL
10542 static int expr_busy = FALSE;
10543#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010544 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010545 int i;
10546 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010547
10548 /*
10549 * Set the info in "*su".
10550 */
10551 vim_memset(su, 0, sizeof(suginfo_T));
10552 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10553 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010554 if (*badptr == NUL)
10555 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010556 hash_init(&su->su_banned);
10557
10558 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010559 if (badlen != 0)
10560 su->su_badlen = badlen;
10561 else
10562 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010563 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010564 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010565
10566 if (su->su_badlen >= MAXWLEN)
10567 su->su_badlen = MAXWLEN - 1; /* just in case */
10568 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10569 (void)spell_casefold(su->su_badptr, su->su_badlen,
10570 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010571 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010572 su->su_badflags = badword_captype(su->su_badptr,
10573 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010574 if (need_cap)
10575 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010576
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010577 /* Find the default language for sound folding. We simply use the first
10578 * one in 'spelllang' that supports sound folding. That's good for when
10579 * using multiple files for one language, it's not that bad when mixing
10580 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010581 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010582 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010583 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010584 if (lp->lp_sallang != NULL)
10585 {
10586 su->su_sallang = lp->lp_sallang;
10587 break;
10588 }
10589 }
10590
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010591 /* Soundfold the bad word with the default sound folding, so that we don't
10592 * have to do this many times. */
10593 if (su->su_sallang != NULL)
10594 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10595 su->su_sal_badword);
10596
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010597 /* If the word is not capitalised and spell_check() doesn't consider the
10598 * word to be bad then it might need to be capitalised. Add a suggestion
10599 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010600 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010601 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010602 {
10603 make_case_word(su->su_badword, buf, WF_ONECAP);
10604 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010605 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010606 }
10607
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010608 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010609 if (banbadword)
10610 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010611
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010612 /* Make a copy of 'spellsuggest', because the expression may change it. */
10613 sps_copy = vim_strsave(p_sps);
10614 if (sps_copy == NULL)
10615 return;
10616
10617 /* Loop over the items in 'spellsuggest'. */
10618 for (p = sps_copy; *p != NUL; )
10619 {
10620 copy_option_part(&p, buf, MAXPATHL, ",");
10621
10622 if (STRNCMP(buf, "expr:", 5) == 0)
10623 {
10624#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010625 /* Evaluate an expression. Skip this when called recursively,
10626 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010627 if (!expr_busy)
10628 {
10629 expr_busy = TRUE;
10630 spell_suggest_expr(su, buf + 5);
10631 expr_busy = FALSE;
10632 }
10633#endif
10634 }
10635 else if (STRNCMP(buf, "file:", 5) == 0)
10636 /* Use list of suggestions in a file. */
10637 spell_suggest_file(su, buf + 5);
10638 else
10639 {
10640 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010641 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010642 if (sps_flags & SPS_DOUBLE)
10643 do_combine = TRUE;
10644 }
10645 }
10646
10647 vim_free(sps_copy);
10648
10649 if (do_combine)
10650 /* Combine the two list of suggestions. This must be done last,
10651 * because sorting changes the order again. */
10652 score_combine(su);
10653}
10654
10655#ifdef FEAT_EVAL
10656/*
10657 * Find suggestions by evaluating expression "expr".
10658 */
10659 static void
10660spell_suggest_expr(su, expr)
10661 suginfo_T *su;
10662 char_u *expr;
10663{
10664 list_T *list;
10665 listitem_T *li;
10666 int score;
10667 char_u *p;
10668
10669 /* The work is split up in a few parts to avoid having to export
10670 * suginfo_T.
10671 * First evaluate the expression and get the resulting list. */
10672 list = eval_spell_expr(su->su_badword, expr);
10673 if (list != NULL)
10674 {
10675 /* Loop over the items in the list. */
10676 for (li = list->lv_first; li != NULL; li = li->li_next)
10677 if (li->li_tv.v_type == VAR_LIST)
10678 {
10679 /* Get the word and the score from the items. */
10680 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010681 if (score >= 0 && score <= su->su_maxscore)
10682 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10683 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010684 }
10685 list_unref(list);
10686 }
10687
Bram Moolenaar4770d092006-01-12 23:22:24 +000010688 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10689 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010690 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10691}
10692#endif
10693
10694/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010695 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010696 */
10697 static void
10698spell_suggest_file(su, fname)
10699 suginfo_T *su;
10700 char_u *fname;
10701{
10702 FILE *fd;
10703 char_u line[MAXWLEN * 2];
10704 char_u *p;
10705 int len;
10706 char_u cword[MAXWLEN];
10707
10708 /* Open the file. */
10709 fd = mch_fopen((char *)fname, "r");
10710 if (fd == NULL)
10711 {
10712 EMSG2(_(e_notopen), fname);
10713 return;
10714 }
10715
10716 /* Read it line by line. */
10717 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10718 {
10719 line_breakcheck();
10720
10721 p = vim_strchr(line, '/');
10722 if (p == NULL)
10723 continue; /* No Tab found, just skip the line. */
10724 *p++ = NUL;
10725 if (STRICMP(su->su_badword, line) == 0)
10726 {
10727 /* Match! Isolate the good word, until CR or NL. */
10728 for (len = 0; p[len] >= ' '; ++len)
10729 ;
10730 p[len] = NUL;
10731
10732 /* If the suggestion doesn't have specific case duplicate the case
10733 * of the bad word. */
10734 if (captype(p, NULL) == 0)
10735 {
10736 make_case_word(p, cword, su->su_badflags);
10737 p = cword;
10738 }
10739
10740 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010741 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010742 }
10743 }
10744
10745 fclose(fd);
10746
Bram Moolenaar4770d092006-01-12 23:22:24 +000010747 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10748 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010749 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10750}
10751
10752/*
10753 * Find suggestions for the internal method indicated by "sps_flags".
10754 */
10755 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010756spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010757 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010758 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010759{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010760 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010761 * Load the .sug file(s) that are available and not done yet.
10762 */
10763 suggest_load_files();
10764
10765 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010766 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010767 *
10768 * Set a maximum score to limit the combination of operations that is
10769 * tried.
10770 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010771 suggest_try_special(su);
10772
10773 /*
10774 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10775 * from the .aff file and inserting a space (split the word).
10776 */
10777 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010778
10779 /* For the resulting top-scorers compute the sound-a-like score. */
10780 if (sps_flags & SPS_DOUBLE)
10781 score_comp_sal(su);
10782
10783 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010784 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010785 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010786 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010787 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010788 if (sps_flags & SPS_BEST)
10789 /* Adjust the word score for the suggestions found so far for how
10790 * they sounds like. */
10791 rescore_suggestions(su);
10792
10793 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010794 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010795 * for the soundfold word, limits the changes that are being tried,
10796 * and "su_sfmaxscore" the rescored score, which is set by
10797 * cleanup_suggestions().
10798 * First find words with a small edit distance, because this is much
10799 * faster and often already finds the top-N suggestions. If we didn't
10800 * find many suggestions try again with a higher edit distance.
10801 * "sl_sounddone" is used to avoid doing the same word twice.
10802 */
10803 suggest_try_soundalike_prep();
10804 su->su_maxscore = SCORE_SFMAX1;
10805 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010806 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10808 {
10809 /* We didn't find enough matches, try again, allowing more
10810 * changes to the soundfold word. */
10811 su->su_maxscore = SCORE_SFMAX2;
10812 suggest_try_soundalike(su);
10813 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10814 {
10815 /* Still didn't find enough matches, try again, allowing even
10816 * more changes to the soundfold word. */
10817 su->su_maxscore = SCORE_SFMAX3;
10818 suggest_try_soundalike(su);
10819 }
10820 }
10821 su->su_maxscore = su->su_sfmaxscore;
10822 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010823 }
10824
Bram Moolenaar4770d092006-01-12 23:22:24 +000010825 /* When CTRL-C was hit while searching do show the results. Only clear
10826 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010827 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010828 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010829 {
10830 (void)vgetc();
10831 got_int = FALSE;
10832 }
10833
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010834 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010835 {
10836 if (sps_flags & SPS_BEST)
10837 /* Adjust the word score for how it sounds like. */
10838 rescore_suggestions(su);
10839
Bram Moolenaar4770d092006-01-12 23:22:24 +000010840 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10841 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010842 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010843 }
10844}
10845
10846/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010847 * Load the .sug files for languages that have one and weren't loaded yet.
10848 */
10849 static void
10850suggest_load_files()
10851{
10852 langp_T *lp;
10853 int lpi;
10854 slang_T *slang;
10855 char_u *dotp;
10856 FILE *fd;
10857 char_u buf[MAXWLEN];
10858 int i;
10859 time_t timestamp;
10860 int wcount;
10861 int wordnr;
10862 garray_T ga;
10863 int c;
10864
10865 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010866 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010867 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010868 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010869 slang = lp->lp_slang;
10870 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10871 {
10872 /* Change ".spl" to ".sug" and open the file. When the file isn't
10873 * found silently skip it. Do set "sl_sugloaded" so that we
10874 * don't try again and again. */
10875 slang->sl_sugloaded = TRUE;
10876
10877 dotp = vim_strrchr(slang->sl_fname, '.');
10878 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10879 continue;
10880 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010881 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010882 if (fd == NULL)
10883 goto nextone;
10884
10885 /*
10886 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10887 */
10888 for (i = 0; i < VIMSUGMAGICL; ++i)
10889 buf[i] = getc(fd); /* <fileID> */
10890 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10891 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010892 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010893 slang->sl_fname);
10894 goto nextone;
10895 }
10896 c = getc(fd); /* <versionnr> */
10897 if (c < VIMSUGVERSION)
10898 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010899 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010900 slang->sl_fname);
10901 goto nextone;
10902 }
10903 else if (c > VIMSUGVERSION)
10904 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010905 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010906 slang->sl_fname);
10907 goto nextone;
10908 }
10909
10910 /* Check the timestamp, it must be exactly the same as the one in
10911 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010912 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010913 if (timestamp != slang->sl_sugtime)
10914 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010915 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010916 slang->sl_fname);
10917 goto nextone;
10918 }
10919
10920 /*
10921 * <SUGWORDTREE>: <wordtree>
10922 * Read the trie with the soundfolded words.
10923 */
10924 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10925 FALSE, 0) != 0)
10926 {
10927someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010928 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010929 slang->sl_fname);
10930 slang_clear_sug(slang);
10931 goto nextone;
10932 }
10933
10934 /*
10935 * <SUGTABLE>: <sugwcount> <sugline> ...
10936 *
10937 * Read the table with word numbers. We use a file buffer for
10938 * this, because it's so much like a file with lines. Makes it
10939 * possible to swap the info and save on memory use.
10940 */
10941 slang->sl_sugbuf = open_spellbuf();
10942 if (slang->sl_sugbuf == NULL)
10943 goto someerror;
10944 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010945 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010946 if (wcount < 0)
10947 goto someerror;
10948
10949 /* Read all the wordnr lists into the buffer, one NUL terminated
10950 * list per line. */
10951 ga_init2(&ga, 1, 100);
10952 for (wordnr = 0; wordnr < wcount; ++wordnr)
10953 {
10954 ga.ga_len = 0;
10955 for (;;)
10956 {
10957 c = getc(fd); /* <sugline> */
10958 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10959 goto someerror;
10960 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10961 if (c == NUL)
10962 break;
10963 }
10964 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10965 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10966 goto someerror;
10967 }
10968 ga_clear(&ga);
10969
10970 /*
10971 * Need to put word counts in the word tries, so that we can find
10972 * a word by its number.
10973 */
10974 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10975 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10976
10977nextone:
10978 if (fd != NULL)
10979 fclose(fd);
10980 STRCPY(dotp, ".spl");
10981 }
10982 }
10983}
10984
10985
10986/*
10987 * Fill in the wordcount fields for a trie.
10988 * Returns the total number of words.
10989 */
10990 static void
10991tree_count_words(byts, idxs)
10992 char_u *byts;
10993 idx_T *idxs;
10994{
10995 int depth;
10996 idx_T arridx[MAXWLEN];
10997 int curi[MAXWLEN];
10998 int c;
10999 idx_T n;
11000 int wordcount[MAXWLEN];
11001
11002 arridx[0] = 0;
11003 curi[0] = 1;
11004 wordcount[0] = 0;
11005 depth = 0;
11006 while (depth >= 0 && !got_int)
11007 {
11008 if (curi[depth] > byts[arridx[depth]])
11009 {
11010 /* Done all bytes at this node, go up one level. */
11011 idxs[arridx[depth]] = wordcount[depth];
11012 if (depth > 0)
11013 wordcount[depth - 1] += wordcount[depth];
11014
11015 --depth;
11016 fast_breakcheck();
11017 }
11018 else
11019 {
11020 /* Do one more byte at this node. */
11021 n = arridx[depth] + curi[depth];
11022 ++curi[depth];
11023
11024 c = byts[n];
11025 if (c == 0)
11026 {
11027 /* End of word, count it. */
11028 ++wordcount[depth];
11029
11030 /* Skip over any other NUL bytes (same word with different
11031 * flags). */
11032 while (byts[n + 1] == 0)
11033 {
11034 ++n;
11035 ++curi[depth];
11036 }
11037 }
11038 else
11039 {
11040 /* Normal char, go one level deeper to count the words. */
11041 ++depth;
11042 arridx[depth] = idxs[n];
11043 curi[depth] = 1;
11044 wordcount[depth] = 0;
11045 }
11046 }
11047 }
11048}
11049
11050/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011051 * Free the info put in "*su" by spell_find_suggest().
11052 */
11053 static void
11054spell_find_cleanup(su)
11055 suginfo_T *su;
11056{
11057 int i;
11058
11059 /* Free the suggestions. */
11060 for (i = 0; i < su->su_ga.ga_len; ++i)
11061 vim_free(SUG(su->su_ga, i).st_word);
11062 ga_clear(&su->su_ga);
11063 for (i = 0; i < su->su_sga.ga_len; ++i)
11064 vim_free(SUG(su->su_sga, i).st_word);
11065 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066
11067 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011068 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069}
11070
11071/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011072 * Make a copy of "word", with the first letter upper or lower cased, to
11073 * "wcopy[MAXWLEN]". "word" must not be empty.
11074 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 */
11076 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011077onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 char_u *wcopy;
11080 int upper; /* TRUE: first letter made upper case */
11081{
11082 char_u *p;
11083 int c;
11084 int l;
11085
11086 p = word;
11087#ifdef FEAT_MBYTE
11088 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011089 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 else
11091#endif
11092 c = *p++;
11093 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011094 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011096 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097#ifdef FEAT_MBYTE
11098 if (has_mbyte)
11099 l = mb_char2bytes(c, wcopy);
11100 else
11101#endif
11102 {
11103 l = 1;
11104 wcopy[0] = c;
11105 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011106 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107}
11108
11109/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011110 * Make a copy of "word" with all the letters upper cased into
11111 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 */
11113 static void
11114allcap_copy(word, wcopy)
11115 char_u *word;
11116 char_u *wcopy;
11117{
11118 char_u *s;
11119 char_u *d;
11120 int c;
11121
11122 d = wcopy;
11123 for (s = word; *s != NUL; )
11124 {
11125#ifdef FEAT_MBYTE
11126 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011127 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 else
11129#endif
11130 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011131
11132#ifdef FEAT_MBYTE
11133 /* We only change ß to SS when we are certain latin1 is used. It
11134 * would cause weird errors in other 8-bit encodings. */
11135 if (enc_latin1like && c == 0xdf)
11136 {
11137 c = 'S';
11138 if (d - wcopy >= MAXWLEN - 1)
11139 break;
11140 *d++ = c;
11141 }
11142 else
11143#endif
11144 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145
11146#ifdef FEAT_MBYTE
11147 if (has_mbyte)
11148 {
11149 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11150 break;
11151 d += mb_char2bytes(c, d);
11152 }
11153 else
11154#endif
11155 {
11156 if (d - wcopy >= MAXWLEN - 1)
11157 break;
11158 *d++ = c;
11159 }
11160 }
11161 *d = NUL;
11162}
11163
11164/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011165 * Try finding suggestions by recognizing specific situations.
11166 */
11167 static void
11168suggest_try_special(su)
11169 suginfo_T *su;
11170{
11171 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011172 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011173 int c;
11174 char_u word[MAXWLEN];
11175
11176 /*
11177 * Recognize a word that is repeated: "the the".
11178 */
11179 p = skiptowhite(su->su_fbadword);
11180 len = p - su->su_fbadword;
11181 p = skipwhite(p);
11182 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11183 {
11184 /* Include badflags: if the badword is onecap or allcap
11185 * use that for the goodword too: "The the" -> "The". */
11186 c = su->su_fbadword[len];
11187 su->su_fbadword[len] = NUL;
11188 make_case_word(su->su_fbadword, word, su->su_badflags);
11189 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011190
11191 /* Give a soundalike score of 0, compute the score as if deleting one
11192 * character. */
11193 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011194 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011195 }
11196}
11197
11198/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011199 * Try finding suggestions by adding/removing/swapping letters.
11200 */
11201 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011202suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 suginfo_T *su;
11204{
11205 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011206 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011208 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011209 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210
11211 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011212 * to find matches (esp. REP items). Append some more text, changing
11213 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011215 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011216 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011217 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218
Bram Moolenaar860cae12010-06-05 23:22:07 +020011219 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011221 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011222
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011223 /* If reloading a spell file fails it's still in the list but
11224 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011225 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011226 continue;
11227
Bram Moolenaar4770d092006-01-12 23:22:24 +000011228 /* Try it for this language. Will add possible suggestions. */
11229 suggest_trie_walk(su, lp, fword, FALSE);
11230 }
11231}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232
Bram Moolenaar4770d092006-01-12 23:22:24 +000011233/* Check the maximum score, if we go over it we won't try this change. */
11234#define TRY_DEEPER(su, stack, depth, add) \
11235 (stack[depth].ts_score + (add) < su->su_maxscore)
11236
11237/*
11238 * Try finding suggestions by adding/removing/swapping letters.
11239 *
11240 * This uses a state machine. At each node in the tree we try various
11241 * operations. When trying if an operation works "depth" is increased and the
11242 * stack[] is used to store info. This allows combinations, thus insert one
11243 * character, replace one and delete another. The number of changes is
11244 * limited by su->su_maxscore.
11245 *
11246 * After implementing this I noticed an article by Kemal Oflazer that
11247 * describes something similar: "Error-tolerant Finite State Recognition with
11248 * Applications to Morphological Analysis and Spelling Correction" (1996).
11249 * The implementation in the article is simplified and requires a stack of
11250 * unknown depth. The implementation here only needs a stack depth equal to
11251 * the length of the word.
11252 *
11253 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11254 * The mechanism is the same, but we find a match with a sound-folded word
11255 * that comes from one or more original words. Each of these words may be
11256 * added, this is done by add_sound_suggest().
11257 * Don't use:
11258 * the prefix tree or the keep-case tree
11259 * "su->su_badlen"
11260 * anything to do with upper and lower case
11261 * anything to do with word or non-word characters ("spell_iswordp()")
11262 * banned words
11263 * word flags (rare, region, compounding)
11264 * word splitting for now
11265 * "similar_chars()"
11266 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11267 */
11268 static void
11269suggest_trie_walk(su, lp, fword, soundfold)
11270 suginfo_T *su;
11271 langp_T *lp;
11272 char_u *fword;
11273 int soundfold;
11274{
11275 char_u tword[MAXWLEN]; /* good word collected so far */
11276 trystate_T stack[MAXWLEN];
11277 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011278 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011279 * words and split word. NUL terminated
11280 * when going deeper but not when coming
11281 * back. */
11282 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11283 trystate_T *sp;
11284 int newscore;
11285 int score;
11286 char_u *byts, *fbyts, *pbyts;
11287 idx_T *idxs, *fidxs, *pidxs;
11288 int depth;
11289 int c, c2, c3;
11290 int n = 0;
11291 int flags;
11292 garray_T *gap;
11293 idx_T arridx;
11294 int len;
11295 char_u *p;
11296 fromto_T *ftp;
11297 int fl = 0, tl;
11298 int repextra = 0; /* extra bytes in fword[] from REP item */
11299 slang_T *slang = lp->lp_slang;
11300 int fword_ends;
11301 int goodword_ends;
11302#ifdef DEBUG_TRIEWALK
11303 /* Stores the name of the change made at each level. */
11304 char_u changename[MAXWLEN][80];
11305#endif
11306 int breakcheckcount = 1000;
11307 int compound_ok;
11308
11309 /*
11310 * Go through the whole case-fold tree, try changes at each node.
11311 * "tword[]" contains the word collected from nodes in the tree.
11312 * "fword[]" the word we are trying to match with (initially the bad
11313 * word).
11314 */
11315 depth = 0;
11316 sp = &stack[0];
11317 vim_memset(sp, 0, sizeof(trystate_T));
11318 sp->ts_curi = 1;
11319
11320 if (soundfold)
11321 {
11322 /* Going through the soundfold tree. */
11323 byts = fbyts = slang->sl_sbyts;
11324 idxs = fidxs = slang->sl_sidxs;
11325 pbyts = NULL;
11326 pidxs = NULL;
11327 sp->ts_prefixdepth = PFD_NOPREFIX;
11328 sp->ts_state = STATE_START;
11329 }
11330 else
11331 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011332 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011333 * When there are postponed prefixes we need to use these first. At
11334 * the end of the prefix we continue in the case-fold tree.
11335 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011336 fbyts = slang->sl_fbyts;
11337 fidxs = slang->sl_fidxs;
11338 pbyts = slang->sl_pbyts;
11339 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011340 if (pbyts != NULL)
11341 {
11342 byts = pbyts;
11343 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011344 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011345 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11346 }
11347 else
11348 {
11349 byts = fbyts;
11350 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011351 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011352 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011353 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011354 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011355
Bram Moolenaar4770d092006-01-12 23:22:24 +000011356 /*
11357 * Loop to find all suggestions. At each round we either:
11358 * - For the current state try one operation, advance "ts_curi",
11359 * increase "depth".
11360 * - When a state is done go to the next, set "ts_state".
11361 * - When all states are tried decrease "depth".
11362 */
11363 while (depth >= 0 && !got_int)
11364 {
11365 sp = &stack[depth];
11366 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011368 case STATE_START:
11369 case STATE_NOPREFIX:
11370 /*
11371 * Start of node: Deal with NUL bytes, which means
11372 * tword[] may end here.
11373 */
11374 arridx = sp->ts_arridx; /* current node in the tree */
11375 len = byts[arridx]; /* bytes in this node */
11376 arridx += sp->ts_curi; /* index of current byte */
11377
11378 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011380 /* Skip over the NUL bytes, we use them later. */
11381 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11382 ;
11383 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384
Bram Moolenaar4770d092006-01-12 23:22:24 +000011385 /* Always past NUL bytes now. */
11386 n = (int)sp->ts_state;
11387 sp->ts_state = STATE_ENDNUL;
11388 sp->ts_save_badflags = su->su_badflags;
11389
11390 /* At end of a prefix or at start of prefixtree: check for
11391 * following word. */
11392 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011393 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011394 /* Set su->su_badflags to the caps type at this position.
11395 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011396#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011397 if (has_mbyte)
11398 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11399 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011400#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011401 n = sp->ts_fidx;
11402 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11403 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011404 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011405#ifdef DEBUG_TRIEWALK
11406 sprintf(changename[depth], "prefix");
11407#endif
11408 go_deeper(stack, depth, 0);
11409 ++depth;
11410 sp = &stack[depth];
11411 sp->ts_prefixdepth = depth - 1;
11412 byts = fbyts;
11413 idxs = fidxs;
11414 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011415
Bram Moolenaar4770d092006-01-12 23:22:24 +000011416 /* Move the prefix to preword[] with the right case
11417 * and make find_keepcap_word() works. */
11418 tword[sp->ts_twordlen] = NUL;
11419 make_case_word(tword + sp->ts_splitoff,
11420 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011421 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011422 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011423 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011424 break;
11425 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011426
Bram Moolenaar4770d092006-01-12 23:22:24 +000011427 if (sp->ts_curi > len || byts[arridx] != 0)
11428 {
11429 /* Past bytes in node and/or past NUL bytes. */
11430 sp->ts_state = STATE_ENDNUL;
11431 sp->ts_save_badflags = su->su_badflags;
11432 break;
11433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011434
Bram Moolenaar4770d092006-01-12 23:22:24 +000011435 /*
11436 * End of word in tree.
11437 */
11438 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439
Bram Moolenaar4770d092006-01-12 23:22:24 +000011440 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011441
11442 /* Skip words with the NOSUGGEST flag. */
11443 if (flags & WF_NOSUGGEST)
11444 break;
11445
Bram Moolenaar4770d092006-01-12 23:22:24 +000011446 fword_ends = (fword[sp->ts_fidx] == NUL
11447 || (soundfold
11448 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011449 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011450 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011451
Bram Moolenaar4770d092006-01-12 23:22:24 +000011452 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011453 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011454 {
11455 /* There was a prefix before the word. Check that the prefix
11456 * can be used with this word. */
11457 /* Count the length of the NULs in the prefix. If there are
11458 * none this must be the first try without a prefix. */
11459 n = stack[sp->ts_prefixdepth].ts_arridx;
11460 len = pbyts[n++];
11461 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11462 ;
11463 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011464 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011466 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011467 if (c == 0)
11468 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011469
Bram Moolenaar4770d092006-01-12 23:22:24 +000011470 /* Use the WF_RARE flag for a rare prefix. */
11471 if (c & WF_RAREPFX)
11472 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011473
Bram Moolenaar4770d092006-01-12 23:22:24 +000011474 /* Tricky: when checking for both prefix and compounding
11475 * we run into the prefix flag first.
11476 * Remember that it's OK, so that we accept the prefix
11477 * when arriving at a compound flag. */
11478 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011479 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011480 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011481
Bram Moolenaar4770d092006-01-12 23:22:24 +000011482 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11483 * appending another compound word below. */
11484 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011485 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011486 goodword_ends = FALSE;
11487 else
11488 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011489
Bram Moolenaar4770d092006-01-12 23:22:24 +000011490 p = NULL;
11491 compound_ok = TRUE;
11492 if (sp->ts_complen > sp->ts_compsplit)
11493 {
11494 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011495 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011496 /* There was a word before this word. When there was no
11497 * change in this word (it was correct) add the first word
11498 * as a suggestion. If this word was corrected too, we
11499 * need to check if a correct word follows. */
11500 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011501 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011502 && STRNCMP(fword + sp->ts_splitfidx,
11503 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011504 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011505 {
11506 preword[sp->ts_prewordlen] = NUL;
11507 newscore = score_wordcount_adj(slang, sp->ts_score,
11508 preword + sp->ts_prewordlen,
11509 sp->ts_prewordlen > 0);
11510 /* Add the suggestion if the score isn't too bad. */
11511 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011512 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011513 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011514 newscore, 0, FALSE,
11515 lp->lp_sallang, FALSE);
11516 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011517 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011518 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011519 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011520 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011521 /* There was a compound word before this word. If this
11522 * word does not support compounding then give up
11523 * (splitting is tried for the word without compound
11524 * flag). */
11525 if (((unsigned)flags >> 24) == 0
11526 || sp->ts_twordlen - sp->ts_splitoff
11527 < slang->sl_compminlen)
11528 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011529#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011530 /* For multi-byte chars check character length against
11531 * COMPOUNDMIN. */
11532 if (has_mbyte
11533 && slang->sl_compminlen > 0
11534 && mb_charlen(tword + sp->ts_splitoff)
11535 < slang->sl_compminlen)
11536 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011537#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011538
Bram Moolenaar4770d092006-01-12 23:22:24 +000011539 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11540 compflags[sp->ts_complen + 1] = NUL;
11541 vim_strncpy(preword + sp->ts_prewordlen,
11542 tword + sp->ts_splitoff,
11543 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011544
11545 /* Verify CHECKCOMPOUNDPATTERN rules. */
11546 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11547 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011548 compound_ok = FALSE;
11549
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011550 if (compound_ok)
11551 {
11552 p = preword;
11553 while (*skiptowhite(p) != NUL)
11554 p = skipwhite(skiptowhite(p));
11555 if (fword_ends && !can_compound(slang, p,
11556 compflags + sp->ts_compsplit))
11557 /* Compound is not allowed. But it may still be
11558 * possible if we add another (short) word. */
11559 compound_ok = FALSE;
11560 }
11561
Bram Moolenaar4770d092006-01-12 23:22:24 +000011562 /* Get pointer to last char of previous word. */
11563 p = preword + sp->ts_prewordlen;
11564 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011565 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011566 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011567
Bram Moolenaar4770d092006-01-12 23:22:24 +000011568 /*
11569 * Form the word with proper case in preword.
11570 * If there is a word from a previous split, append.
11571 * For the soundfold tree don't change the case, simply append.
11572 */
11573 if (soundfold)
11574 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11575 else if (flags & WF_KEEPCAP)
11576 /* Must find the word in the keep-case tree. */
11577 find_keepcap_word(slang, tword + sp->ts_splitoff,
11578 preword + sp->ts_prewordlen);
11579 else
11580 {
11581 /* Include badflags: If the badword is onecap or allcap
11582 * use that for the goodword too. But if the badword is
11583 * allcap and it's only one char long use onecap. */
11584 c = su->su_badflags;
11585 if ((c & WF_ALLCAP)
11586#ifdef FEAT_MBYTE
11587 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11588#else
11589 && su->su_badlen == 1
11590#endif
11591 )
11592 c = WF_ONECAP;
11593 c |= flags;
11594
11595 /* When appending a compound word after a word character don't
11596 * use Onecap. */
11597 if (p != NULL && spell_iswordp_nmw(p))
11598 c &= ~WF_ONECAP;
11599 make_case_word(tword + sp->ts_splitoff,
11600 preword + sp->ts_prewordlen, c);
11601 }
11602
11603 if (!soundfold)
11604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 /* Don't use a banned word. It may appear again as a good
11606 * word, thus remember it. */
11607 if (flags & WF_BANNED)
11608 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011609 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 break;
11611 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011612 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011613 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11614 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011615 {
11616 if (slang->sl_compprog == NULL)
11617 break;
11618 /* the word so far was banned but we may try compounding */
11619 goodword_ends = FALSE;
11620 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622
Bram Moolenaar4770d092006-01-12 23:22:24 +000011623 newscore = 0;
11624 if (!soundfold) /* soundfold words don't have flags */
11625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011627 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011628 newscore += SCORE_REGION;
11629 if (flags & WF_RARE)
11630 newscore += SCORE_RARE;
11631
Bram Moolenaar0c405862005-06-22 22:26:26 +000011632 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011633 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011634 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011636
Bram Moolenaar4770d092006-01-12 23:22:24 +000011637 /* TODO: how about splitting in the soundfold tree? */
11638 if (fword_ends
11639 && goodword_ends
11640 && sp->ts_fidx >= sp->ts_fidxtry
11641 && compound_ok)
11642 {
11643 /* The badword also ends: add suggestions. */
11644#ifdef DEBUG_TRIEWALK
11645 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011647 int j;
11648
11649 /* print the stack of changes that brought us here */
11650 smsg("------ %s -------", fword);
11651 for (j = 0; j < depth; ++j)
11652 smsg("%s", changename[j]);
11653 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011654#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011655 if (soundfold)
11656 {
11657 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011658 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011659 add_sound_suggest(su, preword, sp->ts_score, lp);
11660 }
11661 else
11662 {
11663 /* Give a penalty when changing non-word char to word
11664 * char, e.g., "thes," -> "these". */
11665 p = fword + sp->ts_fidx;
11666 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011667 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011668 {
11669 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011670 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011671 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011672 newscore += SCORE_NONWORD;
11673 }
11674
Bram Moolenaar4770d092006-01-12 23:22:24 +000011675 /* Give a bonus to words seen before. */
11676 score = score_wordcount_adj(slang,
11677 sp->ts_score + newscore,
11678 preword + sp->ts_prewordlen,
11679 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011680
Bram Moolenaar4770d092006-01-12 23:22:24 +000011681 /* Add the suggestion if the score isn't too bad. */
11682 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011683 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011684 add_suggestion(su, &su->su_ga, preword,
11685 sp->ts_fidx - repextra,
11686 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011687
11688 if (su->su_badflags & WF_MIXCAP)
11689 {
11690 /* We really don't know if the word should be
11691 * upper or lower case, add both. */
11692 c = captype(preword, NULL);
11693 if (c == 0 || c == WF_ALLCAP)
11694 {
11695 make_case_word(tword + sp->ts_splitoff,
11696 preword + sp->ts_prewordlen,
11697 c == 0 ? WF_ALLCAP : 0);
11698
11699 add_suggestion(su, &su->su_ga, preword,
11700 sp->ts_fidx - repextra,
11701 score + SCORE_ICASE, 0, FALSE,
11702 lp->lp_sallang, FALSE);
11703 }
11704 }
11705 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011707 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011708
Bram Moolenaar4770d092006-01-12 23:22:24 +000011709 /*
11710 * Try word split and/or compounding.
11711 */
11712 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011713#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011714 /* Don't split halfway a character. */
11715 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011716#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011717 )
11718 {
11719 int try_compound;
11720 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011721
Bram Moolenaar4770d092006-01-12 23:22:24 +000011722 /* If past the end of the bad word don't try a split.
11723 * Otherwise try changing the next word. E.g., find
11724 * suggestions for "the the" where the second "the" is
11725 * different. It's done like a split.
11726 * TODO: word split for soundfold words */
11727 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11728 && !soundfold;
11729
11730 /* Get here in several situations:
11731 * 1. The word in the tree ends:
11732 * If the word allows compounding try that. Otherwise try
11733 * a split by inserting a space. For both check that a
11734 * valid words starts at fword[sp->ts_fidx].
11735 * For NOBREAK do like compounding to be able to check if
11736 * the next word is valid.
11737 * 2. The badword does end, but it was due to a change (e.g.,
11738 * a swap). No need to split, but do check that the
11739 * following word is valid.
11740 * 3. The badword and the word in the tree end. It may still
11741 * be possible to compound another (short) word.
11742 */
11743 try_compound = FALSE;
11744 if (!soundfold
11745 && slang->sl_compprog != NULL
11746 && ((unsigned)flags >> 24) != 0
11747 && sp->ts_twordlen - sp->ts_splitoff
11748 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011749#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011750 && (!has_mbyte
11751 || slang->sl_compminlen == 0
11752 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011753 >= slang->sl_compminlen)
11754#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011755 && (slang->sl_compsylmax < MAXWLEN
11756 || sp->ts_complen + 1 - sp->ts_compsplit
11757 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011758 && (can_be_compound(sp, slang,
11759 compflags, ((unsigned)flags >> 24))))
11760
Bram Moolenaar4770d092006-01-12 23:22:24 +000011761 {
11762 try_compound = TRUE;
11763 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11764 compflags[sp->ts_complen + 1] = NUL;
11765 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011766
Bram Moolenaar4770d092006-01-12 23:22:24 +000011767 /* For NOBREAK we never try splitting, it won't make any word
11768 * valid. */
11769 if (slang->sl_nobreak)
11770 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011771
Bram Moolenaar4770d092006-01-12 23:22:24 +000011772 /* If we could add a compound word, and it's also possible to
11773 * split at this point, do the split first and set
11774 * TSF_DIDSPLIT to avoid doing it again. */
11775 else if (!fword_ends
11776 && try_compound
11777 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11778 {
11779 try_compound = FALSE;
11780 sp->ts_flags |= TSF_DIDSPLIT;
11781 --sp->ts_curi; /* do the same NUL again */
11782 compflags[sp->ts_complen] = NUL;
11783 }
11784 else
11785 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011786
Bram Moolenaar4770d092006-01-12 23:22:24 +000011787 if (try_split || try_compound)
11788 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011789 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011790 {
11791 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011792 * words so far are valid for compounding. If there
11793 * is only one word it must not have the NEEDCOMPOUND
11794 * flag. */
11795 if (sp->ts_complen == sp->ts_compsplit
11796 && (flags & WF_NEEDCOMP))
11797 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011798 p = preword;
11799 while (*skiptowhite(p) != NUL)
11800 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011801 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011802 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011803 compflags + sp->ts_compsplit))
11804 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011805
11806 if (slang->sl_nosplitsugs)
11807 newscore += SCORE_SPLIT_NO;
11808 else
11809 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011810
11811 /* Give a bonus to words seen before. */
11812 newscore = score_wordcount_adj(slang, newscore,
11813 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011814 }
11815
Bram Moolenaar4770d092006-01-12 23:22:24 +000011816 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011818 go_deeper(stack, depth, newscore);
11819#ifdef DEBUG_TRIEWALK
11820 if (!try_compound && !fword_ends)
11821 sprintf(changename[depth], "%.*s-%s: split",
11822 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11823 else
11824 sprintf(changename[depth], "%.*s-%s: compound",
11825 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11826#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011828 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011829 sp->ts_state = STATE_SPLITUNDO;
11830
11831 ++depth;
11832 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011833
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011834 /* Append a space to preword when splitting. */
11835 if (!try_compound && !fword_ends)
11836 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011837 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011838 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011839 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011840
11841 /* If the badword has a non-word character at this
11842 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011843 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011844 * character when the word ends. But only when the
11845 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011846 if (((!try_compound && !spell_iswordp_nmw(fword
11847 + sp->ts_fidx))
11848 || fword_ends)
11849 && fword[sp->ts_fidx] != NUL
11850 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011851 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011852 int l;
11853
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011854#ifdef FEAT_MBYTE
11855 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011856 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011857 else
11858#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011859 l = 1;
11860 if (fword_ends)
11861 {
11862 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011863 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011864 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011865 sp->ts_prewordlen += l;
11866 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011867 }
11868 else
11869 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11870 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011871 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011872
Bram Moolenaard12a1322005-08-21 22:08:24 +000011873 /* When compounding include compound flag in
11874 * compflags[] (already set above). When splitting we
11875 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011876 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011877 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011878 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011879 sp->ts_compsplit = sp->ts_complen;
11880 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011881
Bram Moolenaar53805d12005-08-01 07:08:33 +000011882 /* set su->su_badflags to the caps type at this
11883 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011884#ifdef FEAT_MBYTE
11885 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011886 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011887 else
11888#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011889 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011890 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011891 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011893 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011894 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011895
11896 /* If there are postponed prefixes, try these too. */
11897 if (pbyts != NULL)
11898 {
11899 byts = pbyts;
11900 idxs = pidxs;
11901 sp->ts_prefixdepth = PFD_PREFIXTREE;
11902 sp->ts_state = STATE_NOPREFIX;
11903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 }
11905 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011906 }
11907 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908
Bram Moolenaar4770d092006-01-12 23:22:24 +000011909 case STATE_SPLITUNDO:
11910 /* Undo the changes done for word split or compound word. */
11911 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912
Bram Moolenaar4770d092006-01-12 23:22:24 +000011913 /* Continue looking for NUL bytes. */
11914 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011915
Bram Moolenaar4770d092006-01-12 23:22:24 +000011916 /* In case we went into the prefix tree. */
11917 byts = fbyts;
11918 idxs = fidxs;
11919 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920
Bram Moolenaar4770d092006-01-12 23:22:24 +000011921 case STATE_ENDNUL:
11922 /* Past the NUL bytes in the node. */
11923 su->su_badflags = sp->ts_save_badflags;
11924 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011925#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011926 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011927#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011928 )
11929 {
11930 /* The badword ends, can't use STATE_PLAIN. */
11931 sp->ts_state = STATE_DEL;
11932 break;
11933 }
11934 sp->ts_state = STATE_PLAIN;
11935 /*FALLTHROUGH*/
11936
11937 case STATE_PLAIN:
11938 /*
11939 * Go over all possible bytes at this node, add each to tword[]
11940 * and use child node. "ts_curi" is the index.
11941 */
11942 arridx = sp->ts_arridx;
11943 if (sp->ts_curi > byts[arridx])
11944 {
11945 /* Done all bytes at this node, do next state. When still at
11946 * already changed bytes skip the other tricks. */
11947 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011950 sp->ts_state = STATE_FINAL;
11951 }
11952 else
11953 {
11954 arridx += sp->ts_curi++;
11955 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956
Bram Moolenaar4770d092006-01-12 23:22:24 +000011957 /* Normal byte, go one level deeper. If it's not equal to the
11958 * byte in the bad word adjust the score. But don't even try
11959 * when the byte was already changed. And don't try when we
11960 * just deleted this byte, accepting it is always cheaper then
11961 * delete + substitute. */
11962 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011963#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011965#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011966 )
11967 newscore = 0;
11968 else
11969 newscore = SCORE_SUBST;
11970 if ((newscore == 0
11971 || (sp->ts_fidx >= sp->ts_fidxtry
11972 && ((sp->ts_flags & TSF_DIDDEL) == 0
11973 || c != fword[sp->ts_delidx])))
11974 && TRY_DEEPER(su, stack, depth, newscore))
11975 {
11976 go_deeper(stack, depth, newscore);
11977#ifdef DEBUG_TRIEWALK
11978 if (newscore > 0)
11979 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11980 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11981 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011982 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011983 sprintf(changename[depth], "%.*s-%s: accept %c",
11984 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11985 fword[sp->ts_fidx]);
11986#endif
11987 ++depth;
11988 sp = &stack[depth];
11989 ++sp->ts_fidx;
11990 tword[sp->ts_twordlen++] = c;
11991 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011992#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011993 if (newscore == SCORE_SUBST)
11994 sp->ts_isdiff = DIFF_YES;
11995 if (has_mbyte)
11996 {
11997 /* Multi-byte characters are a bit complicated to
11998 * handle: They differ when any of the bytes differ
11999 * and then their length may also differ. */
12000 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012001 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012002 /* First byte. */
12003 sp->ts_tcharidx = 0;
12004 sp->ts_tcharlen = MB_BYTE2LEN(c);
12005 sp->ts_fcharstart = sp->ts_fidx - 1;
12006 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012007 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012008 }
12009 else if (sp->ts_isdiff == DIFF_INSERT)
12010 /* When inserting trail bytes don't advance in the
12011 * bad word. */
12012 --sp->ts_fidx;
12013 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12014 {
12015 /* Last byte of character. */
12016 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012017 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012018 /* Correct ts_fidx for the byte length of the
12019 * character (we didn't check that before). */
12020 sp->ts_fidx = sp->ts_fcharstart
12021 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012022 fword[sp->ts_fcharstart]);
12023
Bram Moolenaar4770d092006-01-12 23:22:24 +000012024 /* For changing a composing character adjust
12025 * the score from SCORE_SUBST to
12026 * SCORE_SUBCOMP. */
12027 if (enc_utf8
12028 && utf_iscomposing(
12029 mb_ptr2char(tword
12030 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012031 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012032 && utf_iscomposing(
12033 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012034 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012035 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012036 SCORE_SUBST - SCORE_SUBCOMP;
12037
Bram Moolenaar4770d092006-01-12 23:22:24 +000012038 /* For a similar character adjust score from
12039 * SCORE_SUBST to SCORE_SIMILAR. */
12040 else if (!soundfold
12041 && slang->sl_has_map
12042 && similar_chars(slang,
12043 mb_ptr2char(tword
12044 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012045 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012046 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012047 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012048 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012049 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012050 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012051 else if (sp->ts_isdiff == DIFF_INSERT
12052 && sp->ts_twordlen > sp->ts_tcharlen)
12053 {
12054 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12055 c = mb_ptr2char(p);
12056 if (enc_utf8 && utf_iscomposing(c))
12057 {
12058 /* Inserting a composing char doesn't
12059 * count that much. */
12060 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12061 }
12062 else
12063 {
12064 /* If the previous character was the same,
12065 * thus doubling a character, give a bonus
12066 * to the score. Also for the soundfold
12067 * tree (might seem illogical but does
12068 * give better scores). */
12069 mb_ptr_back(tword, p);
12070 if (c == mb_ptr2char(p))
12071 sp->ts_score -= SCORE_INS
12072 - SCORE_INSDUP;
12073 }
12074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012075
Bram Moolenaar4770d092006-01-12 23:22:24 +000012076 /* Starting a new char, reset the length. */
12077 sp->ts_tcharlen = 0;
12078 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012079 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012080 else
12081#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012082 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012083 /* If we found a similar char adjust the score.
12084 * We do this after calling go_deeper() because
12085 * it's slow. */
12086 if (newscore != 0
12087 && !soundfold
12088 && slang->sl_has_map
12089 && similar_chars(slang,
12090 c, fword[sp->ts_fidx - 1]))
12091 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012092 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012094 }
12095 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012096
Bram Moolenaar4770d092006-01-12 23:22:24 +000012097 case STATE_DEL:
12098#ifdef FEAT_MBYTE
12099 /* When past the first byte of a multi-byte char don't try
12100 * delete/insert/swap a character. */
12101 if (has_mbyte && sp->ts_tcharlen > 0)
12102 {
12103 sp->ts_state = STATE_FINAL;
12104 break;
12105 }
12106#endif
12107 /*
12108 * Try skipping one character in the bad word (delete it).
12109 */
12110 sp->ts_state = STATE_INS_PREP;
12111 sp->ts_curi = 1;
12112 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12113 /* Deleting a vowel at the start of a word counts less, see
12114 * soundalike_score(). */
12115 newscore = 2 * SCORE_DEL / 3;
12116 else
12117 newscore = SCORE_DEL;
12118 if (fword[sp->ts_fidx] != NUL
12119 && TRY_DEEPER(su, stack, depth, newscore))
12120 {
12121 go_deeper(stack, depth, newscore);
12122#ifdef DEBUG_TRIEWALK
12123 sprintf(changename[depth], "%.*s-%s: delete %c",
12124 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12125 fword[sp->ts_fidx]);
12126#endif
12127 ++depth;
12128
12129 /* Remember what character we deleted, so that we can avoid
12130 * inserting it again. */
12131 stack[depth].ts_flags |= TSF_DIDDEL;
12132 stack[depth].ts_delidx = sp->ts_fidx;
12133
12134 /* Advance over the character in fword[]. Give a bonus to the
12135 * score if the same character is following "nn" -> "n". It's
12136 * a bit illogical for soundfold tree but it does give better
12137 * results. */
12138#ifdef FEAT_MBYTE
12139 if (has_mbyte)
12140 {
12141 c = mb_ptr2char(fword + sp->ts_fidx);
12142 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12143 if (enc_utf8 && utf_iscomposing(c))
12144 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12145 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12146 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12147 }
12148 else
12149#endif
12150 {
12151 ++stack[depth].ts_fidx;
12152 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12153 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12154 }
12155 break;
12156 }
12157 /*FALLTHROUGH*/
12158
12159 case STATE_INS_PREP:
12160 if (sp->ts_flags & TSF_DIDDEL)
12161 {
12162 /* If we just deleted a byte then inserting won't make sense,
12163 * a substitute is always cheaper. */
12164 sp->ts_state = STATE_SWAP;
12165 break;
12166 }
12167
12168 /* skip over NUL bytes */
12169 n = sp->ts_arridx;
12170 for (;;)
12171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012172 if (sp->ts_curi > byts[n])
12173 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012174 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012176 break;
12177 }
12178 if (byts[n + sp->ts_curi] != NUL)
12179 {
12180 /* Found a byte to insert. */
12181 sp->ts_state = STATE_INS;
12182 break;
12183 }
12184 ++sp->ts_curi;
12185 }
12186 break;
12187
12188 /*FALLTHROUGH*/
12189
12190 case STATE_INS:
12191 /* Insert one byte. Repeat this for each possible byte at this
12192 * node. */
12193 n = sp->ts_arridx;
12194 if (sp->ts_curi > byts[n])
12195 {
12196 /* Done all bytes at this node, go to next state. */
12197 sp->ts_state = STATE_SWAP;
12198 break;
12199 }
12200
12201 /* Do one more byte at this node, but:
12202 * - Skip NUL bytes.
12203 * - Skip the byte if it's equal to the byte in the word,
12204 * accepting that byte is always better.
12205 */
12206 n += sp->ts_curi++;
12207 c = byts[n];
12208 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12209 /* Inserting a vowel at the start of a word counts less,
12210 * see soundalike_score(). */
12211 newscore = 2 * SCORE_INS / 3;
12212 else
12213 newscore = SCORE_INS;
12214 if (c != fword[sp->ts_fidx]
12215 && TRY_DEEPER(su, stack, depth, newscore))
12216 {
12217 go_deeper(stack, depth, newscore);
12218#ifdef DEBUG_TRIEWALK
12219 sprintf(changename[depth], "%.*s-%s: insert %c",
12220 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12221 c);
12222#endif
12223 ++depth;
12224 sp = &stack[depth];
12225 tword[sp->ts_twordlen++] = c;
12226 sp->ts_arridx = idxs[n];
12227#ifdef FEAT_MBYTE
12228 if (has_mbyte)
12229 {
12230 fl = MB_BYTE2LEN(c);
12231 if (fl > 1)
12232 {
12233 /* There are following bytes for the same character.
12234 * We must find all bytes before trying
12235 * delete/insert/swap/etc. */
12236 sp->ts_tcharlen = fl;
12237 sp->ts_tcharidx = 1;
12238 sp->ts_isdiff = DIFF_INSERT;
12239 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012240 }
12241 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012242 fl = 1;
12243 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012244#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012245 {
12246 /* If the previous character was the same, thus doubling a
12247 * character, give a bonus to the score. Also for
12248 * soundfold words (illogical but does give a better
12249 * score). */
12250 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012251 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012252 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012253 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012254 }
12255 break;
12256
12257 case STATE_SWAP:
12258 /*
12259 * Swap two bytes in the bad word: "12" -> "21".
12260 * We change "fword" here, it's changed back afterwards at
12261 * STATE_UNSWAP.
12262 */
12263 p = fword + sp->ts_fidx;
12264 c = *p;
12265 if (c == NUL)
12266 {
12267 /* End of word, can't swap or replace. */
12268 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271
Bram Moolenaar4770d092006-01-12 23:22:24 +000012272 /* Don't swap if the first character is not a word character.
12273 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012274 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012275 {
12276 sp->ts_state = STATE_REP_INI;
12277 break;
12278 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012279
Bram Moolenaar4770d092006-01-12 23:22:24 +000012280#ifdef FEAT_MBYTE
12281 if (has_mbyte)
12282 {
12283 n = mb_cptr2len(p);
12284 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012285 if (p[n] == NUL)
12286 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012287 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012288 c2 = c; /* don't swap non-word char */
12289 else
12290 c2 = mb_ptr2char(p + n);
12291 }
12292 else
12293#endif
12294 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012295 if (p[1] == NUL)
12296 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012297 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012298 c2 = c; /* don't swap non-word char */
12299 else
12300 c2 = p[1];
12301 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012302
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012303 /* When the second character is NUL we can't swap. */
12304 if (c2 == NUL)
12305 {
12306 sp->ts_state = STATE_REP_INI;
12307 break;
12308 }
12309
Bram Moolenaar4770d092006-01-12 23:22:24 +000012310 /* When characters are identical, swap won't do anything.
12311 * Also get here if the second char is not a word character. */
12312 if (c == c2)
12313 {
12314 sp->ts_state = STATE_SWAP3;
12315 break;
12316 }
12317 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12318 {
12319 go_deeper(stack, depth, SCORE_SWAP);
12320#ifdef DEBUG_TRIEWALK
12321 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12322 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12323 c, c2);
12324#endif
12325 sp->ts_state = STATE_UNSWAP;
12326 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012327#ifdef FEAT_MBYTE
12328 if (has_mbyte)
12329 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012330 fl = mb_char2len(c2);
12331 mch_memmove(p, p + n, fl);
12332 mb_char2bytes(c, p + fl);
12333 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012334 }
12335 else
12336#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012337 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012338 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012339 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012340 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012341 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012342 }
12343 else
12344 /* If this swap doesn't work then SWAP3 won't either. */
12345 sp->ts_state = STATE_REP_INI;
12346 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012347
Bram Moolenaar4770d092006-01-12 23:22:24 +000012348 case STATE_UNSWAP:
12349 /* Undo the STATE_SWAP swap: "21" -> "12". */
12350 p = fword + sp->ts_fidx;
12351#ifdef FEAT_MBYTE
12352 if (has_mbyte)
12353 {
12354 n = MB_BYTE2LEN(*p);
12355 c = mb_ptr2char(p + n);
12356 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12357 mb_char2bytes(c, p);
12358 }
12359 else
12360#endif
12361 {
12362 c = *p;
12363 *p = p[1];
12364 p[1] = c;
12365 }
12366 /*FALLTHROUGH*/
12367
12368 case STATE_SWAP3:
12369 /* Swap two bytes, skipping one: "123" -> "321". We change
12370 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12371 p = fword + sp->ts_fidx;
12372#ifdef FEAT_MBYTE
12373 if (has_mbyte)
12374 {
12375 n = mb_cptr2len(p);
12376 c = mb_ptr2char(p);
12377 fl = mb_cptr2len(p + n);
12378 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012379 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012380 c3 = c; /* don't swap non-word char */
12381 else
12382 c3 = mb_ptr2char(p + n + fl);
12383 }
12384 else
12385#endif
12386 {
12387 c = *p;
12388 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012389 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012390 c3 = c; /* don't swap non-word char */
12391 else
12392 c3 = p[2];
12393 }
12394
12395 /* When characters are identical: "121" then SWAP3 result is
12396 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12397 * same as SWAP on next char: "112". Thus skip all swapping.
12398 * Also skip when c3 is NUL.
12399 * Also get here when the third character is not a word character.
12400 * Second character may any char: "a.b" -> "b.a" */
12401 if (c == c3 || c3 == NUL)
12402 {
12403 sp->ts_state = STATE_REP_INI;
12404 break;
12405 }
12406 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12407 {
12408 go_deeper(stack, depth, SCORE_SWAP3);
12409#ifdef DEBUG_TRIEWALK
12410 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12411 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12412 c, c3);
12413#endif
12414 sp->ts_state = STATE_UNSWAP3;
12415 ++depth;
12416#ifdef FEAT_MBYTE
12417 if (has_mbyte)
12418 {
12419 tl = mb_char2len(c3);
12420 mch_memmove(p, p + n + fl, tl);
12421 mb_char2bytes(c2, p + tl);
12422 mb_char2bytes(c, p + fl + tl);
12423 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12424 }
12425 else
12426#endif
12427 {
12428 p[0] = p[2];
12429 p[2] = c;
12430 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12431 }
12432 }
12433 else
12434 sp->ts_state = STATE_REP_INI;
12435 break;
12436
12437 case STATE_UNSWAP3:
12438 /* Undo STATE_SWAP3: "321" -> "123" */
12439 p = fword + sp->ts_fidx;
12440#ifdef FEAT_MBYTE
12441 if (has_mbyte)
12442 {
12443 n = MB_BYTE2LEN(*p);
12444 c2 = mb_ptr2char(p + n);
12445 fl = MB_BYTE2LEN(p[n]);
12446 c = mb_ptr2char(p + n + fl);
12447 tl = MB_BYTE2LEN(p[n + fl]);
12448 mch_memmove(p + fl + tl, p, n);
12449 mb_char2bytes(c, p);
12450 mb_char2bytes(c2, p + tl);
12451 p = p + tl;
12452 }
12453 else
12454#endif
12455 {
12456 c = *p;
12457 *p = p[2];
12458 p[2] = c;
12459 ++p;
12460 }
12461
Bram Moolenaar860cae12010-06-05 23:22:07 +020012462 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012463 {
12464 /* Middle char is not a word char, skip the rotate. First and
12465 * third char were already checked at swap and swap3. */
12466 sp->ts_state = STATE_REP_INI;
12467 break;
12468 }
12469
12470 /* Rotate three characters left: "123" -> "231". We change
12471 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12472 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12473 {
12474 go_deeper(stack, depth, SCORE_SWAP3);
12475#ifdef DEBUG_TRIEWALK
12476 p = fword + sp->ts_fidx;
12477 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12478 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12479 p[0], p[1], p[2]);
12480#endif
12481 sp->ts_state = STATE_UNROT3L;
12482 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012483 p = fword + sp->ts_fidx;
12484#ifdef FEAT_MBYTE
12485 if (has_mbyte)
12486 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012487 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012488 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012489 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012490 fl += mb_cptr2len(p + n + fl);
12491 mch_memmove(p, p + n, fl);
12492 mb_char2bytes(c, p + fl);
12493 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012494 }
12495 else
12496#endif
12497 {
12498 c = *p;
12499 *p = p[1];
12500 p[1] = p[2];
12501 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012502 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012503 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012504 }
12505 else
12506 sp->ts_state = STATE_REP_INI;
12507 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508
Bram Moolenaar4770d092006-01-12 23:22:24 +000012509 case STATE_UNROT3L:
12510 /* Undo ROT3L: "231" -> "123" */
12511 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012512#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012513 if (has_mbyte)
12514 {
12515 n = MB_BYTE2LEN(*p);
12516 n += MB_BYTE2LEN(p[n]);
12517 c = mb_ptr2char(p + n);
12518 tl = MB_BYTE2LEN(p[n]);
12519 mch_memmove(p + tl, p, n);
12520 mb_char2bytes(c, p);
12521 }
12522 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012523#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012524 {
12525 c = p[2];
12526 p[2] = p[1];
12527 p[1] = *p;
12528 *p = c;
12529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012530
Bram Moolenaar4770d092006-01-12 23:22:24 +000012531 /* Rotate three bytes right: "123" -> "312". We change "fword"
12532 * here, it's changed back afterwards at STATE_UNROT3R. */
12533 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12534 {
12535 go_deeper(stack, depth, SCORE_SWAP3);
12536#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012538 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12539 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12540 p[0], p[1], p[2]);
12541#endif
12542 sp->ts_state = STATE_UNROT3R;
12543 ++depth;
12544 p = fword + sp->ts_fidx;
12545#ifdef FEAT_MBYTE
12546 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012547 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012548 n = mb_cptr2len(p);
12549 n += mb_cptr2len(p + n);
12550 c = mb_ptr2char(p + n);
12551 tl = mb_cptr2len(p + n);
12552 mch_memmove(p + tl, p, n);
12553 mb_char2bytes(c, p);
12554 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012555 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012556 else
12557#endif
12558 {
12559 c = p[2];
12560 p[2] = p[1];
12561 p[1] = *p;
12562 *p = c;
12563 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12564 }
12565 }
12566 else
12567 sp->ts_state = STATE_REP_INI;
12568 break;
12569
12570 case STATE_UNROT3R:
12571 /* Undo ROT3R: "312" -> "123" */
12572 p = fword + sp->ts_fidx;
12573#ifdef FEAT_MBYTE
12574 if (has_mbyte)
12575 {
12576 c = mb_ptr2char(p);
12577 tl = MB_BYTE2LEN(*p);
12578 n = MB_BYTE2LEN(p[tl]);
12579 n += MB_BYTE2LEN(p[tl + n]);
12580 mch_memmove(p, p + tl, n);
12581 mb_char2bytes(c, p + n);
12582 }
12583 else
12584#endif
12585 {
12586 c = *p;
12587 *p = p[1];
12588 p[1] = p[2];
12589 p[2] = c;
12590 }
12591 /*FALLTHROUGH*/
12592
12593 case STATE_REP_INI:
12594 /* Check if matching with REP items from the .aff file would work.
12595 * Quickly skip if:
12596 * - there are no REP items and we are not in the soundfold trie
12597 * - the score is going to be too high anyway
12598 * - already applied a REP item or swapped here */
12599 if ((lp->lp_replang == NULL && !soundfold)
12600 || sp->ts_score + SCORE_REP >= su->su_maxscore
12601 || sp->ts_fidx < sp->ts_fidxtry)
12602 {
12603 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012605 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606
Bram Moolenaar4770d092006-01-12 23:22:24 +000012607 /* Use the first byte to quickly find the first entry that may
12608 * match. If the index is -1 there is none. */
12609 if (soundfold)
12610 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12611 else
12612 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012613
Bram Moolenaar4770d092006-01-12 23:22:24 +000012614 if (sp->ts_curi < 0)
12615 {
12616 sp->ts_state = STATE_FINAL;
12617 break;
12618 }
12619
12620 sp->ts_state = STATE_REP;
12621 /*FALLTHROUGH*/
12622
12623 case STATE_REP:
12624 /* Try matching with REP items from the .aff file. For each match
12625 * replace the characters and check if the resulting word is
12626 * valid. */
12627 p = fword + sp->ts_fidx;
12628
12629 if (soundfold)
12630 gap = &slang->sl_repsal;
12631 else
12632 gap = &lp->lp_replang->sl_rep;
12633 while (sp->ts_curi < gap->ga_len)
12634 {
12635 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12636 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012637 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012638 /* past possible matching entries */
12639 sp->ts_curi = gap->ga_len;
12640 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012641 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012642 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12643 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12644 {
12645 go_deeper(stack, depth, SCORE_REP);
12646#ifdef DEBUG_TRIEWALK
12647 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12648 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12649 ftp->ft_from, ftp->ft_to);
12650#endif
12651 /* Need to undo this afterwards. */
12652 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012653
Bram Moolenaar4770d092006-01-12 23:22:24 +000012654 /* Change the "from" to the "to" string. */
12655 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012656 fl = (int)STRLEN(ftp->ft_from);
12657 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012658 if (fl != tl)
12659 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012660 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012661 repextra += tl - fl;
12662 }
12663 mch_memmove(p, ftp->ft_to, tl);
12664 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12665#ifdef FEAT_MBYTE
12666 stack[depth].ts_tcharlen = 0;
12667#endif
12668 break;
12669 }
12670 }
12671
12672 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12673 /* No (more) matches. */
12674 sp->ts_state = STATE_FINAL;
12675
12676 break;
12677
12678 case STATE_REP_UNDO:
12679 /* Undo a REP replacement and continue with the next one. */
12680 if (soundfold)
12681 gap = &slang->sl_repsal;
12682 else
12683 gap = &lp->lp_replang->sl_rep;
12684 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012685 fl = (int)STRLEN(ftp->ft_from);
12686 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012687 p = fword + sp->ts_fidx;
12688 if (fl != tl)
12689 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012690 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012691 repextra -= tl - fl;
12692 }
12693 mch_memmove(p, ftp->ft_from, fl);
12694 sp->ts_state = STATE_REP;
12695 break;
12696
12697 default:
12698 /* Did all possible states at this level, go up one level. */
12699 --depth;
12700
12701 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12702 {
12703 /* Continue in or go back to the prefix tree. */
12704 byts = pbyts;
12705 idxs = pidxs;
12706 }
12707
12708 /* Don't check for CTRL-C too often, it takes time. */
12709 if (--breakcheckcount == 0)
12710 {
12711 ui_breakcheck();
12712 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012713 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 }
12715 }
12716}
12717
Bram Moolenaar4770d092006-01-12 23:22:24 +000012718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012720 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012722 static void
12723go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 trystate_T *stack;
12725 int depth;
12726 int score_add;
12727{
Bram Moolenaarea424162005-06-16 21:51:00 +000012728 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012730 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012732 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733}
12734
Bram Moolenaar53805d12005-08-01 07:08:33 +000012735#ifdef FEAT_MBYTE
12736/*
12737 * Case-folding may change the number of bytes: Count nr of chars in
12738 * fword[flen] and return the byte length of that many chars in "word".
12739 */
12740 static int
12741nofold_len(fword, flen, word)
12742 char_u *fword;
12743 int flen;
12744 char_u *word;
12745{
12746 char_u *p;
12747 int i = 0;
12748
12749 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12750 ++i;
12751 for (p = word; i > 0; mb_ptr_adv(p))
12752 --i;
12753 return (int)(p - word);
12754}
12755#endif
12756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012757/*
12758 * "fword" is a good word with case folded. Find the matching keep-case
12759 * words and put it in "kword".
12760 * Theoretically there could be several keep-case words that result in the
12761 * same case-folded word, but we only find one...
12762 */
12763 static void
12764find_keepcap_word(slang, fword, kword)
12765 slang_T *slang;
12766 char_u *fword;
12767 char_u *kword;
12768{
12769 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12770 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012771 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772
12773 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012774 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 int round[MAXWLEN];
12776 int fwordidx[MAXWLEN];
12777 int uwordidx[MAXWLEN];
12778 int kwordlen[MAXWLEN];
12779
12780 int flen, ulen;
12781 int l;
12782 int len;
12783 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012784 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012785 char_u *p;
12786 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012787 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788
12789 if (byts == NULL)
12790 {
12791 /* array is empty: "cannot happen" */
12792 *kword = NUL;
12793 return;
12794 }
12795
12796 /* Make an all-cap version of "fword". */
12797 allcap_copy(fword, uword);
12798
12799 /*
12800 * Each character needs to be tried both case-folded and upper-case.
12801 * All this gets very complicated if we keep in mind that changing case
12802 * may change the byte length of a multi-byte character...
12803 */
12804 depth = 0;
12805 arridx[0] = 0;
12806 round[0] = 0;
12807 fwordidx[0] = 0;
12808 uwordidx[0] = 0;
12809 kwordlen[0] = 0;
12810 while (depth >= 0)
12811 {
12812 if (fword[fwordidx[depth]] == NUL)
12813 {
12814 /* We are at the end of "fword". If the tree allows a word to end
12815 * here we have found a match. */
12816 if (byts[arridx[depth] + 1] == 0)
12817 {
12818 kword[kwordlen[depth]] = NUL;
12819 return;
12820 }
12821
12822 /* kword is getting too long, continue one level up */
12823 --depth;
12824 }
12825 else if (++round[depth] > 2)
12826 {
12827 /* tried both fold-case and upper-case character, continue one
12828 * level up */
12829 --depth;
12830 }
12831 else
12832 {
12833 /*
12834 * round[depth] == 1: Try using the folded-case character.
12835 * round[depth] == 2: Try using the upper-case character.
12836 */
12837#ifdef FEAT_MBYTE
12838 if (has_mbyte)
12839 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012840 flen = mb_cptr2len(fword + fwordidx[depth]);
12841 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012842 }
12843 else
12844#endif
12845 ulen = flen = 1;
12846 if (round[depth] == 1)
12847 {
12848 p = fword + fwordidx[depth];
12849 l = flen;
12850 }
12851 else
12852 {
12853 p = uword + uwordidx[depth];
12854 l = ulen;
12855 }
12856
12857 for (tryidx = arridx[depth]; l > 0; --l)
12858 {
12859 /* Perform a binary search in the list of accepted bytes. */
12860 len = byts[tryidx++];
12861 c = *p++;
12862 lo = tryidx;
12863 hi = tryidx + len - 1;
12864 while (lo < hi)
12865 {
12866 m = (lo + hi) / 2;
12867 if (byts[m] > c)
12868 hi = m - 1;
12869 else if (byts[m] < c)
12870 lo = m + 1;
12871 else
12872 {
12873 lo = hi = m;
12874 break;
12875 }
12876 }
12877
12878 /* Stop if there is no matching byte. */
12879 if (hi < lo || byts[lo] != c)
12880 break;
12881
12882 /* Continue at the child (if there is one). */
12883 tryidx = idxs[lo];
12884 }
12885
12886 if (l == 0)
12887 {
12888 /*
12889 * Found the matching char. Copy it to "kword" and go a
12890 * level deeper.
12891 */
12892 if (round[depth] == 1)
12893 {
12894 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12895 flen);
12896 kwordlen[depth + 1] = kwordlen[depth] + flen;
12897 }
12898 else
12899 {
12900 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12901 ulen);
12902 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12903 }
12904 fwordidx[depth + 1] = fwordidx[depth] + flen;
12905 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12906
12907 ++depth;
12908 arridx[depth] = tryidx;
12909 round[depth] = 0;
12910 }
12911 }
12912 }
12913
12914 /* Didn't find it: "cannot happen". */
12915 *kword = NUL;
12916}
12917
12918/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012919 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12920 * su->su_sga.
12921 */
12922 static void
12923score_comp_sal(su)
12924 suginfo_T *su;
12925{
12926 langp_T *lp;
12927 char_u badsound[MAXWLEN];
12928 int i;
12929 suggest_T *stp;
12930 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012931 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012932 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012933
12934 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12935 return;
12936
12937 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012938 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012939 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012940 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012941 if (lp->lp_slang->sl_sal.ga_len > 0)
12942 {
12943 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012944 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012945
12946 for (i = 0; i < su->su_ga.ga_len; ++i)
12947 {
12948 stp = &SUG(su->su_ga, i);
12949
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012950 /* Case-fold the suggested word, sound-fold it and compute the
12951 * sound-a-like score. */
12952 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012953 if (score < SCORE_MAXMAX)
12954 {
12955 /* Add the suggestion. */
12956 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12957 sstp->st_word = vim_strsave(stp->st_word);
12958 if (sstp->st_word != NULL)
12959 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012960 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012961 sstp->st_score = score;
12962 sstp->st_altscore = 0;
12963 sstp->st_orglen = stp->st_orglen;
12964 ++su->su_sga.ga_len;
12965 }
12966 }
12967 }
12968 break;
12969 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012970 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012971}
12972
12973/*
12974 * Combine the list of suggestions in su->su_ga and su->su_sga.
12975 * They are intwined.
12976 */
12977 static void
12978score_combine(su)
12979 suginfo_T *su;
12980{
12981 int i;
12982 int j;
12983 garray_T ga;
12984 garray_T *gap;
12985 langp_T *lp;
12986 suggest_T *stp;
12987 char_u *p;
12988 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012989 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012990 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012991 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012992
12993 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012994 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012995 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012996 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012997 if (lp->lp_slang->sl_sal.ga_len > 0)
12998 {
12999 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013000 slang = lp->lp_slang;
13001 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013002
13003 for (i = 0; i < su->su_ga.ga_len; ++i)
13004 {
13005 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013006 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013007 if (stp->st_altscore == SCORE_MAXMAX)
13008 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13009 else
13010 stp->st_score = (stp->st_score * 3
13011 + stp->st_altscore) / 4;
13012 stp->st_salscore = FALSE;
13013 }
13014 break;
13015 }
13016 }
13017
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013018 if (slang == NULL) /* Using "double" without sound folding. */
13019 {
13020 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13021 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013022 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013023 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013024
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013025 /* Add the alternate score to su_sga. */
13026 for (i = 0; i < su->su_sga.ga_len; ++i)
13027 {
13028 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013029 stp->st_altscore = spell_edit_score(slang,
13030 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013031 if (stp->st_score == SCORE_MAXMAX)
13032 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13033 else
13034 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13035 stp->st_salscore = TRUE;
13036 }
13037
Bram Moolenaar4770d092006-01-12 23:22:24 +000013038 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13039 * for both lists. */
13040 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013041 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013042 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013043 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13044
13045 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13046 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13047 return;
13048
13049 stp = &SUG(ga, 0);
13050 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13051 {
13052 /* round 1: get a suggestion from su_ga
13053 * round 2: get a suggestion from su_sga */
13054 for (round = 1; round <= 2; ++round)
13055 {
13056 gap = round == 1 ? &su->su_ga : &su->su_sga;
13057 if (i < gap->ga_len)
13058 {
13059 /* Don't add a word if it's already there. */
13060 p = SUG(*gap, i).st_word;
13061 for (j = 0; j < ga.ga_len; ++j)
13062 if (STRCMP(stp[j].st_word, p) == 0)
13063 break;
13064 if (j == ga.ga_len)
13065 stp[ga.ga_len++] = SUG(*gap, i);
13066 else
13067 vim_free(p);
13068 }
13069 }
13070 }
13071
13072 ga_clear(&su->su_ga);
13073 ga_clear(&su->su_sga);
13074
13075 /* Truncate the list to the number of suggestions that will be displayed. */
13076 if (ga.ga_len > su->su_maxcount)
13077 {
13078 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13079 vim_free(stp[i].st_word);
13080 ga.ga_len = su->su_maxcount;
13081 }
13082
13083 su->su_ga = ga;
13084}
13085
13086/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013087 * For the goodword in "stp" compute the soundalike score compared to the
13088 * badword.
13089 */
13090 static int
13091stp_sal_score(stp, su, slang, badsound)
13092 suggest_T *stp;
13093 suginfo_T *su;
13094 slang_T *slang;
13095 char_u *badsound; /* sound-folded badword */
13096{
13097 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013098 char_u *pbad;
13099 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013100 char_u badsound2[MAXWLEN];
13101 char_u fword[MAXWLEN];
13102 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013103 char_u goodword[MAXWLEN];
13104 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013105
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013106 lendiff = (int)(su->su_badlen - stp->st_orglen);
13107 if (lendiff >= 0)
13108 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013109 else
13110 {
13111 /* soundfold the bad word with more characters following */
13112 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13113
13114 /* When joining two words the sound often changes a lot. E.g., "t he"
13115 * sounds like "t h" while "the" sounds like "@". Avoid that by
13116 * removing the space. Don't do it when the good word also contains a
13117 * space. */
13118 if (vim_iswhite(su->su_badptr[su->su_badlen])
13119 && *skiptowhite(stp->st_word) == NUL)
13120 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013121 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013122
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013123 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013124 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013125 }
13126
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013127 if (lendiff > 0)
13128 {
13129 /* Add part of the bad word to the good word, so that we soundfold
13130 * what replaces the bad word. */
13131 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013132 vim_strncpy(goodword + stp->st_wordlen,
13133 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013134 pgood = goodword;
13135 }
13136 else
13137 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013138
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013139 /* Sound-fold the word and compute the score for the difference. */
13140 spell_soundfold(slang, pgood, FALSE, goodsound);
13141
13142 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013143}
13144
Bram Moolenaar4770d092006-01-12 23:22:24 +000013145/* structure used to store soundfolded words that add_sound_suggest() has
13146 * handled already. */
13147typedef struct
13148{
13149 short sft_score; /* lowest score used */
13150 char_u sft_word[1]; /* soundfolded word, actually longer */
13151} sftword_T;
13152
13153static sftword_T dumsft;
13154#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13155#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13156
13157/*
13158 * Prepare for calling suggest_try_soundalike().
13159 */
13160 static void
13161suggest_try_soundalike_prep()
13162{
13163 langp_T *lp;
13164 int lpi;
13165 slang_T *slang;
13166
13167 /* Do this for all languages that support sound folding and for which a
13168 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013169 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013170 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013171 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013172 slang = lp->lp_slang;
13173 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13174 /* prepare the hashtable used by add_sound_suggest() */
13175 hash_init(&slang->sl_sounddone);
13176 }
13177}
13178
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013179/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013181 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013182 */
13183 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013184suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013185 suginfo_T *su;
13186{
13187 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013189 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013190 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013191
Bram Moolenaar4770d092006-01-12 23:22:24 +000013192 /* Do this for all languages that support sound folding and for which a
13193 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013194 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013195 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013196 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013197 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013198 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 {
13200 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013201 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013202
Bram Moolenaar4770d092006-01-12 23:22:24 +000013203 /* try all kinds of inserts/deletes/swaps/etc. */
13204 /* TODO: also soundfold the next words, so that we can try joining
13205 * and splitting */
13206 suggest_trie_walk(su, lp, salword, TRUE);
13207 }
13208 }
13209}
13210
13211/*
13212 * Finish up after calling suggest_try_soundalike().
13213 */
13214 static void
13215suggest_try_soundalike_finish()
13216{
13217 langp_T *lp;
13218 int lpi;
13219 slang_T *slang;
13220 int todo;
13221 hashitem_T *hi;
13222
13223 /* Do this for all languages that support sound folding and for which a
13224 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013225 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013226 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013227 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013228 slang = lp->lp_slang;
13229 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13230 {
13231 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013232 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013233 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13234 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013236 vim_free(HI2SFT(hi));
13237 --todo;
13238 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013239
13240 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013241 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013242 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013243 }
13244 }
13245}
13246
13247/*
13248 * A match with a soundfolded word is found. Add the good word(s) that
13249 * produce this soundfolded word.
13250 */
13251 static void
13252add_sound_suggest(su, goodword, score, lp)
13253 suginfo_T *su;
13254 char_u *goodword;
13255 int score; /* soundfold score */
13256 langp_T *lp;
13257{
13258 slang_T *slang = lp->lp_slang; /* language for sound folding */
13259 int sfwordnr;
13260 char_u *nrline;
13261 int orgnr;
13262 char_u theword[MAXWLEN];
13263 int i;
13264 int wlen;
13265 char_u *byts;
13266 idx_T *idxs;
13267 int n;
13268 int wordcount;
13269 int wc;
13270 int goodscore;
13271 hash_T hash;
13272 hashitem_T *hi;
13273 sftword_T *sft;
13274 int bc, gc;
13275 int limit;
13276
13277 /*
13278 * It's very well possible that the same soundfold word is found several
13279 * times with different scores. Since the following is quite slow only do
13280 * the words that have a better score than before. Use a hashtable to
13281 * remember the words that have been done.
13282 */
13283 hash = hash_hash(goodword);
13284 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13285 if (HASHITEM_EMPTY(hi))
13286 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013287 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13288 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013289 if (sft != NULL)
13290 {
13291 sft->sft_score = score;
13292 STRCPY(sft->sft_word, goodword);
13293 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13294 }
13295 }
13296 else
13297 {
13298 sft = HI2SFT(hi);
13299 if (score >= sft->sft_score)
13300 return;
13301 sft->sft_score = score;
13302 }
13303
13304 /*
13305 * Find the word nr in the soundfold tree.
13306 */
13307 sfwordnr = soundfold_find(slang, goodword);
13308 if (sfwordnr < 0)
13309 {
13310 EMSG2(_(e_intern2), "add_sound_suggest()");
13311 return;
13312 }
13313
13314 /*
13315 * go over the list of good words that produce this soundfold word
13316 */
13317 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13318 orgnr = 0;
13319 while (*nrline != NUL)
13320 {
13321 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13322 * previous wordnr. */
13323 orgnr += bytes2offset(&nrline);
13324
13325 byts = slang->sl_fbyts;
13326 idxs = slang->sl_fidxs;
13327
13328 /* Lookup the word "orgnr" one of the two tries. */
13329 n = 0;
13330 wlen = 0;
13331 wordcount = 0;
13332 for (;;)
13333 {
13334 i = 1;
13335 if (wordcount == orgnr && byts[n + 1] == NUL)
13336 break; /* found end of word */
13337
13338 if (byts[n + 1] == NUL)
13339 ++wordcount;
13340
13341 /* skip over the NUL bytes */
13342 for ( ; byts[n + i] == NUL; ++i)
13343 if (i > byts[n]) /* safety check */
13344 {
13345 STRCPY(theword + wlen, "BAD");
13346 goto badword;
13347 }
13348
13349 /* One of the siblings must have the word. */
13350 for ( ; i < byts[n]; ++i)
13351 {
13352 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13353 if (wordcount + wc > orgnr)
13354 break;
13355 wordcount += wc;
13356 }
13357
13358 theword[wlen++] = byts[n + i];
13359 n = idxs[n + i];
13360 }
13361badword:
13362 theword[wlen] = NUL;
13363
13364 /* Go over the possible flags and regions. */
13365 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13366 {
13367 char_u cword[MAXWLEN];
13368 char_u *p;
13369 int flags = (int)idxs[n + i];
13370
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013371 /* Skip words with the NOSUGGEST flag */
13372 if (flags & WF_NOSUGGEST)
13373 continue;
13374
Bram Moolenaar4770d092006-01-12 23:22:24 +000013375 if (flags & WF_KEEPCAP)
13376 {
13377 /* Must find the word in the keep-case tree. */
13378 find_keepcap_word(slang, theword, cword);
13379 p = cword;
13380 }
13381 else
13382 {
13383 flags |= su->su_badflags;
13384 if ((flags & WF_CAPMASK) != 0)
13385 {
13386 /* Need to fix case according to "flags". */
13387 make_case_word(theword, cword, flags);
13388 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013389 }
13390 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013391 p = theword;
13392 }
13393
13394 /* Add the suggestion. */
13395 if (sps_flags & SPS_DOUBLE)
13396 {
13397 /* Add the suggestion if the score isn't too bad. */
13398 if (score <= su->su_maxscore)
13399 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13400 score, 0, FALSE, slang, FALSE);
13401 }
13402 else
13403 {
13404 /* Add a penalty for words in another region. */
13405 if ((flags & WF_REGION)
13406 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13407 goodscore = SCORE_REGION;
13408 else
13409 goodscore = 0;
13410
13411 /* Add a small penalty for changing the first letter from
13412 * lower to upper case. Helps for "tath" -> "Kath", which is
13413 * less common thatn "tath" -> "path". Don't do it when the
13414 * letter is the same, that has already been counted. */
13415 gc = PTR2CHAR(p);
13416 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013418 bc = PTR2CHAR(su->su_badword);
13419 if (!SPELL_ISUPPER(bc)
13420 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13421 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 }
13423
Bram Moolenaar4770d092006-01-12 23:22:24 +000013424 /* Compute the score for the good word. This only does letter
13425 * insert/delete/swap/replace. REP items are not considered,
13426 * which may make the score a bit higher.
13427 * Use a limit for the score to make it work faster. Use
13428 * MAXSCORE(), because RESCORE() will change the score.
13429 * If the limit is very high then the iterative method is
13430 * inefficient, using an array is quicker. */
13431 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13432 if (limit > SCORE_LIMITMAX)
13433 goodscore += spell_edit_score(slang, su->su_badword, p);
13434 else
13435 goodscore += spell_edit_score_limit(slang, su->su_badword,
13436 p, limit);
13437
13438 /* When going over the limit don't bother to do the rest. */
13439 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013441 /* Give a bonus to words seen before. */
13442 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443
Bram Moolenaar4770d092006-01-12 23:22:24 +000013444 /* Add the suggestion if the score isn't too bad. */
13445 goodscore = RESCORE(goodscore, score);
13446 if (goodscore <= su->su_sfmaxscore)
13447 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13448 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013450 }
13451 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013452 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 }
13454}
13455
13456/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013457 * Find word "word" in fold-case tree for "slang" and return the word number.
13458 */
13459 static int
13460soundfold_find(slang, word)
13461 slang_T *slang;
13462 char_u *word;
13463{
13464 idx_T arridx = 0;
13465 int len;
13466 int wlen = 0;
13467 int c;
13468 char_u *ptr = word;
13469 char_u *byts;
13470 idx_T *idxs;
13471 int wordnr = 0;
13472
13473 byts = slang->sl_sbyts;
13474 idxs = slang->sl_sidxs;
13475
13476 for (;;)
13477 {
13478 /* First byte is the number of possible bytes. */
13479 len = byts[arridx++];
13480
13481 /* If the first possible byte is a zero the word could end here.
13482 * If the word ends we found the word. If not skip the NUL bytes. */
13483 c = ptr[wlen];
13484 if (byts[arridx] == NUL)
13485 {
13486 if (c == NUL)
13487 break;
13488
13489 /* Skip over the zeros, there can be several. */
13490 while (len > 0 && byts[arridx] == NUL)
13491 {
13492 ++arridx;
13493 --len;
13494 }
13495 if (len == 0)
13496 return -1; /* no children, word should have ended here */
13497 ++wordnr;
13498 }
13499
13500 /* If the word ends we didn't find it. */
13501 if (c == NUL)
13502 return -1;
13503
13504 /* Perform a binary search in the list of accepted bytes. */
13505 if (c == TAB) /* <Tab> is handled like <Space> */
13506 c = ' ';
13507 while (byts[arridx] < c)
13508 {
13509 /* The word count is in the first idxs[] entry of the child. */
13510 wordnr += idxs[idxs[arridx]];
13511 ++arridx;
13512 if (--len == 0) /* end of the bytes, didn't find it */
13513 return -1;
13514 }
13515 if (byts[arridx] != c) /* didn't find the byte */
13516 return -1;
13517
13518 /* Continue at the child (if there is one). */
13519 arridx = idxs[arridx];
13520 ++wlen;
13521
13522 /* One space in the good word may stand for several spaces in the
13523 * checked word. */
13524 if (c == ' ')
13525 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13526 ++wlen;
13527 }
13528
13529 return wordnr;
13530}
13531
13532/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013533 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 */
13535 static void
13536make_case_word(fword, cword, flags)
13537 char_u *fword;
13538 char_u *cword;
13539 int flags;
13540{
13541 if (flags & WF_ALLCAP)
13542 /* Make it all upper-case */
13543 allcap_copy(fword, cword);
13544 else if (flags & WF_ONECAP)
13545 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013546 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013547 else
13548 /* Use goodword as-is. */
13549 STRCPY(cword, fword);
13550}
13551
Bram Moolenaarea424162005-06-16 21:51:00 +000013552/*
13553 * Use map string "map" for languages "lp".
13554 */
13555 static void
13556set_map_str(lp, map)
13557 slang_T *lp;
13558 char_u *map;
13559{
13560 char_u *p;
13561 int headc = 0;
13562 int c;
13563 int i;
13564
13565 if (*map == NUL)
13566 {
13567 lp->sl_has_map = FALSE;
13568 return;
13569 }
13570 lp->sl_has_map = TRUE;
13571
Bram Moolenaar4770d092006-01-12 23:22:24 +000013572 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013573 for (i = 0; i < 256; ++i)
13574 lp->sl_map_array[i] = 0;
13575#ifdef FEAT_MBYTE
13576 hash_init(&lp->sl_map_hash);
13577#endif
13578
13579 /*
13580 * The similar characters are stored separated with slashes:
13581 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13582 * before the same slash. For characters above 255 sl_map_hash is used.
13583 */
13584 for (p = map; *p != NUL; )
13585 {
13586#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013587 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013588#else
13589 c = *p++;
13590#endif
13591 if (c == '/')
13592 headc = 0;
13593 else
13594 {
13595 if (headc == 0)
13596 headc = c;
13597
13598#ifdef FEAT_MBYTE
13599 /* Characters above 255 don't fit in sl_map_array[], put them in
13600 * the hash table. Each entry is the char, a NUL the headchar and
13601 * a NUL. */
13602 if (c >= 256)
13603 {
13604 int cl = mb_char2len(c);
13605 int headcl = mb_char2len(headc);
13606 char_u *b;
13607 hash_T hash;
13608 hashitem_T *hi;
13609
13610 b = alloc((unsigned)(cl + headcl + 2));
13611 if (b == NULL)
13612 return;
13613 mb_char2bytes(c, b);
13614 b[cl] = NUL;
13615 mb_char2bytes(headc, b + cl + 1);
13616 b[cl + 1 + headcl] = NUL;
13617 hash = hash_hash(b);
13618 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13619 if (HASHITEM_EMPTY(hi))
13620 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13621 else
13622 {
13623 /* This should have been checked when generating the .spl
13624 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013625 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013626 vim_free(b);
13627 }
13628 }
13629 else
13630#endif
13631 lp->sl_map_array[c] = headc;
13632 }
13633 }
13634}
13635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636/*
13637 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13638 * lines in the .aff file.
13639 */
13640 static int
13641similar_chars(slang, c1, c2)
13642 slang_T *slang;
13643 int c1;
13644 int c2;
13645{
Bram Moolenaarea424162005-06-16 21:51:00 +000013646 int m1, m2;
13647#ifdef FEAT_MBYTE
13648 char_u buf[MB_MAXBYTES];
13649 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013650
Bram Moolenaarea424162005-06-16 21:51:00 +000013651 if (c1 >= 256)
13652 {
13653 buf[mb_char2bytes(c1, buf)] = 0;
13654 hi = hash_find(&slang->sl_map_hash, buf);
13655 if (HASHITEM_EMPTY(hi))
13656 m1 = 0;
13657 else
13658 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13659 }
13660 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013661#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013662 m1 = slang->sl_map_array[c1];
13663 if (m1 == 0)
13664 return FALSE;
13665
13666
13667#ifdef FEAT_MBYTE
13668 if (c2 >= 256)
13669 {
13670 buf[mb_char2bytes(c2, buf)] = 0;
13671 hi = hash_find(&slang->sl_map_hash, buf);
13672 if (HASHITEM_EMPTY(hi))
13673 m2 = 0;
13674 else
13675 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13676 }
13677 else
13678#endif
13679 m2 = slang->sl_map_array[c2];
13680
13681 return m1 == m2;
13682}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683
13684/*
13685 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013686 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 */
13688 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013689add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13690 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013692 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013694 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013695 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013696 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013697 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013698 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013699 int maxsf; /* su_maxscore applies to soundfold score,
13700 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013701{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013702 int goodlen; /* len of goodword changed */
13703 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013705 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013707 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013709 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13710 * "thee the" is added next to changing the first "the" the "thee". */
13711 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013712 pbad = su->su_badptr + badlenarg;
13713 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013715 goodlen = (int)(pgood - goodword);
13716 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013717 if (goodlen <= 0 || badlen <= 0)
13718 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013719 mb_ptr_back(goodword, pgood);
13720 mb_ptr_back(su->su_badptr, pbad);
13721#ifdef FEAT_MBYTE
13722 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013723 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013724 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13725 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013726 }
13727 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013728#endif
13729 if (*pgood != *pbad)
13730 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013731 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013732
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013733 if (badlen == 0 && goodlen == 0)
13734 /* goodword doesn't change anything; may happen for "the the" changing
13735 * the first "the" to itself. */
13736 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013737
Bram Moolenaar89d40322006-08-29 15:30:07 +000013738 if (gap->ga_len == 0)
13739 i = -1;
13740 else
13741 {
13742 /* Check if the word is already there. Also check the length that is
13743 * being replaced "thes," -> "these" is a different suggestion from
13744 * "thes" -> "these". */
13745 stp = &SUG(*gap, 0);
13746 for (i = gap->ga_len; --i >= 0; ++stp)
13747 if (stp->st_wordlen == goodlen
13748 && stp->st_orglen == badlen
13749 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013751 /*
13752 * Found it. Remember the word with the lowest score.
13753 */
13754 if (stp->st_slang == NULL)
13755 stp->st_slang = slang;
13756
13757 new_sug.st_score = score;
13758 new_sug.st_altscore = altscore;
13759 new_sug.st_had_bonus = had_bonus;
13760
13761 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013762 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013763 /* Only one of the two had the soundalike score computed.
13764 * Need to do that for the other one now, otherwise the
13765 * scores can't be compared. This happens because
13766 * suggest_try_change() doesn't compute the soundalike
13767 * word to keep it fast, while some special methods set
13768 * the soundalike score to zero. */
13769 if (had_bonus)
13770 rescore_one(su, stp);
13771 else
13772 {
13773 new_sug.st_word = stp->st_word;
13774 new_sug.st_wordlen = stp->st_wordlen;
13775 new_sug.st_slang = stp->st_slang;
13776 new_sug.st_orglen = badlen;
13777 rescore_one(su, &new_sug);
13778 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013779 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780
Bram Moolenaar89d40322006-08-29 15:30:07 +000013781 if (stp->st_score > new_sug.st_score)
13782 {
13783 stp->st_score = new_sug.st_score;
13784 stp->st_altscore = new_sug.st_altscore;
13785 stp->st_had_bonus = new_sug.st_had_bonus;
13786 }
13787 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013788 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013790
Bram Moolenaar4770d092006-01-12 23:22:24 +000013791 if (i < 0 && ga_grow(gap, 1) == OK)
13792 {
13793 /* Add a suggestion. */
13794 stp = &SUG(*gap, gap->ga_len);
13795 stp->st_word = vim_strnsave(goodword, goodlen);
13796 if (stp->st_word != NULL)
13797 {
13798 stp->st_wordlen = goodlen;
13799 stp->st_score = score;
13800 stp->st_altscore = altscore;
13801 stp->st_had_bonus = had_bonus;
13802 stp->st_orglen = badlen;
13803 stp->st_slang = slang;
13804 ++gap->ga_len;
13805
13806 /* If we have too many suggestions now, sort the list and keep
13807 * the best suggestions. */
13808 if (gap->ga_len > SUG_MAX_COUNT(su))
13809 {
13810 if (maxsf)
13811 su->su_sfmaxscore = cleanup_suggestions(gap,
13812 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13813 else
13814 {
13815 i = su->su_maxscore;
13816 su->su_maxscore = cleanup_suggestions(gap,
13817 su->su_maxscore, SUG_CLEAN_COUNT(su));
13818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 }
13820 }
13821 }
13822}
13823
13824/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013825 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13826 * for split words, such as "the the". Remove these from the list here.
13827 */
13828 static void
13829check_suggestions(su, gap)
13830 suginfo_T *su;
13831 garray_T *gap; /* either su_ga or su_sga */
13832{
13833 suggest_T *stp;
13834 int i;
13835 char_u longword[MAXWLEN + 1];
13836 int len;
13837 hlf_T attr;
13838
13839 stp = &SUG(*gap, 0);
13840 for (i = gap->ga_len - 1; i >= 0; --i)
13841 {
13842 /* Need to append what follows to check for "the the". */
13843 STRCPY(longword, stp[i].st_word);
13844 len = stp[i].st_wordlen;
13845 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13846 MAXWLEN - len);
13847 attr = HLF_COUNT;
13848 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13849 if (attr != HLF_COUNT)
13850 {
13851 /* Remove this entry. */
13852 vim_free(stp[i].st_word);
13853 --gap->ga_len;
13854 if (i < gap->ga_len)
13855 mch_memmove(stp + i, stp + i + 1,
13856 sizeof(suggest_T) * (gap->ga_len - i));
13857 }
13858 }
13859}
13860
13861
13862/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 * Add a word to be banned.
13864 */
13865 static void
13866add_banned(su, word)
13867 suginfo_T *su;
13868 char_u *word;
13869{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013870 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 hash_T hash;
13872 hashitem_T *hi;
13873
Bram Moolenaar4770d092006-01-12 23:22:24 +000013874 hash = hash_hash(word);
13875 hi = hash_lookup(&su->su_banned, word, hash);
13876 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013878 s = vim_strsave(word);
13879 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013880 hash_add_item(&su->su_banned, hi, s, hash);
13881 }
13882}
13883
13884/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013885 * Recompute the score for all suggestions if sound-folding is possible. This
13886 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013887 */
13888 static void
13889rescore_suggestions(su)
13890 suginfo_T *su;
13891{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013892 int i;
13893
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013894 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013895 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013896 rescore_one(su, &SUG(su->su_ga, i));
13897}
13898
13899/*
13900 * Recompute the score for one suggestion if sound-folding is possible.
13901 */
13902 static void
13903rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013904 suginfo_T *su;
13905 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013906{
13907 slang_T *slang = stp->st_slang;
13908 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013909 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013910
13911 /* Only rescore suggestions that have no sal score yet and do have a
13912 * language. */
13913 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13914 {
13915 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013916 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013917 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013918 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013919 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013920 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013921 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013922
13923 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013924 if (stp->st_altscore == SCORE_MAXMAX)
13925 stp->st_altscore = SCORE_BIG;
13926 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13927 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013928 }
13929}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013931static int
13932#ifdef __BORLANDC__
13933_RTLENTRYF
13934#endif
13935sug_compare __ARGS((const void *s1, const void *s2));
13936
13937/*
13938 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013939 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 */
13941 static int
13942#ifdef __BORLANDC__
13943_RTLENTRYF
13944#endif
13945sug_compare(s1, s2)
13946 const void *s1;
13947 const void *s2;
13948{
13949 suggest_T *p1 = (suggest_T *)s1;
13950 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013951 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013952
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013953 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013954 {
13955 n = p1->st_altscore - p2->st_altscore;
13956 if (n == 0)
13957 n = STRICMP(p1->st_word, p2->st_word);
13958 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013959 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960}
13961
13962/*
13963 * Cleanup the suggestions:
13964 * - Sort on score.
13965 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013966 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013967 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013968 static int
13969cleanup_suggestions(gap, maxscore, keep)
13970 garray_T *gap;
13971 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013972 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013974 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 int i;
13976
13977 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013978 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013979
13980 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013981 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013983 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013984 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013985 gap->ga_len = keep;
13986 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013988 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013989}
13990
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013991#if defined(FEAT_EVAL) || defined(PROTO)
13992/*
13993 * Soundfold a string, for soundfold().
13994 * Result is in allocated memory, NULL for an error.
13995 */
13996 char_u *
13997eval_soundfold(word)
13998 char_u *word;
13999{
14000 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014001 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014002 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014003
Bram Moolenaar860cae12010-06-05 23:22:07 +020014004 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014005 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014006 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014007 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014008 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014009 if (lp->lp_slang->sl_sal.ga_len > 0)
14010 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014011 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014012 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014013 return vim_strsave(sound);
14014 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014015 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014016
14017 /* No language with sound folding, return word as-is. */
14018 return vim_strsave(word);
14019}
14020#endif
14021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022/*
14023 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014024 *
14025 * There are many ways to turn a word into a sound-a-like representation. The
14026 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14027 * swedish name matching - survey and test of different algorithms" by Klas
14028 * Erikson.
14029 *
14030 * We support two methods:
14031 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14032 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 */
14034 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014035spell_soundfold(slang, inword, folded, res)
14036 slang_T *slang;
14037 char_u *inword;
14038 int folded; /* "inword" is already case-folded */
14039 char_u *res;
14040{
14041 char_u fword[MAXWLEN];
14042 char_u *word;
14043
14044 if (slang->sl_sofo)
14045 /* SOFOFROM and SOFOTO used */
14046 spell_soundfold_sofo(slang, inword, res);
14047 else
14048 {
14049 /* SAL items used. Requires the word to be case-folded. */
14050 if (folded)
14051 word = inword;
14052 else
14053 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014054 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014055 word = fword;
14056 }
14057
14058#ifdef FEAT_MBYTE
14059 if (has_mbyte)
14060 spell_soundfold_wsal(slang, word, res);
14061 else
14062#endif
14063 spell_soundfold_sal(slang, word, res);
14064 }
14065}
14066
14067/*
14068 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14069 * SOFOTO lines.
14070 */
14071 static void
14072spell_soundfold_sofo(slang, inword, res)
14073 slang_T *slang;
14074 char_u *inword;
14075 char_u *res;
14076{
14077 char_u *s;
14078 int ri = 0;
14079 int c;
14080
14081#ifdef FEAT_MBYTE
14082 if (has_mbyte)
14083 {
14084 int prevc = 0;
14085 int *ip;
14086
14087 /* The sl_sal_first[] table contains the translation for chars up to
14088 * 255, sl_sal the rest. */
14089 for (s = inword; *s != NUL; )
14090 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014091 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014092 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14093 c = ' ';
14094 else if (c < 256)
14095 c = slang->sl_sal_first[c];
14096 else
14097 {
14098 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14099 if (ip == NULL) /* empty list, can't match */
14100 c = NUL;
14101 else
14102 for (;;) /* find "c" in the list */
14103 {
14104 if (*ip == 0) /* not found */
14105 {
14106 c = NUL;
14107 break;
14108 }
14109 if (*ip == c) /* match! */
14110 {
14111 c = ip[1];
14112 break;
14113 }
14114 ip += 2;
14115 }
14116 }
14117
14118 if (c != NUL && c != prevc)
14119 {
14120 ri += mb_char2bytes(c, res + ri);
14121 if (ri + MB_MAXBYTES > MAXWLEN)
14122 break;
14123 prevc = c;
14124 }
14125 }
14126 }
14127 else
14128#endif
14129 {
14130 /* The sl_sal_first[] table contains the translation. */
14131 for (s = inword; (c = *s) != NUL; ++s)
14132 {
14133 if (vim_iswhite(c))
14134 c = ' ';
14135 else
14136 c = slang->sl_sal_first[c];
14137 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14138 res[ri++] = c;
14139 }
14140 }
14141
14142 res[ri] = NUL;
14143}
14144
14145 static void
14146spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 slang_T *slang;
14148 char_u *inword;
14149 char_u *res;
14150{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014151 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014153 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014155 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014157 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 int n, k = 0;
14159 int z0;
14160 int k0;
14161 int n0;
14162 int c;
14163 int pri;
14164 int p0 = -333;
14165 int c0;
14166
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014167 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014168 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 if (slang->sl_rem_accents)
14170 {
14171 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014172 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014174 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014175 {
14176 *t++ = ' ';
14177 s = skipwhite(s);
14178 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014179 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014181 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 *t++ = *s;
14183 ++s;
14184 }
14185 }
14186 *t = NUL;
14187 }
14188 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014189 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014191 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192
14193 /*
14194 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014195 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014197 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 while ((c = word[i]) != NUL)
14199 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014200 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014201 n = slang->sl_sal_first[c];
14202 z0 = 0;
14203
14204 if (n >= 0)
14205 {
14206 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014207 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014209 /* Quickly skip entries that don't match the word. Most
14210 * entries are less then three chars, optimize for that. */
14211 k = smp[n].sm_leadlen;
14212 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014214 if (word[i + 1] != s[1])
14215 continue;
14216 if (k > 2)
14217 {
14218 for (j = 2; j < k; ++j)
14219 if (word[i + j] != s[j])
14220 break;
14221 if (j < k)
14222 continue;
14223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 }
14225
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014226 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014228 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014229 while (*pf != NUL && *pf != word[i + k])
14230 ++pf;
14231 if (*pf == NUL)
14232 continue;
14233 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014235 s = smp[n].sm_rules;
14236 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237
14238 p0 = *s;
14239 k0 = k;
14240 while (*s == '-' && k > 1)
14241 {
14242 k--;
14243 s++;
14244 }
14245 if (*s == '<')
14246 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014247 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 {
14249 /* determine priority */
14250 pri = *s - '0';
14251 s++;
14252 }
14253 if (*s == '^' && *(s + 1) == '^')
14254 s++;
14255
14256 if (*s == NUL
14257 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014258 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014259 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014261 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014263 && spell_iswordp(word + i - 1, curwin)
14264 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 {
14266 /* search for followup rules, if: */
14267 /* followup and k > 1 and NO '-' in searchstring */
14268 c0 = word[i + k - 1];
14269 n0 = slang->sl_sal_first[c0];
14270
14271 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014272 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 {
14274 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014275 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014277 /* Quickly skip entries that don't match the word.
14278 * */
14279 k0 = smp[n0].sm_leadlen;
14280 if (k0 > 1)
14281 {
14282 if (word[i + k] != s[1])
14283 continue;
14284 if (k0 > 2)
14285 {
14286 pf = word + i + k + 1;
14287 for (j = 2; j < k0; ++j)
14288 if (*pf++ != s[j])
14289 break;
14290 if (j < k0)
14291 continue;
14292 }
14293 }
14294 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014296 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014297 {
14298 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014299 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014300 while (*pf != NUL && *pf != word[i + k0])
14301 ++pf;
14302 if (*pf == NUL)
14303 continue;
14304 ++k0;
14305 }
14306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014308 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 while (*s == '-')
14310 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014311 /* "k0" gets NOT reduced because
14312 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 s++;
14314 }
14315 if (*s == '<')
14316 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014317 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318 {
14319 p0 = *s - '0';
14320 s++;
14321 }
14322
14323 if (*s == NUL
14324 /* *s == '^' cuts */
14325 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014326 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014327 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 {
14329 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332
14333 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014334 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 /* rule fits; stop search */
14337 break;
14338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014339 }
14340
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014341 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 }
14344
14345 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014346 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014347 if (s == NULL)
14348 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014349 pf = smp[n].sm_rules;
14350 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 if (p0 == 1 && z == 0)
14352 {
14353 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014354 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14355 || res[reslen - 1] == *s))
14356 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 z0 = 1;
14358 z = 1;
14359 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014360 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 {
14362 word[i + k0] = *s;
14363 k0++;
14364 s++;
14365 }
14366 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014367 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368
14369 /* new "actual letter" */
14370 c = word[i];
14371 }
14372 else
14373 {
14374 /* no '<' rule used */
14375 i += k - 1;
14376 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014377 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014378 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014379 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014380 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 s++;
14382 }
14383 /* new "actual letter" */
14384 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014385 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 {
14387 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014388 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014389 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 i = 0;
14391 z0 = 1;
14392 }
14393 }
14394 break;
14395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 }
14397 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014398 else if (vim_iswhite(c))
14399 {
14400 c = ' ';
14401 k = 1;
14402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014403
14404 if (z0 == 0)
14405 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014406 if (k && !p0 && reslen < MAXWLEN && c != NUL
14407 && (!slang->sl_collapse || reslen == 0
14408 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014410 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411
14412 i++;
14413 z = 0;
14414 k = 0;
14415 }
14416 }
14417
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014418 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419}
14420
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014421#ifdef FEAT_MBYTE
14422/*
14423 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14424 * Multi-byte version of spell_soundfold().
14425 */
14426 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014427spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014428 slang_T *slang;
14429 char_u *inword;
14430 char_u *res;
14431{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014432 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014433 int word[MAXWLEN];
14434 int wres[MAXWLEN];
14435 int l;
14436 char_u *s;
14437 int *ws;
14438 char_u *t;
14439 int *pf;
14440 int i, j, z;
14441 int reslen;
14442 int n, k = 0;
14443 int z0;
14444 int k0;
14445 int n0;
14446 int c;
14447 int pri;
14448 int p0 = -333;
14449 int c0;
14450 int did_white = FALSE;
14451
14452 /*
14453 * Convert the multi-byte string to a wide-character string.
14454 * Remove accents, if wanted. We actually remove all non-word characters.
14455 * But keep white space.
14456 */
14457 n = 0;
14458 for (s = inword; *s != NUL; )
14459 {
14460 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014461 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014462 if (slang->sl_rem_accents)
14463 {
14464 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14465 {
14466 if (did_white)
14467 continue;
14468 c = ' ';
14469 did_white = TRUE;
14470 }
14471 else
14472 {
14473 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014474 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014475 continue;
14476 }
14477 }
14478 word[n++] = c;
14479 }
14480 word[n] = NUL;
14481
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014482 /*
14483 * This comes from Aspell phonet.cpp.
14484 * Converted from C++ to C. Added support for multi-byte chars.
14485 * Changed to keep spaces.
14486 */
14487 i = reslen = z = 0;
14488 while ((c = word[i]) != NUL)
14489 {
14490 /* Start with the first rule that has the character in the word. */
14491 n = slang->sl_sal_first[c & 0xff];
14492 z0 = 0;
14493
14494 if (n >= 0)
14495 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014496 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014497 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14498 {
14499 /* Quickly skip entries that don't match the word. Most
14500 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014501 if (c != ws[0])
14502 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014503 k = smp[n].sm_leadlen;
14504 if (k > 1)
14505 {
14506 if (word[i + 1] != ws[1])
14507 continue;
14508 if (k > 2)
14509 {
14510 for (j = 2; j < k; ++j)
14511 if (word[i + j] != ws[j])
14512 break;
14513 if (j < k)
14514 continue;
14515 }
14516 }
14517
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014518 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014519 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014520 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014521 while (*pf != NUL && *pf != word[i + k])
14522 ++pf;
14523 if (*pf == NUL)
14524 continue;
14525 ++k;
14526 }
14527 s = smp[n].sm_rules;
14528 pri = 5; /* default priority */
14529
14530 p0 = *s;
14531 k0 = k;
14532 while (*s == '-' && k > 1)
14533 {
14534 k--;
14535 s++;
14536 }
14537 if (*s == '<')
14538 s++;
14539 if (VIM_ISDIGIT(*s))
14540 {
14541 /* determine priority */
14542 pri = *s - '0';
14543 s++;
14544 }
14545 if (*s == '^' && *(s + 1) == '^')
14546 s++;
14547
14548 if (*s == NUL
14549 || (*s == '^'
14550 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014551 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014552 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014553 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014554 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014555 && spell_iswordp_w(word + i - 1, curwin)
14556 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014557 {
14558 /* search for followup rules, if: */
14559 /* followup and k > 1 and NO '-' in searchstring */
14560 c0 = word[i + k - 1];
14561 n0 = slang->sl_sal_first[c0 & 0xff];
14562
14563 if (slang->sl_followup && k > 1 && n0 >= 0
14564 && p0 != '-' && word[i + k] != NUL)
14565 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014566 /* Test follow-up rule for "word[i + k]"; loop over
14567 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014568 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14569 == (c0 & 0xff); ++n0)
14570 {
14571 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014572 */
14573 if (c0 != ws[0])
14574 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014575 k0 = smp[n0].sm_leadlen;
14576 if (k0 > 1)
14577 {
14578 if (word[i + k] != ws[1])
14579 continue;
14580 if (k0 > 2)
14581 {
14582 pf = word + i + k + 1;
14583 for (j = 2; j < k0; ++j)
14584 if (*pf++ != ws[j])
14585 break;
14586 if (j < k0)
14587 continue;
14588 }
14589 }
14590 k0 += k - 1;
14591
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014592 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014593 {
14594 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014595 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014596 while (*pf != NUL && *pf != word[i + k0])
14597 ++pf;
14598 if (*pf == NUL)
14599 continue;
14600 ++k0;
14601 }
14602
14603 p0 = 5;
14604 s = smp[n0].sm_rules;
14605 while (*s == '-')
14606 {
14607 /* "k0" gets NOT reduced because
14608 * "if (k0 == k)" */
14609 s++;
14610 }
14611 if (*s == '<')
14612 s++;
14613 if (VIM_ISDIGIT(*s))
14614 {
14615 p0 = *s - '0';
14616 s++;
14617 }
14618
14619 if (*s == NUL
14620 /* *s == '^' cuts */
14621 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014622 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014623 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014624 {
14625 if (k0 == k)
14626 /* this is just a piece of the string */
14627 continue;
14628
14629 if (p0 < pri)
14630 /* priority too low */
14631 continue;
14632 /* rule fits; stop search */
14633 break;
14634 }
14635 }
14636
14637 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14638 == (c0 & 0xff))
14639 continue;
14640 }
14641
14642 /* replace string */
14643 ws = smp[n].sm_to_w;
14644 s = smp[n].sm_rules;
14645 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14646 if (p0 == 1 && z == 0)
14647 {
14648 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014649 if (reslen > 0 && ws != NULL && *ws != NUL
14650 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014651 || wres[reslen - 1] == *ws))
14652 reslen--;
14653 z0 = 1;
14654 z = 1;
14655 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014656 if (ws != NULL)
14657 while (*ws != NUL && word[i + k0] != NUL)
14658 {
14659 word[i + k0] = *ws;
14660 k0++;
14661 ws++;
14662 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014663 if (k > k0)
14664 mch_memmove(word + i + k0, word + i + k,
14665 sizeof(int) * (STRLEN(word + i + k) + 1));
14666
14667 /* new "actual letter" */
14668 c = word[i];
14669 }
14670 else
14671 {
14672 /* no '<' rule used */
14673 i += k - 1;
14674 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014675 if (ws != NULL)
14676 while (*ws != NUL && ws[1] != NUL
14677 && reslen < MAXWLEN)
14678 {
14679 if (reslen == 0 || wres[reslen - 1] != *ws)
14680 wres[reslen++] = *ws;
14681 ws++;
14682 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014683 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014684 if (ws == NULL)
14685 c = NUL;
14686 else
14687 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014688 if (strstr((char *)s, "^^") != NULL)
14689 {
14690 if (c != NUL)
14691 wres[reslen++] = c;
14692 mch_memmove(word, word + i + 1,
14693 sizeof(int) * (STRLEN(word + i + 1) + 1));
14694 i = 0;
14695 z0 = 1;
14696 }
14697 }
14698 break;
14699 }
14700 }
14701 }
14702 else if (vim_iswhite(c))
14703 {
14704 c = ' ';
14705 k = 1;
14706 }
14707
14708 if (z0 == 0)
14709 {
14710 if (k && !p0 && reslen < MAXWLEN && c != NUL
14711 && (!slang->sl_collapse || reslen == 0
14712 || wres[reslen - 1] != c))
14713 /* condense only double letters */
14714 wres[reslen++] = c;
14715
14716 i++;
14717 z = 0;
14718 k = 0;
14719 }
14720 }
14721
14722 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14723 l = 0;
14724 for (n = 0; n < reslen; ++n)
14725 {
14726 l += mb_char2bytes(wres[n], res + l);
14727 if (l + MB_MAXBYTES > MAXWLEN)
14728 break;
14729 }
14730 res[l] = NUL;
14731}
14732#endif
14733
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014734/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014735 * Compute a score for two sound-a-like words.
14736 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14737 * Instead of a generic loop we write out the code. That keeps it fast by
14738 * avoiding checks that will not be possible.
14739 */
14740 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014741soundalike_score(goodstart, badstart)
14742 char_u *goodstart; /* sound-folded good word */
14743 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014744{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014745 char_u *goodsound = goodstart;
14746 char_u *badsound = badstart;
14747 int goodlen;
14748 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014749 int n;
14750 char_u *pl, *ps;
14751 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014752 int score = 0;
14753
14754 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14755 * counted so much, vowels halfway the word aren't counted at all. */
14756 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14757 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014758 if (badsound[1] == goodsound[1]
14759 || (badsound[1] != NUL
14760 && goodsound[1] != NUL
14761 && badsound[2] == goodsound[2]))
14762 {
14763 /* handle like a substitute */
14764 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014765 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014766 {
14767 score = 2 * SCORE_DEL / 3;
14768 if (*badsound == '*')
14769 ++badsound;
14770 else
14771 ++goodsound;
14772 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014773 }
14774
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014775 goodlen = (int)STRLEN(goodsound);
14776 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014777
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014778 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014779 * changes. */
14780 n = goodlen - badlen;
14781 if (n < -2 || n > 2)
14782 return SCORE_MAXMAX;
14783
14784 if (n > 0)
14785 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014786 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014787 ps = badsound;
14788 }
14789 else
14790 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014791 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014792 ps = goodsound;
14793 }
14794
14795 /* Skip over the identical part. */
14796 while (*pl == *ps && *pl != NUL)
14797 {
14798 ++pl;
14799 ++ps;
14800 }
14801
14802 switch (n)
14803 {
14804 case -2:
14805 case 2:
14806 /*
14807 * Must delete two characters from "pl".
14808 */
14809 ++pl; /* first delete */
14810 while (*pl == *ps)
14811 {
14812 ++pl;
14813 ++ps;
14814 }
14815 /* strings must be equal after second delete */
14816 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014817 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014818
14819 /* Failed to compare. */
14820 break;
14821
14822 case -1:
14823 case 1:
14824 /*
14825 * Minimal one delete from "pl" required.
14826 */
14827
14828 /* 1: delete */
14829 pl2 = pl + 1;
14830 ps2 = ps;
14831 while (*pl2 == *ps2)
14832 {
14833 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014834 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014835 ++pl2;
14836 ++ps2;
14837 }
14838
14839 /* 2: delete then swap, then rest must be equal */
14840 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14841 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014842 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014843
14844 /* 3: delete then substitute, then the rest must be equal */
14845 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014846 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014847
14848 /* 4: first swap then delete */
14849 if (pl[0] == ps[1] && pl[1] == ps[0])
14850 {
14851 pl2 = pl + 2; /* swap, skip two chars */
14852 ps2 = ps + 2;
14853 while (*pl2 == *ps2)
14854 {
14855 ++pl2;
14856 ++ps2;
14857 }
14858 /* delete a char and then strings must be equal */
14859 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014860 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014861 }
14862
14863 /* 5: first substitute then delete */
14864 pl2 = pl + 1; /* substitute, skip one char */
14865 ps2 = ps + 1;
14866 while (*pl2 == *ps2)
14867 {
14868 ++pl2;
14869 ++ps2;
14870 }
14871 /* delete a char and then strings must be equal */
14872 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014873 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014874
14875 /* Failed to compare. */
14876 break;
14877
14878 case 0:
14879 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014880 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014881 * insert is only possible in combination with a delete.
14882 * 1: check if for identical strings
14883 */
14884 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014885 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014886
14887 /* 2: swap */
14888 if (pl[0] == ps[1] && pl[1] == ps[0])
14889 {
14890 pl2 = pl + 2; /* swap, skip two chars */
14891 ps2 = ps + 2;
14892 while (*pl2 == *ps2)
14893 {
14894 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014895 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014896 ++pl2;
14897 ++ps2;
14898 }
14899 /* 3: swap and swap again */
14900 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14901 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014902 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014903
14904 /* 4: swap and substitute */
14905 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014906 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014907 }
14908
14909 /* 5: substitute */
14910 pl2 = pl + 1;
14911 ps2 = ps + 1;
14912 while (*pl2 == *ps2)
14913 {
14914 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014915 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014916 ++pl2;
14917 ++ps2;
14918 }
14919
14920 /* 6: substitute and swap */
14921 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14922 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014923 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014924
14925 /* 7: substitute and substitute */
14926 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014927 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014928
14929 /* 8: insert then delete */
14930 pl2 = pl;
14931 ps2 = ps + 1;
14932 while (*pl2 == *ps2)
14933 {
14934 ++pl2;
14935 ++ps2;
14936 }
14937 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014938 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014939
14940 /* 9: delete then insert */
14941 pl2 = pl + 1;
14942 ps2 = ps;
14943 while (*pl2 == *ps2)
14944 {
14945 ++pl2;
14946 ++ps2;
14947 }
14948 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014949 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014950
14951 /* Failed to compare. */
14952 break;
14953 }
14954
14955 return SCORE_MAXMAX;
14956}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958/*
14959 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014960 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014961 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014962 * The algorithm is described by Du and Chang, 1992.
14963 * The implementation of the algorithm comes from Aspell editdist.cpp,
14964 * edit_distance(). It has been converted from C++ to C and modified to
14965 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966 */
14967 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014968spell_edit_score(slang, badword, goodword)
14969 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 char_u *badword;
14971 char_u *goodword;
14972{
14973 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014974 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014975 int j, i;
14976 int t;
14977 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014978 int pbc, pgc;
14979#ifdef FEAT_MBYTE
14980 char_u *p;
14981 int wbadword[MAXWLEN];
14982 int wgoodword[MAXWLEN];
14983
14984 if (has_mbyte)
14985 {
14986 /* Get the characters from the multi-byte strings and put them in an
14987 * int array for easy access. */
14988 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014989 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014990 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014991 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014992 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014993 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014994 }
14995 else
14996#endif
14997 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014998 badlen = (int)STRLEN(badword) + 1;
14999 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015000 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015001
15002 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15003#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15005 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015006 if (cnt == NULL)
15007 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015008
15009 CNT(0, 0) = 0;
15010 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015011 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015012
15013 for (i = 1; i <= badlen; ++i)
15014 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015015 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015016 for (j = 1; j <= goodlen; ++j)
15017 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015018#ifdef FEAT_MBYTE
15019 if (has_mbyte)
15020 {
15021 bc = wbadword[i - 1];
15022 gc = wgoodword[j - 1];
15023 }
15024 else
15025#endif
15026 {
15027 bc = badword[i - 1];
15028 gc = goodword[j - 1];
15029 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015030 if (bc == gc)
15031 CNT(i, j) = CNT(i - 1, j - 1);
15032 else
15033 {
15034 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015035 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15037 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015038 {
15039 /* For a similar character use SCORE_SIMILAR. */
15040 if (slang != NULL
15041 && slang->sl_has_map
15042 && similar_chars(slang, gc, bc))
15043 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15044 else
15045 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015048 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015050#ifdef FEAT_MBYTE
15051 if (has_mbyte)
15052 {
15053 pbc = wbadword[i - 2];
15054 pgc = wgoodword[j - 2];
15055 }
15056 else
15057#endif
15058 {
15059 pbc = badword[i - 2];
15060 pgc = goodword[j - 2];
15061 }
15062 if (bc == pgc && pbc == gc)
15063 {
15064 t = SCORE_SWAP + CNT(i - 2, j - 2);
15065 if (t < CNT(i, j))
15066 CNT(i, j) = t;
15067 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 }
15069 t = SCORE_DEL + CNT(i - 1, j);
15070 if (t < CNT(i, j))
15071 CNT(i, j) = t;
15072 t = SCORE_INS + CNT(i, j - 1);
15073 if (t < CNT(i, j))
15074 CNT(i, j) = t;
15075 }
15076 }
15077 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015078
15079 i = CNT(badlen - 1, goodlen - 1);
15080 vim_free(cnt);
15081 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015082}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015083
Bram Moolenaar4770d092006-01-12 23:22:24 +000015084typedef struct
15085{
15086 int badi;
15087 int goodi;
15088 int score;
15089} limitscore_T;
15090
15091/*
15092 * Like spell_edit_score(), but with a limit on the score to make it faster.
15093 * May return SCORE_MAXMAX when the score is higher than "limit".
15094 *
15095 * This uses a stack for the edits still to be tried.
15096 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15097 * for multi-byte characters.
15098 */
15099 static int
15100spell_edit_score_limit(slang, badword, goodword, limit)
15101 slang_T *slang;
15102 char_u *badword;
15103 char_u *goodword;
15104 int limit;
15105{
15106 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15107 int stackidx;
15108 int bi, gi;
15109 int bi2, gi2;
15110 int bc, gc;
15111 int score;
15112 int score_off;
15113 int minscore;
15114 int round;
15115
15116#ifdef FEAT_MBYTE
15117 /* Multi-byte characters require a bit more work, use a different function
15118 * to avoid testing "has_mbyte" quite often. */
15119 if (has_mbyte)
15120 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15121#endif
15122
15123 /*
15124 * The idea is to go from start to end over the words. So long as
15125 * characters are equal just continue, this always gives the lowest score.
15126 * When there is a difference try several alternatives. Each alternative
15127 * increases "score" for the edit distance. Some of the alternatives are
15128 * pushed unto a stack and tried later, some are tried right away. At the
15129 * end of the word the score for one alternative is known. The lowest
15130 * possible score is stored in "minscore".
15131 */
15132 stackidx = 0;
15133 bi = 0;
15134 gi = 0;
15135 score = 0;
15136 minscore = limit + 1;
15137
15138 for (;;)
15139 {
15140 /* Skip over an equal part, score remains the same. */
15141 for (;;)
15142 {
15143 bc = badword[bi];
15144 gc = goodword[gi];
15145 if (bc != gc) /* stop at a char that's different */
15146 break;
15147 if (bc == NUL) /* both words end */
15148 {
15149 if (score < minscore)
15150 minscore = score;
15151 goto pop; /* do next alternative */
15152 }
15153 ++bi;
15154 ++gi;
15155 }
15156
15157 if (gc == NUL) /* goodword ends, delete badword chars */
15158 {
15159 do
15160 {
15161 if ((score += SCORE_DEL) >= minscore)
15162 goto pop; /* do next alternative */
15163 } while (badword[++bi] != NUL);
15164 minscore = score;
15165 }
15166 else if (bc == NUL) /* badword ends, insert badword chars */
15167 {
15168 do
15169 {
15170 if ((score += SCORE_INS) >= minscore)
15171 goto pop; /* do next alternative */
15172 } while (goodword[++gi] != NUL);
15173 minscore = score;
15174 }
15175 else /* both words continue */
15176 {
15177 /* If not close to the limit, perform a change. Only try changes
15178 * that may lead to a lower score than "minscore".
15179 * round 0: try deleting a char from badword
15180 * round 1: try inserting a char in badword */
15181 for (round = 0; round <= 1; ++round)
15182 {
15183 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15184 if (score_off < minscore)
15185 {
15186 if (score_off + SCORE_EDIT_MIN >= minscore)
15187 {
15188 /* Near the limit, rest of the words must match. We
15189 * can check that right now, no need to push an item
15190 * onto the stack. */
15191 bi2 = bi + 1 - round;
15192 gi2 = gi + round;
15193 while (goodword[gi2] == badword[bi2])
15194 {
15195 if (goodword[gi2] == NUL)
15196 {
15197 minscore = score_off;
15198 break;
15199 }
15200 ++bi2;
15201 ++gi2;
15202 }
15203 }
15204 else
15205 {
15206 /* try deleting/inserting a character later */
15207 stack[stackidx].badi = bi + 1 - round;
15208 stack[stackidx].goodi = gi + round;
15209 stack[stackidx].score = score_off;
15210 ++stackidx;
15211 }
15212 }
15213 }
15214
15215 if (score + SCORE_SWAP < minscore)
15216 {
15217 /* If swapping two characters makes a match then the
15218 * substitution is more expensive, thus there is no need to
15219 * try both. */
15220 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15221 {
15222 /* Swap two characters, that is: skip them. */
15223 gi += 2;
15224 bi += 2;
15225 score += SCORE_SWAP;
15226 continue;
15227 }
15228 }
15229
15230 /* Substitute one character for another which is the same
15231 * thing as deleting a character from both goodword and badword.
15232 * Use a better score when there is only a case difference. */
15233 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15234 score += SCORE_ICASE;
15235 else
15236 {
15237 /* For a similar character use SCORE_SIMILAR. */
15238 if (slang != NULL
15239 && slang->sl_has_map
15240 && similar_chars(slang, gc, bc))
15241 score += SCORE_SIMILAR;
15242 else
15243 score += SCORE_SUBST;
15244 }
15245
15246 if (score < minscore)
15247 {
15248 /* Do the substitution. */
15249 ++gi;
15250 ++bi;
15251 continue;
15252 }
15253 }
15254pop:
15255 /*
15256 * Get here to try the next alternative, pop it from the stack.
15257 */
15258 if (stackidx == 0) /* stack is empty, finished */
15259 break;
15260
15261 /* pop an item from the stack */
15262 --stackidx;
15263 gi = stack[stackidx].goodi;
15264 bi = stack[stackidx].badi;
15265 score = stack[stackidx].score;
15266 }
15267
15268 /* When the score goes over "limit" it may actually be much higher.
15269 * Return a very large number to avoid going below the limit when giving a
15270 * bonus. */
15271 if (minscore > limit)
15272 return SCORE_MAXMAX;
15273 return minscore;
15274}
15275
15276#ifdef FEAT_MBYTE
15277/*
15278 * Multi-byte version of spell_edit_score_limit().
15279 * Keep it in sync with the above!
15280 */
15281 static int
15282spell_edit_score_limit_w(slang, badword, goodword, limit)
15283 slang_T *slang;
15284 char_u *badword;
15285 char_u *goodword;
15286 int limit;
15287{
15288 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15289 int stackidx;
15290 int bi, gi;
15291 int bi2, gi2;
15292 int bc, gc;
15293 int score;
15294 int score_off;
15295 int minscore;
15296 int round;
15297 char_u *p;
15298 int wbadword[MAXWLEN];
15299 int wgoodword[MAXWLEN];
15300
15301 /* Get the characters from the multi-byte strings and put them in an
15302 * int array for easy access. */
15303 bi = 0;
15304 for (p = badword; *p != NUL; )
15305 wbadword[bi++] = mb_cptr2char_adv(&p);
15306 wbadword[bi++] = 0;
15307 gi = 0;
15308 for (p = goodword; *p != NUL; )
15309 wgoodword[gi++] = mb_cptr2char_adv(&p);
15310 wgoodword[gi++] = 0;
15311
15312 /*
15313 * The idea is to go from start to end over the words. So long as
15314 * characters are equal just continue, this always gives the lowest score.
15315 * When there is a difference try several alternatives. Each alternative
15316 * increases "score" for the edit distance. Some of the alternatives are
15317 * pushed unto a stack and tried later, some are tried right away. At the
15318 * end of the word the score for one alternative is known. The lowest
15319 * possible score is stored in "minscore".
15320 */
15321 stackidx = 0;
15322 bi = 0;
15323 gi = 0;
15324 score = 0;
15325 minscore = limit + 1;
15326
15327 for (;;)
15328 {
15329 /* Skip over an equal part, score remains the same. */
15330 for (;;)
15331 {
15332 bc = wbadword[bi];
15333 gc = wgoodword[gi];
15334
15335 if (bc != gc) /* stop at a char that's different */
15336 break;
15337 if (bc == NUL) /* both words end */
15338 {
15339 if (score < minscore)
15340 minscore = score;
15341 goto pop; /* do next alternative */
15342 }
15343 ++bi;
15344 ++gi;
15345 }
15346
15347 if (gc == NUL) /* goodword ends, delete badword chars */
15348 {
15349 do
15350 {
15351 if ((score += SCORE_DEL) >= minscore)
15352 goto pop; /* do next alternative */
15353 } while (wbadword[++bi] != NUL);
15354 minscore = score;
15355 }
15356 else if (bc == NUL) /* badword ends, insert badword chars */
15357 {
15358 do
15359 {
15360 if ((score += SCORE_INS) >= minscore)
15361 goto pop; /* do next alternative */
15362 } while (wgoodword[++gi] != NUL);
15363 minscore = score;
15364 }
15365 else /* both words continue */
15366 {
15367 /* If not close to the limit, perform a change. Only try changes
15368 * that may lead to a lower score than "minscore".
15369 * round 0: try deleting a char from badword
15370 * round 1: try inserting a char in badword */
15371 for (round = 0; round <= 1; ++round)
15372 {
15373 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15374 if (score_off < minscore)
15375 {
15376 if (score_off + SCORE_EDIT_MIN >= minscore)
15377 {
15378 /* Near the limit, rest of the words must match. We
15379 * can check that right now, no need to push an item
15380 * onto the stack. */
15381 bi2 = bi + 1 - round;
15382 gi2 = gi + round;
15383 while (wgoodword[gi2] == wbadword[bi2])
15384 {
15385 if (wgoodword[gi2] == NUL)
15386 {
15387 minscore = score_off;
15388 break;
15389 }
15390 ++bi2;
15391 ++gi2;
15392 }
15393 }
15394 else
15395 {
15396 /* try deleting a character from badword later */
15397 stack[stackidx].badi = bi + 1 - round;
15398 stack[stackidx].goodi = gi + round;
15399 stack[stackidx].score = score_off;
15400 ++stackidx;
15401 }
15402 }
15403 }
15404
15405 if (score + SCORE_SWAP < minscore)
15406 {
15407 /* If swapping two characters makes a match then the
15408 * substitution is more expensive, thus there is no need to
15409 * try both. */
15410 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15411 {
15412 /* Swap two characters, that is: skip them. */
15413 gi += 2;
15414 bi += 2;
15415 score += SCORE_SWAP;
15416 continue;
15417 }
15418 }
15419
15420 /* Substitute one character for another which is the same
15421 * thing as deleting a character from both goodword and badword.
15422 * Use a better score when there is only a case difference. */
15423 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15424 score += SCORE_ICASE;
15425 else
15426 {
15427 /* For a similar character use SCORE_SIMILAR. */
15428 if (slang != NULL
15429 && slang->sl_has_map
15430 && similar_chars(slang, gc, bc))
15431 score += SCORE_SIMILAR;
15432 else
15433 score += SCORE_SUBST;
15434 }
15435
15436 if (score < minscore)
15437 {
15438 /* Do the substitution. */
15439 ++gi;
15440 ++bi;
15441 continue;
15442 }
15443 }
15444pop:
15445 /*
15446 * Get here to try the next alternative, pop it from the stack.
15447 */
15448 if (stackidx == 0) /* stack is empty, finished */
15449 break;
15450
15451 /* pop an item from the stack */
15452 --stackidx;
15453 gi = stack[stackidx].goodi;
15454 bi = stack[stackidx].badi;
15455 score = stack[stackidx].score;
15456 }
15457
15458 /* When the score goes over "limit" it may actually be much higher.
15459 * Return a very large number to avoid going below the limit when giving a
15460 * bonus. */
15461 if (minscore > limit)
15462 return SCORE_MAXMAX;
15463 return minscore;
15464}
15465#endif
15466
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015467/*
15468 * ":spellinfo"
15469 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015470 void
15471ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015472 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015473{
15474 int lpi;
15475 langp_T *lp;
15476 char_u *p;
15477
15478 if (no_spell_checking(curwin))
15479 return;
15480
15481 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015482 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015483 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015484 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015485 msg_puts((char_u *)"file: ");
15486 msg_puts(lp->lp_slang->sl_fname);
15487 msg_putchar('\n');
15488 p = lp->lp_slang->sl_info;
15489 if (p != NULL)
15490 {
15491 msg_puts(p);
15492 msg_putchar('\n');
15493 }
15494 }
15495 msg_end();
15496}
15497
Bram Moolenaar4770d092006-01-12 23:22:24 +000015498#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15499#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015500#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015501#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15502#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015503
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015504/*
15505 * ":spelldump"
15506 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015507 void
15508ex_spelldump(eap)
15509 exarg_T *eap;
15510{
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015511 if (no_spell_checking(curwin))
15512 return;
15513
15514 /* Create a new empty buffer by splitting the window. */
15515 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar860cae12010-06-05 23:22:07 +020015516 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015517 return;
15518
Bram Moolenaar860cae12010-06-05 23:22:07 +020015519 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015520
15521 /* Delete the empty line that we started with. */
15522 if (curbuf->b_ml.ml_line_count > 1)
15523 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15524
15525 redraw_later(NOT_VALID);
15526}
15527
15528/*
15529 * Go through all possible words and:
15530 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15531 * "ic" and "dir" are not used.
15532 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15533 */
15534 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015535spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015536 char_u *pat; /* leading part of the word */
15537 int ic; /* ignore case */
15538 int *dir; /* direction for adding matches */
15539 int dumpflags_arg; /* DUMPFLAG_* */
15540{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015541 langp_T *lp;
15542 slang_T *slang;
15543 idx_T arridx[MAXWLEN];
15544 int curi[MAXWLEN];
15545 char_u word[MAXWLEN];
15546 int c;
15547 char_u *byts;
15548 idx_T *idxs;
15549 linenr_T lnum = 0;
15550 int round;
15551 int depth;
15552 int n;
15553 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015554 char_u *region_names = NULL; /* region names being used */
15555 int do_region = TRUE; /* dump region names and numbers */
15556 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015557 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015558 int dumpflags = dumpflags_arg;
15559 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015560
Bram Moolenaard0131a82006-03-04 21:46:13 +000015561 /* When ignoring case or when the pattern starts with capital pass this on
15562 * to dump_word(). */
15563 if (pat != NULL)
15564 {
15565 if (ic)
15566 dumpflags |= DUMPFLAG_ICASE;
15567 else
15568 {
15569 n = captype(pat, NULL);
15570 if (n == WF_ONECAP)
15571 dumpflags |= DUMPFLAG_ONECAP;
15572 else if (n == WF_ALLCAP
15573#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015574 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015575#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015576 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015577#endif
15578 )
15579 dumpflags |= DUMPFLAG_ALLCAP;
15580 }
15581 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015582
Bram Moolenaar7887d882005-07-01 22:33:52 +000015583 /* Find out if we can support regions: All languages must support the same
15584 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015585 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015586 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015587 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015588 p = lp->lp_slang->sl_regions;
15589 if (p[0] != 0)
15590 {
15591 if (region_names == NULL) /* first language with regions */
15592 region_names = p;
15593 else if (STRCMP(region_names, p) != 0)
15594 {
15595 do_region = FALSE; /* region names are different */
15596 break;
15597 }
15598 }
15599 }
15600
15601 if (do_region && region_names != NULL)
15602 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015603 if (pat == NULL)
15604 {
15605 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15606 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15607 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015608 }
15609 else
15610 do_region = FALSE;
15611
15612 /*
15613 * Loop over all files loaded for the entries in 'spelllang'.
15614 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015615 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015616 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015617 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015618 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015619 if (slang->sl_fbyts == NULL) /* reloading failed */
15620 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015621
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015622 if (pat == NULL)
15623 {
15624 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15625 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15626 }
15627
15628 /* When matching with a pattern and there are no prefixes only use
15629 * parts of the tree that match "pat". */
15630 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015631 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015632 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015633 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015634
15635 /* round 1: case-folded tree
15636 * round 2: keep-case tree */
15637 for (round = 1; round <= 2; ++round)
15638 {
15639 if (round == 1)
15640 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015641 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015642 byts = slang->sl_fbyts;
15643 idxs = slang->sl_fidxs;
15644 }
15645 else
15646 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015647 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015648 byts = slang->sl_kbyts;
15649 idxs = slang->sl_kidxs;
15650 }
15651 if (byts == NULL)
15652 continue; /* array is empty */
15653
15654 depth = 0;
15655 arridx[0] = 0;
15656 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015657 while (depth >= 0 && !got_int
15658 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015659 {
15660 if (curi[depth] > byts[arridx[depth]])
15661 {
15662 /* Done all bytes at this node, go up one level. */
15663 --depth;
15664 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015665 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015666 }
15667 else
15668 {
15669 /* Do one more byte at this node. */
15670 n = arridx[depth] + curi[depth];
15671 ++curi[depth];
15672 c = byts[n];
15673 if (c == 0)
15674 {
15675 /* End of word, deal with the word.
15676 * Don't use keep-case words in the fold-case tree,
15677 * they will appear in the keep-case tree.
15678 * Only use the word when the region matches. */
15679 flags = (int)idxs[n];
15680 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015681 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015682 && (do_region
15683 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015684 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015685 & lp->lp_region) != 0))
15686 {
15687 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015688 if (!do_region)
15689 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015690
15691 /* Dump the basic word if there is no prefix or
15692 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015693 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015694 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015695 {
15696 dump_word(slang, word, pat, dir,
15697 dumpflags, flags, lnum);
15698 if (pat == NULL)
15699 ++lnum;
15700 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015701
15702 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015703 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015704 lnum = dump_prefixes(slang, word, pat, dir,
15705 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015706 }
15707 }
15708 else
15709 {
15710 /* Normal char, go one level deeper. */
15711 word[depth++] = c;
15712 arridx[depth] = idxs[n];
15713 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015714
15715 /* Check if this characters matches with the pattern.
15716 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015717 * Always ignore case here, dump_word() will check
15718 * proper case later. This isn't exactly right when
15719 * length changes for multi-byte characters with
15720 * ignore case... */
15721 if (depth <= patlen
15722 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015723 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015724 }
15725 }
15726 }
15727 }
15728 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015729}
15730
15731/*
15732 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015733 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015734 */
15735 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015736dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015737 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015738 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015739 char_u *pat;
15740 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015741 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015742 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015743 linenr_T lnum;
15744{
15745 int keepcap = FALSE;
15746 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015747 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015748 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015749 char_u badword[MAXWLEN + 10];
15750 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015751 int flags = wordflags;
15752
15753 if (dumpflags & DUMPFLAG_ONECAP)
15754 flags |= WF_ONECAP;
15755 if (dumpflags & DUMPFLAG_ALLCAP)
15756 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015757
Bram Moolenaar4770d092006-01-12 23:22:24 +000015758 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015759 {
15760 /* Need to fix case according to "flags". */
15761 make_case_word(word, cword, flags);
15762 p = cword;
15763 }
15764 else
15765 {
15766 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015767 if ((dumpflags & DUMPFLAG_KEEPCASE)
15768 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015769 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015770 keepcap = TRUE;
15771 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015772 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015773
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015774 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015775 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015776 /* Add flags and regions after a slash. */
15777 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015778 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015779 STRCPY(badword, p);
15780 STRCAT(badword, "/");
15781 if (keepcap)
15782 STRCAT(badword, "=");
15783 if (flags & WF_BANNED)
15784 STRCAT(badword, "!");
15785 else if (flags & WF_RARE)
15786 STRCAT(badword, "?");
15787 if (flags & WF_REGION)
15788 for (i = 0; i < 7; ++i)
15789 if (flags & (0x10000 << i))
15790 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15791 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015792 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015793
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015794 if (dumpflags & DUMPFLAG_COUNT)
15795 {
15796 hashitem_T *hi;
15797
15798 /* Include the word count for ":spelldump!". */
15799 hi = hash_find(&slang->sl_wordcount, tw);
15800 if (!HASHITEM_EMPTY(hi))
15801 {
15802 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15803 tw, HI2WC(hi)->wc_count);
15804 p = IObuff;
15805 }
15806 }
15807
15808 ml_append(lnum, p, (colnr_T)0, FALSE);
15809 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015810 else if (((dumpflags & DUMPFLAG_ICASE)
15811 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15812 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015813 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015814 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015815 /* if dir was BACKWARD then honor it just once */
15816 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015817}
15818
15819/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015820 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15821 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015822 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015823 * Return the updated line number.
15824 */
15825 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015826dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015827 slang_T *slang;
15828 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015829 char_u *pat;
15830 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015831 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015832 int flags; /* flags with prefix ID */
15833 linenr_T startlnum;
15834{
15835 idx_T arridx[MAXWLEN];
15836 int curi[MAXWLEN];
15837 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015838 char_u word_up[MAXWLEN];
15839 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015840 int c;
15841 char_u *byts;
15842 idx_T *idxs;
15843 linenr_T lnum = startlnum;
15844 int depth;
15845 int n;
15846 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015847 int i;
15848
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015849 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015850 * upper-case letter in word_up[]. */
15851 c = PTR2CHAR(word);
15852 if (SPELL_TOUPPER(c) != c)
15853 {
15854 onecap_copy(word, word_up, TRUE);
15855 has_word_up = TRUE;
15856 }
15857
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015858 byts = slang->sl_pbyts;
15859 idxs = slang->sl_pidxs;
15860 if (byts != NULL) /* array not is empty */
15861 {
15862 /*
15863 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015864 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015865 */
15866 depth = 0;
15867 arridx[0] = 0;
15868 curi[0] = 1;
15869 while (depth >= 0 && !got_int)
15870 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015871 n = arridx[depth];
15872 len = byts[n];
15873 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015874 {
15875 /* Done all bytes at this node, go up one level. */
15876 --depth;
15877 line_breakcheck();
15878 }
15879 else
15880 {
15881 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015882 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015883 ++curi[depth];
15884 c = byts[n];
15885 if (c == 0)
15886 {
15887 /* End of prefix, find out how many IDs there are. */
15888 for (i = 1; i < len; ++i)
15889 if (byts[n + i] != 0)
15890 break;
15891 curi[depth] += i - 1;
15892
Bram Moolenaar53805d12005-08-01 07:08:33 +000015893 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15894 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015895 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015896 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015897 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015898 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015899 : flags, lnum);
15900 if (lnum != 0)
15901 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015902 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015903
15904 /* Check for prefix that matches the word when the
15905 * first letter is upper-case, but only if the prefix has
15906 * a condition. */
15907 if (has_word_up)
15908 {
15909 c = valid_word_prefix(i, n, flags, word_up, slang,
15910 TRUE);
15911 if (c != 0)
15912 {
15913 vim_strncpy(prefix + depth, word_up,
15914 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015915 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015916 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015917 : flags, lnum);
15918 if (lnum != 0)
15919 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015920 }
15921 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015922 }
15923 else
15924 {
15925 /* Normal char, go one level deeper. */
15926 prefix[depth++] = c;
15927 arridx[depth] = idxs[n];
15928 curi[depth] = 1;
15929 }
15930 }
15931 }
15932 }
15933
15934 return lnum;
15935}
15936
Bram Moolenaar95529562005-08-25 21:21:38 +000015937/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015938 * Move "p" to the end of word "start".
15939 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015940 */
15941 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020015942spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015943 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015944 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000015945{
15946 char_u *p = start;
15947
Bram Moolenaar860cae12010-06-05 23:22:07 +020015948 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000015949 mb_ptr_adv(p);
15950 return p;
15951}
15952
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015953#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015954/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015955 * For Insert mode completion CTRL-X s:
15956 * Find start of the word in front of column "startcol".
15957 * We don't check if it is badly spelled, with completion we can only change
15958 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015959 * Returns the column number of the word.
15960 */
15961 int
15962spell_word_start(startcol)
15963 int startcol;
15964{
15965 char_u *line;
15966 char_u *p;
15967 int col = 0;
15968
Bram Moolenaar95529562005-08-25 21:21:38 +000015969 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015970 return startcol;
15971
15972 /* Find a word character before "startcol". */
15973 line = ml_get_curline();
15974 for (p = line + startcol; p > line; )
15975 {
15976 mb_ptr_back(line, p);
15977 if (spell_iswordp_nmw(p))
15978 break;
15979 }
15980
15981 /* Go back to start of the word. */
15982 while (p > line)
15983 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015984 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015985 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020015986 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015987 break;
15988 col = 0;
15989 }
15990
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015991 return col;
15992}
15993
15994/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015995 * Need to check for 'spellcapcheck' now, the word is removed before
15996 * expand_spelling() is called. Therefore the ugly global variable.
15997 */
15998static int spell_expand_need_cap;
15999
16000 void
16001spell_expand_check_cap(col)
16002 colnr_T col;
16003{
16004 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16005}
16006
16007/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016008 * Get list of spelling suggestions.
16009 * Used for Insert mode completion CTRL-X ?.
16010 * Returns the number of matches. The matches are in "matchp[]", array of
16011 * allocated strings.
16012 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016013 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016014expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016015 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016016 char_u *pat;
16017 char_u ***matchp;
16018{
16019 garray_T ga;
16020
Bram Moolenaar4770d092006-01-12 23:22:24 +000016021 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016022 *matchp = ga.ga_data;
16023 return ga.ga_len;
16024}
16025#endif
16026
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016027#endif /* FEAT_SPELL */