blob: eeb362991282f0dee6933eb183e7cfc2b7089299 [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,
80 * compute the the maximum word score that can be used.
81 */
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
314#ifdef HAVE_FCNTL_H
315# include <fcntl.h>
316#endif
317
Bram Moolenaar4770d092006-01-12 23:22:24 +0000318#ifndef UNIX /* it's in os_unix.h for Unix */
319# include <time.h> /* for time_t */
320#endif
321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000322#define MAXWLEN 250 /* Assume max. word len is this many bytes.
323 Some places assume a word length fits in a
324 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000325
Bram Moolenaare52325c2005-08-22 22:54:29 +0000326/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000327 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000328#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000329typedef int idx_T;
330#else
331typedef long idx_T;
332#endif
333
334/* Flags used for a word. Only the lowest byte can be used, the region byte
335 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336#define WF_REGION 0x01 /* region byte follows */
337#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
338#define WF_ALLCAP 0x04 /* word must be all capitals */
339#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000340#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000341#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000342#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000345/* for <flags2>, shifted up one byte to be used in wn_flags */
346#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000347#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000348#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000349#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000350#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
351#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000352
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000353/* only used for su_badflags */
354#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
355
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000356#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000357
Bram Moolenaar53805d12005-08-01 07:08:33 +0000358/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000359#define WFP_RARE 0x01 /* rare prefix */
360#define WFP_NC 0x02 /* prefix is not combining */
361#define WFP_UP 0x04 /* to-upper prefix */
362#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
363#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000364
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000365/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
366 * byte) and prefcondnr (two bytes). */
367#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
368#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
369#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
370#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
371 * COMPOUNDPERMITFLAG */
372#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
373 * COMPOUNDFORBIDFLAG */
374
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000375
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000376/* flags for <compoptions> */
377#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
378#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
379#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
380#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
381
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000382/* Special byte values for <byte>. Some are only used in the tree for
383 * postponed prefixes, some only in the other trees. This is a bit messy... */
384#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000385 * postponed prefix: no <pflags> */
386#define BY_INDEX 1 /* child is shared, index follows */
387#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
388 * postponed prefix: <pflags> follows */
389#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
390 * follow; never used in prefix tree */
391#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000392
Bram Moolenaar4770d092006-01-12 23:22:24 +0000393/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
394 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 * One replacement: from "ft_from" to "ft_to". */
396typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398 char_u *ft_from;
399 char_u *ft_to;
400} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000401
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000402/* Info from "SAL" entries in ".aff" file used in sl_sal.
403 * The info is split for quick processing by spell_soundfold().
404 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
405typedef struct salitem_S
406{
407 char_u *sm_lead; /* leading letters */
408 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000409 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000410 char_u *sm_rules; /* rules like ^, $, priority */
411 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000412#ifdef FEAT_MBYTE
413 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000414 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000415 int *sm_to_w; /* wide character copy of "sm_to" */
416#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000417} salitem_T;
418
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000419#ifdef FEAT_MBYTE
420typedef int salfirst_T;
421#else
422typedef short salfirst_T;
423#endif
424
Bram Moolenaar5195e452005-08-19 20:32:47 +0000425/* Values for SP_*ERROR are negative, positive values are used by
426 * read_cnt_string(). */
427#define SP_TRUNCERROR -1 /* spell file truncated error */
428#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000429#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000430
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000432 * Structure used to store words and other info for one language, loaded from
433 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000434 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
435 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
436 *
437 * The "byts" array stores the possible bytes in each tree node, preceded by
438 * the number of possible bytes, sorted on byte value:
439 * <len> <byte1> <byte2> ...
440 * The "idxs" array stores the index of the child node corresponding to the
441 * byte in "byts".
442 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000443 * the flags, region mask and affixID for the word. There may be several
444 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000445 */
446typedef struct slang_S slang_T;
447struct slang_S
448{
449 slang_T *sl_next; /* next language */
450 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000451 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000452 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000453
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000455 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000458 char_u *sl_pbyts; /* prefix tree word bytes */
459 idx_T *sl_pidxs; /* prefix tree word indexes */
460
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000461 char_u *sl_info; /* infotext string or NULL */
462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000463 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000465 char_u *sl_midword; /* MIDWORD string or NULL */
466
Bram Moolenaar4770d092006-01-12 23:22:24 +0000467 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
468
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000469 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000470 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000472 int sl_compoptions; /* COMP_* flags */
473 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000475 * (NULL when no compounding) */
476 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000477 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000478 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
480 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000481
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000482 int sl_prefixcnt; /* number of items in "sl_prefprog" */
483 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485 garray_T sl_rep; /* list of fromto_T entries from REP lines */
486 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
487 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000489 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490 there is none */
491 int sl_followup; /* SAL followup */
492 int sl_collapse; /* SAL collapse_result */
493 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000494 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
495 * "sl_sal_first" maps chars, when has_mbyte
496 * "sl_sal" is a list of wide char lists. */
497 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
498 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000499 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000500
501 /* Info from the .sug file. Loaded on demand. */
502 time_t sl_sugtime; /* timestamp for .sug file */
503 char_u *sl_sbyts; /* soundfolded word bytes */
504 idx_T *sl_sidxs; /* soundfolded word indexes */
505 buf_T *sl_sugbuf; /* buffer with word number table */
506 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
507 load */
508
Bram Moolenaarea424162005-06-16 21:51:00 +0000509 int sl_has_map; /* TRUE if there is a MAP line */
510#ifdef FEAT_MBYTE
511 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
512 int sl_map_array[256]; /* MAP for first 256 chars */
513#else
514 char_u sl_map_array[256]; /* MAP for first 256 chars */
515#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516 hashtab_T sl_sounddone; /* table with soundfolded words that have
517 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000518};
519
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000520/* First language that is loaded, start of the linked list of loaded
521 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000522static slang_T *first_lang = NULL;
523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000524/* Flags used in .spl file for soundsalike flags. */
525#define SAL_F0LLOWUP 1
526#define SAL_COLLAPSE 2
527#define SAL_REM_ACCENTS 4
528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529/*
530 * Structure used in "b_langp", filled from 'spelllang'.
531 */
532typedef struct langp_S
533{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000534 slang_T *lp_slang; /* info for this language */
535 slang_T *lp_sallang; /* language used for sound folding or NULL */
536 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 int lp_region; /* bitmask for region or REGION_ALL */
538} langp_T;
539
540#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
541
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542#define REGION_ALL 0xff /* word valid in all regions */
543
Bram Moolenaar5195e452005-08-19 20:32:47 +0000544#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
545#define VIMSPELLMAGICL 8
546#define VIMSPELLVERSION 50
547
Bram Moolenaar4770d092006-01-12 23:22:24 +0000548#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
549#define VIMSUGMAGICL 6
550#define VIMSUGVERSION 1
551
Bram Moolenaar5195e452005-08-19 20:32:47 +0000552/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
553#define SN_REGION 0 /* <regionname> section */
554#define SN_CHARFLAGS 1 /* charflags section */
555#define SN_MIDWORD 2 /* <midword> section */
556#define SN_PREFCOND 3 /* <prefcond> section */
557#define SN_REP 4 /* REP items section */
558#define SN_SAL 5 /* SAL items section */
559#define SN_SOFO 6 /* soundfolding section */
560#define SN_MAP 7 /* MAP items section */
561#define SN_COMPOUND 8 /* compound words section */
562#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000563#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000564#define SN_SUGFILE 11 /* timestamp for .sug file */
565#define SN_REPSAL 12 /* REPSAL items section */
566#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000567#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000568#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000569#define SN_END 255 /* end of sections */
570
571#define SNF_REQUIRED 1 /* <sectionflags>: required section */
572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573/* Result values. Lower number is accepted over higher one. */
574#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000575#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576#define SP_RARE 1
577#define SP_LOCAL 2
578#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000579
Bram Moolenaar7887d882005-07-01 22:33:52 +0000580/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000581static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000582
Bram Moolenaar4770d092006-01-12 23:22:24 +0000583typedef struct wordcount_S
584{
585 short_u wc_count; /* nr of times word was seen */
586 char_u wc_word[1]; /* word, actually longer */
587} wordcount_T;
588
589static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000590#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000591#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
592#define MAXWORDCOUNT 0xffff
593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000594/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000595 * Information used when looking for suggestions.
596 */
597typedef struct suginfo_S
598{
599 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000602 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 char_u *su_badptr; /* start of bad word in line */
605 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000606 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
608 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000609 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000611 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000612} suginfo_T;
613
614/* One word suggestion. Used in "si_ga". */
615typedef struct suggest_S
616{
617 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000618 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619 int st_orglen; /* length of replaced text */
620 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621 int st_altscore; /* used when st_score compares equal */
622 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000624 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625} suggest_T;
626
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628
Bram Moolenaar4770d092006-01-12 23:22:24 +0000629/* TRUE if a word appears in the list of banned words. */
630#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
631
632/* Number of suggestions kept when cleaning up. we need to keep more than
633 * what is displayed, because when rescore_suggestions() is called the score
634 * may change and wrong suggestions may be removed later. */
635#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000636
637/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
638 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000639#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640
641/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000642#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000643#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000645#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000647#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000649#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650#define SCORE_SUBST 93 /* substitute a character */
651#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000652#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000653#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000654#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000658#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000659#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000661#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
663 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000664
Bram Moolenaar4770d092006-01-12 23:22:24 +0000665#define SCORE_COMMON1 30 /* subtracted for words seen before */
666#define SCORE_COMMON2 40 /* subtracted for words often seen */
667#define SCORE_COMMON3 50 /* subtracted for words very often seen */
668#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
669#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
670
671/* When trying changed soundfold words it becomes slow when trying more than
672 * two changes. With less then two changes it's slightly faster but we miss a
673 * few good suggestions. In rare cases we need to try three of four changes.
674 */
675#define SCORE_SFMAX1 200 /* maximum score for first try */
676#define SCORE_SFMAX2 300 /* maximum score for second try */
677#define SCORE_SFMAX3 400 /* maximum score for third try */
678
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000679#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000680#define SCORE_MAXMAX 999999 /* accept any score */
681#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
682
683/* for spell_edit_score_limit() we need to know the minimum value of
684 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
685#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686
687/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000688 * Structure to store info for word matching.
689 */
690typedef struct matchinf_S
691{
692 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000693
694 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000696 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000698 char_u *mi_cend; /* char after what was used for
699 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
701 /* case-folded text */
702 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000704
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000705 /* for when checking word after a prefix */
706 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000707 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 int mi_prefcnt; /* number of entries at mi_prefarridx */
709 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000710#ifdef FEAT_MBYTE
711 int mi_cprefixlen; /* byte length of prefix in original
712 case */
713#else
714# define mi_cprefixlen mi_prefixlen /* it's the same value */
715#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000716
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000717 /* for when checking a compound word */
718 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000719 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
720 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000721 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000722
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000723 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000724 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000725 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000726 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000727
728 /* for NOBREAK */
729 int mi_result2; /* "mi_resul" without following word */
730 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731} matchinf_T;
732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000733/*
734 * The tables used for recognizing word characters according to spelling.
735 * These are only used for the first 256 characters of 'encoding'.
736 */
737typedef struct spelltab_S
738{
739 char_u st_isw[256]; /* flags: is word char */
740 char_u st_isu[256]; /* flags: is uppercase char */
741 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000742 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000743} spelltab_T;
744
745static spelltab_T spelltab;
746static int did_set_spelltab;
747
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000748#define CF_WORD 0x01
749#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000750
751static void clear_spell_chartab __ARGS((spelltab_T *sp));
752static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000753static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
754static int spell_iswordp_nmw __ARGS((char_u *p));
755#ifdef FEAT_MBYTE
756static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
757#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000758static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000759
760/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000762 */
763typedef enum
764{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 STATE_START = 0, /* At start of node check for NUL bytes (goodword
766 * ends); if badword ends there is a match, otherwise
767 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000768 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
771 STATE_PLAIN, /* Use each byte of the node. */
772 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000773 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_INS, /* Insert a byte in the bad word. */
775 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNSWAP, /* Undo swap two characters. */
777 STATE_SWAP3, /* Swap two characters over three. */
778 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000779 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000781 STATE_REP_INI, /* Prepare for using REP items. */
782 STATE_REP, /* Use matching REP items from the .aff file. */
783 STATE_REP_UNDO, /* Undo a REP item replacement. */
784 STATE_FINAL /* End of this node. */
785} state_T;
786
787/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000788 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789 */
790typedef struct trystate_S
791{
Bram Moolenaarea424162005-06-16 21:51:00 +0000792 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000795 short ts_curi; /* index in list of child nodes */
796 char_u ts_fidx; /* index in fword[], case-folded bad word */
797 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
798 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000799 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000800 * PFD_PREFIXTREE or PFD_NOPREFIX */
801 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000802#ifdef FEAT_MBYTE
803 char_u ts_tcharlen; /* number of bytes in tword character */
804 char_u ts_tcharidx; /* current byte index in tword character */
805 char_u ts_isdiff; /* DIFF_ values */
806 char_u ts_fcharstart; /* index in fword where badword char started */
807#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000808 char_u ts_prewordlen; /* length of word in "preword[]" */
809 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000810 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000811 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000812 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000813 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000814 char_u ts_delidx; /* index in fword for char that was deleted,
815 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816} trystate_T;
817
Bram Moolenaarea424162005-06-16 21:51:00 +0000818/* values for ts_isdiff */
819#define DIFF_NONE 0 /* no different byte (yet) */
820#define DIFF_YES 1 /* different byte found */
821#define DIFF_INSERT 2 /* inserting character */
822
Bram Moolenaard12a1322005-08-21 22:08:24 +0000823/* values for ts_flags */
824#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
825#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000826#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000828/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000829#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000830#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000831#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000834#define FIND_FOLDWORD 0 /* find word case-folded */
835#define FIND_KEEPWORD 1 /* find keep-case word */
836#define FIND_PREFIX 2 /* find word after prefix */
837#define FIND_COMPOUND 3 /* find case-folded compound word */
838#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000839
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840static slang_T *slang_alloc __ARGS((char_u *lang));
841static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000842static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000843static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000845static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000846static 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 +0000847static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000849static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000850static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000851static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000852static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000853static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000854static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000855static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000856static int get2c __ARGS((FILE *fd));
857static int get3c __ARGS((FILE *fd));
858static int get4c __ARGS((FILE *fd));
859static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000860static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000861static char_u *read_string __ARGS((FILE *fd, int cnt));
862static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
863static int read_charflags_section __ARGS((FILE *fd));
864static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000865static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
868static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
869static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
871static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000872static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000873static int init_syl_tab __ARGS((slang_T *slang));
874static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000875static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
876static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000877#ifdef FEAT_MBYTE
878static int *mb_str2wide __ARGS((char_u *s));
879#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000880static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
881static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000882static void clear_midword __ARGS((buf_T *buf));
883static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000884static int find_region __ARGS((char_u *rp, char_u *region));
885static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000886static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000887static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000891static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000892static 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 +0000893#ifdef FEAT_EVAL
894static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
895#endif
896static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000897static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
898static void suggest_load_files __ARGS((void));
899static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000903static void suggest_try_special __ARGS((suginfo_T *su));
904static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000905static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
906static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000907#ifdef FEAT_MBYTE
908static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000911static void score_comp_sal __ARGS((suginfo_T *su));
912static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000913static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000914static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000915static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_finish __ARGS((void));
917static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
918static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000919static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000920static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922static 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));
923static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000924static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000925static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000926static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000927static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
929static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
930static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000931#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000932static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000934static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000935static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
936static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
937#ifdef FEAT_MBYTE
938static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000940static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
941static 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 +0000942static buf_T *open_spellbuf __ARGS((void));
943static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000944
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000945/*
946 * Use our own character-case definitions, because the current locale may
947 * differ from what the .spl file uses.
948 * These must not be called with negative number!
949 */
950#ifndef FEAT_MBYTE
951/* Non-multi-byte implementation. */
952# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
953# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
954# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
955#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000956# if defined(HAVE_WCHAR_H)
957# include <wchar.h> /* for towupper() and towlower() */
958# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000959/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
960 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
961 * the "w" library function for characters above 255 if available. */
962# ifdef HAVE_TOWLOWER
963# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
964 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
965# else
966# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
967 : (c) < 256 ? spelltab.st_fold[c] : (c))
968# endif
969
970# ifdef HAVE_TOWUPPER
971# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
972 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
973# else
974# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
975 : (c) < 256 ? spelltab.st_upper[c] : (c))
976# endif
977
978# ifdef HAVE_ISWUPPER
979# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
980 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
981# else
982# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000983 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000984# endif
985#endif
986
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000987
988static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000989static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000990static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000991static char *e_affname = N_("Affix name too long in %s line %d: %s");
992static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
993static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000994static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000996/* Remember what "z?" replaced. */
997static char_u *repl_from = NULL;
998static char_u *repl_to = NULL;
999
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001000/*
1001 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001002 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001003 * "*attrp" is set to the highlight index for a badly spelled word. For a
1004 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001007 * "capcol" is used to check for a Capitalised word after the end of a
1008 * sentence. If it's zero then perform the check. Return the column where to
1009 * check next, or -1 when no sentence end was found. If it's NULL then don't
1010 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001011 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 * Returns the length of the word in bytes, also when it's OK, so that the
1013 * caller can skip over the word.
1014 */
1015 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001016spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001019 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001020 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001021 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022{
1023 matchinf_T mi; /* Most things are put in "mi" so that it can
1024 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001025 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001026 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001027 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001028 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001029 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001030
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001031 /* A word never starts at a space or a control character. Return quickly
1032 * then, skipping over the character. */
1033 if (*ptr <= ' ')
1034 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001035
1036 /* Return here when loading language files failed. */
1037 if (wp->w_buffer->b_langp.ga_len == 0)
1038 return 1;
1039
Bram Moolenaar5195e452005-08-19 20:32:47 +00001040 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001042 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001043 * 0X99FF. But always do check spelling to find "3GPP" and "11
1044 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001045 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001046 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001047 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1048 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 else
1050 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001051 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001053
Bram Moolenaar0c405862005-06-22 22:26:26 +00001054 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001056 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001057 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001058 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001059 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001061 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001062 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001063
1064 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1065 {
1066 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001067 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001069 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 if (capcol != NULL)
1073 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* We always use the characters up to the next non-word character,
1076 * also for bad words. */
1077 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001078
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001079 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001080 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001081
Bram Moolenaar5195e452005-08-19 20:32:47 +00001082 /* case-fold the word with one non-word character, so that we can check
1083 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001084 if (*mi.mi_fend != NUL)
1085 mb_ptr_adv(mi.mi_fend);
1086
1087 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1088 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001089 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090
1091 /* The word is bad unless we recognize it. */
1092 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001093 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /*
1096 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001097 * We check them all, because a word may be matched longer in another
1098 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001099 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001100 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001102 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1103
1104 /* If reloading fails the language is still in the list but everything
1105 * has been cleared. */
1106 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1107 continue;
1108
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001109 /* Check for a matching word in case-folded words. */
1110 find_word(&mi, FIND_FOLDWORD);
1111
1112 /* Check for a matching word in keep-case words. */
1113 find_word(&mi, FIND_KEEPWORD);
1114
1115 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001116 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001117
1118 /* For a NOBREAK language, may want to use a word without a following
1119 * word as a backup. */
1120 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1121 && mi.mi_result2 != SP_BAD)
1122 {
1123 mi.mi_result = mi.mi_result2;
1124 mi.mi_end = mi.mi_end2;
1125 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001126
1127 /* Count the word in the first language where it's found to be OK. */
1128 if (count_word && mi.mi_result == SP_OK)
1129 {
1130 count_common_word(mi.mi_lp->lp_slang, ptr,
1131 (int)(mi.mi_end - ptr), 1);
1132 count_word = FALSE;
1133 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001134 }
1135
1136 if (mi.mi_result != SP_OK)
1137 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001138 /* If we found a number skip over it. Allows for "42nd". Do flag
1139 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001140 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001141 {
1142 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1143 return nrlen;
1144 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001145
1146 /* When we are at a non-word character there is no error, just
1147 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001148 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001149 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001150 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1151 {
1152 regmatch_T regmatch;
1153
1154 /* Check for end of sentence. */
1155 regmatch.regprog = wp->w_buffer->b_cap_prog;
1156 regmatch.rm_ic = FALSE;
1157 if (vim_regexec(&regmatch, ptr, 0))
1158 *capcol = (int)(regmatch.endp[0] - ptr);
1159 }
1160
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001161#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001163 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001165 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001166 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001167 else if (mi.mi_end == ptr)
1168 /* Always include at least one character. Required for when there
1169 * is a mixup in "midword". */
1170 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 else if (mi.mi_result == SP_BAD
1172 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1173 {
1174 char_u *p, *fp;
1175 int save_result = mi.mi_result;
1176
1177 /* First language in 'spelllang' is NOBREAK. Find first position
1178 * at which any word would be valid. */
1179 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001180 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001182 p = mi.mi_word;
1183 fp = mi.mi_fword;
1184 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001186 mb_ptr_adv(p);
1187 mb_ptr_adv(fp);
1188 if (p >= mi.mi_end)
1189 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001190 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001191 find_word(&mi, FIND_COMPOUND);
1192 if (mi.mi_result != SP_BAD)
1193 {
1194 mi.mi_end = p;
1195 break;
1196 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001198 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001199 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001201
1202 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001204 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001205 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001206 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001207 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001208 }
1209
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1211 {
1212 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001213 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001214 return wrongcaplen;
1215 }
1216
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217 return (int)(mi.mi_end - ptr);
1218}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001219
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220/*
1221 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001222 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1223 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1224 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1225 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 *
1227 * For a match mip->mi_result is updated.
1228 */
1229 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001234 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001236 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 int endidxcnt = 0;
1238 int len;
1239 int wlen = 0;
1240 int flen;
1241 int c;
1242 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001243 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244#ifdef FEAT_MBYTE
1245 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001246#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001247 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001248 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 slang_T *slang = mip->mi_lp->lp_slang;
1250 unsigned flags;
1251 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001252 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001253 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001254 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001255 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 /* Check for word with matching case in keep-case tree. */
1260 ptr = mip->mi_word;
1261 flen = 9999; /* no case folding, always enough bytes */
1262 byts = slang->sl_kbyts;
1263 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001264
1265 if (mode == FIND_KEEPCOMPOUND)
1266 /* Skip over the previously found word(s). */
1267 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001268 }
1269 else
1270 {
1271 /* Check for case-folded in case-folded tree. */
1272 ptr = mip->mi_fword;
1273 flen = mip->mi_fwordlen; /* available case-folded bytes */
1274 byts = slang->sl_fbyts;
1275 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001276
1277 if (mode == FIND_PREFIX)
1278 {
1279 /* Skip over the prefix. */
1280 wlen = mip->mi_prefixlen;
1281 flen -= mip->mi_prefixlen;
1282 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001283 else if (mode == FIND_COMPOUND)
1284 {
1285 /* Skip over the previously found word(s). */
1286 wlen = mip->mi_compoff;
1287 flen -= mip->mi_compoff;
1288 }
1289
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290 }
1291
Bram Moolenaar51485f02005-06-04 21:55:20 +00001292 if (byts == NULL)
1293 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294
Bram Moolenaar51485f02005-06-04 21:55:20 +00001295 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001296 * Repeat advancing in the tree until:
1297 * - there is a byte that doesn't match,
1298 * - we reach the end of the tree,
1299 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001300 */
1301 for (;;)
1302 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001303 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001304 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001305
1306 len = byts[arridx++];
1307
1308 /* If the first possible byte is a zero the word could end here.
1309 * Remember this index, we first check for the longest word. */
1310 if (byts[arridx] == 0)
1311 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001312 if (endidxcnt == MAXWLEN)
1313 {
1314 /* Must be a corrupted spell file. */
1315 EMSG(_(e_format));
1316 return;
1317 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 endlen[endidxcnt] = wlen;
1319 endidx[endidxcnt++] = arridx++;
1320 --len;
1321
1322 /* Skip over the zeros, there can be several flag/region
1323 * combinations. */
1324 while (len > 0 && byts[arridx] == 0)
1325 {
1326 ++arridx;
1327 --len;
1328 }
1329 if (len == 0)
1330 break; /* no children, word must end here */
1331 }
1332
1333 /* Stop looking at end of the line. */
1334 if (ptr[wlen] == NUL)
1335 break;
1336
1337 /* Perform a binary search in the list of accepted bytes. */
1338 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001339 if (c == TAB) /* <Tab> is handled like <Space> */
1340 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001341 lo = arridx;
1342 hi = arridx + len - 1;
1343 while (lo < hi)
1344 {
1345 m = (lo + hi) / 2;
1346 if (byts[m] > c)
1347 hi = m - 1;
1348 else if (byts[m] < c)
1349 lo = m + 1;
1350 else
1351 {
1352 lo = hi = m;
1353 break;
1354 }
1355 }
1356
1357 /* Stop if there is no matching byte. */
1358 if (hi < lo || byts[lo] != c)
1359 break;
1360
1361 /* Continue at the child (if there is one). */
1362 arridx = idxs[lo];
1363 ++wlen;
1364 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001365
1366 /* One space in the good word may stand for several spaces in the
1367 * checked word. */
1368 if (c == ' ')
1369 {
1370 for (;;)
1371 {
1372 if (flen <= 0 && *mip->mi_fend != NUL)
1373 flen = fold_more(mip);
1374 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1375 break;
1376 ++wlen;
1377 --flen;
1378 }
1379 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380 }
1381
1382 /*
1383 * Verify that one of the possible endings is valid. Try the longest
1384 * first.
1385 */
1386 while (endidxcnt > 0)
1387 {
1388 --endidxcnt;
1389 arridx = endidx[endidxcnt];
1390 wlen = endlen[endidxcnt];
1391
1392#ifdef FEAT_MBYTE
1393 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1394 continue; /* not at first byte of character */
1395#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001396 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001399 continue; /* next char is a word character */
1400 word_ends = FALSE;
1401 }
1402 else
1403 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001404 /* The prefix flag is before compound flags. Once a valid prefix flag
1405 * has been found we try compound flags. */
1406 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407
1408#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 {
1411 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 * when folding case. This can be slow, take a shortcut when the
1413 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001415 if (STRNCMP(ptr, p, wlen) != 0)
1416 {
1417 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1418 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001419 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001421 }
1422#endif
1423
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 /* Check flags and region. For FIND_PREFIX check the condition and
1425 * prefix ID.
1426 * Repeat this if there are more flags/region alternatives until there
1427 * is a match. */
1428 res = SP_BAD;
1429 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1430 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 {
1432 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001433
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001434 /* For the fold-case tree check that the case of the checked word
1435 * matches with what the word in the tree requires.
1436 * For keep-case tree the case is always right. For prefixes we
1437 * don't bother to check. */
1438 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 if (mip->mi_cend != mip->mi_word + wlen)
1441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 /* mi_capflags was set for a different word length, need
1443 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 }
1447
Bram Moolenaar0c405862005-06-22 22:26:26 +00001448 if (mip->mi_capflags == WF_KEEPCAP
1449 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001450 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001451 }
1452
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001453 /* When mode is FIND_PREFIX the word must support the prefix:
1454 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001455 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001456 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001458 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001459 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001460 mip->mi_word + mip->mi_cprefixlen, slang,
1461 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001463 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001464
1465 /* Use the WF_RARE flag for a rare prefix. */
1466 if (c & WF_RAREPFX)
1467 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001468 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469 }
1470
Bram Moolenaar78622822005-08-23 21:00:13 +00001471 if (slang->sl_nobreak)
1472 {
1473 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1474 && (flags & WF_BANNED) == 0)
1475 {
1476 /* NOBREAK: found a valid following word. That's all we
1477 * need to know, so return. */
1478 mip->mi_result = SP_OK;
1479 break;
1480 }
1481 }
1482
1483 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1484 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001485 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001486 /* If there is no flag or the word is shorter than
1487 * COMPOUNDMIN reject it quickly.
1488 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 * that's too short... Myspell compatibility requires this
1490 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001491 if (((unsigned)flags >> 24) == 0
1492 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001494#ifdef FEAT_MBYTE
1495 /* For multi-byte chars check character length against
1496 * COMPOUNDMIN. */
1497 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001498 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001499 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1500 wlen - mip->mi_compoff) < slang->sl_compminlen)
1501 continue;
1502#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001503
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001504 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001505 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001506 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1507 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001508 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001509 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001510
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001511 /* Don't allow compounding on a side where an affix was added,
1512 * unless COMPOUNDPERMITFLAG was used. */
1513 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1514 continue;
1515 if (!word_ends && (flags & WF_NOCOMPAFT))
1516 continue;
1517
Bram Moolenaard12a1322005-08-21 22:08:24 +00001518 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001519 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001520 ? slang->sl_compstartflags
1521 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001522 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001523 continue;
1524
Bram Moolenaare52325c2005-08-22 22:54:29 +00001525 if (mode == FIND_COMPOUND)
1526 {
1527 int capflags;
1528
1529 /* Need to check the caps type of the appended compound
1530 * word. */
1531#ifdef FEAT_MBYTE
1532 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1533 mip->mi_compoff) != 0)
1534 {
1535 /* case folding may have changed the length */
1536 p = mip->mi_word;
1537 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1538 mb_ptr_adv(p);
1539 }
1540 else
1541#endif
1542 p = mip->mi_word + mip->mi_compoff;
1543 capflags = captype(p, mip->mi_word + wlen);
1544 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1545 && (flags & WF_FIXCAP) != 0))
1546 continue;
1547
1548 if (capflags != WF_ALLCAP)
1549 {
1550 /* When the character before the word is a word
1551 * character we do not accept a Onecap word. We do
1552 * accept a no-caps word, even when the dictionary
1553 * word specifies ONECAP. */
1554 mb_ptr_back(mip->mi_word, p);
1555 if (spell_iswordp_nmw(p)
1556 ? capflags == WF_ONECAP
1557 : (flags & WF_ONECAP) != 0
1558 && capflags != WF_ONECAP)
1559 continue;
1560 }
1561 }
1562
Bram Moolenaar5195e452005-08-19 20:32:47 +00001563 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001564 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001565 * the number of syllables must not be too large. */
1566 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1567 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1568 if (word_ends)
1569 {
1570 char_u fword[MAXWLEN];
1571
1572 if (slang->sl_compsylmax < MAXWLEN)
1573 {
1574 /* "fword" is only needed for checking syllables. */
1575 if (ptr == mip->mi_word)
1576 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1577 else
1578 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1579 }
1580 if (!can_compound(slang, fword, mip->mi_compflags))
1581 continue;
1582 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001583 }
1584
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001585 /* Check NEEDCOMPOUND: can't use word without compounding. */
1586 else if (flags & WF_NEEDCOMP)
1587 continue;
1588
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 nobreak_result = SP_OK;
1590
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001591 if (!word_ends)
1592 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001593 int save_result = mip->mi_result;
1594 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001595 langp_T *save_lp = mip->mi_lp;
1596 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001597
1598 /* Check that a valid word follows. If there is one and we
1599 * are compounding, it will set "mi_result", thus we are
1600 * always finished here. For NOBREAK we only check that a
1601 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001602 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 if (slang->sl_nobreak)
1604 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001605
1606 /* Find following word in case-folded tree. */
1607 mip->mi_compoff = endlen[endidxcnt];
1608#ifdef FEAT_MBYTE
1609 if (has_mbyte && mode == FIND_KEEPWORD)
1610 {
1611 /* Compute byte length in case-folded word from "wlen":
1612 * byte length in keep-case word. Length may change when
1613 * folding case. This can be slow, take a shortcut when
1614 * the case-folded word is equal to the keep-case word. */
1615 p = mip->mi_fword;
1616 if (STRNCMP(ptr, p, wlen) != 0)
1617 {
1618 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1619 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001620 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001621 }
1622 }
1623#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001624 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001625 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001626 if (flags & WF_COMPROOT)
1627 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001628
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001629 /* For NOBREAK we need to try all NOBREAK languages, at least
1630 * to find the ".add" file(s). */
1631 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001632 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001633 if (slang->sl_nobreak)
1634 {
1635 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1636 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1637 || !mip->mi_lp->lp_slang->sl_nobreak)
1638 continue;
1639 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001640
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001641 find_word(mip, FIND_COMPOUND);
1642
1643 /* When NOBREAK any word that matches is OK. Otherwise we
1644 * need to find the longest match, thus try with keep-case
1645 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001646 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1647 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001648 /* Find following word in keep-case tree. */
1649 mip->mi_compoff = wlen;
1650 find_word(mip, FIND_KEEPCOMPOUND);
1651
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001652#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1653 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1654 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1656 {
1657 /* Check for following word with prefix. */
1658 mip->mi_compoff = c;
1659 find_prefix(mip, FIND_COMPOUND);
1660 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001661#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663
1664 if (!slang->sl_nobreak)
1665 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001666 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001667 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001668 if (flags & WF_COMPROOT)
1669 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001670 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001671
Bram Moolenaar78622822005-08-23 21:00:13 +00001672 if (slang->sl_nobreak)
1673 {
1674 nobreak_result = mip->mi_result;
1675 mip->mi_result = save_result;
1676 mip->mi_end = save_end;
1677 }
1678 else
1679 {
1680 if (mip->mi_result == SP_OK)
1681 break;
1682 continue;
1683 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001684 }
1685
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001686 if (flags & WF_BANNED)
1687 res = SP_BANNED;
1688 else if (flags & WF_REGION)
1689 {
1690 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001691 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001692 res = SP_OK;
1693 else
1694 res = SP_LOCAL;
1695 }
1696 else if (flags & WF_RARE)
1697 res = SP_RARE;
1698 else
1699 res = SP_OK;
1700
Bram Moolenaar78622822005-08-23 21:00:13 +00001701 /* Always use the longest match and the best result. For NOBREAK
1702 * we separately keep the longest match without a following good
1703 * word as a fall-back. */
1704 if (nobreak_result == SP_BAD)
1705 {
1706 if (mip->mi_result2 > res)
1707 {
1708 mip->mi_result2 = res;
1709 mip->mi_end2 = mip->mi_word + wlen;
1710 }
1711 else if (mip->mi_result2 == res
1712 && mip->mi_end2 < mip->mi_word + wlen)
1713 mip->mi_end2 = mip->mi_word + wlen;
1714 }
1715 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716 {
1717 mip->mi_result = res;
1718 mip->mi_end = mip->mi_word + wlen;
1719 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001720 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001721 mip->mi_end = mip->mi_word + wlen;
1722
Bram Moolenaar78622822005-08-23 21:00:13 +00001723 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 }
1726
Bram Moolenaar78622822005-08-23 21:00:13 +00001727 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001728 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001729 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001730}
1731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001733 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1734 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001735 */
1736 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001737can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001738 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001739 char_u *word;
1740 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001741{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001742 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001743#ifdef FEAT_MBYTE
1744 char_u uflags[MAXWLEN * 2];
1745 int i;
1746#endif
1747 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001748
1749 if (slang->sl_compprog == NULL)
1750 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001751#ifdef FEAT_MBYTE
1752 if (enc_utf8)
1753 {
1754 /* Need to convert the single byte flags to utf8 characters. */
1755 p = uflags;
1756 for (i = 0; flags[i] != NUL; ++i)
1757 p += mb_char2bytes(flags[i], p);
1758 *p = NUL;
1759 p = uflags;
1760 }
1761 else
1762#endif
1763 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001764 regmatch.regprog = slang->sl_compprog;
1765 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001766 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001767 return FALSE;
1768
Bram Moolenaare52325c2005-08-22 22:54:29 +00001769 /* Count the number of syllables. This may be slow, do it last. If there
1770 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001771 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001772 if (slang->sl_compsylmax < MAXWLEN
1773 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001774 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001775 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001776}
1777
1778/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001779 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1780 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001781 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001782 */
1783 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001784valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001785 int totprefcnt; /* nr of prefix IDs */
1786 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001787 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001788 char_u *word;
1789 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001790 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791{
1792 int prefcnt;
1793 int pidx;
1794 regprog_T *rp;
1795 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001796 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001797
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001798 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1800 {
1801 pidx = slang->sl_pidxs[arridx + prefcnt];
1802
1803 /* Check the prefix ID. */
1804 if (prefid != (pidx & 0xff))
1805 continue;
1806
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001807 /* Check if the prefix doesn't combine and the word already has a
1808 * suffix. */
1809 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1810 continue;
1811
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001812 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001813 * stored in the two bytes above the prefix ID byte. */
1814 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001815 if (rp != NULL)
1816 {
1817 regmatch.regprog = rp;
1818 regmatch.rm_ic = FALSE;
1819 if (!vim_regexec(&regmatch, word, 0))
1820 continue;
1821 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001822 else if (cond_req)
1823 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001824
Bram Moolenaar53805d12005-08-01 07:08:33 +00001825 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001826 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001827 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001828 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001829}
1830
1831/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001832 * Check if the word at "mip->mi_word" has a matching prefix.
1833 * If it does, then check the following word.
1834 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001835 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1836 * prefix in a compound word.
1837 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838 * For a match mip->mi_result is updated.
1839 */
1840 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001841find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001842 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001843 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001844{
1845 idx_T arridx = 0;
1846 int len;
1847 int wlen = 0;
1848 int flen;
1849 int c;
1850 char_u *ptr;
1851 idx_T lo, hi, m;
1852 slang_T *slang = mip->mi_lp->lp_slang;
1853 char_u *byts;
1854 idx_T *idxs;
1855
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001856 byts = slang->sl_pbyts;
1857 if (byts == NULL)
1858 return; /* array is empty */
1859
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001860 /* We use the case-folded word here, since prefixes are always
1861 * case-folded. */
1862 ptr = mip->mi_fword;
1863 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001864 if (mode == FIND_COMPOUND)
1865 {
1866 /* Skip over the previously found word(s). */
1867 ptr += mip->mi_compoff;
1868 flen -= mip->mi_compoff;
1869 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001870 idxs = slang->sl_pidxs;
1871
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001872 /*
1873 * Repeat advancing in the tree until:
1874 * - there is a byte that doesn't match,
1875 * - we reach the end of the tree,
1876 * - or we reach the end of the line.
1877 */
1878 for (;;)
1879 {
1880 if (flen == 0 && *mip->mi_fend != NUL)
1881 flen = fold_more(mip);
1882
1883 len = byts[arridx++];
1884
1885 /* If the first possible byte is a zero the prefix could end here.
1886 * Check if the following word matches and supports the prefix. */
1887 if (byts[arridx] == 0)
1888 {
1889 /* There can be several prefixes with different conditions. We
1890 * try them all, since we don't know which one will give the
1891 * longest match. The word is the same each time, pass the list
1892 * of possible prefixes to find_word(). */
1893 mip->mi_prefarridx = arridx;
1894 mip->mi_prefcnt = len;
1895 while (len > 0 && byts[arridx] == 0)
1896 {
1897 ++arridx;
1898 --len;
1899 }
1900 mip->mi_prefcnt -= len;
1901
1902 /* Find the word that comes after the prefix. */
1903 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001904 if (mode == FIND_COMPOUND)
1905 /* Skip over the previously found word(s). */
1906 mip->mi_prefixlen += mip->mi_compoff;
1907
Bram Moolenaar53805d12005-08-01 07:08:33 +00001908#ifdef FEAT_MBYTE
1909 if (has_mbyte)
1910 {
1911 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001912 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1913 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001914 }
1915 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001916 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001917#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001918 find_word(mip, FIND_PREFIX);
1919
1920
1921 if (len == 0)
1922 break; /* no children, word must end here */
1923 }
1924
1925 /* Stop looking at end of the line. */
1926 if (ptr[wlen] == NUL)
1927 break;
1928
1929 /* Perform a binary search in the list of accepted bytes. */
1930 c = ptr[wlen];
1931 lo = arridx;
1932 hi = arridx + len - 1;
1933 while (lo < hi)
1934 {
1935 m = (lo + hi) / 2;
1936 if (byts[m] > c)
1937 hi = m - 1;
1938 else if (byts[m] < c)
1939 lo = m + 1;
1940 else
1941 {
1942 lo = hi = m;
1943 break;
1944 }
1945 }
1946
1947 /* Stop if there is no matching byte. */
1948 if (hi < lo || byts[lo] != c)
1949 break;
1950
1951 /* Continue at the child (if there is one). */
1952 arridx = idxs[lo];
1953 ++wlen;
1954 --flen;
1955 }
1956}
1957
1958/*
1959 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001960 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001961 * Return the length of the folded chars in bytes.
1962 */
1963 static int
1964fold_more(mip)
1965 matchinf_T *mip;
1966{
1967 int flen;
1968 char_u *p;
1969
1970 p = mip->mi_fend;
1971 do
1972 {
1973 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001974 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001976 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 if (*mip->mi_fend != NUL)
1978 mb_ptr_adv(mip->mi_fend);
1979
1980 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1981 mip->mi_fword + mip->mi_fwordlen,
1982 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001984 mip->mi_fwordlen += flen;
1985 return flen;
1986}
1987
1988/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 * Check case flags for a word. Return TRUE if the word has the requested
1990 * case.
1991 */
1992 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001993spell_valid_case(wordflags, treeflags)
1994 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 int treeflags; /* flags for the word in the spell tree */
1996{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001997 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001999 && ((treeflags & WF_ONECAP) == 0
2000 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001}
2002
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002003/*
2004 * Return TRUE if spell checking is not enabled.
2005 */
2006 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002007no_spell_checking(wp)
2008 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002009{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002010 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2011 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002012 {
2013 EMSG(_("E756: Spell checking is not enabled"));
2014 return TRUE;
2015 }
2016 return FALSE;
2017}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019/*
2020 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002021 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2022 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002023 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2024 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002025 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002026 */
2027 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002028spell_move_to(wp, dir, allwords, curline, attrp)
2029 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002030 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002031 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002033 hlf_T *attrp; /* return: attributes of bad word or NULL
2034 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002036 linenr_T lnum;
2037 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002038 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 char_u *line;
2040 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002041 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002042 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002043 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002044# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002045 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002046 int col;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002047# endif
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002048 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002049 char_u *buf = NULL;
2050 int buflen = 0;
2051 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002052 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002053 int found_one = FALSE;
2054 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaar95529562005-08-25 21:21:38 +00002056 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002057 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002058
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002059 /*
2060 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002061 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002062 *
2063 * When searching backwards, we continue in the line to find the last
2064 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002065 *
2066 * We concatenate the start of the next line, so that wrapped words work
2067 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2068 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002069 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002070 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002071 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072
2073 while (!got_int)
2074 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002075 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002078 if (buflen < len + MAXWLEN + 2)
2079 {
2080 vim_free(buf);
2081 buflen = len + MAXWLEN + 2;
2082 buf = alloc(buflen);
2083 if (buf == NULL)
2084 break;
2085 }
2086
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002087 /* In first line check first word for Capital. */
2088 if (lnum == 1)
2089 capcol = 0;
2090
2091 /* For checking first word with a capital skip white space. */
2092 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 capcol = (int)(skipwhite(line) - line);
2094 else if (curline && wp == curwin)
2095 {
2096 int col = (int)(skipwhite(line) - line);
2097
2098 /* For spellbadword(): check if first word needs a capital. */
2099 if (check_need_cap(lnum, col))
2100 capcol = col;
2101
2102 /* Need to get the line again, may have looked at the previous
2103 * one. */
2104 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2105 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002106
Bram Moolenaar0c405862005-06-22 22:26:26 +00002107 /* Copy the line into "buf" and append the start of the next line if
2108 * possible. */
2109 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002110 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002111 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2112
2113 p = buf + skip;
2114 endp = buf + len;
2115 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002117 /* When searching backward don't search after the cursor. Unless
2118 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002119 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002120 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002121 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002122 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002123 break;
2124
2125 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002126 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002127 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002128
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002129 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002132 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002133 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002134 found_one = TRUE;
2135
Bram Moolenaar51485f02005-06-04 21:55:20 +00002136 /* When searching forward only accept a bad word after
2137 * the cursor. */
2138 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002139 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002140 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002141 && (wrapped
2142 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002143 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002144 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002145 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002146# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002147 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002148 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002149 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002150 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002151 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 }
2153 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002154#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002155 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 if (can_spell)
2158 {
2159 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002160 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002161#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002162 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002163#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002164 if (dir == FORWARD)
2165 {
2166 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002167 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002168 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002169 if (attrp != NULL)
2170 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002171 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002172 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002173 else if (curline)
2174 /* Insert mode completion: put cursor after
2175 * the bad word. */
2176 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002177 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002178 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002179 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002181 }
2182
Bram Moolenaar51485f02005-06-04 21:55:20 +00002183 /* advance to character after the word */
2184 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002185 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002186 }
2187
Bram Moolenaar5195e452005-08-19 20:32:47 +00002188 if (dir == BACKWARD && found_pos.lnum != 0)
2189 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002190 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002191 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002192 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002193 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002194 }
2195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002197 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002198
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002199 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002200 if (dir == BACKWARD)
2201 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002202 /* If we are back at the starting line and searched it again there
2203 * is no match, give up. */
2204 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002205 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002206
2207 if (lnum > 1)
2208 --lnum;
2209 else if (!p_ws)
2210 break; /* at first line and 'nowrapscan' */
2211 else
2212 {
2213 /* Wrap around to the end of the buffer. May search the
2214 * starting line again and accept the last match. */
2215 lnum = wp->w_buffer->b_ml.ml_line_count;
2216 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002217 if (!shortmess(SHM_SEARCH))
2218 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002219 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002220 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002221 }
2222 else
2223 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002224 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2225 ++lnum;
2226 else if (!p_ws)
2227 break; /* at first line and 'nowrapscan' */
2228 else
2229 {
2230 /* Wrap around to the start of the buffer. May search the
2231 * starting line again and accept the first match. */
2232 lnum = 1;
2233 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002234 if (!shortmess(SHM_SEARCH))
2235 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002236 }
2237
2238 /* If we are back at the starting line and there is no match then
2239 * give up. */
2240 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002241 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002242
2243 /* Skip the characters at the start of the next line that were
2244 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002245 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002246 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002247 else
2248 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002249
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002250 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002251 --capcol;
2252
2253 /* But after empty line check first word in next line */
2254 if (*skipwhite(line) == NUL)
2255 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002256 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002257
2258 line_breakcheck();
2259 }
2260
Bram Moolenaar0c405862005-06-22 22:26:26 +00002261 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002262 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002263}
2264
2265/*
2266 * For spell checking: concatenate the start of the following line "line" into
2267 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2268 */
2269 void
2270spell_cat_line(buf, line, maxlen)
2271 char_u *buf;
2272 char_u *line;
2273 int maxlen;
2274{
2275 char_u *p;
2276 int n;
2277
2278 p = skipwhite(line);
2279 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2280 p = skipwhite(p + 1);
2281
2282 if (*p != NUL)
2283 {
2284 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002285 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002286 n = (int)(p - line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002287 if (n >= maxlen)
2288 n = maxlen - 1;
2289 vim_memset(buf + 1, ' ', n);
2290 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002291}
2292
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002293/*
2294 * Structure used for the cookie argument of do_in_runtimepath().
2295 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002296typedef struct spelload_S
2297{
2298 char_u sl_lang[MAXWLEN + 1]; /* language name */
2299 slang_T *sl_slang; /* resulting slang_T struct */
2300 int sl_nobreak; /* NOBREAK language found */
2301} spelload_T;
2302
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002303/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002304 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002305 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002306 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002307 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002308spell_load_lang(lang)
2309 char_u *lang;
2310{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002311 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002312 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002313 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002314#ifdef FEAT_AUTOCMD
2315 int round;
2316#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002317
Bram Moolenaarb765d632005-06-07 21:00:02 +00002318 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002319 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002320 STRCPY(sl.sl_lang, lang);
2321 sl.sl_slang = NULL;
2322 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002323
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002324#ifdef FEAT_AUTOCMD
2325 /* We may retry when no spell file is found for the language, an
2326 * autocommand may load it then. */
2327 for (round = 1; round <= 2; ++round)
2328#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002329 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002330 /*
2331 * Find the first spell file for "lang" in 'runtimepath' and load it.
2332 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002333 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002334 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002335 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002336
2337 if (r == FAIL && *sl.sl_lang != NUL)
2338 {
2339 /* Try loading the ASCII version. */
2340 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2341 "spell/%s.ascii.spl", lang);
2342 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2343
2344#ifdef FEAT_AUTOCMD
2345 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2346 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2347 curbuf->b_fname, FALSE, curbuf))
2348 continue;
2349 break;
2350#endif
2351 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002352#ifdef FEAT_AUTOCMD
2353 break;
2354#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002355 }
2356
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002357 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002358 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002359 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2360 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002361 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002362 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002363 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002364 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002365 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002366 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002367 }
2368}
2369
2370/*
2371 * Return the encoding used for spell checking: Use 'encoding', except that we
2372 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2373 */
2374 static char_u *
2375spell_enc()
2376{
2377
2378#ifdef FEAT_MBYTE
2379 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2380 return p_enc;
2381#endif
2382 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383}
2384
2385/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002386 * Get the name of the .spl file for the internal wordlist into
2387 * "fname[MAXPATHL]".
2388 */
2389 static void
2390int_wordlist_spl(fname)
2391 char_u *fname;
2392{
2393 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2394 int_wordlist, spell_enc());
2395}
2396
2397/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002398 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399 * Caller must fill "sl_next".
2400 */
2401 static slang_T *
2402slang_alloc(lang)
2403 char_u *lang;
2404{
2405 slang_T *lp;
2406
Bram Moolenaar51485f02005-06-04 21:55:20 +00002407 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002408 if (lp != NULL)
2409 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002410 if (lang != NULL)
2411 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002413 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002414 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002415 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002416 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002417 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002418
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002419 return lp;
2420}
2421
2422/*
2423 * Free the contents of an slang_T and the structure itself.
2424 */
2425 static void
2426slang_free(lp)
2427 slang_T *lp;
2428{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002429 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002430 vim_free(lp->sl_fname);
2431 slang_clear(lp);
2432 vim_free(lp);
2433}
2434
2435/*
2436 * Clear an slang_T so that the file can be reloaded.
2437 */
2438 static void
2439slang_clear(lp)
2440 slang_T *lp;
2441{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002442 garray_T *gap;
2443 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002444 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002445 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002446 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447
Bram Moolenaar51485f02005-06-04 21:55:20 +00002448 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002449 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002450 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002452 vim_free(lp->sl_pbyts);
2453 lp->sl_pbyts = NULL;
2454
Bram Moolenaar51485f02005-06-04 21:55:20 +00002455 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002456 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002457 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002458 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002459 vim_free(lp->sl_pidxs);
2460 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461
Bram Moolenaar4770d092006-01-12 23:22:24 +00002462 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002464 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2465 while (gap->ga_len > 0)
2466 {
2467 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2468 vim_free(ftp->ft_from);
2469 vim_free(ftp->ft_to);
2470 }
2471 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002473
2474 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002475 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002476 {
2477 /* "ga_len" is set to 1 without adding an item for latin1 */
2478 if (gap->ga_data != NULL)
2479 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2480 for (i = 0; i < gap->ga_len; ++i)
2481 vim_free(((int **)gap->ga_data)[i]);
2482 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002483 else
2484 /* SAL items: free salitem_T items */
2485 while (gap->ga_len > 0)
2486 {
2487 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2488 vim_free(smp->sm_lead);
2489 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2490 vim_free(smp->sm_to);
2491#ifdef FEAT_MBYTE
2492 vim_free(smp->sm_lead_w);
2493 vim_free(smp->sm_oneof_w);
2494 vim_free(smp->sm_to_w);
2495#endif
2496 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002497 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002499 for (i = 0; i < lp->sl_prefixcnt; ++i)
2500 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002501 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002502 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002503 lp->sl_prefprog = NULL;
2504
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002505 vim_free(lp->sl_info);
2506 lp->sl_info = NULL;
2507
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002508 vim_free(lp->sl_midword);
2509 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002510
Bram Moolenaar5195e452005-08-19 20:32:47 +00002511 vim_free(lp->sl_compprog);
2512 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002513 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002514 lp->sl_compprog = NULL;
2515 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002516 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002517
2518 vim_free(lp->sl_syllable);
2519 lp->sl_syllable = NULL;
2520 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002521
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002522 ga_clear_strings(&lp->sl_comppat);
2523
Bram Moolenaar4770d092006-01-12 23:22:24 +00002524 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2525 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002526
Bram Moolenaar4770d092006-01-12 23:22:24 +00002527#ifdef FEAT_MBYTE
2528 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002529#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002530
Bram Moolenaar4770d092006-01-12 23:22:24 +00002531 /* Clear info from .sug file. */
2532 slang_clear_sug(lp);
2533
Bram Moolenaar5195e452005-08-19 20:32:47 +00002534 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002535 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002536 lp->sl_compsylmax = MAXWLEN;
2537 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002538}
2539
2540/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002541 * Clear the info from the .sug file in "lp".
2542 */
2543 static void
2544slang_clear_sug(lp)
2545 slang_T *lp;
2546{
2547 vim_free(lp->sl_sbyts);
2548 lp->sl_sbyts = NULL;
2549 vim_free(lp->sl_sidxs);
2550 lp->sl_sidxs = NULL;
2551 close_spellbuf(lp->sl_sugbuf);
2552 lp->sl_sugbuf = NULL;
2553 lp->sl_sugloaded = FALSE;
2554 lp->sl_sugtime = 0;
2555}
2556
2557/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002558 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559 * Invoked through do_in_runtimepath().
2560 */
2561 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002562spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002564 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002565{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002566 spelload_T *slp = (spelload_T *)cookie;
2567 slang_T *slang;
2568
2569 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2570 if (slang != NULL)
2571 {
2572 /* When a previously loaded file has NOBREAK also use it for the
2573 * ".add" files. */
2574 if (slp->sl_nobreak && slang->sl_add)
2575 slang->sl_nobreak = TRUE;
2576 else if (slang->sl_nobreak)
2577 slp->sl_nobreak = TRUE;
2578
2579 slp->sl_slang = slang;
2580 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002581}
2582
2583/*
2584 * Load one spell file and store the info into a slang_T.
2585 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002586 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002587 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2588 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2589 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2590 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002591 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002592 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2593 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002594 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002595 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002596 static slang_T *
2597spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002598 char_u *fname;
2599 char_u *lang;
2600 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002602{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002603 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002604 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002605 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002606 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002607 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002608 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 char_u *save_sourcing_name = sourcing_name;
2610 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002611 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002612 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002613 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002614
Bram Moolenaarb765d632005-06-07 21:00:02 +00002615 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616 if (fd == NULL)
2617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002618 if (!silent)
2619 EMSG2(_(e_notopen), fname);
2620 else if (p_verbose > 2)
2621 {
2622 verbose_enter();
2623 smsg((char_u *)e_notopen, fname);
2624 verbose_leave();
2625 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002626 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002627 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002628 if (p_verbose > 2)
2629 {
2630 verbose_enter();
2631 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2632 verbose_leave();
2633 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634
Bram Moolenaarb765d632005-06-07 21:00:02 +00002635 if (old_lp == NULL)
2636 {
2637 lp = slang_alloc(lang);
2638 if (lp == NULL)
2639 goto endFAIL;
2640
2641 /* Remember the file name, used to reload the file when it's updated. */
2642 lp->sl_fname = vim_strsave(fname);
2643 if (lp->sl_fname == NULL)
2644 goto endFAIL;
2645
2646 /* Check for .add.spl. */
2647 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2648 }
2649 else
2650 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002651
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002652 /* Set sourcing_name, so that error messages mention the file name. */
2653 sourcing_name = fname;
2654 sourcing_lnum = 0;
2655
Bram Moolenaar4770d092006-01-12 23:22:24 +00002656 /*
2657 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002658 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659 for (i = 0; i < VIMSPELLMAGICL; ++i)
2660 buf[i] = getc(fd); /* <fileID> */
2661 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2662 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002663 EMSG(_("E757: This does not look like a spell file"));
2664 goto endFAIL;
2665 }
2666 c = getc(fd); /* <versionnr> */
2667 if (c < VIMSPELLVERSION)
2668 {
2669 EMSG(_("E771: Old spell file, needs to be updated"));
2670 goto endFAIL;
2671 }
2672 else if (c > VIMSPELLVERSION)
2673 {
2674 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002675 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002676 }
2677
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678
2679 /*
2680 * <SECTIONS>: <section> ... <sectionend>
2681 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2682 */
2683 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002684 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002685 n = getc(fd); /* <sectionID> or <sectionend> */
2686 if (n == SN_END)
2687 break;
2688 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002689 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690 if (len < 0)
2691 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002692
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693 res = 0;
2694 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002695 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002696 case SN_INFO:
2697 lp->sl_info = read_string(fd, len); /* <infotext> */
2698 if (lp->sl_info == NULL)
2699 goto endFAIL;
2700 break;
2701
Bram Moolenaar5195e452005-08-19 20:32:47 +00002702 case SN_REGION:
2703 res = read_region_section(fd, lp, len);
2704 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002705
Bram Moolenaar5195e452005-08-19 20:32:47 +00002706 case SN_CHARFLAGS:
2707 res = read_charflags_section(fd);
2708 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002709
Bram Moolenaar5195e452005-08-19 20:32:47 +00002710 case SN_MIDWORD:
2711 lp->sl_midword = read_string(fd, len); /* <midword> */
2712 if (lp->sl_midword == NULL)
2713 goto endFAIL;
2714 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002715
Bram Moolenaar5195e452005-08-19 20:32:47 +00002716 case SN_PREFCOND:
2717 res = read_prefcond_section(fd, lp);
2718 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002719
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002721 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2722 break;
2723
2724 case SN_REPSAL:
2725 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002726 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002727
Bram Moolenaar5195e452005-08-19 20:32:47 +00002728 case SN_SAL:
2729 res = read_sal_section(fd, lp);
2730 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002731
Bram Moolenaar5195e452005-08-19 20:32:47 +00002732 case SN_SOFO:
2733 res = read_sofo_section(fd, lp);
2734 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002735
Bram Moolenaar5195e452005-08-19 20:32:47 +00002736 case SN_MAP:
2737 p = read_string(fd, len); /* <mapstr> */
2738 if (p == NULL)
2739 goto endFAIL;
2740 set_map_str(lp, p);
2741 vim_free(p);
2742 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002743
Bram Moolenaar4770d092006-01-12 23:22:24 +00002744 case SN_WORDS:
2745 res = read_words_section(fd, lp, len);
2746 break;
2747
2748 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002749 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002750 break;
2751
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002752 case SN_NOSPLITSUGS:
2753 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2754 break;
2755
Bram Moolenaar5195e452005-08-19 20:32:47 +00002756 case SN_COMPOUND:
2757 res = read_compound(fd, lp, len);
2758 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002759
Bram Moolenaar78622822005-08-23 21:00:13 +00002760 case SN_NOBREAK:
2761 lp->sl_nobreak = TRUE;
2762 break;
2763
Bram Moolenaar5195e452005-08-19 20:32:47 +00002764 case SN_SYLLABLE:
2765 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2766 if (lp->sl_syllable == NULL)
2767 goto endFAIL;
2768 if (init_syl_tab(lp) == FAIL)
2769 goto endFAIL;
2770 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002771
Bram Moolenaar5195e452005-08-19 20:32:47 +00002772 default:
2773 /* Unsupported section. When it's required give an error
2774 * message. When it's not required skip the contents. */
2775 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002776 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002777 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002778 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002779 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002780 while (--len >= 0)
2781 if (getc(fd) < 0)
2782 goto truncerr;
2783 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002784 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002785someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002786 if (res == SP_FORMERROR)
2787 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002788 EMSG(_(e_format));
2789 goto endFAIL;
2790 }
2791 if (res == SP_TRUNCERROR)
2792 {
2793truncerr:
2794 EMSG(_(e_spell_trunc));
2795 goto endFAIL;
2796 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002797 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002798 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002799 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002800
Bram Moolenaar4770d092006-01-12 23:22:24 +00002801 /* <LWORDTREE> */
2802 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2803 if (res != 0)
2804 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002805
Bram Moolenaar4770d092006-01-12 23:22:24 +00002806 /* <KWORDTREE> */
2807 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2808 if (res != 0)
2809 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002810
Bram Moolenaar4770d092006-01-12 23:22:24 +00002811 /* <PREFIXTREE> */
2812 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2813 lp->sl_prefixcnt);
2814 if (res != 0)
2815 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002816
Bram Moolenaarb765d632005-06-07 21:00:02 +00002817 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002818 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002819 {
2820 lp->sl_next = first_lang;
2821 first_lang = lp;
2822 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002823
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002824 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002825
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002826endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002827 if (lang != NULL)
2828 /* truncating the name signals the error to spell_load_lang() */
2829 *lang = NUL;
2830 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002831 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002832 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002833
2834endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002835 if (fd != NULL)
2836 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002837 sourcing_name = save_sourcing_name;
2838 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002839
2840 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002841}
2842
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002843/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002844 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2845 */
2846 static int
2847get2c(fd)
2848 FILE *fd;
2849{
2850 long n;
2851
2852 n = getc(fd);
2853 n = (n << 8) + getc(fd);
2854 return n;
2855}
2856
2857/*
2858 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2859 */
2860 static int
2861get3c(fd)
2862 FILE *fd;
2863{
2864 long n;
2865
2866 n = getc(fd);
2867 n = (n << 8) + getc(fd);
2868 n = (n << 8) + getc(fd);
2869 return n;
2870}
2871
2872/*
2873 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2874 */
2875 static int
2876get4c(fd)
2877 FILE *fd;
2878{
2879 long n;
2880
2881 n = getc(fd);
2882 n = (n << 8) + getc(fd);
2883 n = (n << 8) + getc(fd);
2884 n = (n << 8) + getc(fd);
2885 return n;
2886}
2887
2888/*
2889 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2890 */
2891 static time_t
2892get8c(fd)
2893 FILE *fd;
2894{
2895 time_t n = 0;
2896 int i;
2897
2898 for (i = 0; i < 8; ++i)
2899 n = (n << 8) + getc(fd);
2900 return n;
2901}
2902
2903/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002904 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002905 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002906 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002907 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2908 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002909 */
2910 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002911read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002912 FILE *fd;
2913 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002914 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002915{
2916 int cnt = 0;
2917 int i;
2918 char_u *str;
2919
2920 /* read the length bytes, MSB first */
2921 for (i = 0; i < cnt_bytes; ++i)
2922 cnt = (cnt << 8) + getc(fd);
2923 if (cnt < 0)
2924 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002925 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002926 return NULL;
2927 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002928 *cntp = cnt;
2929 if (cnt == 0)
2930 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002931
Bram Moolenaar5195e452005-08-19 20:32:47 +00002932 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002933 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002934 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002935 return str;
2936}
2937
Bram Moolenaar7887d882005-07-01 22:33:52 +00002938/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002939 * Read a string of length "cnt" from "fd" into allocated memory.
2940 * Returns NULL when out of memory.
2941 */
2942 static char_u *
2943read_string(fd, cnt)
2944 FILE *fd;
2945 int cnt;
2946{
2947 char_u *str;
2948 int i;
2949
2950 /* allocate memory */
2951 str = alloc((unsigned)cnt + 1);
2952 if (str != NULL)
2953 {
2954 /* Read the string. Doesn't check for truncated file. */
2955 for (i = 0; i < cnt; ++i)
2956 str[i] = getc(fd);
2957 str[i] = NUL;
2958 }
2959 return str;
2960}
2961
2962/*
2963 * Read SN_REGION: <regionname> ...
2964 * Return SP_*ERROR flags.
2965 */
2966 static int
2967read_region_section(fd, lp, len)
2968 FILE *fd;
2969 slang_T *lp;
2970 int len;
2971{
2972 int i;
2973
2974 if (len > 16)
2975 return SP_FORMERROR;
2976 for (i = 0; i < len; ++i)
2977 lp->sl_regions[i] = getc(fd); /* <regionname> */
2978 lp->sl_regions[len] = NUL;
2979 return 0;
2980}
2981
2982/*
2983 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2984 * <folcharslen> <folchars>
2985 * Return SP_*ERROR flags.
2986 */
2987 static int
2988read_charflags_section(fd)
2989 FILE *fd;
2990{
2991 char_u *flags;
2992 char_u *fol;
2993 int flagslen, follen;
2994
2995 /* <charflagslen> <charflags> */
2996 flags = read_cnt_string(fd, 1, &flagslen);
2997 if (flagslen < 0)
2998 return flagslen;
2999
3000 /* <folcharslen> <folchars> */
3001 fol = read_cnt_string(fd, 2, &follen);
3002 if (follen < 0)
3003 {
3004 vim_free(flags);
3005 return follen;
3006 }
3007
3008 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3009 if (flags != NULL && fol != NULL)
3010 set_spell_charflags(flags, flagslen, fol);
3011
3012 vim_free(flags);
3013 vim_free(fol);
3014
3015 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3016 if ((flags == NULL) != (fol == NULL))
3017 return SP_FORMERROR;
3018 return 0;
3019}
3020
3021/*
3022 * Read SN_PREFCOND section.
3023 * Return SP_*ERROR flags.
3024 */
3025 static int
3026read_prefcond_section(fd, lp)
3027 FILE *fd;
3028 slang_T *lp;
3029{
3030 int cnt;
3031 int i;
3032 int n;
3033 char_u *p;
3034 char_u buf[MAXWLEN + 1];
3035
3036 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003037 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003038 if (cnt <= 0)
3039 return SP_FORMERROR;
3040
3041 lp->sl_prefprog = (regprog_T **)alloc_clear(
3042 (unsigned)sizeof(regprog_T *) * cnt);
3043 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003044 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003045 lp->sl_prefixcnt = cnt;
3046
3047 for (i = 0; i < cnt; ++i)
3048 {
3049 /* <prefcond> : <condlen> <condstr> */
3050 n = getc(fd); /* <condlen> */
3051 if (n < 0 || n >= MAXWLEN)
3052 return SP_FORMERROR;
3053
3054 /* When <condlen> is zero we have an empty condition. Otherwise
3055 * compile the regexp program used to check for the condition. */
3056 if (n > 0)
3057 {
3058 buf[0] = '^'; /* always match at one position only */
3059 p = buf + 1;
3060 while (n-- > 0)
3061 *p++ = getc(fd); /* <condstr> */
3062 *p = NUL;
3063 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3064 }
3065 }
3066 return 0;
3067}
3068
3069/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003070 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003071 * Return SP_*ERROR flags.
3072 */
3073 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003074read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003075 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003076 garray_T *gap;
3077 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003078{
3079 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003080 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003081 int i;
3082
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003083 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003084 if (cnt < 0)
3085 return SP_TRUNCERROR;
3086
Bram Moolenaar5195e452005-08-19 20:32:47 +00003087 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003088 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003089
3090 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3091 for (; gap->ga_len < cnt; ++gap->ga_len)
3092 {
3093 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3094 ftp->ft_from = read_cnt_string(fd, 1, &i);
3095 if (i < 0)
3096 return i;
3097 if (i == 0)
3098 return SP_FORMERROR;
3099 ftp->ft_to = read_cnt_string(fd, 1, &i);
3100 if (i <= 0)
3101 {
3102 vim_free(ftp->ft_from);
3103 if (i < 0)
3104 return i;
3105 return SP_FORMERROR;
3106 }
3107 }
3108
3109 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003110 for (i = 0; i < 256; ++i)
3111 first[i] = -1;
3112 for (i = 0; i < gap->ga_len; ++i)
3113 {
3114 ftp = &((fromto_T *)gap->ga_data)[i];
3115 if (first[*ftp->ft_from] == -1)
3116 first[*ftp->ft_from] = i;
3117 }
3118 return 0;
3119}
3120
3121/*
3122 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3123 * Return SP_*ERROR flags.
3124 */
3125 static int
3126read_sal_section(fd, slang)
3127 FILE *fd;
3128 slang_T *slang;
3129{
3130 int i;
3131 int cnt;
3132 garray_T *gap;
3133 salitem_T *smp;
3134 int ccnt;
3135 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003136 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003137
3138 slang->sl_sofo = FALSE;
3139
3140 i = getc(fd); /* <salflags> */
3141 if (i & SAL_F0LLOWUP)
3142 slang->sl_followup = TRUE;
3143 if (i & SAL_COLLAPSE)
3144 slang->sl_collapse = TRUE;
3145 if (i & SAL_REM_ACCENTS)
3146 slang->sl_rem_accents = TRUE;
3147
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003148 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003149 if (cnt < 0)
3150 return SP_TRUNCERROR;
3151
3152 gap = &slang->sl_sal;
3153 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003154 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003155 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003156
3157 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3158 for (; gap->ga_len < cnt; ++gap->ga_len)
3159 {
3160 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3161 ccnt = getc(fd); /* <salfromlen> */
3162 if (ccnt < 0)
3163 return SP_TRUNCERROR;
3164 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003165 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003166 smp->sm_lead = p;
3167
3168 /* Read up to the first special char into sm_lead. */
3169 for (i = 0; i < ccnt; ++i)
3170 {
3171 c = getc(fd); /* <salfrom> */
3172 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3173 break;
3174 *p++ = c;
3175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003176 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003177 *p++ = NUL;
3178
3179 /* Put (abc) chars in sm_oneof, if any. */
3180 if (c == '(')
3181 {
3182 smp->sm_oneof = p;
3183 for (++i; i < ccnt; ++i)
3184 {
3185 c = getc(fd); /* <salfrom> */
3186 if (c == ')')
3187 break;
3188 *p++ = c;
3189 }
3190 *p++ = NUL;
3191 if (++i < ccnt)
3192 c = getc(fd);
3193 }
3194 else
3195 smp->sm_oneof = NULL;
3196
3197 /* Any following chars go in sm_rules. */
3198 smp->sm_rules = p;
3199 if (i < ccnt)
3200 /* store the char we got while checking for end of sm_lead */
3201 *p++ = c;
3202 for (++i; i < ccnt; ++i)
3203 *p++ = getc(fd); /* <salfrom> */
3204 *p++ = NUL;
3205
3206 /* <saltolen> <salto> */
3207 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3208 if (ccnt < 0)
3209 {
3210 vim_free(smp->sm_lead);
3211 return ccnt;
3212 }
3213
3214#ifdef FEAT_MBYTE
3215 if (has_mbyte)
3216 {
3217 /* convert the multi-byte strings to wide char strings */
3218 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3219 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3220 if (smp->sm_oneof == NULL)
3221 smp->sm_oneof_w = NULL;
3222 else
3223 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3224 if (smp->sm_to == NULL)
3225 smp->sm_to_w = NULL;
3226 else
3227 smp->sm_to_w = mb_str2wide(smp->sm_to);
3228 if (smp->sm_lead_w == NULL
3229 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3230 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3231 {
3232 vim_free(smp->sm_lead);
3233 vim_free(smp->sm_to);
3234 vim_free(smp->sm_lead_w);
3235 vim_free(smp->sm_oneof_w);
3236 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003237 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003238 }
3239 }
3240#endif
3241 }
3242
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003243 if (gap->ga_len > 0)
3244 {
3245 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3246 * that we need to check the index every time. */
3247 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3248 if ((p = alloc(1)) == NULL)
3249 return SP_OTHERERROR;
3250 p[0] = NUL;
3251 smp->sm_lead = p;
3252 smp->sm_leadlen = 0;
3253 smp->sm_oneof = NULL;
3254 smp->sm_rules = p;
3255 smp->sm_to = NULL;
3256#ifdef FEAT_MBYTE
3257 if (has_mbyte)
3258 {
3259 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3260 smp->sm_leadlen = 0;
3261 smp->sm_oneof_w = NULL;
3262 smp->sm_to_w = NULL;
3263 }
3264#endif
3265 ++gap->ga_len;
3266 }
3267
Bram Moolenaar5195e452005-08-19 20:32:47 +00003268 /* Fill the first-index table. */
3269 set_sal_first(slang);
3270
3271 return 0;
3272}
3273
3274/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003275 * Read SN_WORDS: <word> ...
3276 * Return SP_*ERROR flags.
3277 */
3278 static int
3279read_words_section(fd, lp, len)
3280 FILE *fd;
3281 slang_T *lp;
3282 int len;
3283{
3284 int done = 0;
3285 int i;
3286 char_u word[MAXWLEN];
3287
3288 while (done < len)
3289 {
3290 /* Read one word at a time. */
3291 for (i = 0; ; ++i)
3292 {
3293 word[i] = getc(fd);
3294 if (word[i] == NUL)
3295 break;
3296 if (i == MAXWLEN - 1)
3297 return SP_FORMERROR;
3298 }
3299
3300 /* Init the count to 10. */
3301 count_common_word(lp, word, -1, 10);
3302 done += i + 1;
3303 }
3304 return 0;
3305}
3306
3307/*
3308 * Add a word to the hashtable of common words.
3309 * If it's already there then the counter is increased.
3310 */
3311 static void
3312count_common_word(lp, word, len, count)
3313 slang_T *lp;
3314 char_u *word;
3315 int len; /* word length, -1 for upto NUL */
3316 int count; /* 1 to count once, 10 to init */
3317{
3318 hash_T hash;
3319 hashitem_T *hi;
3320 wordcount_T *wc;
3321 char_u buf[MAXWLEN];
3322 char_u *p;
3323
3324 if (len == -1)
3325 p = word;
3326 else
3327 {
3328 vim_strncpy(buf, word, len);
3329 p = buf;
3330 }
3331
3332 hash = hash_hash(p);
3333 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3334 if (HASHITEM_EMPTY(hi))
3335 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003336 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003337 if (wc == NULL)
3338 return;
3339 STRCPY(wc->wc_word, p);
3340 wc->wc_count = count;
3341 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3342 }
3343 else
3344 {
3345 wc = HI2WC(hi);
3346 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3347 wc->wc_count = MAXWORDCOUNT;
3348 }
3349}
3350
3351/*
3352 * Adjust the score of common words.
3353 */
3354 static int
3355score_wordcount_adj(slang, score, word, split)
3356 slang_T *slang;
3357 int score;
3358 char_u *word;
3359 int split; /* word was split, less bonus */
3360{
3361 hashitem_T *hi;
3362 wordcount_T *wc;
3363 int bonus;
3364 int newscore;
3365
3366 hi = hash_find(&slang->sl_wordcount, word);
3367 if (!HASHITEM_EMPTY(hi))
3368 {
3369 wc = HI2WC(hi);
3370 if (wc->wc_count < SCORE_THRES2)
3371 bonus = SCORE_COMMON1;
3372 else if (wc->wc_count < SCORE_THRES3)
3373 bonus = SCORE_COMMON2;
3374 else
3375 bonus = SCORE_COMMON3;
3376 if (split)
3377 newscore = score - bonus / 2;
3378 else
3379 newscore = score - bonus;
3380 if (newscore < 0)
3381 return 0;
3382 return newscore;
3383 }
3384 return score;
3385}
3386
3387/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003388 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3389 * Return SP_*ERROR flags.
3390 */
3391 static int
3392read_sofo_section(fd, slang)
3393 FILE *fd;
3394 slang_T *slang;
3395{
3396 int cnt;
3397 char_u *from, *to;
3398 int res;
3399
3400 slang->sl_sofo = TRUE;
3401
3402 /* <sofofromlen> <sofofrom> */
3403 from = read_cnt_string(fd, 2, &cnt);
3404 if (cnt < 0)
3405 return cnt;
3406
3407 /* <sofotolen> <sofoto> */
3408 to = read_cnt_string(fd, 2, &cnt);
3409 if (cnt < 0)
3410 {
3411 vim_free(from);
3412 return cnt;
3413 }
3414
3415 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3416 if (from != NULL && to != NULL)
3417 res = set_sofo(slang, from, to);
3418 else if (from != NULL || to != NULL)
3419 res = SP_FORMERROR; /* only one of two strings is an error */
3420 else
3421 res = 0;
3422
3423 vim_free(from);
3424 vim_free(to);
3425 return res;
3426}
3427
3428/*
3429 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003430 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003431 * Returns SP_*ERROR flags.
3432 */
3433 static int
3434read_compound(fd, slang, len)
3435 FILE *fd;
3436 slang_T *slang;
3437 int len;
3438{
3439 int todo = len;
3440 int c;
3441 int atstart;
3442 char_u *pat;
3443 char_u *pp;
3444 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003445 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003446 int cnt;
3447 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003448
3449 if (todo < 2)
3450 return SP_FORMERROR; /* need at least two bytes */
3451
3452 --todo;
3453 c = getc(fd); /* <compmax> */
3454 if (c < 2)
3455 c = MAXWLEN;
3456 slang->sl_compmax = c;
3457
3458 --todo;
3459 c = getc(fd); /* <compminlen> */
3460 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003461 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003462 slang->sl_compminlen = c;
3463
3464 --todo;
3465 c = getc(fd); /* <compsylmax> */
3466 if (c < 1)
3467 c = MAXWLEN;
3468 slang->sl_compsylmax = c;
3469
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003470 c = getc(fd); /* <compoptions> */
3471 if (c != 0)
3472 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3473 else
3474 {
3475 --todo;
3476 c = getc(fd); /* only use the lower byte for now */
3477 --todo;
3478 slang->sl_compoptions = c;
3479
3480 gap = &slang->sl_comppat;
3481 c = get2c(fd); /* <comppatcount> */
3482 todo -= 2;
3483 ga_init2(gap, sizeof(char_u *), c);
3484 if (ga_grow(gap, c) == OK)
3485 while (--c >= 0)
3486 {
3487 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3488 read_cnt_string(fd, 1, &cnt);
3489 /* <comppatlen> <comppattext> */
3490 if (cnt < 0)
3491 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003492 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003493 }
3494 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003495 if (todo < 0)
3496 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003497
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003498 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003499 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003500 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3501 * Conversion to utf-8 may double the size. */
3502 c = todo * 2 + 7;
3503#ifdef FEAT_MBYTE
3504 if (enc_utf8)
3505 c += todo * 2;
3506#endif
3507 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003508 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003509 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003510
Bram Moolenaard12a1322005-08-21 22:08:24 +00003511 /* We also need a list of all flags that can appear at the start and one
3512 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003513 cp = alloc(todo + 1);
3514 if (cp == NULL)
3515 {
3516 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003517 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003518 }
3519 slang->sl_compstartflags = cp;
3520 *cp = NUL;
3521
Bram Moolenaard12a1322005-08-21 22:08:24 +00003522 ap = alloc(todo + 1);
3523 if (ap == NULL)
3524 {
3525 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003526 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003527 }
3528 slang->sl_compallflags = ap;
3529 *ap = NUL;
3530
Bram Moolenaar5195e452005-08-19 20:32:47 +00003531 pp = pat;
3532 *pp++ = '^';
3533 *pp++ = '\\';
3534 *pp++ = '(';
3535
3536 atstart = 1;
3537 while (todo-- > 0)
3538 {
3539 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003540
3541 /* Add all flags to "sl_compallflags". */
3542 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003543 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003544 {
3545 *ap++ = c;
3546 *ap = NUL;
3547 }
3548
Bram Moolenaar5195e452005-08-19 20:32:47 +00003549 if (atstart != 0)
3550 {
3551 /* At start of item: copy flags to "sl_compstartflags". For a
3552 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3553 if (c == '[')
3554 atstart = 2;
3555 else if (c == ']')
3556 atstart = 0;
3557 else
3558 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003559 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003560 {
3561 *cp++ = c;
3562 *cp = NUL;
3563 }
3564 if (atstart == 1)
3565 atstart = 0;
3566 }
3567 }
3568 if (c == '/') /* slash separates two items */
3569 {
3570 *pp++ = '\\';
3571 *pp++ = '|';
3572 atstart = 1;
3573 }
3574 else /* normal char, "[abc]" and '*' are copied as-is */
3575 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003576 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003577 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003578#ifdef FEAT_MBYTE
3579 if (enc_utf8)
3580 pp += mb_char2bytes(c, pp);
3581 else
3582#endif
3583 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003584 }
3585 }
3586
3587 *pp++ = '\\';
3588 *pp++ = ')';
3589 *pp++ = '$';
3590 *pp = NUL;
3591
3592 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3593 vim_free(pat);
3594 if (slang->sl_compprog == NULL)
3595 return SP_FORMERROR;
3596
3597 return 0;
3598}
3599
Bram Moolenaar6de68532005-08-24 22:08:48 +00003600/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003601 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003602 * Like strchr() but independent of locale.
3603 */
3604 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003605byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003606 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003607 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003608{
3609 char_u *p;
3610
3611 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003612 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003613 return TRUE;
3614 return FALSE;
3615}
3616
Bram Moolenaar5195e452005-08-19 20:32:47 +00003617#define SY_MAXLEN 30
3618typedef struct syl_item_S
3619{
3620 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3621 int sy_len;
3622} syl_item_T;
3623
3624/*
3625 * Truncate "slang->sl_syllable" at the first slash and put the following items
3626 * in "slang->sl_syl_items".
3627 */
3628 static int
3629init_syl_tab(slang)
3630 slang_T *slang;
3631{
3632 char_u *p;
3633 char_u *s;
3634 int l;
3635 syl_item_T *syl;
3636
3637 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3638 p = vim_strchr(slang->sl_syllable, '/');
3639 while (p != NULL)
3640 {
3641 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003642 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003643 break;
3644 s = p;
3645 p = vim_strchr(p, '/');
3646 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003647 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003648 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003649 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003650 if (l >= SY_MAXLEN)
3651 return SP_FORMERROR;
3652 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003653 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003654 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3655 + slang->sl_syl_items.ga_len++;
3656 vim_strncpy(syl->sy_chars, s, l);
3657 syl->sy_len = l;
3658 }
3659 return OK;
3660}
3661
3662/*
3663 * Count the number of syllables in "word".
3664 * When "word" contains spaces the syllables after the last space are counted.
3665 * Returns zero if syllables are not defines.
3666 */
3667 static int
3668count_syllables(slang, word)
3669 slang_T *slang;
3670 char_u *word;
3671{
3672 int cnt = 0;
3673 int skip = FALSE;
3674 char_u *p;
3675 int len;
3676 int i;
3677 syl_item_T *syl;
3678 int c;
3679
3680 if (slang->sl_syllable == NULL)
3681 return 0;
3682
3683 for (p = word; *p != NUL; p += len)
3684 {
3685 /* When running into a space reset counter. */
3686 if (*p == ' ')
3687 {
3688 len = 1;
3689 cnt = 0;
3690 continue;
3691 }
3692
3693 /* Find longest match of syllable items. */
3694 len = 0;
3695 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3696 {
3697 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3698 if (syl->sy_len > len
3699 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3700 len = syl->sy_len;
3701 }
3702 if (len != 0) /* found a match, count syllable */
3703 {
3704 ++cnt;
3705 skip = FALSE;
3706 }
3707 else
3708 {
3709 /* No recognized syllable item, at least a syllable char then? */
3710#ifdef FEAT_MBYTE
3711 c = mb_ptr2char(p);
3712 len = (*mb_ptr2len)(p);
3713#else
3714 c = *p;
3715 len = 1;
3716#endif
3717 if (vim_strchr(slang->sl_syllable, c) == NULL)
3718 skip = FALSE; /* No, search for next syllable */
3719 else if (!skip)
3720 {
3721 ++cnt; /* Yes, count it */
3722 skip = TRUE; /* don't count following syllable chars */
3723 }
3724 }
3725 }
3726 return cnt;
3727}
3728
3729/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003730 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003731 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003732 */
3733 static int
3734set_sofo(lp, from, to)
3735 slang_T *lp;
3736 char_u *from;
3737 char_u *to;
3738{
3739 int i;
3740
3741#ifdef FEAT_MBYTE
3742 garray_T *gap;
3743 char_u *s;
3744 char_u *p;
3745 int c;
3746 int *inp;
3747
3748 if (has_mbyte)
3749 {
3750 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3751 * characters. The index is the low byte of the character.
3752 * The list contains from-to pairs with a terminating NUL.
3753 * sl_sal_first[] is used for latin1 "from" characters. */
3754 gap = &lp->sl_sal;
3755 ga_init2(gap, sizeof(int *), 1);
3756 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003757 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003758 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3759 gap->ga_len = 256;
3760
3761 /* First count the number of items for each list. Temporarily use
3762 * sl_sal_first[] for this. */
3763 for (p = from, s = to; *p != NUL && *s != NUL; )
3764 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003765 c = mb_cptr2char_adv(&p);
3766 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003767 if (c >= 256)
3768 ++lp->sl_sal_first[c & 0xff];
3769 }
3770 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003771 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003772
3773 /* Allocate the lists. */
3774 for (i = 0; i < 256; ++i)
3775 if (lp->sl_sal_first[i] > 0)
3776 {
3777 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3778 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003779 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003780 ((int **)gap->ga_data)[i] = (int *)p;
3781 *(int *)p = 0;
3782 }
3783
3784 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3785 * list. */
3786 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3787 for (p = from, s = to; *p != NUL && *s != NUL; )
3788 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003789 c = mb_cptr2char_adv(&p);
3790 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003791 if (c >= 256)
3792 {
3793 /* Append the from-to chars at the end of the list with
3794 * the low byte. */
3795 inp = ((int **)gap->ga_data)[c & 0xff];
3796 while (*inp != 0)
3797 ++inp;
3798 *inp++ = c; /* from char */
3799 *inp++ = i; /* to char */
3800 *inp++ = NUL; /* NUL at the end */
3801 }
3802 else
3803 /* mapping byte to char is done in sl_sal_first[] */
3804 lp->sl_sal_first[c] = i;
3805 }
3806 }
3807 else
3808#endif
3809 {
3810 /* mapping bytes to bytes is done in sl_sal_first[] */
3811 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003812 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003813
3814 for (i = 0; to[i] != NUL; ++i)
3815 lp->sl_sal_first[from[i]] = to[i];
3816 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3817 }
3818
Bram Moolenaar5195e452005-08-19 20:32:47 +00003819 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003820}
3821
3822/*
3823 * Fill the first-index table for "lp".
3824 */
3825 static void
3826set_sal_first(lp)
3827 slang_T *lp;
3828{
3829 salfirst_T *sfirst;
3830 int i;
3831 salitem_T *smp;
3832 int c;
3833 garray_T *gap = &lp->sl_sal;
3834
3835 sfirst = lp->sl_sal_first;
3836 for (i = 0; i < 256; ++i)
3837 sfirst[i] = -1;
3838 smp = (salitem_T *)gap->ga_data;
3839 for (i = 0; i < gap->ga_len; ++i)
3840 {
3841#ifdef FEAT_MBYTE
3842 if (has_mbyte)
3843 /* Use the lowest byte of the first character. For latin1 it's
3844 * the character, for other encodings it should differ for most
3845 * characters. */
3846 c = *smp[i].sm_lead_w & 0xff;
3847 else
3848#endif
3849 c = *smp[i].sm_lead;
3850 if (sfirst[c] == -1)
3851 {
3852 sfirst[c] = i;
3853#ifdef FEAT_MBYTE
3854 if (has_mbyte)
3855 {
3856 int n;
3857
3858 /* Make sure all entries with this byte are following each
3859 * other. Move the ones that are in the wrong position. Do
3860 * keep the same ordering! */
3861 while (i + 1 < gap->ga_len
3862 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3863 /* Skip over entry with same index byte. */
3864 ++i;
3865
3866 for (n = 1; i + n < gap->ga_len; ++n)
3867 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3868 {
3869 salitem_T tsal;
3870
3871 /* Move entry with same index byte after the entries
3872 * we already found. */
3873 ++i;
3874 --n;
3875 tsal = smp[i + n];
3876 mch_memmove(smp + i + 1, smp + i,
3877 sizeof(salitem_T) * n);
3878 smp[i] = tsal;
3879 }
3880 }
3881#endif
3882 }
3883 }
3884}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003885
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003886#ifdef FEAT_MBYTE
3887/*
3888 * Turn a multi-byte string into a wide character string.
3889 * Return it in allocated memory (NULL for out-of-memory)
3890 */
3891 static int *
3892mb_str2wide(s)
3893 char_u *s;
3894{
3895 int *res;
3896 char_u *p;
3897 int i = 0;
3898
3899 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3900 if (res != NULL)
3901 {
3902 for (p = s; *p != NUL; )
3903 res[i++] = mb_ptr2char_adv(&p);
3904 res[i] = NUL;
3905 }
3906 return res;
3907}
3908#endif
3909
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003911 * Read a tree from the .spl or .sug file.
3912 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3913 * This is skipped when the tree has zero length.
3914 * Returns zero when OK, SP_ value for an error.
3915 */
3916 static int
3917spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3918 FILE *fd;
3919 char_u **bytsp;
3920 idx_T **idxsp;
3921 int prefixtree; /* TRUE for the prefix tree */
3922 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3923{
3924 int len;
3925 int idx;
3926 char_u *bp;
3927 idx_T *ip;
3928
3929 /* The tree size was computed when writing the file, so that we can
3930 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003931 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003932 if (len < 0)
3933 return SP_TRUNCERROR;
3934 if (len > 0)
3935 {
3936 /* Allocate the byte array. */
3937 bp = lalloc((long_u)len, TRUE);
3938 if (bp == NULL)
3939 return SP_OTHERERROR;
3940 *bytsp = bp;
3941
3942 /* Allocate the index array. */
3943 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3944 if (ip == NULL)
3945 return SP_OTHERERROR;
3946 *idxsp = ip;
3947
3948 /* Recursively read the tree and store it in the array. */
3949 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3950 if (idx < 0)
3951 return idx;
3952 }
3953 return 0;
3954}
3955
3956/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003957 * Read one row of siblings from the spell file and store it in the byte array
3958 * "byts" and index array "idxs". Recursively read the children.
3959 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003960 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003961 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003962 * Returns the index (>= 0) following the siblings.
3963 * Returns SP_TRUNCERROR if the file is shorter than expected.
3964 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003965 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003966 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003967read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003968 FILE *fd;
3969 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003970 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003971 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003972 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003973 int prefixtree; /* TRUE for reading PREFIXTREE */
3974 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003975{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003976 int len;
3977 int i;
3978 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003979 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003980 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003981 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003982#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003983
Bram Moolenaar51485f02005-06-04 21:55:20 +00003984 len = getc(fd); /* <siblingcount> */
3985 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003986 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003987
3988 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003989 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003990 byts[idx++] = len;
3991
3992 /* Read the byte values, flag/region bytes and shared indexes. */
3993 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003994 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003995 c = getc(fd); /* <byte> */
3996 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003997 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003998 if (c <= BY_SPECIAL)
3999 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004000 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004001 {
4002 /* No flags, all regions. */
4003 idxs[idx] = 0;
4004 c = 0;
4005 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004006 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004007 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004008 if (prefixtree)
4009 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004010 /* Read the optional pflags byte, the prefix ID and the
4011 * condition nr. In idxs[] store the prefix ID in the low
4012 * byte, the condition index shifted up 8 bits, the flags
4013 * shifted up 24 bits. */
4014 if (c == BY_FLAGS)
4015 c = getc(fd) << 24; /* <pflags> */
4016 else
4017 c = 0;
4018
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004019 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004020
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004021 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004022 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004023 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004024 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004025 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004026 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004027 {
4028 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004029 * idxs[] the flags go in the low two bytes, region above
4030 * that and prefix ID above the region. */
4031 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004032 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004033 if (c2 == BY_FLAGS2)
4034 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004036 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004037 if (c & WF_AFX)
4038 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004039 }
4040
Bram Moolenaar51485f02005-06-04 21:55:20 +00004041 idxs[idx] = c;
4042 c = 0;
4043 }
4044 else /* c == BY_INDEX */
4045 {
4046 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004047 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004049 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004050 idxs[idx] = n + SHARED_MASK;
4051 c = getc(fd); /* <xbyte> */
4052 }
4053 }
4054 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004055 }
4056
Bram Moolenaar51485f02005-06-04 21:55:20 +00004057 /* Recursively read the children for non-shared siblings.
4058 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4059 * remove SHARED_MASK) */
4060 for (i = 1; i <= len; ++i)
4061 if (byts[startidx + i] != 0)
4062 {
4063 if (idxs[startidx + i] & SHARED_MASK)
4064 idxs[startidx + i] &= ~SHARED_MASK;
4065 else
4066 {
4067 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004068 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004069 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004070 if (idx < 0)
4071 break;
4072 }
4073 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004076}
4077
4078/*
4079 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004080 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004081 */
4082 char_u *
4083did_set_spelllang(buf)
4084 buf_T *buf;
4085{
4086 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004087 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004089 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004090 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004091 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004092 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004094 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004096 int len;
4097 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004098 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004099 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004100 char_u *use_region = NULL;
4101 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004102 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004103 int i, j;
4104 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004105
4106 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004107 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004108
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004109 /* loop over comma separated language names. */
4110 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004111 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004112 /* Get one language name. */
4113 copy_option_part(&splp, lang, MAXWLEN, ",");
4114
Bram Moolenaar5482f332005-04-17 20:18:43 +00004115 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004116 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004117
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004118 /* If the name ends in ".spl" use it as the name of the spell file.
4119 * If there is a region name let "region" point to it and remove it
4120 * from the name. */
4121 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4122 {
4123 filename = TRUE;
4124
Bram Moolenaarb6356332005-07-18 21:40:44 +00004125 /* Locate a region and remove it from the file name. */
4126 p = vim_strchr(gettail(lang), '_');
4127 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4128 && !ASCII_ISALPHA(p[3]))
4129 {
4130 vim_strncpy(region_cp, p + 1, 2);
4131 mch_memmove(p, p + 3, len - (p - lang) - 2);
4132 len -= 3;
4133 region = region_cp;
4134 }
4135 else
4136 dont_use_region = TRUE;
4137
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004138 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004139 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4140 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004141 break;
4142 }
4143 else
4144 {
4145 filename = FALSE;
4146 if (len > 3 && lang[len - 3] == '_')
4147 {
4148 region = lang + len - 2;
4149 len -= 3;
4150 lang[len] = NUL;
4151 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004152 else
4153 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004154
4155 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004156 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4157 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004158 break;
4159 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004160
Bram Moolenaarb6356332005-07-18 21:40:44 +00004161 if (region != NULL)
4162 {
4163 /* If the region differs from what was used before then don't
4164 * use it for 'spellfile'. */
4165 if (use_region != NULL && STRCMP(region, use_region) != 0)
4166 dont_use_region = TRUE;
4167 use_region = region;
4168 }
4169
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004170 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004171 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004172 {
4173 if (filename)
4174 (void)spell_load_file(lang, lang, NULL, FALSE);
4175 else
4176 spell_load_lang(lang);
4177 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004178
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004179 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004180 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004181 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004182 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4183 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4184 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004185 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004186 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004187 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004188 {
4189 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004190 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004191 if (c == REGION_ALL)
4192 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004193 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004194 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004195 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004196 /* This addition file is for other regions. */
4197 region_mask = 0;
4198 }
4199 else
4200 /* This is probably an error. Give a warning and
4201 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004202 smsg((char_u *)
4203 _("Warning: region %s not supported"),
4204 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004205 }
4206 else
4207 region_mask = 1 << c;
4208 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004210 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004211 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004212 if (ga_grow(&ga, 1) == FAIL)
4213 {
4214 ga_clear(&ga);
4215 return e_outofmem;
4216 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004218 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4219 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004220 use_midword(slang, buf);
4221 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004222 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004223 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004224 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004225 }
4226
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004227 /* round 0: load int_wordlist, if possible.
4228 * round 1: load first name in 'spellfile'.
4229 * round 2: load second name in 'spellfile.
4230 * etc. */
4231 spf = curbuf->b_p_spf;
4232 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004234 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004235 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004236 /* Internal wordlist, if there is one. */
4237 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004238 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004239 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004240 }
4241 else
4242 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004243 /* One entry in 'spellfile'. */
4244 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4245 STRCAT(spf_name, ".spl");
4246
4247 /* If it was already found above then skip it. */
4248 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004249 {
4250 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4251 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004252 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004253 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004254 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004255 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004256 }
4257
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004258 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004259 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4260 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004264 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004265 * region name, the region is ignored otherwise. for int_wordlist
4266 * use an arbitrary name. */
4267 if (round == 0)
4268 STRCPY(lang, "internal wordlist");
4269 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004270 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004271 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004272 p = vim_strchr(lang, '.');
4273 if (p != NULL)
4274 *p = NUL; /* truncate at ".encoding.add" */
4275 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004276 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004277
4278 /* If one of the languages has NOBREAK we assume the addition
4279 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004280 if (slang != NULL && nobreak)
4281 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004283 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004285 region_mask = REGION_ALL;
4286 if (use_region != NULL && !dont_use_region)
4287 {
4288 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004289 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004290 if (c != REGION_ALL)
4291 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004292 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004293 /* This spell file is for other regions. */
4294 region_mask = 0;
4295 }
4296
4297 if (region_mask != 0)
4298 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004299 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4300 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4301 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004302 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4303 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004304 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 }
4307 }
4308
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004309 /* Everything is fine, store the new b_langp value. */
4310 ga_clear(&buf->b_langp);
4311 buf->b_langp = ga;
4312
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004313 /* For each language figure out what language to use for sound folding and
4314 * REP items. If the language doesn't support it itself use another one
4315 * with the same name. E.g. for "en-math" use "en". */
4316 for (i = 0; i < ga.ga_len; ++i)
4317 {
4318 lp = LANGP_ENTRY(ga, i);
4319
4320 /* sound folding */
4321 if (lp->lp_slang->sl_sal.ga_len > 0)
4322 /* language does sound folding itself */
4323 lp->lp_sallang = lp->lp_slang;
4324 else
4325 /* find first similar language that does sound folding */
4326 for (j = 0; j < ga.ga_len; ++j)
4327 {
4328 lp2 = LANGP_ENTRY(ga, j);
4329 if (lp2->lp_slang->sl_sal.ga_len > 0
4330 && STRNCMP(lp->lp_slang->sl_name,
4331 lp2->lp_slang->sl_name, 2) == 0)
4332 {
4333 lp->lp_sallang = lp2->lp_slang;
4334 break;
4335 }
4336 }
4337
4338 /* REP items */
4339 if (lp->lp_slang->sl_rep.ga_len > 0)
4340 /* language has REP items itself */
4341 lp->lp_replang = lp->lp_slang;
4342 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004343 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004344 for (j = 0; j < ga.ga_len; ++j)
4345 {
4346 lp2 = LANGP_ENTRY(ga, j);
4347 if (lp2->lp_slang->sl_rep.ga_len > 0
4348 && STRNCMP(lp->lp_slang->sl_name,
4349 lp2->lp_slang->sl_name, 2) == 0)
4350 {
4351 lp->lp_replang = lp2->lp_slang;
4352 break;
4353 }
4354 }
4355 }
4356
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004357 return NULL;
4358}
4359
4360/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004361 * Clear the midword characters for buffer "buf".
4362 */
4363 static void
4364clear_midword(buf)
4365 buf_T *buf;
4366{
4367 vim_memset(buf->b_spell_ismw, 0, 256);
4368#ifdef FEAT_MBYTE
4369 vim_free(buf->b_spell_ismw_mb);
4370 buf->b_spell_ismw_mb = NULL;
4371#endif
4372}
4373
4374/*
4375 * Use the "sl_midword" field of language "lp" for buffer "buf".
4376 * They add up to any currently used midword characters.
4377 */
4378 static void
4379use_midword(lp, buf)
4380 slang_T *lp;
4381 buf_T *buf;
4382{
4383 char_u *p;
4384
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004385 if (lp->sl_midword == NULL) /* there aren't any */
4386 return;
4387
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004388 for (p = lp->sl_midword; *p != NUL; )
4389#ifdef FEAT_MBYTE
4390 if (has_mbyte)
4391 {
4392 int c, l, n;
4393 char_u *bp;
4394
4395 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004396 l = (*mb_ptr2len)(p);
4397 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004398 buf->b_spell_ismw[c] = TRUE;
4399 else if (buf->b_spell_ismw_mb == NULL)
4400 /* First multi-byte char in "b_spell_ismw_mb". */
4401 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4402 else
4403 {
4404 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004405 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004406 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4407 if (bp != NULL)
4408 {
4409 vim_free(buf->b_spell_ismw_mb);
4410 buf->b_spell_ismw_mb = bp;
4411 vim_strncpy(bp + n, p, l);
4412 }
4413 }
4414 p += l;
4415 }
4416 else
4417#endif
4418 buf->b_spell_ismw[*p++] = TRUE;
4419}
4420
4421/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004422 * Find the region "region[2]" in "rp" (points to "sl_regions").
4423 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004424 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004425 */
4426 static int
4427find_region(rp, region)
4428 char_u *rp;
4429 char_u *region;
4430{
4431 int i;
4432
4433 for (i = 0; ; i += 2)
4434 {
4435 if (rp[i] == NUL)
4436 return REGION_ALL;
4437 if (rp[i] == region[0] && rp[i + 1] == region[1])
4438 break;
4439 }
4440 return i / 2;
4441}
4442
4443/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004444 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004445 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004446 * Word WF_ONECAP
4447 * W WORD WF_ALLCAP
4448 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004449 */
4450 static int
4451captype(word, end)
4452 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004453 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004454{
4455 char_u *p;
4456 int c;
4457 int firstcap;
4458 int allcap;
4459 int past_second = FALSE; /* past second word char */
4460
4461 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004462 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 return 0; /* only non-word characters, illegal word */
4465#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004466 if (has_mbyte)
4467 c = mb_ptr2char_adv(&p);
4468 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004469#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004470 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004471 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472
4473 /*
4474 * Need to check all letters to find a word with mixed upper/lower.
4475 * But a word with an upper char only at start is a ONECAP.
4476 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004477 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004478 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004480 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004481 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004482 {
4483 /* UUl -> KEEPCAP */
4484 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004486 allcap = FALSE;
4487 }
4488 else if (!allcap)
4489 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004490 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004491 past_second = TRUE;
4492 }
4493
4494 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004495 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004496 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004497 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004498 return 0;
4499}
4500
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004501/*
4502 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4503 * capital. So that make_case_word() can turn WOrd into Word.
4504 * Add ALLCAP for "WOrD".
4505 */
4506 static int
4507badword_captype(word, end)
4508 char_u *word;
4509 char_u *end;
4510{
4511 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004512 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004513 int l, u;
4514 int first;
4515 char_u *p;
4516
4517 if (flags & WF_KEEPCAP)
4518 {
4519 /* Count the number of UPPER and lower case letters. */
4520 l = u = 0;
4521 first = FALSE;
4522 for (p = word; p < end; mb_ptr_adv(p))
4523 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004524 c = PTR2CHAR(p);
4525 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004526 {
4527 ++u;
4528 if (p == word)
4529 first = TRUE;
4530 }
4531 else
4532 ++l;
4533 }
4534
4535 /* If there are more UPPER than lower case letters suggest an
4536 * ALLCAP word. Otherwise, if the first letter is UPPER then
4537 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4538 * require three upper case letters. */
4539 if (u > l && u > 2)
4540 flags |= WF_ALLCAP;
4541 else if (first)
4542 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004543
4544 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4545 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004546 }
4547 return flags;
4548}
4549
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004550# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4551/*
4552 * Free all languages.
4553 */
4554 void
4555spell_free_all()
4556{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004557 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004558 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004559 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004560
4561 /* Go through all buffers and handle 'spelllang'. */
4562 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4563 ga_clear(&buf->b_langp);
4564
4565 while (first_lang != NULL)
4566 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004567 slang = first_lang;
4568 first_lang = slang->sl_next;
4569 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004570 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004571
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004572 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004573 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004574 /* Delete the internal wordlist and its .spl file */
4575 mch_remove(int_wordlist);
4576 int_wordlist_spl(fname);
4577 mch_remove(fname);
4578 vim_free(int_wordlist);
4579 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004580 }
4581
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004582 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004583
4584 vim_free(repl_to);
4585 repl_to = NULL;
4586 vim_free(repl_from);
4587 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004588}
4589# endif
4590
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591# if defined(FEAT_MBYTE) || defined(PROTO)
4592/*
4593 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004594 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 */
4596 void
4597spell_reload()
4598{
4599 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004600 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004601
Bram Moolenaarea408852005-06-25 22:49:46 +00004602 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004603 init_spell_chartab();
4604
4605 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004606 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607
4608 /* Go through all buffers and handle 'spelllang'. */
4609 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4610 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004611 /* Only load the wordlists when 'spelllang' is set and there is a
4612 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004613 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004614 {
4615 FOR_ALL_WINDOWS(wp)
4616 if (wp->w_buffer == buf && wp->w_p_spell)
4617 {
4618 (void)did_set_spelllang(buf);
4619# ifdef FEAT_WINDOWS
4620 break;
4621# endif
4622 }
4623 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004624 }
4625}
4626# endif
4627
Bram Moolenaarb765d632005-06-07 21:00:02 +00004628/*
4629 * Reload the spell file "fname" if it's loaded.
4630 */
4631 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004633 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004635{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004636 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004638
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004639 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004640 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004641 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004642 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004643 slang_clear(slang);
4644 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004645 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004646 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004647 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004649 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651
4652 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004653 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004654 if (added_word && !didit)
4655 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004656}
4657
4658
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659/*
4660 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004661 */
4662
Bram Moolenaar51485f02005-06-04 21:55:20 +00004663#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004664 and .dic file. */
4665/*
4666 * Main structure to store the contents of a ".aff" file.
4667 */
4668typedef struct afffile_S
4669{
4670 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004671 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004672 unsigned af_rare; /* RARE ID for rare word */
4673 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004674 unsigned af_bad; /* BAD ID for banned word */
4675 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004676 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004677 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004678 unsigned af_comproot; /* COMPOUNDROOT ID */
4679 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4680 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004681 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004682 int af_pfxpostpone; /* postpone prefixes without chop string and
4683 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004684 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4685 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004686 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004687} afffile_T;
4688
Bram Moolenaar6de68532005-08-24 22:08:48 +00004689#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004690#define AFT_LONG 1 /* flags are two characters */
4691#define AFT_CAPLONG 2 /* flags are one or two characters */
4692#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004693
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004694typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004695/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4696struct affentry_S
4697{
4698 affentry_T *ae_next; /* next affix with same name/number */
4699 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4700 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004701 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004702 char_u *ae_cond; /* condition (NULL for ".") */
4703 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004704 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4705 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004706};
4707
Bram Moolenaar6de68532005-08-24 22:08:48 +00004708#ifdef FEAT_MBYTE
4709# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4710#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004711# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004712#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004713
Bram Moolenaar51485f02005-06-04 21:55:20 +00004714/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4715typedef struct affheader_S
4716{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004717 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4718 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4719 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004720 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004721 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004722 affentry_T *ah_first; /* first affix entry */
4723} affheader_T;
4724
4725#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4726
Bram Moolenaar6de68532005-08-24 22:08:48 +00004727/* Flag used in compound items. */
4728typedef struct compitem_S
4729{
4730 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4731 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4732 int ci_newID; /* affix ID after renumbering. */
4733} compitem_T;
4734
4735#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4736
Bram Moolenaar51485f02005-06-04 21:55:20 +00004737/*
4738 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004739 * the need to keep track of each allocated thing, everything is freed all at
4740 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004741 */
4742#define SBLOCKSIZE 16000 /* size of sb_data */
4743typedef struct sblock_S sblock_T;
4744struct sblock_S
4745{
4746 sblock_T *sb_next; /* next block in list */
4747 int sb_used; /* nr of bytes already in use */
4748 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004749};
4750
4751/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004752 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004753 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004754typedef struct wordnode_S wordnode_T;
4755struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004756{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004757 union /* shared to save space */
4758 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004759 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004760 int index; /* index in written nodes (valid after first
4761 round) */
4762 } wn_u1;
4763 union /* shared to save space */
4764 {
4765 wordnode_T *next; /* next node with same hash key */
4766 wordnode_T *wnode; /* parent node that will write this node */
4767 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004768 wordnode_T *wn_child; /* child (next byte in word) */
4769 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4770 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004771 int wn_refs; /* Nr. of references to this node. Only
4772 relevant for first node in a list of
4773 siblings, in following siblings it is
4774 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004775 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004776
4777 /* Info for when "wn_byte" is NUL.
4778 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4779 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4780 * "wn_region" the LSW of the wordnr. */
4781 char_u wn_affixID; /* supported/required prefix ID or 0 */
4782 short_u wn_flags; /* WF_ flags */
4783 short wn_region; /* region mask */
4784
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004785#ifdef SPELL_PRINTTREE
4786 int wn_nr; /* sequence nr for printing */
4787#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004788};
4789
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004790#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4791
Bram Moolenaar51485f02005-06-04 21:55:20 +00004792#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004793
Bram Moolenaar51485f02005-06-04 21:55:20 +00004794/*
4795 * Info used while reading the spell files.
4796 */
4797typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004798{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004799 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004800 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004801
Bram Moolenaar51485f02005-06-04 21:55:20 +00004802 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004803 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004804
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004805 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004806
Bram Moolenaar4770d092006-01-12 23:22:24 +00004807 long si_sugtree; /* creating the soundfolding trie */
4808
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004810 long si_blocks_cnt; /* memory blocks allocated */
4811 long si_compress_cnt; /* words to add before lowering
4812 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004813 wordnode_T *si_first_free; /* List of nodes that have been freed during
4814 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004815 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004816#ifdef SPELL_PRINTTREE
4817 int si_wordnode_nr; /* sequence nr for nodes */
4818#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004819 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004820
Bram Moolenaar51485f02005-06-04 21:55:20 +00004821 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004822 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004823 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004824 int si_region; /* region mask */
4825 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004826 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004827 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004828 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004829 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004830 int si_region_count; /* number of regions supported (1 when there
4831 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004832 char_u si_region_name[16]; /* region names; used only if
4833 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834
4835 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004836 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004837 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004838 char_u *si_sofofr; /* SOFOFROM text */
4839 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004840 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004841 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 int si_followup; /* soundsalike: ? */
4843 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004844 hashtab_T si_commonwords; /* hashtable for common words */
4845 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 int si_rem_accents; /* soundsalike: remove accents */
4847 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004848 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004849 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004850 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004851 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004852 int si_compoptions; /* COMP_ flags */
4853 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4854 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004855 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004856 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004857 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004858 garray_T si_prefcond; /* table with conditions for postponed
4859 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004860 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004861 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004862} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004863
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004864static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004865static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004866static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004867static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4868static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4869static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004870static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004871static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4872static void aff_check_number __ARGS((int spinval, int affval, char *name));
4873static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004874static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4876static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004877static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004878static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004879static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004880static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004881static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004882static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004883static 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 +00004884static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4885static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4886static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004887static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004888static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004889static 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 +00004890static 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 +00004891static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004892static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004893static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4894static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4895static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004896static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004897static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004898static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004899static void clear_node __ARGS((wordnode_T *node));
4900static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004901static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4902static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4903static int sug_maketable __ARGS((spellinfo_T *spin));
4904static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4905static int offset2bytes __ARGS((int nr, char_u *buf));
4906static int bytes2offset __ARGS((char_u **pp));
4907static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004908static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004909static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004910static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004911
Bram Moolenaar53805d12005-08-01 07:08:33 +00004912/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4913 * but it must be negative to indicate the prefix tree to tree_add_word().
4914 * Use a negative number with the lower 8 bits zero. */
4915#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004916
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004917/* flags for "condit" argument of store_aff_word() */
4918#define CONDIT_COMB 1 /* affix must combine */
4919#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4920#define CONDIT_SUF 4 /* add a suffix for matching flags */
4921#define CONDIT_AFF 8 /* word already has an affix */
4922
Bram Moolenaar5195e452005-08-19 20:32:47 +00004923/*
4924 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4925 */
4926static long compress_start = 30000; /* memory / SBLOCKSIZE */
4927static long compress_inc = 100; /* memory / SBLOCKSIZE */
4928static long compress_added = 500000; /* word count */
4929
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004930#ifdef SPELL_PRINTTREE
4931/*
4932 * For debugging the tree code: print the current tree in a (more or less)
4933 * readable format, so that we can see what happens when adding a word and/or
4934 * compressing the tree.
4935 * Based on code from Olaf Seibert.
4936 */
4937#define PRINTLINESIZE 1000
4938#define PRINTWIDTH 6
4939
4940#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4941 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4942
4943static char line1[PRINTLINESIZE];
4944static char line2[PRINTLINESIZE];
4945static char line3[PRINTLINESIZE];
4946
4947 static void
4948spell_clear_flags(wordnode_T *node)
4949{
4950 wordnode_T *np;
4951
4952 for (np = node; np != NULL; np = np->wn_sibling)
4953 {
4954 np->wn_u1.index = FALSE;
4955 spell_clear_flags(np->wn_child);
4956 }
4957}
4958
4959 static void
4960spell_print_node(wordnode_T *node, int depth)
4961{
4962 if (node->wn_u1.index)
4963 {
4964 /* Done this node before, print the reference. */
4965 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4966 PRINTSOME(line2, depth, " ", 0, 0);
4967 PRINTSOME(line3, depth, " ", 0, 0);
4968 msg(line1);
4969 msg(line2);
4970 msg(line3);
4971 }
4972 else
4973 {
4974 node->wn_u1.index = TRUE;
4975
4976 if (node->wn_byte != NUL)
4977 {
4978 if (node->wn_child != NULL)
4979 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4980 else
4981 /* Cannot happen? */
4982 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4983 }
4984 else
4985 PRINTSOME(line1, depth, " $ ", 0, 0);
4986
4987 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4988
4989 if (node->wn_sibling != NULL)
4990 PRINTSOME(line3, depth, " | ", 0, 0);
4991 else
4992 PRINTSOME(line3, depth, " ", 0, 0);
4993
4994 if (node->wn_byte == NUL)
4995 {
4996 msg(line1);
4997 msg(line2);
4998 msg(line3);
4999 }
5000
5001 /* do the children */
5002 if (node->wn_byte != NUL && node->wn_child != NULL)
5003 spell_print_node(node->wn_child, depth + 1);
5004
5005 /* do the siblings */
5006 if (node->wn_sibling != NULL)
5007 {
5008 /* get rid of all parent details except | */
5009 STRCPY(line1, line3);
5010 STRCPY(line2, line3);
5011 spell_print_node(node->wn_sibling, depth);
5012 }
5013 }
5014}
5015
5016 static void
5017spell_print_tree(wordnode_T *root)
5018{
5019 if (root != NULL)
5020 {
5021 /* Clear the "wn_u1.index" fields, used to remember what has been
5022 * done. */
5023 spell_clear_flags(root);
5024
5025 /* Recursively print the tree. */
5026 spell_print_node(root, 0);
5027 }
5028}
5029#endif /* SPELL_PRINTTREE */
5030
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005032 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005033 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005034 */
5035 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005036spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005037 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005038 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005039{
5040 FILE *fd;
5041 afffile_T *aff;
5042 char_u rline[MAXLINELEN];
5043 char_u *line;
5044 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005045#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005046 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005047 int itemcnt;
5048 char_u *p;
5049 int lnum = 0;
5050 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005051 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005052 int aff_todo = 0;
5053 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005054 char_u *low = NULL;
5055 char_u *fol = NULL;
5056 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005057 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005058 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005059 int do_sal;
5060 int do_map;
5061 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005062 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005063 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005064 int compminlen = 0; /* COMPOUNDMIN value */
5065 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005066 int compoptions = 0; /* COMP_ flags */
5067 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005068 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005069 concatenated */
5070 char_u *midword = NULL; /* MIDWORD value */
5071 char_u *syllable = NULL; /* SYLLABLE value */
5072 char_u *sofofrom = NULL; /* SOFOFROM value */
5073 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005074
Bram Moolenaar51485f02005-06-04 21:55:20 +00005075 /*
5076 * Open the file.
5077 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005078 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005079 if (fd == NULL)
5080 {
5081 EMSG2(_(e_notopen), fname);
5082 return NULL;
5083 }
5084
Bram Moolenaar4770d092006-01-12 23:22:24 +00005085 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5086 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005088 /* Only do REP lines when not done in another .aff file already. */
5089 do_rep = spin->si_rep.ga_len == 0;
5090
Bram Moolenaar4770d092006-01-12 23:22:24 +00005091 /* Only do REPSAL lines when not done in another .aff file already. */
5092 do_repsal = spin->si_repsal.ga_len == 0;
5093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005094 /* Only do SAL lines when not done in another .aff file already. */
5095 do_sal = spin->si_sal.ga_len == 0;
5096
5097 /* Only do MAP lines when not done in another .aff file already. */
5098 do_map = spin->si_map.ga_len == 0;
5099
Bram Moolenaar51485f02005-06-04 21:55:20 +00005100 /*
5101 * Allocate and init the afffile_T structure.
5102 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005103 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005104 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005105 {
5106 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005107 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005108 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005109 hash_init(&aff->af_pref);
5110 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005111 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005112
5113 /*
5114 * Read all the lines in the file one by one.
5115 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005116 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005117 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005118 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005119 ++lnum;
5120
5121 /* Skip comment lines. */
5122 if (*rline == '#')
5123 continue;
5124
5125 /* Convert from "SET" to 'encoding' when needed. */
5126 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005127#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005128 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005129 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005130 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005131 if (pc == NULL)
5132 {
5133 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5134 fname, lnum, rline);
5135 continue;
5136 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005137 line = pc;
5138 }
5139 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005140#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141 {
5142 pc = NULL;
5143 line = rline;
5144 }
5145
5146 /* Split the line up in white separated items. Put a NUL after each
5147 * item. */
5148 itemcnt = 0;
5149 for (p = line; ; )
5150 {
5151 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5152 ++p;
5153 if (*p == NUL)
5154 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005155 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005156 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005157 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005158 /* A few items have arbitrary text argument, don't split them. */
5159 if (itemcnt == 2 && spell_info_item(items[0]))
5160 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5161 ++p;
5162 else
5163 while (*p > ' ') /* skip until white space or CR/NL */
5164 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165 if (*p == NUL)
5166 break;
5167 *p++ = NUL;
5168 }
5169
5170 /* Handle non-empty lines. */
5171 if (itemcnt > 0)
5172 {
5173 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5174 && aff->af_enc == NULL)
5175 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005176#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005177 /* Setup for conversion from "ENC" to 'encoding'. */
5178 aff->af_enc = enc_canonize(items[1]);
5179 if (aff->af_enc != NULL && !spin->si_ascii
5180 && convert_setup(&spin->si_conv, aff->af_enc,
5181 p_enc) == FAIL)
5182 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5183 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005184 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005185#else
5186 smsg((char_u *)_("Conversion in %s not supported"), fname);
5187#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005188 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005189 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5190 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005191 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005192 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005193 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005194 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005195 aff->af_flagtype = AFT_NUM;
5196 else if (STRCMP(items[1], "caplong") == 0)
5197 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005198 else
5199 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5200 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005201 if (aff->af_rare != 0
5202 || aff->af_keepcase != 0
5203 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005204 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005205 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005206 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005207 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005208 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005209 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005210 || aff->af_suff.ht_used > 0
5211 || aff->af_pref.ht_used > 0)
5212 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5213 fname, lnum, items[1]);
5214 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005215 else if (spell_info_item(items[0]))
5216 {
5217 p = (char_u *)getroom(spin,
5218 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5219 + STRLEN(items[0])
5220 + STRLEN(items[1]) + 3, FALSE);
5221 if (p != NULL)
5222 {
5223 if (spin->si_info != NULL)
5224 {
5225 STRCPY(p, spin->si_info);
5226 STRCAT(p, "\n");
5227 }
5228 STRCAT(p, items[0]);
5229 STRCAT(p, " ");
5230 STRCAT(p, items[1]);
5231 spin->si_info = p;
5232 }
5233 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005234 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5235 && midword == NULL)
5236 {
5237 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005241 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005242 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005243 /* TODO: remove "RAR" later */
5244 else if ((STRCMP(items[0], "RAR") == 0
5245 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5246 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005247 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005248 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005249 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005250 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005251 /* TODO: remove "KEP" later */
5252 else if ((STRCMP(items[0], "KEP") == 0
5253 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5254 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005255 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005256 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005257 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005258 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005259 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5260 && aff->af_bad == 0)
5261 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005262 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5263 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005264 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005265 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5266 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005267 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005268 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5269 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005270 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005271 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5272 && aff->af_circumfix == 0)
5273 {
5274 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5275 fname, lnum);
5276 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005277 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5278 && aff->af_nosuggest == 0)
5279 {
5280 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5281 fname, lnum);
5282 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005283 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5284 && aff->af_needcomp == 0)
5285 {
5286 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5287 fname, lnum);
5288 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005289 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5290 && aff->af_comproot == 0)
5291 {
5292 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5293 fname, lnum);
5294 }
5295 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5296 && itemcnt == 2 && aff->af_compforbid == 0)
5297 {
5298 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5299 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005300 if (aff->af_pref.ht_used > 0)
5301 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5302 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005303 }
5304 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5305 && itemcnt == 2 && aff->af_comppermit == 0)
5306 {
5307 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5308 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005309 if (aff->af_pref.ht_used > 0)
5310 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5311 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005312 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005313 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005314 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005315 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005316 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005317 * "Na" into "Na+", "1234" into "1234+". */
5318 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005319 if (p != NULL)
5320 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005321 STRCPY(p, items[1]);
5322 STRCAT(p, "+");
5323 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005324 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005325 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005326 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005327 {
5328 /* Concatenate this string to previously defined ones, using a
5329 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005330 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005331 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005332 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005333 p = getroom(spin, l, FALSE);
5334 if (p != NULL)
5335 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005336 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005337 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005338 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005339 STRCAT(p, "/");
5340 }
5341 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005342 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005343 }
5344 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005345 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005346 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005347 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005348 compmax = atoi((char *)items[1]);
5349 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005350 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005351 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005352 }
5353 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005354 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005355 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005356 compminlen = atoi((char *)items[1]);
5357 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005358 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5359 fname, lnum, items[1]);
5360 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005361 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005362 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005363 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005364 compsylmax = atoi((char *)items[1]);
5365 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005366 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5367 fname, lnum, items[1]);
5368 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005369 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5370 {
5371 compoptions |= COMP_CHECKDUP;
5372 }
5373 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5374 {
5375 compoptions |= COMP_CHECKREP;
5376 }
5377 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5378 {
5379 compoptions |= COMP_CHECKCASE;
5380 }
5381 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5382 && itemcnt == 1)
5383 {
5384 compoptions |= COMP_CHECKTRIPLE;
5385 }
5386 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5387 && itemcnt == 2)
5388 {
5389 if (atoi((char *)items[1]) == 0)
5390 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5391 fname, lnum, items[1]);
5392 }
5393 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5394 && itemcnt == 3)
5395 {
5396 garray_T *gap = &spin->si_comppat;
5397 int i;
5398
5399 /* Only add the couple if it isn't already there. */
5400 for (i = 0; i < gap->ga_len - 1; i += 2)
5401 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5402 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5403 items[2]) == 0)
5404 break;
5405 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5406 {
5407 ((char_u **)(gap->ga_data))[gap->ga_len++]
5408 = getroom_save(spin, items[1]);
5409 ((char_u **)(gap->ga_data))[gap->ga_len++]
5410 = getroom_save(spin, items[2]);
5411 }
5412 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005413 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005414 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005415 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005416 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005417 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005418 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5419 {
5420 spin->si_nobreak = TRUE;
5421 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005422 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5423 {
5424 spin->si_nosplitsugs = TRUE;
5425 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005426 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5427 {
5428 spin->si_nosugfile = TRUE;
5429 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005430 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5431 {
5432 aff->af_pfxpostpone = TRUE;
5433 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005434 else if ((STRCMP(items[0], "PFX") == 0
5435 || STRCMP(items[0], "SFX") == 0)
5436 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005437 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005438 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005439 int lasti = 4;
5440 char_u key[AH_KEY_LEN];
5441
5442 if (*items[0] == 'P')
5443 tp = &aff->af_pref;
5444 else
5445 tp = &aff->af_suff;
5446
5447 /* Myspell allows the same affix name to be used multiple
5448 * times. The affix files that do this have an undocumented
5449 * "S" flag on all but the last block, thus we check for that
5450 * and store it in ah_follows. */
5451 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5452 hi = hash_find(tp, key);
5453 if (!HASHITEM_EMPTY(hi))
5454 {
5455 cur_aff = HI2AH(hi);
5456 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5457 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5458 fname, lnum, items[1]);
5459 if (!cur_aff->ah_follows)
5460 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5461 fname, lnum, items[1]);
5462 }
5463 else
5464 {
5465 /* New affix letter. */
5466 cur_aff = (affheader_T *)getroom(spin,
5467 sizeof(affheader_T), TRUE);
5468 if (cur_aff == NULL)
5469 break;
5470 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5471 fname, lnum);
5472 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5473 break;
5474 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005475 || cur_aff->ah_flag == aff->af_rare
5476 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005477 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005478 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005479 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005480 || cur_aff->ah_flag == aff->af_needcomp
5481 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005482 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 +00005483 fname, lnum, items[1]);
5484 STRCPY(cur_aff->ah_key, items[1]);
5485 hash_add(tp, cur_aff->ah_key);
5486
5487 cur_aff->ah_combine = (*items[2] == 'Y');
5488 }
5489
5490 /* Check for the "S" flag, which apparently means that another
5491 * block with the same affix name is following. */
5492 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5493 {
5494 ++lasti;
5495 cur_aff->ah_follows = TRUE;
5496 }
5497 else
5498 cur_aff->ah_follows = FALSE;
5499
Bram Moolenaar8db73182005-06-17 21:51:16 +00005500 /* Myspell allows extra text after the item, but that might
5501 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005502 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005503 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005504
Bram Moolenaar95529562005-08-25 21:21:38 +00005505 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005506 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5507 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005508
Bram Moolenaar95529562005-08-25 21:21:38 +00005509 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005510 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005511 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005512 {
5513 /* Use a new number in the .spl file later, to be able
5514 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005515 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005516 cur_aff->ah_newID = ++spin->si_newprefID;
5517
5518 /* We only really use ah_newID if the prefix is
5519 * postponed. We know that only after handling all
5520 * the items. */
5521 did_postpone_prefix = FALSE;
5522 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005523 else
5524 /* Did use the ID in a previous block. */
5525 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005526 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005527
Bram Moolenaar51485f02005-06-04 21:55:20 +00005528 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005529 }
5530 else if ((STRCMP(items[0], "PFX") == 0
5531 || STRCMP(items[0], "SFX") == 0)
5532 && aff_todo > 0
5533 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005534 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005535 {
5536 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005537 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005538 int lasti = 5;
5539
Bram Moolenaar8db73182005-06-17 21:51:16 +00005540 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005541 * mean mistakes go unnoticed. Require a comment-starter.
5542 * Hunspell uses a "-" item. */
5543 if (itemcnt > lasti && *items[lasti] != '#'
5544 && (STRCMP(items[lasti], "-") != 0
5545 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005546 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005547
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005548 /* New item for an affix letter. */
5549 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005550 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005551 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005552 if (aff_entry == NULL)
5553 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005554
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005555 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005556 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005557 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005558 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005559 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005560
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005561 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005562 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5563 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005564 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005565 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005566 aff_process_flags(aff, aff_entry);
5567 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005568 }
5569
Bram Moolenaar51485f02005-06-04 21:55:20 +00005570 /* Don't use an affix entry with non-ASCII characters when
5571 * "spin->si_ascii" is TRUE. */
5572 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005573 || has_non_ascii(aff_entry->ae_add)))
5574 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005575 aff_entry->ae_next = cur_aff->ah_first;
5576 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005577
5578 if (STRCMP(items[4], ".") != 0)
5579 {
5580 char_u buf[MAXLINELEN];
5581
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005582 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005583 if (*items[0] == 'P')
5584 sprintf((char *)buf, "^%s", items[4]);
5585 else
5586 sprintf((char *)buf, "%s$", items[4]);
5587 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005588 RE_MAGIC + RE_STRING + RE_STRICT);
5589 if (aff_entry->ae_prog == NULL)
5590 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5591 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005592 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005593
5594 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005595 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005596 * Can't be done for an affix with flags, ignoring
5597 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005598 if (*items[0] == 'P' && aff->af_pfxpostpone
5599 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005600 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005601 /* When the chop string is one lower-case letter and
5602 * the add string ends in the upper-case letter we set
5603 * the "upper" flag, clear "ae_chop" and remove the
5604 * letters from "ae_add". The condition must either
5605 * be empty or start with the same letter. */
5606 if (aff_entry->ae_chop != NULL
5607 && aff_entry->ae_add != NULL
5608#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005609 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005610 aff_entry->ae_chop)] == NUL
5611#else
5612 && aff_entry->ae_chop[1] == NUL
5613#endif
5614 )
5615 {
5616 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005617
Bram Moolenaar53805d12005-08-01 07:08:33 +00005618 c = PTR2CHAR(aff_entry->ae_chop);
5619 c_up = SPELL_TOUPPER(c);
5620 if (c_up != c
5621 && (aff_entry->ae_cond == NULL
5622 || PTR2CHAR(aff_entry->ae_cond) == c))
5623 {
5624 p = aff_entry->ae_add
5625 + STRLEN(aff_entry->ae_add);
5626 mb_ptr_back(aff_entry->ae_add, p);
5627 if (PTR2CHAR(p) == c_up)
5628 {
5629 upper = TRUE;
5630 aff_entry->ae_chop = NULL;
5631 *p = NUL;
5632
5633 /* The condition is matched with the
5634 * actual word, thus must check for the
5635 * upper-case letter. */
5636 if (aff_entry->ae_cond != NULL)
5637 {
5638 char_u buf[MAXLINELEN];
5639#ifdef FEAT_MBYTE
5640 if (has_mbyte)
5641 {
5642 onecap_copy(items[4], buf, TRUE);
5643 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005644 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005645 }
5646 else
5647#endif
5648 *aff_entry->ae_cond = c_up;
5649 if (aff_entry->ae_cond != NULL)
5650 {
5651 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005652 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005653 vim_free(aff_entry->ae_prog);
5654 aff_entry->ae_prog = vim_regcomp(
5655 buf, RE_MAGIC + RE_STRING);
5656 }
5657 }
5658 }
5659 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005660 }
5661
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005662 if (aff_entry->ae_chop == NULL
5663 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005664 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005665 int idx;
5666 char_u **pp;
5667 int n;
5668
Bram Moolenaar6de68532005-08-24 22:08:48 +00005669 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005670 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5671 --idx)
5672 {
5673 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5674 if (str_equal(p, aff_entry->ae_cond))
5675 break;
5676 }
5677 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5678 {
5679 /* Not found, add a new condition. */
5680 idx = spin->si_prefcond.ga_len++;
5681 pp = ((char_u **)spin->si_prefcond.ga_data)
5682 + idx;
5683 if (aff_entry->ae_cond == NULL)
5684 *pp = NULL;
5685 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005686 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005687 aff_entry->ae_cond);
5688 }
5689
5690 /* Add the prefix to the prefix tree. */
5691 if (aff_entry->ae_add == NULL)
5692 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005693 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005694 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005695
Bram Moolenaar53805d12005-08-01 07:08:33 +00005696 /* PFX_FLAGS is a negative number, so that
5697 * tree_add_word() knows this is the prefix tree. */
5698 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005699 if (!cur_aff->ah_combine)
5700 n |= WFP_NC;
5701 if (upper)
5702 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005703 if (aff_entry->ae_comppermit)
5704 n |= WFP_COMPPERMIT;
5705 if (aff_entry->ae_compforbid)
5706 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005707 tree_add_word(spin, p, spin->si_prefroot, n,
5708 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005709 did_postpone_prefix = TRUE;
5710 }
5711
5712 /* Didn't actually use ah_newID, backup si_newprefID. */
5713 if (aff_todo == 0 && !did_postpone_prefix)
5714 {
5715 --spin->si_newprefID;
5716 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005717 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005718 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005719 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005720 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005721 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5722 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005723 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005724 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005725 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005726 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5727 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005728 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005729 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005730 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005731 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5732 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005733 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005734 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005735 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005736 else if ((STRCMP(items[0], "REP") == 0
5737 || STRCMP(items[0], "REPSAL") == 0)
5738 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005739 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005740 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005741 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005742 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005743 fname, lnum);
5744 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005745 else if ((STRCMP(items[0], "REP") == 0
5746 || STRCMP(items[0], "REPSAL") == 0)
5747 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005748 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005749 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005750 /* Myspell ignores extra arguments, we require it starts with
5751 * # to detect mistakes. */
5752 if (itemcnt > 3 && items[3][0] != '#')
5753 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005754 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005755 {
5756 /* Replace underscore with space (can't include a space
5757 * directly). */
5758 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5759 if (*p == '_')
5760 *p = ' ';
5761 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5762 if (*p == '_')
5763 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005764 add_fromto(spin, items[0][3] == 'S'
5765 ? &spin->si_repsal
5766 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005767 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005768 }
5769 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5770 {
5771 /* MAP item or count */
5772 if (!found_map)
5773 {
5774 /* First line contains the count. */
5775 found_map = TRUE;
5776 if (!isdigit(*items[1]))
5777 smsg((char_u *)_("Expected MAP count in %s line %d"),
5778 fname, lnum);
5779 }
5780 else if (do_map)
5781 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005782 int c;
5783
5784 /* Check that every character appears only once. */
5785 for (p = items[1]; *p != NUL; )
5786 {
5787#ifdef FEAT_MBYTE
5788 c = mb_ptr2char_adv(&p);
5789#else
5790 c = *p++;
5791#endif
5792 if ((spin->si_map.ga_len > 0
5793 && vim_strchr(spin->si_map.ga_data, c)
5794 != NULL)
5795 || vim_strchr(p, c) != NULL)
5796 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5797 fname, lnum);
5798 }
5799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005800 /* We simply concatenate all the MAP strings, separated by
5801 * slashes. */
5802 ga_concat(&spin->si_map, items[1]);
5803 ga_append(&spin->si_map, '/');
5804 }
5805 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005806 /* Accept "SAL from to" and "SAL from to # comment". */
5807 else if (STRCMP(items[0], "SAL") == 0
5808 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005809 {
5810 if (do_sal)
5811 {
5812 /* SAL item (sounds-a-like)
5813 * Either one of the known keys or a from-to pair. */
5814 if (STRCMP(items[1], "followup") == 0)
5815 spin->si_followup = sal_to_bool(items[2]);
5816 else if (STRCMP(items[1], "collapse_result") == 0)
5817 spin->si_collapse = sal_to_bool(items[2]);
5818 else if (STRCMP(items[1], "remove_accents") == 0)
5819 spin->si_rem_accents = sal_to_bool(items[2]);
5820 else
5821 /* when "to" is "_" it means empty */
5822 add_fromto(spin, &spin->si_sal, items[1],
5823 STRCMP(items[2], "_") == 0 ? (char_u *)""
5824 : items[2]);
5825 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005826 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005827 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005828 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005829 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005830 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005831 }
5832 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005833 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005834 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005835 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005836 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005837 else if (STRCMP(items[0], "COMMON") == 0)
5838 {
5839 int i;
5840
5841 for (i = 1; i < itemcnt; ++i)
5842 {
5843 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5844 items[i])))
5845 {
5846 p = vim_strsave(items[i]);
5847 if (p == NULL)
5848 break;
5849 hash_add(&spin->si_commonwords, p);
5850 }
5851 }
5852 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005853 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005854 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005855 fname, lnum, items[0]);
5856 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005857 }
5858
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005859 if (fol != NULL || low != NULL || upp != NULL)
5860 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005861 if (spin->si_clear_chartab)
5862 {
5863 /* Clear the char type tables, don't want to use any of the
5864 * currently used spell properties. */
5865 init_spell_chartab();
5866 spin->si_clear_chartab = FALSE;
5867 }
5868
Bram Moolenaar3982c542005-06-08 21:56:31 +00005869 /*
5870 * Don't write a word table for an ASCII file, so that we don't check
5871 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005872 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005873 * mb_get_class(), the list of chars in the file will be incomplete.
5874 */
5875 if (!spin->si_ascii
5876#ifdef FEAT_MBYTE
5877 && !enc_utf8
5878#endif
5879 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005880 {
5881 if (fol == NULL || low == NULL || upp == NULL)
5882 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5883 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005884 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005885 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005886
5887 vim_free(fol);
5888 vim_free(low);
5889 vim_free(upp);
5890 }
5891
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005892 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005893 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005894 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005895 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005896 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005897 }
5898
Bram Moolenaar6de68532005-08-24 22:08:48 +00005899 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005900 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005901 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5902 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005903 }
5904
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005906 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005907 if (syllable == NULL)
5908 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5909 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5910 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005911 }
5912
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005913 if (compoptions != 0)
5914 {
5915 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5916 spin->si_compoptions |= compoptions;
5917 }
5918
Bram Moolenaar6de68532005-08-24 22:08:48 +00005919 if (compflags != NULL)
5920 process_compflags(spin, aff, compflags);
5921
5922 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005923 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005924 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005925 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005926 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005927 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005928 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005929 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005930 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005931 }
5932
Bram Moolenaar6de68532005-08-24 22:08:48 +00005933 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005934 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005935 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5936 spin->si_syllable = syllable;
5937 }
5938
5939 if (sofofrom != NULL || sofoto != NULL)
5940 {
5941 if (sofofrom == NULL || sofoto == NULL)
5942 smsg((char_u *)_("Missing SOFO%s line in %s"),
5943 sofofrom == NULL ? "FROM" : "TO", fname);
5944 else if (spin->si_sal.ga_len > 0)
5945 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005946 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005947 {
5948 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5949 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5950 spin->si_sofofr = sofofrom;
5951 spin->si_sofoto = sofoto;
5952 }
5953 }
5954
5955 if (midword != NULL)
5956 {
5957 aff_check_string(spin->si_midword, midword, "MIDWORD");
5958 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005959 }
5960
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005961 vim_free(pc);
5962 fclose(fd);
5963 return aff;
5964}
5965
5966/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005967 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
5968 * ae_flags to ae_comppermit and ae_compforbid.
5969 */
5970 static void
5971aff_process_flags(affile, entry)
5972 afffile_T *affile;
5973 affentry_T *entry;
5974{
5975 char_u *p;
5976 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005977 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005978
5979 if (entry->ae_flags != NULL
5980 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
5981 {
5982 for (p = entry->ae_flags; *p != NUL; )
5983 {
5984 prevp = p;
5985 flag = get_affitem(affile->af_flagtype, &p);
5986 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
5987 {
5988 mch_memmove(prevp, p, STRLEN(p) + 1);
5989 p = prevp;
5990 if (flag == affile->af_comppermit)
5991 entry->ae_comppermit = TRUE;
5992 else
5993 entry->ae_compforbid = TRUE;
5994 }
5995 if (affile->af_flagtype == AFT_NUM && *p == ',')
5996 ++p;
5997 }
5998 if (*entry->ae_flags == NUL)
5999 entry->ae_flags = NULL; /* nothing left */
6000 }
6001}
6002
6003/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006004 * Return TRUE if "s" is the name of an info item in the affix file.
6005 */
6006 static int
6007spell_info_item(s)
6008 char_u *s;
6009{
6010 return STRCMP(s, "NAME") == 0
6011 || STRCMP(s, "HOME") == 0
6012 || STRCMP(s, "VERSION") == 0
6013 || STRCMP(s, "AUTHOR") == 0
6014 || STRCMP(s, "EMAIL") == 0
6015 || STRCMP(s, "COPYRIGHT") == 0;
6016}
6017
6018/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006019 * Turn an affix flag name into a number, according to the FLAG type.
6020 * returns zero for failure.
6021 */
6022 static unsigned
6023affitem2flag(flagtype, item, fname, lnum)
6024 int flagtype;
6025 char_u *item;
6026 char_u *fname;
6027 int lnum;
6028{
6029 unsigned res;
6030 char_u *p = item;
6031
6032 res = get_affitem(flagtype, &p);
6033 if (res == 0)
6034 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006035 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006036 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6037 fname, lnum, item);
6038 else
6039 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6040 fname, lnum, item);
6041 }
6042 if (*p != NUL)
6043 {
6044 smsg((char_u *)_(e_affname), fname, lnum, item);
6045 return 0;
6046 }
6047
6048 return res;
6049}
6050
6051/*
6052 * Get one affix name from "*pp" and advance the pointer.
6053 * Returns zero for an error, still advances the pointer then.
6054 */
6055 static unsigned
6056get_affitem(flagtype, pp)
6057 int flagtype;
6058 char_u **pp;
6059{
6060 int res;
6061
Bram Moolenaar95529562005-08-25 21:21:38 +00006062 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006063 {
6064 if (!VIM_ISDIGIT(**pp))
6065 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006066 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006067 return 0;
6068 }
6069 res = getdigits(pp);
6070 }
6071 else
6072 {
6073#ifdef FEAT_MBYTE
6074 res = mb_ptr2char_adv(pp);
6075#else
6076 res = *(*pp)++;
6077#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006078 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006079 && res >= 'A' && res <= 'Z'))
6080 {
6081 if (**pp == NUL)
6082 return 0;
6083#ifdef FEAT_MBYTE
6084 res = mb_ptr2char_adv(pp) + (res << 16);
6085#else
6086 res = *(*pp)++ + (res << 16);
6087#endif
6088 }
6089 }
6090 return res;
6091}
6092
6093/*
6094 * Process the "compflags" string used in an affix file and append it to
6095 * spin->si_compflags.
6096 * The processing involves changing the affix names to ID numbers, so that
6097 * they fit in one byte.
6098 */
6099 static void
6100process_compflags(spin, aff, compflags)
6101 spellinfo_T *spin;
6102 afffile_T *aff;
6103 char_u *compflags;
6104{
6105 char_u *p;
6106 char_u *prevp;
6107 unsigned flag;
6108 compitem_T *ci;
6109 int id;
6110 int len;
6111 char_u *tp;
6112 char_u key[AH_KEY_LEN];
6113 hashitem_T *hi;
6114
6115 /* Make room for the old and the new compflags, concatenated with a / in
6116 * between. Processing it makes it shorter, but we don't know by how
6117 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006118 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006119 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006120 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006121 p = getroom(spin, len, FALSE);
6122 if (p == NULL)
6123 return;
6124 if (spin->si_compflags != NULL)
6125 {
6126 STRCPY(p, spin->si_compflags);
6127 STRCAT(p, "/");
6128 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006129 spin->si_compflags = p;
6130 tp = p + STRLEN(p);
6131
6132 for (p = compflags; *p != NUL; )
6133 {
6134 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6135 /* Copy non-flag characters directly. */
6136 *tp++ = *p++;
6137 else
6138 {
6139 /* First get the flag number, also checks validity. */
6140 prevp = p;
6141 flag = get_affitem(aff->af_flagtype, &p);
6142 if (flag != 0)
6143 {
6144 /* Find the flag in the hashtable. If it was used before, use
6145 * the existing ID. Otherwise add a new entry. */
6146 vim_strncpy(key, prevp, p - prevp);
6147 hi = hash_find(&aff->af_comp, key);
6148 if (!HASHITEM_EMPTY(hi))
6149 id = HI2CI(hi)->ci_newID;
6150 else
6151 {
6152 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6153 if (ci == NULL)
6154 break;
6155 STRCPY(ci->ci_key, key);
6156 ci->ci_flag = flag;
6157 /* Avoid using a flag ID that has a special meaning in a
6158 * regexp (also inside []). */
6159 do
6160 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006161 check_renumber(spin);
6162 id = spin->si_newcompID--;
6163 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006164 ci->ci_newID = id;
6165 hash_add(&aff->af_comp, ci->ci_key);
6166 }
6167 *tp++ = id;
6168 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006169 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006170 ++p;
6171 }
6172 }
6173
6174 *tp = NUL;
6175}
6176
6177/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006178 * Check that the new IDs for postponed affixes and compounding don't overrun
6179 * each other. We have almost 255 available, but start at 0-127 to avoid
6180 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6181 * When that is used up an error message is given.
6182 */
6183 static void
6184check_renumber(spin)
6185 spellinfo_T *spin;
6186{
6187 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6188 {
6189 spin->si_newprefID = 127;
6190 spin->si_newcompID = 255;
6191 }
6192}
6193
6194/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006195 * Return TRUE if flag "flag" appears in affix list "afflist".
6196 */
6197 static int
6198flag_in_afflist(flagtype, afflist, flag)
6199 int flagtype;
6200 char_u *afflist;
6201 unsigned flag;
6202{
6203 char_u *p;
6204 unsigned n;
6205
6206 switch (flagtype)
6207 {
6208 case AFT_CHAR:
6209 return vim_strchr(afflist, flag) != NULL;
6210
Bram Moolenaar95529562005-08-25 21:21:38 +00006211 case AFT_CAPLONG:
6212 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006213 for (p = afflist; *p != NUL; )
6214 {
6215#ifdef FEAT_MBYTE
6216 n = mb_ptr2char_adv(&p);
6217#else
6218 n = *p++;
6219#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006220 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006221 && *p != NUL)
6222#ifdef FEAT_MBYTE
6223 n = mb_ptr2char_adv(&p) + (n << 16);
6224#else
6225 n = *p++ + (n << 16);
6226#endif
6227 if (n == flag)
6228 return TRUE;
6229 }
6230 break;
6231
Bram Moolenaar95529562005-08-25 21:21:38 +00006232 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006233 for (p = afflist; *p != NUL; )
6234 {
6235 n = getdigits(&p);
6236 if (n == flag)
6237 return TRUE;
6238 if (*p != NUL) /* skip over comma */
6239 ++p;
6240 }
6241 break;
6242 }
6243 return FALSE;
6244}
6245
6246/*
6247 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6248 */
6249 static void
6250aff_check_number(spinval, affval, name)
6251 int spinval;
6252 int affval;
6253 char *name;
6254{
6255 if (spinval != 0 && spinval != affval)
6256 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6257}
6258
6259/*
6260 * Give a warning when "spinval" and "affval" strings are set and not the same.
6261 */
6262 static void
6263aff_check_string(spinval, affval, name)
6264 char_u *spinval;
6265 char_u *affval;
6266 char *name;
6267{
6268 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6269 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6270}
6271
6272/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006273 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6274 * NULL as equal.
6275 */
6276 static int
6277str_equal(s1, s2)
6278 char_u *s1;
6279 char_u *s2;
6280{
6281 if (s1 == NULL || s2 == NULL)
6282 return s1 == s2;
6283 return STRCMP(s1, s2) == 0;
6284}
6285
6286/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006287 * Add a from-to item to "gap". Used for REP and SAL items.
6288 * They are stored case-folded.
6289 */
6290 static void
6291add_fromto(spin, gap, from, to)
6292 spellinfo_T *spin;
6293 garray_T *gap;
6294 char_u *from;
6295 char_u *to;
6296{
6297 fromto_T *ftp;
6298 char_u word[MAXWLEN];
6299
6300 if (ga_grow(gap, 1) == OK)
6301 {
6302 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006303 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006304 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006305 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006306 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006307 ++gap->ga_len;
6308 }
6309}
6310
6311/*
6312 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6313 */
6314 static int
6315sal_to_bool(s)
6316 char_u *s;
6317{
6318 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6319}
6320
6321/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006322 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6323 * When "s" is NULL FALSE is returned.
6324 */
6325 static int
6326has_non_ascii(s)
6327 char_u *s;
6328{
6329 char_u *p;
6330
6331 if (s != NULL)
6332 for (p = s; *p != NUL; ++p)
6333 if (*p >= 128)
6334 return TRUE;
6335 return FALSE;
6336}
6337
6338/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006339 * Free the structure filled by spell_read_aff().
6340 */
6341 static void
6342spell_free_aff(aff)
6343 afffile_T *aff;
6344{
6345 hashtab_T *ht;
6346 hashitem_T *hi;
6347 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006348 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006349 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006350
6351 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006352
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006353 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006354 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6355 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006356 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006357 for (hi = ht->ht_array; todo > 0; ++hi)
6358 {
6359 if (!HASHITEM_EMPTY(hi))
6360 {
6361 --todo;
6362 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006363 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6364 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006365 }
6366 }
6367 if (ht == &aff->af_suff)
6368 break;
6369 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006370
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006371 hash_clear(&aff->af_pref);
6372 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006373 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006374}
6375
6376/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006377 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006378 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006379 */
6380 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006381spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006382 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006383 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006384 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006385{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006386 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006387 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006388 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006389 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006390 char_u store_afflist[MAXWLEN];
6391 int pfxlen;
6392 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006393 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006394 char_u *pc;
6395 char_u *w;
6396 int l;
6397 hash_T hash;
6398 hashitem_T *hi;
6399 FILE *fd;
6400 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006401 int non_ascii = 0;
6402 int retval = OK;
6403 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006404 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006405 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006406
Bram Moolenaar51485f02005-06-04 21:55:20 +00006407 /*
6408 * Open the file.
6409 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006410 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006411 if (fd == NULL)
6412 {
6413 EMSG2(_(e_notopen), fname);
6414 return FAIL;
6415 }
6416
Bram Moolenaar51485f02005-06-04 21:55:20 +00006417 /* The hashtable is only used to detect duplicated words. */
6418 hash_init(&ht);
6419
Bram Moolenaar4770d092006-01-12 23:22:24 +00006420 vim_snprintf((char *)IObuff, IOSIZE,
6421 _("Reading dictionary file %s ..."), fname);
6422 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006423
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006424 /* start with a message for the first line */
6425 spin->si_msg_count = 999999;
6426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006427 /* Read and ignore the first line: word count. */
6428 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006429 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006430 EMSG2(_("E760: No word count in %s"), fname);
6431
6432 /*
6433 * Read all the lines in the file one by one.
6434 * The words are converted to 'encoding' here, before being added to
6435 * the hashtable.
6436 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006437 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006438 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006439 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006440 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006441 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006442 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006443
Bram Moolenaar51485f02005-06-04 21:55:20 +00006444 /* Remove CR, LF and white space from the end. White space halfway
6445 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006446 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006447 while (l > 0 && line[l - 1] <= ' ')
6448 --l;
6449 if (l == 0)
6450 continue; /* empty line */
6451 line[l] = NUL;
6452
Bram Moolenaarb765d632005-06-07 21:00:02 +00006453#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006454 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006455 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006456 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006457 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006458 if (pc == NULL)
6459 {
6460 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6461 fname, lnum, line);
6462 continue;
6463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006464 w = pc;
6465 }
6466 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006467#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006468 {
6469 pc = NULL;
6470 w = line;
6471 }
6472
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006473 /* Truncate the word at the "/", set "afflist" to what follows.
6474 * Replace "\/" by "/" and "\\" by "\". */
6475 afflist = NULL;
6476 for (p = w; *p != NUL; mb_ptr_adv(p))
6477 {
6478 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6479 mch_memmove(p, p + 1, STRLEN(p));
6480 else if (*p == '/')
6481 {
6482 *p = NUL;
6483 afflist = p + 1;
6484 break;
6485 }
6486 }
6487
6488 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6489 if (spin->si_ascii && has_non_ascii(w))
6490 {
6491 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006492 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006493 continue;
6494 }
6495
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006496 /* This takes time, print a message every 10000 words. */
6497 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006498 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006499 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006500 vim_snprintf((char *)message, sizeof(message),
6501 _("line %6d, word %6d - %s"),
6502 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6503 msg_start();
6504 msg_puts_long_attr(message, 0);
6505 msg_clr_eos();
6506 msg_didout = FALSE;
6507 msg_col = 0;
6508 out_flush();
6509 }
6510
Bram Moolenaar51485f02005-06-04 21:55:20 +00006511 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006512 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006513 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006514 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006515 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006516 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006517 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006518 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006519
Bram Moolenaar51485f02005-06-04 21:55:20 +00006520 hash = hash_hash(dw);
6521 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006522 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006523 {
6524 if (p_verbose > 0)
6525 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006526 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006527 else if (duplicate == 0)
6528 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6529 fname, lnum, dw);
6530 ++duplicate;
6531 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006532 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006533 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006534
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006535 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006536 store_afflist[0] = NUL;
6537 pfxlen = 0;
6538 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006539 if (afflist != NULL)
6540 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006541 /* Extract flags from the affix list. */
6542 flags |= get_affix_flags(affile, afflist);
6543
Bram Moolenaar6de68532005-08-24 22:08:48 +00006544 if (affile->af_needaffix != 0 && flag_in_afflist(
6545 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006546 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006547
6548 if (affile->af_pfxpostpone)
6549 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006550 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006551
Bram Moolenaar5195e452005-08-19 20:32:47 +00006552 if (spin->si_compflags != NULL)
6553 /* Need to store the list of compound flags with the word.
6554 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006555 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006556 }
6557
Bram Moolenaar51485f02005-06-04 21:55:20 +00006558 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006559 if (store_word(spin, dw, flags, spin->si_region,
6560 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 retval = FAIL;
6562
6563 if (afflist != NULL)
6564 {
6565 /* Find all matching suffixes and add the resulting words.
6566 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006567 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006568 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006569 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006570 retval = FAIL;
6571
6572 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006573 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006574 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006575 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006576 retval = FAIL;
6577 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006578
6579 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006580 }
6581
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006582 if (duplicate > 0)
6583 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006584 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006585 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6586 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006587 hash_clear(&ht);
6588
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006589 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006590 return retval;
6591}
6592
6593/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006594 * Check for affix flags in "afflist" that are turned into word flags.
6595 * Return WF_ flags.
6596 */
6597 static int
6598get_affix_flags(affile, afflist)
6599 afffile_T *affile;
6600 char_u *afflist;
6601{
6602 int flags = 0;
6603
6604 if (affile->af_keepcase != 0 && flag_in_afflist(
6605 affile->af_flagtype, afflist, affile->af_keepcase))
6606 flags |= WF_KEEPCAP | WF_FIXCAP;
6607 if (affile->af_rare != 0 && flag_in_afflist(
6608 affile->af_flagtype, afflist, affile->af_rare))
6609 flags |= WF_RARE;
6610 if (affile->af_bad != 0 && flag_in_afflist(
6611 affile->af_flagtype, afflist, affile->af_bad))
6612 flags |= WF_BANNED;
6613 if (affile->af_needcomp != 0 && flag_in_afflist(
6614 affile->af_flagtype, afflist, affile->af_needcomp))
6615 flags |= WF_NEEDCOMP;
6616 if (affile->af_comproot != 0 && flag_in_afflist(
6617 affile->af_flagtype, afflist, affile->af_comproot))
6618 flags |= WF_COMPROOT;
6619 if (affile->af_nosuggest != 0 && flag_in_afflist(
6620 affile->af_flagtype, afflist, affile->af_nosuggest))
6621 flags |= WF_NOSUGGEST;
6622 return flags;
6623}
6624
6625/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006626 * Get the list of prefix IDs from the affix list "afflist".
6627 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006628 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6629 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006630 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006631 static int
6632get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006633 afffile_T *affile;
6634 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006635 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006636{
6637 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006638 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006639 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006640 int id;
6641 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006642 hashitem_T *hi;
6643
Bram Moolenaar6de68532005-08-24 22:08:48 +00006644 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006645 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006646 prevp = p;
6647 if (get_affitem(affile->af_flagtype, &p) != 0)
6648 {
6649 /* A flag is a postponed prefix flag if it appears in "af_pref"
6650 * and it's ID is not zero. */
6651 vim_strncpy(key, prevp, p - prevp);
6652 hi = hash_find(&affile->af_pref, key);
6653 if (!HASHITEM_EMPTY(hi))
6654 {
6655 id = HI2AH(hi)->ah_newID;
6656 if (id != 0)
6657 store_afflist[cnt++] = id;
6658 }
6659 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006660 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006661 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006662 }
6663
Bram Moolenaar5195e452005-08-19 20:32:47 +00006664 store_afflist[cnt] = NUL;
6665 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006666}
6667
6668/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006669 * Get the list of compound IDs from the affix list "afflist" that are used
6670 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006671 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006672 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006673 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006674get_compflags(affile, afflist, store_afflist)
6675 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006676 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006677 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006678{
6679 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006680 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006681 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006682 char_u key[AH_KEY_LEN];
6683 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006684
Bram Moolenaar6de68532005-08-24 22:08:48 +00006685 for (p = afflist; *p != NUL; )
6686 {
6687 prevp = p;
6688 if (get_affitem(affile->af_flagtype, &p) != 0)
6689 {
6690 /* A flag is a compound flag if it appears in "af_comp". */
6691 vim_strncpy(key, prevp, p - prevp);
6692 hi = hash_find(&affile->af_comp, key);
6693 if (!HASHITEM_EMPTY(hi))
6694 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6695 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006696 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006697 ++p;
6698 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006699
Bram Moolenaar5195e452005-08-19 20:32:47 +00006700 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006701}
6702
6703/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006704 * Apply affixes to a word and store the resulting words.
6705 * "ht" is the hashtable with affentry_T that need to be applied, either
6706 * prefixes or suffixes.
6707 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6708 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006709 *
6710 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006711 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006712 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006713store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006714 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006716 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006717 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006718 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006719 hashtab_T *ht;
6720 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006721 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006722 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006723 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006724 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6725 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006726{
6727 int todo;
6728 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006729 affheader_T *ah;
6730 affentry_T *ae;
6731 regmatch_T regmatch;
6732 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006733 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006734 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006735 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006736 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006737 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006738 int use_pfxlen;
6739 int need_affix;
6740 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006741 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006742 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006743 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006744
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006745 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006746 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006747 {
6748 if (!HASHITEM_EMPTY(hi))
6749 {
6750 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006751 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006752
Bram Moolenaar51485f02005-06-04 21:55:20 +00006753 /* Check that the affix combines, if required, and that the word
6754 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006755 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6756 && flag_in_afflist(affile->af_flagtype, afflist,
6757 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006758 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006759 /* Loop over all affix entries with this name. */
6760 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006761 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006762 /* Check the condition. It's not logical to match case
6763 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006764 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006765 * Another requirement from Myspell is that the chop
6766 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006767 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006768 * prefixes with a chop string and/or flags.
6769 * When a previously added affix had CIRCUMFIX this one
6770 * must have it too, if it had not then this one must not
6771 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006772 regmatch.regprog = ae->ae_prog;
6773 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006774 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006775 || ae->ae_chop != NULL
6776 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006777 && (ae->ae_chop == NULL
6778 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006779 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006780 || vim_regexec(&regmatch, word, (colnr_T)0))
6781 && (((condit & CONDIT_CFIX) == 0)
6782 == ((condit & CONDIT_AFF) == 0
6783 || ae->ae_flags == NULL
6784 || !flag_in_afflist(affile->af_flagtype,
6785 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006786 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006787 /* Match. Remove the chop and add the affix. */
6788 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006790 /* prefix: chop/add at the start of the word */
6791 if (ae->ae_add == NULL)
6792 *newword = NUL;
6793 else
6794 STRCPY(newword, ae->ae_add);
6795 p = word;
6796 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006797 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006798 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006799#ifdef FEAT_MBYTE
6800 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006801 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006802 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006803 for ( ; i > 0; --i)
6804 mb_ptr_adv(p);
6805 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006806 else
6807#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006808 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006809 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006810 STRCAT(newword, p);
6811 }
6812 else
6813 {
6814 /* suffix: chop/add at the end of the word */
6815 STRCPY(newword, word);
6816 if (ae->ae_chop != NULL)
6817 {
6818 /* Remove chop string. */
6819 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006820 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006821 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006822 mb_ptr_back(newword, p);
6823 *p = NUL;
6824 }
6825 if (ae->ae_add != NULL)
6826 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006827 }
6828
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006829 use_flags = flags;
6830 use_pfxlist = pfxlist;
6831 use_pfxlen = pfxlen;
6832 need_affix = FALSE;
6833 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6834 if (ae->ae_flags != NULL)
6835 {
6836 /* Extract flags from the affix list. */
6837 use_flags |= get_affix_flags(affile, ae->ae_flags);
6838
6839 if (affile->af_needaffix != 0 && flag_in_afflist(
6840 affile->af_flagtype, ae->ae_flags,
6841 affile->af_needaffix))
6842 need_affix = TRUE;
6843
6844 /* When there is a CIRCUMFIX flag the other affix
6845 * must also have it and we don't add the word
6846 * with one affix. */
6847 if (affile->af_circumfix != 0 && flag_in_afflist(
6848 affile->af_flagtype, ae->ae_flags,
6849 affile->af_circumfix))
6850 {
6851 use_condit |= CONDIT_CFIX;
6852 if ((condit & CONDIT_CFIX) == 0)
6853 need_affix = TRUE;
6854 }
6855
6856 if (affile->af_pfxpostpone
6857 || spin->si_compflags != NULL)
6858 {
6859 if (affile->af_pfxpostpone)
6860 /* Get prefix IDS from the affix list. */
6861 use_pfxlen = get_pfxlist(affile,
6862 ae->ae_flags, store_afflist);
6863 else
6864 use_pfxlen = 0;
6865 use_pfxlist = store_afflist;
6866
6867 /* Combine the prefix IDs. Avoid adding the
6868 * same ID twice. */
6869 for (i = 0; i < pfxlen; ++i)
6870 {
6871 for (j = 0; j < use_pfxlen; ++j)
6872 if (pfxlist[i] == use_pfxlist[j])
6873 break;
6874 if (j == use_pfxlen)
6875 use_pfxlist[use_pfxlen++] = pfxlist[i];
6876 }
6877
6878 if (spin->si_compflags != NULL)
6879 /* Get compound IDS from the affix list. */
6880 get_compflags(affile, ae->ae_flags,
6881 use_pfxlist + use_pfxlen);
6882
6883 /* Combine the list of compound flags.
6884 * Concatenate them to the prefix IDs list.
6885 * Avoid adding the same ID twice. */
6886 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6887 {
6888 for (j = use_pfxlen;
6889 use_pfxlist[j] != NUL; ++j)
6890 if (pfxlist[i] == use_pfxlist[j])
6891 break;
6892 if (use_pfxlist[j] == NUL)
6893 {
6894 use_pfxlist[j++] = pfxlist[i];
6895 use_pfxlist[j] = NUL;
6896 }
6897 }
6898 }
6899 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006900
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006902 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006903 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006904 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006905 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006906 use_pfxlist = pfx_pfxlist;
6907 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006908
6909 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006910 if (spin->si_prefroot != NULL
6911 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006912 {
6913 /* ... add a flag to indicate an affix was used. */
6914 use_flags |= WF_HAS_AFF;
6915
6916 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006917 * affixes is not allowed. But do use the
6918 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006919 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006920 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006921 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006922
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006923 /* When compounding is supported and there is no
6924 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6925 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006926 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927 {
6928 if (xht != NULL)
6929 use_flags |= WF_NOCOMPAFT;
6930 else
6931 use_flags |= WF_NOCOMPBEF;
6932 }
6933
Bram Moolenaar51485f02005-06-04 21:55:20 +00006934 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006935 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006936 spin->si_region, use_pfxlist,
6937 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006938 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006939
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006940 /* When added a prefix or a first suffix and the affix
6941 * has flags may add a(nother) suffix. RECURSIVE! */
6942 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6943 if (store_aff_word(spin, newword, ae->ae_flags,
6944 affile, &affile->af_suff, xht,
6945 use_condit & (xht == NULL
6946 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006947 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006948 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006949
6950 /* When added a suffix and combining is allowed also
6951 * try adding a prefix additionally. Both for the
6952 * word flags and for the affix flags. RECURSIVE! */
6953 if (xht != NULL && ah->ah_combine)
6954 {
6955 if (store_aff_word(spin, newword,
6956 afflist, affile,
6957 xht, NULL, use_condit,
6958 use_flags, use_pfxlist,
6959 pfxlen) == FAIL
6960 || (ae->ae_flags != NULL
6961 && store_aff_word(spin, newword,
6962 ae->ae_flags, affile,
6963 xht, NULL, use_condit,
6964 use_flags, use_pfxlist,
6965 pfxlen) == FAIL))
6966 retval = FAIL;
6967 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006968 }
6969 }
6970 }
6971 }
6972 }
6973
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006974 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006975}
6976
6977/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006978 * Read a file with a list of words.
6979 */
6980 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006981spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006982 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006983 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006984{
6985 FILE *fd;
6986 long lnum = 0;
6987 char_u rline[MAXLINELEN];
6988 char_u *line;
6989 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006990 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006991 int l;
6992 int retval = OK;
6993 int did_word = FALSE;
6994 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006995 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006996 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006997
6998 /*
6999 * Open the file.
7000 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007001 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007002 if (fd == NULL)
7003 {
7004 EMSG2(_(e_notopen), fname);
7005 return FAIL;
7006 }
7007
Bram Moolenaar4770d092006-01-12 23:22:24 +00007008 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7009 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007010
7011 /*
7012 * Read all the lines in the file one by one.
7013 */
7014 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7015 {
7016 line_breakcheck();
7017 ++lnum;
7018
7019 /* Skip comment lines. */
7020 if (*rline == '#')
7021 continue;
7022
7023 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007024 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007025 while (l > 0 && rline[l - 1] <= ' ')
7026 --l;
7027 if (l == 0)
7028 continue; /* empty or blank line */
7029 rline[l] = NUL;
7030
7031 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
7032 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007033#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007034 if (spin->si_conv.vc_type != CONV_NONE)
7035 {
7036 pc = string_convert(&spin->si_conv, rline, NULL);
7037 if (pc == NULL)
7038 {
7039 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7040 fname, lnum, rline);
7041 continue;
7042 }
7043 line = pc;
7044 }
7045 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007046#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007047 {
7048 pc = NULL;
7049 line = rline;
7050 }
7051
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007052 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007053 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007054 ++line;
7055 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007056 {
7057 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007058 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7059 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007060 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007061 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7062 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007063 else
7064 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007065#ifdef FEAT_MBYTE
7066 char_u *enc;
7067
Bram Moolenaar51485f02005-06-04 21:55:20 +00007068 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007069 line += 10;
7070 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007071 if (enc != NULL && !spin->si_ascii
7072 && convert_setup(&spin->si_conv, enc,
7073 p_enc) == FAIL)
7074 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007075 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007076 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007077 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007078#else
7079 smsg((char_u *)_("Conversion in %s not supported"), fname);
7080#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007081 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007082 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007083 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007084
Bram Moolenaar3982c542005-06-08 21:56:31 +00007085 if (STRNCMP(line, "regions=", 8) == 0)
7086 {
7087 if (spin->si_region_count > 1)
7088 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7089 fname, lnum, line);
7090 else
7091 {
7092 line += 8;
7093 if (STRLEN(line) > 16)
7094 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7095 fname, lnum, line);
7096 else
7097 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007098 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007099 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007100
7101 /* Adjust the mask for a word valid in all regions. */
7102 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007103 }
7104 }
7105 continue;
7106 }
7107
Bram Moolenaar7887d882005-07-01 22:33:52 +00007108 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7109 fname, lnum, line - 1);
7110 continue;
7111 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007112
Bram Moolenaar7887d882005-07-01 22:33:52 +00007113 flags = 0;
7114 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007115
Bram Moolenaar7887d882005-07-01 22:33:52 +00007116 /* Check for flags and region after a slash. */
7117 p = vim_strchr(line, '/');
7118 if (p != NULL)
7119 {
7120 *p++ = NUL;
7121 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007122 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007123 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007124 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007125 else if (*p == '!') /* Bad, bad, wicked word. */
7126 flags |= WF_BANNED;
7127 else if (*p == '?') /* Rare word. */
7128 flags |= WF_RARE;
7129 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007130 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007131 if ((flags & WF_REGION) == 0) /* first one */
7132 regionmask = 0;
7133 flags |= WF_REGION;
7134
7135 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007136 if (l > spin->si_region_count)
7137 {
7138 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007139 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007140 break;
7141 }
7142 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007143 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007144 else
7145 {
7146 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7147 fname, lnum, p);
7148 break;
7149 }
7150 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007151 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007152 }
7153
7154 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7155 if (spin->si_ascii && has_non_ascii(line))
7156 {
7157 ++non_ascii;
7158 continue;
7159 }
7160
7161 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007162 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007163 {
7164 retval = FAIL;
7165 break;
7166 }
7167 did_word = TRUE;
7168 }
7169
7170 vim_free(pc);
7171 fclose(fd);
7172
Bram Moolenaar4770d092006-01-12 23:22:24 +00007173 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007174 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007175 vim_snprintf((char *)IObuff, IOSIZE,
7176 _("Ignored %d words with non-ASCII characters"), non_ascii);
7177 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007178 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007179
Bram Moolenaar51485f02005-06-04 21:55:20 +00007180 return retval;
7181}
7182
7183/*
7184 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007185 * This avoids calling free() for every little struct we use (and keeping
7186 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007187 * The memory is cleared to all zeros.
7188 * Returns NULL when out of memory.
7189 */
7190 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007191getroom(spin, len, align)
7192 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007193 size_t len; /* length needed */
7194 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007195{
7196 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007197 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007199 if (align && bl != NULL)
7200 /* Round size up for alignment. On some systems structures need to be
7201 * aligned to the size of a pointer (e.g., SPARC). */
7202 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7203 & ~(sizeof(char *) - 1);
7204
Bram Moolenaar51485f02005-06-04 21:55:20 +00007205 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7206 {
7207 /* Allocate a block of memory. This is not freed until much later. */
7208 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7209 if (bl == NULL)
7210 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007211 bl->sb_next = spin->si_blocks;
7212 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007213 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007214 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007215 }
7216
7217 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007219
7220 return p;
7221}
7222
7223/*
7224 * Make a copy of a string into memory allocated with getroom().
7225 */
7226 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007227getroom_save(spin, s)
7228 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007229 char_u *s;
7230{
7231 char_u *sc;
7232
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007233 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007234 if (sc != NULL)
7235 STRCPY(sc, s);
7236 return sc;
7237}
7238
7239
7240/*
7241 * Free the list of allocated sblock_T.
7242 */
7243 static void
7244free_blocks(bl)
7245 sblock_T *bl;
7246{
7247 sblock_T *next;
7248
7249 while (bl != NULL)
7250 {
7251 next = bl->sb_next;
7252 vim_free(bl);
7253 bl = next;
7254 }
7255}
7256
7257/*
7258 * Allocate the root of a word tree.
7259 */
7260 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007261wordtree_alloc(spin)
7262 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007263{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007264 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007265}
7266
7267/*
7268 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007269 * Always store it in the case-folded tree. For a keep-case word this is
7270 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7271 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007272 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007273 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7274 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007275 */
7276 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007277store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007278 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007279 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007280 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007281 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007282 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007283 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007284{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007286 int ct = captype(word, word + len);
7287 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007288 int res = OK;
7289 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007291 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007292 for (p = pfxlist; res == OK; ++p)
7293 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007294 if (!need_affix || (p != NULL && *p != NUL))
7295 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007296 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007297 if (p == NULL || *p == NUL)
7298 break;
7299 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007300 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007301
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007302 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007303 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007304 for (p = pfxlist; res == OK; ++p)
7305 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007306 if (!need_affix || (p != NULL && *p != NUL))
7307 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007308 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007309 if (p == NULL || *p == NUL)
7310 break;
7311 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007312 ++spin->si_keepwcount;
7313 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007314 return res;
7315}
7316
7317/*
7318 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007319 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007320 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007321 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007322 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007323 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007324tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007325 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007326 char_u *word;
7327 wordnode_T *root;
7328 int flags;
7329 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007330 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007331{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007332 wordnode_T *node = root;
7333 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007334 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007335 wordnode_T **prev = NULL;
7336 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007337
Bram Moolenaar51485f02005-06-04 21:55:20 +00007338 /* Add each byte of the word to the tree, including the NUL at the end. */
7339 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007340 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007341 /* When there is more than one reference to this node we need to make
7342 * a copy, so that we can modify it. Copy the whole list of siblings
7343 * (we don't optimize for a partly shared list of siblings). */
7344 if (node != NULL && node->wn_refs > 1)
7345 {
7346 --node->wn_refs;
7347 copyprev = prev;
7348 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7349 {
7350 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007351 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007352 if (np == NULL)
7353 return FAIL;
7354 np->wn_child = copyp->wn_child;
7355 if (np->wn_child != NULL)
7356 ++np->wn_child->wn_refs; /* child gets extra ref */
7357 np->wn_byte = copyp->wn_byte;
7358 if (np->wn_byte == NUL)
7359 {
7360 np->wn_flags = copyp->wn_flags;
7361 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007362 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007363 }
7364
7365 /* Link the new node in the list, there will be one ref. */
7366 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007367 if (copyprev != NULL)
7368 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007369 copyprev = &np->wn_sibling;
7370
7371 /* Let "node" point to the head of the copied list. */
7372 if (copyp == node)
7373 node = np;
7374 }
7375 }
7376
Bram Moolenaar51485f02005-06-04 21:55:20 +00007377 /* Look for the sibling that has the same character. They are sorted
7378 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007379 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007380 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007381 while (node != NULL
7382 && (node->wn_byte < word[i]
7383 || (node->wn_byte == NUL
7384 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007385 ? node->wn_affixID < (unsigned)affixID
7386 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007387 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007388 && (spin->si_sugtree
7389 ? (node->wn_region & 0xffff) < region
7390 : node->wn_affixID
7391 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007392 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007393 prev = &node->wn_sibling;
7394 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007395 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007396 if (node == NULL
7397 || node->wn_byte != word[i]
7398 || (word[i] == NUL
7399 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007400 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007401 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007402 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007403 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007404 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007405 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007406 if (np == NULL)
7407 return FAIL;
7408 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007409
7410 /* If "node" is NULL this is a new child or the end of the sibling
7411 * list: ref count is one. Otherwise use ref count of sibling and
7412 * make ref count of sibling one (matters when inserting in front
7413 * of the list of siblings). */
7414 if (node == NULL)
7415 np->wn_refs = 1;
7416 else
7417 {
7418 np->wn_refs = node->wn_refs;
7419 node->wn_refs = 1;
7420 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007421 *prev = np;
7422 np->wn_sibling = node;
7423 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007424 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007425
Bram Moolenaar51485f02005-06-04 21:55:20 +00007426 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007427 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007428 node->wn_flags = flags;
7429 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007430 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007431 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007432 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007433 prev = &node->wn_child;
7434 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007435 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007436#ifdef SPELL_PRINTTREE
7437 smsg("Added \"%s\"", word);
7438 spell_print_tree(root->wn_sibling);
7439#endif
7440
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007441 /* count nr of words added since last message */
7442 ++spin->si_msg_count;
7443
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007444 if (spin->si_compress_cnt > 1)
7445 {
7446 if (--spin->si_compress_cnt == 1)
7447 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007448 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007449 }
7450
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007451 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007452 * When we have allocated lots of memory we need to compress the word tree
7453 * to free up some room. But compression is slow, and we might actually
7454 * need that room, thus only compress in the following situations:
7455 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007456 * "compress_start" blocks.
7457 * 2. When compressed before and used "compress_inc" blocks before
7458 * adding "compress_added" words (si_compress_cnt > 1).
7459 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007460 * (si_compress_cnt == 1) and the number of free nodes drops below the
7461 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007462 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007463#ifndef SPELL_PRINTTREE
7464 if (spin->si_compress_cnt == 1
7465 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007466 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007467#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007468 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007469 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007470 * when the freed up room has been used and another "compress_inc"
7471 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007472 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007473 spin->si_blocks_cnt -= compress_inc;
7474 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007475
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007476 if (spin->si_verbose)
7477 {
7478 msg_start();
7479 msg_puts((char_u *)_(msg_compressing));
7480 msg_clr_eos();
7481 msg_didout = FALSE;
7482 msg_col = 0;
7483 out_flush();
7484 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007485
7486 /* Compress both trees. Either they both have many nodes, which makes
7487 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007488 * compression goes fast. But when filling the souldfold word tree
7489 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007490 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007491 if (affixID >= 0)
7492 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007493 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007494
7495 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007496}
7497
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007498/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007499 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7500 * Sets "sps_flags".
7501 */
7502 int
7503spell_check_msm()
7504{
7505 char_u *p = p_msm;
7506 long start = 0;
7507 long inc = 0;
7508 long added = 0;
7509
7510 if (!VIM_ISDIGIT(*p))
7511 return FAIL;
7512 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7513 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7514 if (*p != ',')
7515 return FAIL;
7516 ++p;
7517 if (!VIM_ISDIGIT(*p))
7518 return FAIL;
7519 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7520 if (*p != ',')
7521 return FAIL;
7522 ++p;
7523 if (!VIM_ISDIGIT(*p))
7524 return FAIL;
7525 added = getdigits(&p) * 1024;
7526 if (*p != NUL)
7527 return FAIL;
7528
7529 if (start == 0 || inc == 0 || added == 0 || inc > start)
7530 return FAIL;
7531
7532 compress_start = start;
7533 compress_inc = inc;
7534 compress_added = added;
7535 return OK;
7536}
7537
7538
7539/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007540 * Get a wordnode_T, either from the list of previously freed nodes or
7541 * allocate a new one.
7542 */
7543 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007544get_wordnode(spin)
7545 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007546{
7547 wordnode_T *n;
7548
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007549 if (spin->si_first_free == NULL)
7550 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007551 else
7552 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007553 n = spin->si_first_free;
7554 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007555 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007556 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007557 }
7558#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007559 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007560#endif
7561 return n;
7562}
7563
7564/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007565 * Decrement the reference count on a node (which is the head of a list of
7566 * siblings). If the reference count becomes zero free the node and its
7567 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007568 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007569 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007570 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007571deref_wordnode(spin, node)
7572 spellinfo_T *spin;
7573 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007574{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007575 wordnode_T *np;
7576 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007577
7578 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007579 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007580 for (np = node; np != NULL; np = np->wn_sibling)
7581 {
7582 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007583 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007584 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007585 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007586 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007587 ++cnt; /* length field */
7588 }
7589 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007590}
7591
7592/*
7593 * Free a wordnode_T for re-use later.
7594 * Only the "wn_child" field becomes invalid.
7595 */
7596 static void
7597free_wordnode(spin, n)
7598 spellinfo_T *spin;
7599 wordnode_T *n;
7600{
7601 n->wn_child = spin->si_first_free;
7602 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007603 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007604}
7605
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007606/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007607 * Compress a tree: find tails that are identical and can be shared.
7608 */
7609 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007610wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007611 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007612 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007613{
7614 hashtab_T ht;
7615 int n;
7616 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007617 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007618
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007619 /* Skip the root itself, it's not actually used. The first sibling is the
7620 * start of the tree. */
7621 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007622 {
7623 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007624 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007625
7626#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007627 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007628#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007629 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007630 if (tot > 1000000)
7631 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007632 else if (tot == 0)
7633 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007634 else
7635 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007636 vim_snprintf((char *)IObuff, IOSIZE,
7637 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7638 n, tot, tot - n, perc);
7639 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007640 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007641#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007642 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007643#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007644 hash_clear(&ht);
7645 }
7646}
7647
7648/*
7649 * Compress a node, its siblings and its children, depth first.
7650 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007651 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007652 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007653node_compress(spin, node, ht, tot)
7654 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007655 wordnode_T *node;
7656 hashtab_T *ht;
7657 int *tot; /* total count of nodes before compressing,
7658 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007659{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007660 wordnode_T *np;
7661 wordnode_T *tp;
7662 wordnode_T *child;
7663 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007664 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007665 int len = 0;
7666 unsigned nr, n;
7667 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007668
Bram Moolenaar51485f02005-06-04 21:55:20 +00007669 /*
7670 * Go through the list of siblings. Compress each child and then try
7671 * finding an identical child to replace it.
7672 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007673 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007674 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007675 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007676 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007677 ++len;
7678 if ((child = np->wn_child) != NULL)
7679 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007680 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007681 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007682
7683 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007684 hash = hash_hash(child->wn_u1.hashkey);
7685 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007686 if (!HASHITEM_EMPTY(hi))
7687 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007688 /* There are children we encountered before with a hash value
7689 * identical to the current child. Now check if there is one
7690 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007691 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007692 if (node_equal(child, tp))
7693 {
7694 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007695 * current one. This means the current child and all
7696 * its siblings is unlinked from the tree. */
7697 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007698 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007699 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007700 break;
7701 }
7702 if (tp == NULL)
7703 {
7704 /* No other child with this hash value equals the child of
7705 * the node, add it to the linked list after the first
7706 * item. */
7707 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007708 child->wn_u2.next = tp->wn_u2.next;
7709 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007710 }
7711 }
7712 else
7713 /* No other child has this hash value, add it to the
7714 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007715 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007716 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007717 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007718 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007719
7720 /*
7721 * Make a hash key for the node and its siblings, so that we can quickly
7722 * find a lookalike node. This must be done after compressing the sibling
7723 * list, otherwise the hash key would become invalid by the compression.
7724 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007725 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007726 nr = 0;
7727 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007728 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007729 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007730 /* end node: use wn_flags, wn_region and wn_affixID */
7731 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007732 else
7733 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007734 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007735 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007736 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007737
7738 /* Avoid NUL bytes, it terminates the hash key. */
7739 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007740 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007741 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007742 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007743 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007744 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007745 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007746 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7747 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007748
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007749 /* Check for CTRL-C pressed now and then. */
7750 fast_breakcheck();
7751
Bram Moolenaar51485f02005-06-04 21:55:20 +00007752 return compressed;
7753}
7754
7755/*
7756 * Return TRUE when two nodes have identical siblings and children.
7757 */
7758 static int
7759node_equal(n1, n2)
7760 wordnode_T *n1;
7761 wordnode_T *n2;
7762{
7763 wordnode_T *p1;
7764 wordnode_T *p2;
7765
7766 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7767 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7768 if (p1->wn_byte != p2->wn_byte
7769 || (p1->wn_byte == NUL
7770 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007771 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007772 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007773 : (p1->wn_child != p2->wn_child)))
7774 break;
7775
7776 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007777}
7778
7779/*
7780 * Write a number to file "fd", MSB first, in "len" bytes.
7781 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007782 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007783put_bytes(fd, nr, len)
7784 FILE *fd;
7785 long_u nr;
7786 int len;
7787{
7788 int i;
7789
7790 for (i = len - 1; i >= 0; --i)
7791 putc((int)(nr >> (i * 8)), fd);
7792}
7793
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007794#ifdef _MSC_VER
7795# if (_MSC_VER <= 1200)
7796/* This line is required for VC6 without the service pack. Also see the
7797 * matching #pragma below. */
7798/* # pragma optimize("", off) */
7799# endif
7800#endif
7801
Bram Moolenaar4770d092006-01-12 23:22:24 +00007802/*
7803 * Write spin->si_sugtime to file "fd".
7804 */
7805 static void
7806put_sugtime(spin, fd)
7807 spellinfo_T *spin;
7808 FILE *fd;
7809{
7810 int c;
7811 int i;
7812
7813 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7814 * can't use put_bytes() here. */
7815 for (i = 7; i >= 0; --i)
7816 if (i + 1 > sizeof(time_t))
7817 /* ">>" doesn't work well when shifting more bits than avail */
7818 putc(0, fd);
7819 else
7820 {
7821 c = (unsigned)spin->si_sugtime >> (i * 8);
7822 putc(c, fd);
7823 }
7824}
7825
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007826#ifdef _MSC_VER
7827# if (_MSC_VER <= 1200)
7828/* # pragma optimize("", on) */
7829# endif
7830#endif
7831
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832static int
7833#ifdef __BORLANDC__
7834_RTLENTRYF
7835#endif
7836rep_compare __ARGS((const void *s1, const void *s2));
7837
7838/*
7839 * Function given to qsort() to sort the REP items on "from" string.
7840 */
7841 static int
7842#ifdef __BORLANDC__
7843_RTLENTRYF
7844#endif
7845rep_compare(s1, s2)
7846 const void *s1;
7847 const void *s2;
7848{
7849 fromto_T *p1 = (fromto_T *)s1;
7850 fromto_T *p2 = (fromto_T *)s2;
7851
7852 return STRCMP(p1->ft_from, p2->ft_from);
7853}
7854
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007855/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007856 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007857 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007858 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007859 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007860write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007861 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007862 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007863{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007864 FILE *fd;
7865 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007866 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007867 wordnode_T *tree;
7868 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869 int i;
7870 int l;
7871 garray_T *gap;
7872 fromto_T *ftp;
7873 char_u *p;
7874 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007875 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007876
Bram Moolenaarb765d632005-06-07 21:00:02 +00007877 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007878 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007879 {
7880 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007881 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007882 }
7883
Bram Moolenaar5195e452005-08-19 20:32:47 +00007884 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007885 /* <fileID> */
7886 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007887 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007888 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007889 retval = FAIL;
7890 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007891 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007892
Bram Moolenaar5195e452005-08-19 20:32:47 +00007893 /*
7894 * <SECTIONS>: <section> ... <sectionend>
7895 */
7896
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007897 /* SN_INFO: <infotext> */
7898 if (spin->si_info != NULL)
7899 {
7900 putc(SN_INFO, fd); /* <sectionID> */
7901 putc(0, fd); /* <sectionflags> */
7902
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007903 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007904 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7905 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7906 }
7907
Bram Moolenaar5195e452005-08-19 20:32:47 +00007908 /* SN_REGION: <regionname> ...
7909 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007910 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007911 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007912 putc(SN_REGION, fd); /* <sectionID> */
7913 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7914 l = spin->si_region_count * 2;
7915 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7916 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7917 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007918 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007919 }
7920 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007921 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007922
Bram Moolenaar5195e452005-08-19 20:32:47 +00007923 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7924 *
7925 * The table with character flags and the table for case folding.
7926 * This makes sure the same characters are recognized as word characters
7927 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007928 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007929 * 'encoding'.
7930 * Also skip this for an .add.spl file, the main spell file must contain
7931 * the table (avoids that it conflicts). File is shorter too.
7932 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007933 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007934 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007935 char_u folchars[128 * 8];
7936 int flags;
7937
Bram Moolenaard12a1322005-08-21 22:08:24 +00007938 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007939 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7940
7941 /* Form the <folchars> string first, we need to know its length. */
7942 l = 0;
7943 for (i = 128; i < 256; ++i)
7944 {
7945#ifdef FEAT_MBYTE
7946 if (has_mbyte)
7947 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7948 else
7949#endif
7950 folchars[l++] = spelltab.st_fold[i];
7951 }
7952 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7953
7954 fputc(128, fd); /* <charflagslen> */
7955 for (i = 128; i < 256; ++i)
7956 {
7957 flags = 0;
7958 if (spelltab.st_isw[i])
7959 flags |= CF_WORD;
7960 if (spelltab.st_isu[i])
7961 flags |= CF_UPPER;
7962 fputc(flags, fd); /* <charflags> */
7963 }
7964
7965 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7966 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007967 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007968
Bram Moolenaar5195e452005-08-19 20:32:47 +00007969 /* SN_MIDWORD: <midword> */
7970 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007971 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007972 putc(SN_MIDWORD, fd); /* <sectionID> */
7973 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7974
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007975 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007976 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007977 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7978 }
7979
Bram Moolenaar5195e452005-08-19 20:32:47 +00007980 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7981 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007982 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007983 putc(SN_PREFCOND, fd); /* <sectionID> */
7984 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7985
7986 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7987 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7988
7989 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007990 }
7991
Bram Moolenaar5195e452005-08-19 20:32:47 +00007992 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007993 * SN_SAL: <salflags> <salcount> <sal> ...
7994 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007995
Bram Moolenaar5195e452005-08-19 20:32:47 +00007996 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007997 * round 2: SN_SAL section (unless SN_SOFO is used)
7998 * round 3: SN_REPSAL section */
7999 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008000 {
8001 if (round == 1)
8002 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008003 else if (round == 2)
8004 {
8005 /* Don't write SN_SAL when using a SN_SOFO section */
8006 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8007 continue;
8008 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008010 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008011 gap = &spin->si_repsal;
8012
8013 /* Don't write the section if there are no items. */
8014 if (gap->ga_len == 0)
8015 continue;
8016
8017 /* Sort the REP/REPSAL items. */
8018 if (round != 2)
8019 qsort(gap->ga_data, (size_t)gap->ga_len,
8020 sizeof(fromto_T), rep_compare);
8021
8022 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8023 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008024
Bram Moolenaar5195e452005-08-19 20:32:47 +00008025 /* This is for making suggestions, section is not required. */
8026 putc(0, fd); /* <sectionflags> */
8027
8028 /* Compute the length of what follows. */
8029 l = 2; /* count <repcount> or <salcount> */
8030 for (i = 0; i < gap->ga_len; ++i)
8031 {
8032 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008033 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8034 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008035 }
8036 if (round == 2)
8037 ++l; /* count <salflags> */
8038 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8039
8040 if (round == 2)
8041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008042 i = 0;
8043 if (spin->si_followup)
8044 i |= SAL_F0LLOWUP;
8045 if (spin->si_collapse)
8046 i |= SAL_COLLAPSE;
8047 if (spin->si_rem_accents)
8048 i |= SAL_REM_ACCENTS;
8049 putc(i, fd); /* <salflags> */
8050 }
8051
8052 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8053 for (i = 0; i < gap->ga_len; ++i)
8054 {
8055 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8056 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8057 ftp = &((fromto_T *)gap->ga_data)[i];
8058 for (rr = 1; rr <= 2; ++rr)
8059 {
8060 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008061 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008062 putc(l, fd);
8063 fwrite(p, l, (size_t)1, fd);
8064 }
8065 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 }
8068
Bram Moolenaar5195e452005-08-19 20:32:47 +00008069 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8070 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008071 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8072 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008073 putc(SN_SOFO, fd); /* <sectionID> */
8074 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008075
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008076 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008077 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8078 /* <sectionlen> */
8079
8080 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8081 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008083 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008084 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8085 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008086 }
8087
Bram Moolenaar4770d092006-01-12 23:22:24 +00008088 /* SN_WORDS: <word> ...
8089 * This is for making suggestions, section is not required. */
8090 if (spin->si_commonwords.ht_used > 0)
8091 {
8092 putc(SN_WORDS, fd); /* <sectionID> */
8093 putc(0, fd); /* <sectionflags> */
8094
8095 /* round 1: count the bytes
8096 * round 2: write the bytes */
8097 for (round = 1; round <= 2; ++round)
8098 {
8099 int todo;
8100 int len = 0;
8101 hashitem_T *hi;
8102
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008103 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008104 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8105 if (!HASHITEM_EMPTY(hi))
8106 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008107 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008108 len += l;
8109 if (round == 2) /* <word> */
8110 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8111 --todo;
8112 }
8113 if (round == 1)
8114 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8115 }
8116 }
8117
Bram Moolenaar5195e452005-08-19 20:32:47 +00008118 /* SN_MAP: <mapstr>
8119 * This is for making suggestions, section is not required. */
8120 if (spin->si_map.ga_len > 0)
8121 {
8122 putc(SN_MAP, fd); /* <sectionID> */
8123 putc(0, fd); /* <sectionflags> */
8124 l = spin->si_map.ga_len;
8125 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8126 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8127 /* <mapstr> */
8128 }
8129
Bram Moolenaar4770d092006-01-12 23:22:24 +00008130 /* SN_SUGFILE: <timestamp>
8131 * This is used to notify that a .sug file may be available and at the
8132 * same time allows for checking that a .sug file that is found matches
8133 * with this .spl file. That's because the word numbers must be exactly
8134 * right. */
8135 if (!spin->si_nosugfile
8136 && (spin->si_sal.ga_len > 0
8137 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8138 {
8139 putc(SN_SUGFILE, fd); /* <sectionID> */
8140 putc(0, fd); /* <sectionflags> */
8141 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8142
8143 /* Set si_sugtime and write it to the file. */
8144 spin->si_sugtime = time(NULL);
8145 put_sugtime(spin, fd); /* <timestamp> */
8146 }
8147
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008148 /* SN_NOSPLITSUGS: nothing
8149 * This is used to notify that no suggestions with word splits are to be
8150 * made. */
8151 if (spin->si_nosplitsugs)
8152 {
8153 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8154 putc(0, fd); /* <sectionflags> */
8155 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8156 }
8157
Bram Moolenaar5195e452005-08-19 20:32:47 +00008158 /* SN_COMPOUND: compound info.
8159 * We don't mark it required, when not supported all compound words will
8160 * be bad words. */
8161 if (spin->si_compflags != NULL)
8162 {
8163 putc(SN_COMPOUND, fd); /* <sectionID> */
8164 putc(0, fd); /* <sectionflags> */
8165
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008166 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008167 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008168 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008169 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8170
Bram Moolenaar5195e452005-08-19 20:32:47 +00008171 putc(spin->si_compmax, fd); /* <compmax> */
8172 putc(spin->si_compminlen, fd); /* <compminlen> */
8173 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008174 putc(0, fd); /* for Vim 7.0b compatibility */
8175 putc(spin->si_compoptions, fd); /* <compoptions> */
8176 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8177 /* <comppatcount> */
8178 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8179 {
8180 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008181 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008182 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8183 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008184 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008185 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8186 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008187 }
8188
Bram Moolenaar78622822005-08-23 21:00:13 +00008189 /* SN_NOBREAK: NOBREAK flag */
8190 if (spin->si_nobreak)
8191 {
8192 putc(SN_NOBREAK, fd); /* <sectionID> */
8193 putc(0, fd); /* <sectionflags> */
8194
8195 /* It's empty, the precense of the section flags the feature. */
8196 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8197 }
8198
Bram Moolenaar5195e452005-08-19 20:32:47 +00008199 /* SN_SYLLABLE: syllable info.
8200 * We don't mark it required, when not supported syllables will not be
8201 * counted. */
8202 if (spin->si_syllable != NULL)
8203 {
8204 putc(SN_SYLLABLE, fd); /* <sectionID> */
8205 putc(0, fd); /* <sectionflags> */
8206
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008207 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008208 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8209 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8210 }
8211
8212 /* end of <SECTIONS> */
8213 putc(SN_END, fd); /* <sectionend> */
8214
Bram Moolenaar50cde822005-06-05 21:54:54 +00008215
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008216 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008217 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008218 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008219 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008220 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008221 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008222 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008223 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008224 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008225 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008226 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008227 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008228
Bram Moolenaar0c405862005-06-22 22:26:26 +00008229 /* Clear the index and wnode fields in the tree. */
8230 clear_node(tree);
8231
Bram Moolenaar51485f02005-06-04 21:55:20 +00008232 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008233 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008234 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008235 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008236
Bram Moolenaar51485f02005-06-04 21:55:20 +00008237 /* number of nodes in 4 bytes */
8238 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008239 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008240
Bram Moolenaar51485f02005-06-04 21:55:20 +00008241 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008242 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008243 }
8244
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008245 /* Write another byte to check for errors. */
8246 if (putc(0, fd) == EOF)
8247 retval = FAIL;
8248
8249 if (fclose(fd) == EOF)
8250 retval = FAIL;
8251
8252 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008253}
8254
8255/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008256 * Clear the index and wnode fields of "node", it siblings and its
8257 * children. This is needed because they are a union with other items to save
8258 * space.
8259 */
8260 static void
8261clear_node(node)
8262 wordnode_T *node;
8263{
8264 wordnode_T *np;
8265
8266 if (node != NULL)
8267 for (np = node; np != NULL; np = np->wn_sibling)
8268 {
8269 np->wn_u1.index = 0;
8270 np->wn_u2.wnode = NULL;
8271
8272 if (np->wn_byte != NUL)
8273 clear_node(np->wn_child);
8274 }
8275}
8276
8277
8278/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008279 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008280 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008281 * This first writes the list of possible bytes (siblings). Then for each
8282 * byte recursively write the children.
8283 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008284 * NOTE: The code here must match the code in read_tree_node(), since
8285 * assumptions are made about the indexes (so that we don't have to write them
8286 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008287 *
8288 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008289 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008290 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00008291put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008292 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008293 wordnode_T *node;
8294 int index;
8295 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008296 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008297{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008298 int newindex = index;
8299 int siblingcount = 0;
8300 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008301 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008302
Bram Moolenaar51485f02005-06-04 21:55:20 +00008303 /* If "node" is zero the tree is empty. */
8304 if (node == NULL)
8305 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008306
Bram Moolenaar51485f02005-06-04 21:55:20 +00008307 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008308 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008309
8310 /* Count the number of siblings. */
8311 for (np = node; np != NULL; np = np->wn_sibling)
8312 ++siblingcount;
8313
8314 /* Write the sibling count. */
8315 if (fd != NULL)
8316 putc(siblingcount, fd); /* <siblingcount> */
8317
8318 /* Write each sibling byte and optionally extra info. */
8319 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008320 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008321 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008322 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008323 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008324 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008325 /* For a NUL byte (end of word) write the flags etc. */
8326 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008327 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008328 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008329 * associated condition nr (stored in wn_region). The
8330 * byte value is misused to store the "rare" and "not
8331 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008332 if (np->wn_flags == (short_u)PFX_FLAGS)
8333 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008334 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008335 {
8336 putc(BY_FLAGS, fd); /* <byte> */
8337 putc(np->wn_flags, fd); /* <pflags> */
8338 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008339 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008340 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008341 }
8342 else
8343 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008344 /* For word trees we write the flag/region items. */
8345 flags = np->wn_flags;
8346 if (regionmask != 0 && np->wn_region != regionmask)
8347 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008348 if (np->wn_affixID != 0)
8349 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008350 if (flags == 0)
8351 {
8352 /* word without flags or region */
8353 putc(BY_NOFLAGS, fd); /* <byte> */
8354 }
8355 else
8356 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008357 if (np->wn_flags >= 0x100)
8358 {
8359 putc(BY_FLAGS2, fd); /* <byte> */
8360 putc(flags, fd); /* <flags> */
8361 putc((unsigned)flags >> 8, fd); /* <flags2> */
8362 }
8363 else
8364 {
8365 putc(BY_FLAGS, fd); /* <byte> */
8366 putc(flags, fd); /* <flags> */
8367 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008368 if (flags & WF_REGION)
8369 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008370 if (flags & WF_AFX)
8371 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008372 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008373 }
8374 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008375 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008376 else
8377 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008378 if (np->wn_child->wn_u1.index != 0
8379 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008380 {
8381 /* The child is written elsewhere, write the reference. */
8382 if (fd != NULL)
8383 {
8384 putc(BY_INDEX, fd); /* <byte> */
8385 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008386 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008387 }
8388 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008389 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008390 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008391 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008392
Bram Moolenaar51485f02005-06-04 21:55:20 +00008393 if (fd != NULL)
8394 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8395 {
8396 EMSG(_(e_write));
8397 return 0;
8398 }
8399 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008400 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008401
8402 /* Space used in the array when reading: one for each sibling and one for
8403 * the count. */
8404 newindex += siblingcount + 1;
8405
8406 /* Recursively dump the children of each sibling. */
8407 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008408 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8409 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008410 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008411
8412 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008413}
8414
8415
8416/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008417 * ":mkspell [-ascii] outfile infile ..."
8418 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008419 */
8420 void
8421ex_mkspell(eap)
8422 exarg_T *eap;
8423{
8424 int fcount;
8425 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008426 char_u *arg = eap->arg;
8427 int ascii = FALSE;
8428
8429 if (STRNCMP(arg, "-ascii", 6) == 0)
8430 {
8431 ascii = TRUE;
8432 arg = skipwhite(arg + 6);
8433 }
8434
8435 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8436 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008438 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008439 FreeWild(fcount, fnames);
8440 }
8441}
8442
8443/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008444 * Create the .sug file.
8445 * Uses the soundfold info in "spin".
8446 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8447 */
8448 static void
8449spell_make_sugfile(spin, wfname)
8450 spellinfo_T *spin;
8451 char_u *wfname;
8452{
8453 char_u fname[MAXPATHL];
8454 int len;
8455 slang_T *slang;
8456 int free_slang = FALSE;
8457
8458 /*
8459 * Read back the .spl file that was written. This fills the required
8460 * info for soundfolding. This also uses less memory than the
8461 * pointer-linked version of the trie. And it avoids having two versions
8462 * of the code for the soundfolding stuff.
8463 * It might have been done already by spell_reload_one().
8464 */
8465 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8466 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8467 break;
8468 if (slang == NULL)
8469 {
8470 spell_message(spin, (char_u *)_("Reading back spell file..."));
8471 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8472 if (slang == NULL)
8473 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008474 free_slang = TRUE;
8475 }
8476
8477 /*
8478 * Clear the info in "spin" that is used.
8479 */
8480 spin->si_blocks = NULL;
8481 spin->si_blocks_cnt = 0;
8482 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8483 spin->si_free_count = 0;
8484 spin->si_first_free = NULL;
8485 spin->si_foldwcount = 0;
8486
8487 /*
8488 * Go through the trie of good words, soundfold each word and add it to
8489 * the soundfold trie.
8490 */
8491 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8492 if (sug_filltree(spin, slang) == FAIL)
8493 goto theend;
8494
8495 /*
8496 * Create the table which links each soundfold word with a list of the
8497 * good words it may come from. Creates buffer "spin->si_spellbuf".
8498 * This also removes the wordnr from the NUL byte entries to make
8499 * compression possible.
8500 */
8501 if (sug_maketable(spin) == FAIL)
8502 goto theend;
8503
8504 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8505 (long)spin->si_spellbuf->b_ml.ml_line_count);
8506
8507 /*
8508 * Compress the soundfold trie.
8509 */
8510 spell_message(spin, (char_u *)_(msg_compressing));
8511 wordtree_compress(spin, spin->si_foldroot);
8512
8513 /*
8514 * Write the .sug file.
8515 * Make the file name by changing ".spl" to ".sug".
8516 */
8517 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008518 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008519 fname[len - 2] = 'u';
8520 fname[len - 1] = 'g';
8521 sug_write(spin, fname);
8522
8523theend:
8524 if (free_slang)
8525 slang_free(slang);
8526 free_blocks(spin->si_blocks);
8527 close_spellbuf(spin->si_spellbuf);
8528}
8529
8530/*
8531 * Build the soundfold trie for language "slang".
8532 */
8533 static int
8534sug_filltree(spin, slang)
8535 spellinfo_T *spin;
8536 slang_T *slang;
8537{
8538 char_u *byts;
8539 idx_T *idxs;
8540 int depth;
8541 idx_T arridx[MAXWLEN];
8542 int curi[MAXWLEN];
8543 char_u tword[MAXWLEN];
8544 char_u tsalword[MAXWLEN];
8545 int c;
8546 idx_T n;
8547 unsigned words_done = 0;
8548 int wordcount[MAXWLEN];
8549
8550 /* We use si_foldroot for the souldfolded trie. */
8551 spin->si_foldroot = wordtree_alloc(spin);
8552 if (spin->si_foldroot == NULL)
8553 return FAIL;
8554
8555 /* let tree_add_word() know we're adding to the soundfolded tree */
8556 spin->si_sugtree = TRUE;
8557
8558 /*
8559 * Go through the whole case-folded tree, soundfold each word and put it
8560 * in the trie.
8561 */
8562 byts = slang->sl_fbyts;
8563 idxs = slang->sl_fidxs;
8564
8565 arridx[0] = 0;
8566 curi[0] = 1;
8567 wordcount[0] = 0;
8568
8569 depth = 0;
8570 while (depth >= 0 && !got_int)
8571 {
8572 if (curi[depth] > byts[arridx[depth]])
8573 {
8574 /* Done all bytes at this node, go up one level. */
8575 idxs[arridx[depth]] = wordcount[depth];
8576 if (depth > 0)
8577 wordcount[depth - 1] += wordcount[depth];
8578
8579 --depth;
8580 line_breakcheck();
8581 }
8582 else
8583 {
8584
8585 /* Do one more byte at this node. */
8586 n = arridx[depth] + curi[depth];
8587 ++curi[depth];
8588
8589 c = byts[n];
8590 if (c == 0)
8591 {
8592 /* Sound-fold the word. */
8593 tword[depth] = NUL;
8594 spell_soundfold(slang, tword, TRUE, tsalword);
8595
8596 /* We use the "flags" field for the MSB of the wordnr,
8597 * "region" for the LSB of the wordnr. */
8598 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8599 words_done >> 16, words_done & 0xffff,
8600 0) == FAIL)
8601 return FAIL;
8602
8603 ++words_done;
8604 ++wordcount[depth];
8605
8606 /* Reset the block count each time to avoid compression
8607 * kicking in. */
8608 spin->si_blocks_cnt = 0;
8609
8610 /* Skip over any other NUL bytes (same word with different
8611 * flags). */
8612 while (byts[n + 1] == 0)
8613 {
8614 ++n;
8615 ++curi[depth];
8616 }
8617 }
8618 else
8619 {
8620 /* Normal char, go one level deeper. */
8621 tword[depth++] = c;
8622 arridx[depth] = idxs[n];
8623 curi[depth] = 1;
8624 wordcount[depth] = 0;
8625 }
8626 }
8627 }
8628
8629 smsg((char_u *)_("Total number of words: %d"), words_done);
8630
8631 return OK;
8632}
8633
8634/*
8635 * Make the table that links each word in the soundfold trie to the words it
8636 * can be produced from.
8637 * This is not unlike lines in a file, thus use a memfile to be able to access
8638 * the table efficiently.
8639 * Returns FAIL when out of memory.
8640 */
8641 static int
8642sug_maketable(spin)
8643 spellinfo_T *spin;
8644{
8645 garray_T ga;
8646 int res = OK;
8647
8648 /* Allocate a buffer, open a memline for it and create the swap file
8649 * (uses a temp file, not a .swp file). */
8650 spin->si_spellbuf = open_spellbuf();
8651 if (spin->si_spellbuf == NULL)
8652 return FAIL;
8653
8654 /* Use a buffer to store the line info, avoids allocating many small
8655 * pieces of memory. */
8656 ga_init2(&ga, 1, 100);
8657
8658 /* recursively go through the tree */
8659 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8660 res = FAIL;
8661
8662 ga_clear(&ga);
8663 return res;
8664}
8665
8666/*
8667 * Fill the table for one node and its children.
8668 * Returns the wordnr at the start of the node.
8669 * Returns -1 when out of memory.
8670 */
8671 static int
8672sug_filltable(spin, node, startwordnr, gap)
8673 spellinfo_T *spin;
8674 wordnode_T *node;
8675 int startwordnr;
8676 garray_T *gap; /* place to store line of numbers */
8677{
8678 wordnode_T *p, *np;
8679 int wordnr = startwordnr;
8680 int nr;
8681 int prev_nr;
8682
8683 for (p = node; p != NULL; p = p->wn_sibling)
8684 {
8685 if (p->wn_byte == NUL)
8686 {
8687 gap->ga_len = 0;
8688 prev_nr = 0;
8689 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8690 {
8691 if (ga_grow(gap, 10) == FAIL)
8692 return -1;
8693
8694 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8695 /* Compute the offset from the previous nr and store the
8696 * offset in a way that it takes a minimum number of bytes.
8697 * It's a bit like utf-8, but without the need to mark
8698 * following bytes. */
8699 nr -= prev_nr;
8700 prev_nr += nr;
8701 gap->ga_len += offset2bytes(nr,
8702 (char_u *)gap->ga_data + gap->ga_len);
8703 }
8704
8705 /* add the NUL byte */
8706 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8707
8708 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8709 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8710 return -1;
8711 ++wordnr;
8712
8713 /* Remove extra NUL entries, we no longer need them. We don't
8714 * bother freeing the nodes, the won't be reused anyway. */
8715 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8716 p->wn_sibling = p->wn_sibling->wn_sibling;
8717
8718 /* Clear the flags on the remaining NUL node, so that compression
8719 * works a lot better. */
8720 p->wn_flags = 0;
8721 p->wn_region = 0;
8722 }
8723 else
8724 {
8725 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8726 if (wordnr == -1)
8727 return -1;
8728 }
8729 }
8730 return wordnr;
8731}
8732
8733/*
8734 * Convert an offset into a minimal number of bytes.
8735 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8736 * bytes.
8737 */
8738 static int
8739offset2bytes(nr, buf)
8740 int nr;
8741 char_u *buf;
8742{
8743 int rem;
8744 int b1, b2, b3, b4;
8745
8746 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8747 b1 = nr % 255 + 1;
8748 rem = nr / 255;
8749 b2 = rem % 255 + 1;
8750 rem = rem / 255;
8751 b3 = rem % 255 + 1;
8752 b4 = rem / 255 + 1;
8753
8754 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8755 {
8756 buf[0] = 0xe0 + b4;
8757 buf[1] = b3;
8758 buf[2] = b2;
8759 buf[3] = b1;
8760 return 4;
8761 }
8762 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8763 {
8764 buf[0] = 0xc0 + b3;
8765 buf[1] = b2;
8766 buf[2] = b1;
8767 return 3;
8768 }
8769 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8770 {
8771 buf[0] = 0x80 + b2;
8772 buf[1] = b1;
8773 return 2;
8774 }
8775 /* 1 byte */
8776 buf[0] = b1;
8777 return 1;
8778}
8779
8780/*
8781 * Opposite of offset2bytes().
8782 * "pp" points to the bytes and is advanced over it.
8783 * Returns the offset.
8784 */
8785 static int
8786bytes2offset(pp)
8787 char_u **pp;
8788{
8789 char_u *p = *pp;
8790 int nr;
8791 int c;
8792
8793 c = *p++;
8794 if ((c & 0x80) == 0x00) /* 1 byte */
8795 {
8796 nr = c - 1;
8797 }
8798 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8799 {
8800 nr = (c & 0x3f) - 1;
8801 nr = nr * 255 + (*p++ - 1);
8802 }
8803 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8804 {
8805 nr = (c & 0x1f) - 1;
8806 nr = nr * 255 + (*p++ - 1);
8807 nr = nr * 255 + (*p++ - 1);
8808 }
8809 else /* 4 bytes */
8810 {
8811 nr = (c & 0x0f) - 1;
8812 nr = nr * 255 + (*p++ - 1);
8813 nr = nr * 255 + (*p++ - 1);
8814 nr = nr * 255 + (*p++ - 1);
8815 }
8816
8817 *pp = p;
8818 return nr;
8819}
8820
8821/*
8822 * Write the .sug file in "fname".
8823 */
8824 static void
8825sug_write(spin, fname)
8826 spellinfo_T *spin;
8827 char_u *fname;
8828{
8829 FILE *fd;
8830 wordnode_T *tree;
8831 int nodecount;
8832 int wcount;
8833 char_u *line;
8834 linenr_T lnum;
8835 int len;
8836
8837 /* Create the file. Note that an existing file is silently overwritten! */
8838 fd = mch_fopen((char *)fname, "w");
8839 if (fd == NULL)
8840 {
8841 EMSG2(_(e_notopen), fname);
8842 return;
8843 }
8844
8845 vim_snprintf((char *)IObuff, IOSIZE,
8846 _("Writing suggestion file %s ..."), fname);
8847 spell_message(spin, IObuff);
8848
8849 /*
8850 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8851 */
8852 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8853 {
8854 EMSG(_(e_write));
8855 goto theend;
8856 }
8857 putc(VIMSUGVERSION, fd); /* <versionnr> */
8858
8859 /* Write si_sugtime to the file. */
8860 put_sugtime(spin, fd); /* <timestamp> */
8861
8862 /*
8863 * <SUGWORDTREE>
8864 */
8865 spin->si_memtot = 0;
8866 tree = spin->si_foldroot->wn_sibling;
8867
8868 /* Clear the index and wnode fields in the tree. */
8869 clear_node(tree);
8870
8871 /* Count the number of nodes. Needed to be able to allocate the
8872 * memory when reading the nodes. Also fills in index for shared
8873 * nodes. */
8874 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8875
8876 /* number of nodes in 4 bytes */
8877 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8878 spin->si_memtot += nodecount + nodecount * sizeof(int);
8879
8880 /* Write the nodes. */
8881 (void)put_node(fd, tree, 0, 0, FALSE);
8882
8883 /*
8884 * <SUGTABLE>: <sugwcount> <sugline> ...
8885 */
8886 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8887 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8888
8889 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8890 {
8891 /* <sugline>: <sugnr> ... NUL */
8892 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008893 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008894 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8895 {
8896 EMSG(_(e_write));
8897 goto theend;
8898 }
8899 spin->si_memtot += len;
8900 }
8901
8902 /* Write another byte to check for errors. */
8903 if (putc(0, fd) == EOF)
8904 EMSG(_(e_write));
8905
8906 vim_snprintf((char *)IObuff, IOSIZE,
8907 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8908 spell_message(spin, IObuff);
8909
8910theend:
8911 /* close the file */
8912 fclose(fd);
8913}
8914
8915/*
8916 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8917 * list and only contains text lines. Can use a swapfile to reduce memory
8918 * use.
8919 * Most other fields are invalid! Esp. watch out for string options being
8920 * NULL and there is no undo info.
8921 * Returns NULL when out of memory.
8922 */
8923 static buf_T *
8924open_spellbuf()
8925{
8926 buf_T *buf;
8927
8928 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8929 if (buf != NULL)
8930 {
8931 buf->b_spell = TRUE;
8932 buf->b_p_swf = TRUE; /* may create a swap file */
8933 ml_open(buf);
8934 ml_open_file(buf); /* create swap file now */
8935 }
8936 return buf;
8937}
8938
8939/*
8940 * Close the buffer used for spell info.
8941 */
8942 static void
8943close_spellbuf(buf)
8944 buf_T *buf;
8945{
8946 if (buf != NULL)
8947 {
8948 ml_close(buf, TRUE);
8949 vim_free(buf);
8950 }
8951}
8952
8953
8954/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008955 * Create a Vim spell file from one or more word lists.
8956 * "fnames[0]" is the output file name.
8957 * "fnames[fcount - 1]" is the last input file name.
8958 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8959 * and ".spl" is appended to make the output file name.
8960 */
8961 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008963 int fcount;
8964 char_u **fnames;
8965 int ascii; /* -ascii argument given */
8966 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008968{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008969 char_u fname[MAXPATHL];
8970 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008971 char_u **innames;
8972 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008973 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008974 int i;
8975 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008976 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008977 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008978 spellinfo_T spin;
8979
8980 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008982 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 spin.si_followup = TRUE;
8984 spin.si_rem_accents = TRUE;
8985 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008986 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8988 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008989 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008990 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008991 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008992 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008993
Bram Moolenaarb765d632005-06-07 21:00:02 +00008994 /* default: fnames[0] is output file, following are input files */
8995 innames = &fnames[1];
8996 incount = fcount - 1;
8997
8998 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008999 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009000 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009001 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9002 {
9003 /* For ":mkspell path/en.latin1.add" output file is
9004 * "path/en.latin1.add.spl". */
9005 innames = &fnames[0];
9006 incount = 1;
9007 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9008 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009009 else if (fcount == 1)
9010 {
9011 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9012 innames = &fnames[0];
9013 incount = 1;
9014 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9015 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9016 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009017 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9018 {
9019 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009021 }
9022 else
9023 /* Name should be language, make the file name from it. */
9024 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9025 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9026
9027 /* Check for .ascii.spl. */
9028 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9029 spin.si_ascii = TRUE;
9030
9031 /* Check for .add.spl. */
9032 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9033 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009034 }
9035
Bram Moolenaarb765d632005-06-07 21:00:02 +00009036 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009037 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009038 else if (vim_strchr(gettail(wfname), '_') != NULL)
9039 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009040 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009041 EMSG(_("E754: Only up to 8 regions supported"));
9042 else
9043 {
9044 /* Check for overwriting before doing things that may take a lot of
9045 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009046 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009047 {
9048 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009049 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009050 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009051 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009052 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009053 EMSG2(_(e_isadir2), wfname);
9054 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009055 }
9056
9057 /*
9058 * Init the aff and dic pointers.
9059 * Get the region names if there are more than 2 arguments.
9060 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009061 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009062 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009063 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009064
Bram Moolenaar3982c542005-06-08 21:56:31 +00009065 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009066 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009067 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009068 if (STRLEN(gettail(innames[i])) < 5
9069 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009070 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009071 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9072 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009073 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009074 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9075 spin.si_region_name[i * 2 + 1] =
9076 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009077 }
9078 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009079 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009080
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009081 spin.si_foldroot = wordtree_alloc(&spin);
9082 spin.si_keeproot = wordtree_alloc(&spin);
9083 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009084 if (spin.si_foldroot == NULL
9085 || spin.si_keeproot == NULL
9086 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009087 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009088 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009089 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009090 }
9091
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009092 /* When not producing a .add.spl file clear the character table when
9093 * we encounter one in the .aff file. This means we dump the current
9094 * one in the .spl file if the .aff file doesn't define one. That's
9095 * better than guessing the contents, the table will match a
9096 * previously loaded spell file. */
9097 if (!spin.si_add)
9098 spin.si_clear_chartab = TRUE;
9099
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009100 /*
9101 * Read all the .aff and .dic files.
9102 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009103 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009104 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009105 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009106 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009107 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009108 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109
Bram Moolenaarb765d632005-06-07 21:00:02 +00009110 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009111 if (mch_stat((char *)fname, &st) >= 0)
9112 {
9113 /* Read the .aff file. Will init "spin->si_conv" based on the
9114 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009115 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009116 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009117 error = TRUE;
9118 else
9119 {
9120 /* Read the .dic file and store the words in the trees. */
9121 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009122 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009123 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009124 error = TRUE;
9125 }
9126 }
9127 else
9128 {
9129 /* No .aff file, try reading the file as a word list. Store
9130 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009131 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009132 error = TRUE;
9133 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134
Bram Moolenaarb765d632005-06-07 21:00:02 +00009135#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009136 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009137 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009138#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009139 }
9140
Bram Moolenaar78622822005-08-23 21:00:13 +00009141 if (spin.si_compflags != NULL && spin.si_nobreak)
9142 MSG(_("Warning: both compounding and NOBREAK specified"));
9143
Bram Moolenaar4770d092006-01-12 23:22:24 +00009144 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009145 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009146 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009147 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009148 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009149 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009150 wordtree_compress(&spin, spin.si_foldroot);
9151 wordtree_compress(&spin, spin.si_keeproot);
9152 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009153 }
9154
Bram Moolenaar4770d092006-01-12 23:22:24 +00009155 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009156 {
9157 /*
9158 * Write the info in the spell file.
9159 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009160 vim_snprintf((char *)IObuff, IOSIZE,
9161 _("Writing spell file %s ..."), wfname);
9162 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009163
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009164 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009165
Bram Moolenaar4770d092006-01-12 23:22:24 +00009166 spell_message(&spin, (char_u *)_("Done!"));
9167 vim_snprintf((char *)IObuff, IOSIZE,
9168 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9169 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009170
Bram Moolenaar4770d092006-01-12 23:22:24 +00009171 /*
9172 * If the file is loaded need to reload it.
9173 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009174 if (!error)
9175 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009176 }
9177
9178 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009180 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 ga_clear(&spin.si_sal);
9182 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009183 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009184 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009185 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009186
9187 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009188 for (i = 0; i < incount; ++i)
9189 if (afile[i] != NULL)
9190 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009191
9192 /* Free all the bits and pieces at once. */
9193 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009194
9195 /*
9196 * If there is soundfolding info and no NOSUGFILE item create the
9197 * .sug file with the soundfolded word trie.
9198 */
9199 if (spin.si_sugtime != 0 && !error && !got_int)
9200 spell_make_sugfile(&spin, wfname);
9201
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009202 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009203}
9204
Bram Moolenaar4770d092006-01-12 23:22:24 +00009205/*
9206 * Display a message for spell file processing when 'verbose' is set or using
9207 * ":mkspell". "str" can be IObuff.
9208 */
9209 static void
9210spell_message(spin, str)
9211 spellinfo_T *spin;
9212 char_u *str;
9213{
9214 if (spin->si_verbose || p_verbose > 2)
9215 {
9216 if (!spin->si_verbose)
9217 verbose_enter();
9218 MSG(str);
9219 out_flush();
9220 if (!spin->si_verbose)
9221 verbose_leave();
9222 }
9223}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009224
9225/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009226 * ":[count]spellgood {word}"
9227 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009228 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009229 */
9230 void
9231ex_spell(eap)
9232 exarg_T *eap;
9233{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009234 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009235 eap->forceit ? 0 : (int)eap->line2,
9236 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009237}
9238
9239/*
9240 * Add "word[len]" to 'spellfile' as a good or bad word.
9241 */
9242 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00009243spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009244 char_u *word;
9245 int len;
9246 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009247 int index; /* "zG" and "zW": zero, otherwise index in
9248 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009249 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009250{
9251 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009252 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009253 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009254 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009255 char_u fnamebuf[MAXPATHL];
9256 char_u line[MAXWLEN * 2];
9257 long fpos, fpos_next = 0;
9258 int i;
9259 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009260
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009261 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009262 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009263 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009264 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009265 int_wordlist = vim_tempname('s');
9266 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009267 return;
9268 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009269 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009270 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009271 else
9272 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009273 /* If 'spellfile' isn't set figure out a good default value. */
9274 if (*curbuf->b_p_spf == NUL)
9275 {
9276 init_spellfile();
9277 new_spf = TRUE;
9278 }
9279
9280 if (*curbuf->b_p_spf == NUL)
9281 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009282 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009283 return;
9284 }
9285
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009286 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9287 {
9288 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9289 if (i == index)
9290 break;
9291 if (*spf == NUL)
9292 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00009293 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009294 return;
9295 }
9296 }
9297
Bram Moolenaarb765d632005-06-07 21:00:02 +00009298 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009299 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009300 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9301 buf = NULL;
9302 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009303 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009304 EMSG(_(e_bufloaded));
9305 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009306 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009307
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009308 fname = fnamebuf;
9309 }
9310
Bram Moolenaard0131a82006-03-04 21:46:13 +00009311 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009312 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009313 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009314 * since its flags sort before the one with WF_BANNED. */
9315 fd = mch_fopen((char *)fname, "r");
9316 if (fd != NULL)
9317 {
9318 while (!vim_fgets(line, MAXWLEN * 2, fd))
9319 {
9320 fpos = fpos_next;
9321 fpos_next = ftell(fd);
9322 if (STRNCMP(word, line, len) == 0
9323 && (line[len] == '/' || line[len] < ' '))
9324 {
9325 /* Found duplicate word. Remove it by writing a '#' at
9326 * the start of the line. Mixing reading and writing
9327 * doesn't work for all systems, close the file first. */
9328 fclose(fd);
9329 fd = mch_fopen((char *)fname, "r+");
9330 if (fd == NULL)
9331 break;
9332 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009333 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009334 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009335 if (undo)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009336 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009337 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009338 fseek(fd, fpos_next, SEEK_SET);
9339 }
9340 }
9341 fclose(fd);
9342 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009343 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009344 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00009345 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009346 fd = mch_fopen((char *)fname, "a");
9347 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009348 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009349 /* We just initialized the 'spellfile' option and can't open the
9350 * file. We may need to create the "spell" directory first. We
9351 * already checked the runtime directory is writable in
9352 * init_spellfile(). */
9353 if (!dir_of_file_exists(fname))
9354 {
9355 /* The directory doesn't exist. Try creating it and opening
9356 * the file again. */
9357 vim_mkdir(NameBuff, 0755);
9358 fd = mch_fopen((char *)fname, "a");
9359 }
9360 }
9361
9362 if (fd == NULL)
9363 EMSG2(_(e_notopen), fname);
9364 else
9365 {
9366 if (bad)
9367 fprintf(fd, "%.*s/!\n", len, word);
9368 else
9369 fprintf(fd, "%.*s\n", len, word);
9370 fclose(fd);
9371
9372 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9373 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009374 }
9375 }
9376
Bram Moolenaard0131a82006-03-04 21:46:13 +00009377 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009378 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009379 /* Update the .add.spl file. */
9380 mkspell(1, &fname, FALSE, TRUE, TRUE);
9381
9382 /* If the .add file is edited somewhere, reload it. */
9383 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009384 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009385
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009386 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009387 }
9388}
9389
9390/*
9391 * Initialize 'spellfile' for the current buffer.
9392 */
9393 static void
9394init_spellfile()
9395{
9396 char_u buf[MAXPATHL];
9397 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009398 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009399 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009400 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009401 int aspath = FALSE;
9402 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009403
9404 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9405 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009406 /* Find the end of the language name. Exclude the region. If there
9407 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009408 for (lend = curbuf->b_p_spl; *lend != NUL
9409 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009410 if (vim_ispathsep(*lend))
9411 {
9412 aspath = TRUE;
9413 lstart = lend + 1;
9414 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009415
9416 /* Loop over all entries in 'runtimepath'. Use the first one where we
9417 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009418 rtp = p_rtp;
9419 while (*rtp != NUL)
9420 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009421 if (aspath)
9422 /* Use directory of an entry with path, e.g., for
9423 * "/dir/lg.utf-8.spl" use "/dir". */
9424 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9425 else
9426 /* Copy the path from 'runtimepath' to buf[]. */
9427 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009428 if (filewritable(buf) == 2)
9429 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009430 /* Use the first language name from 'spelllang' and the
9431 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009432 if (aspath)
9433 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9434 else
9435 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009436 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009437 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009438 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9439 if (!filewritable(buf) != 2)
9440 vim_mkdir(buf, 0755);
9441
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009442 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009443 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009444 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009445 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009446 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009447 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9448 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9449 fname != NULL
9450 && strstr((char *)gettail(fname), ".ascii.") != NULL
9451 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009452 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9453 break;
9454 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009455 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009456 }
9457 }
9458}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009459
Bram Moolenaar51485f02005-06-04 21:55:20 +00009460
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009461/*
9462 * Init the chartab used for spelling for ASCII.
9463 * EBCDIC is not supported!
9464 */
9465 static void
9466clear_spell_chartab(sp)
9467 spelltab_T *sp;
9468{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009469 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009470
9471 /* Init everything to FALSE. */
9472 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9473 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9474 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009475 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009476 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009477 sp->st_upper[i] = i;
9478 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009479
9480 /* We include digits. A word shouldn't start with a digit, but handling
9481 * that is done separately. */
9482 for (i = '0'; i <= '9'; ++i)
9483 sp->st_isw[i] = TRUE;
9484 for (i = 'A'; i <= 'Z'; ++i)
9485 {
9486 sp->st_isw[i] = TRUE;
9487 sp->st_isu[i] = TRUE;
9488 sp->st_fold[i] = i + 0x20;
9489 }
9490 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009491 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009492 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009493 sp->st_upper[i] = i - 0x20;
9494 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009495}
9496
9497/*
9498 * Init the chartab used for spelling. Only depends on 'encoding'.
9499 * Called once while starting up and when 'encoding' changes.
9500 * The default is to use isalpha(), but the spell file should define the word
9501 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009502 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009503 */
9504 void
9505init_spell_chartab()
9506{
9507 int i;
9508
9509 did_set_spelltab = FALSE;
9510 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009511#ifdef FEAT_MBYTE
9512 if (enc_dbcs)
9513 {
9514 /* DBCS: assume double-wide characters are word characters. */
9515 for (i = 128; i <= 255; ++i)
9516 if (MB_BYTE2LEN(i) == 2)
9517 spelltab.st_isw[i] = TRUE;
9518 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009519 else if (enc_utf8)
9520 {
9521 for (i = 128; i < 256; ++i)
9522 {
9523 spelltab.st_isu[i] = utf_isupper(i);
9524 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9525 spelltab.st_fold[i] = utf_fold(i);
9526 spelltab.st_upper[i] = utf_toupper(i);
9527 }
9528 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009529 else
9530#endif
9531 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009532 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009533 for (i = 128; i < 256; ++i)
9534 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009535 if (MB_ISUPPER(i))
9536 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009537 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009538 spelltab.st_isu[i] = TRUE;
9539 spelltab.st_fold[i] = MB_TOLOWER(i);
9540 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009541 else if (MB_ISLOWER(i))
9542 {
9543 spelltab.st_isw[i] = TRUE;
9544 spelltab.st_upper[i] = MB_TOUPPER(i);
9545 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009546 }
9547 }
9548}
9549
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009550/*
9551 * Set the spell character tables from strings in the affix file.
9552 */
9553 static int
9554set_spell_chartab(fol, low, upp)
9555 char_u *fol;
9556 char_u *low;
9557 char_u *upp;
9558{
9559 /* We build the new tables here first, so that we can compare with the
9560 * previous one. */
9561 spelltab_T new_st;
9562 char_u *pf = fol, *pl = low, *pu = upp;
9563 int f, l, u;
9564
9565 clear_spell_chartab(&new_st);
9566
9567 while (*pf != NUL)
9568 {
9569 if (*pl == NUL || *pu == NUL)
9570 {
9571 EMSG(_(e_affform));
9572 return FAIL;
9573 }
9574#ifdef FEAT_MBYTE
9575 f = mb_ptr2char_adv(&pf);
9576 l = mb_ptr2char_adv(&pl);
9577 u = mb_ptr2char_adv(&pu);
9578#else
9579 f = *pf++;
9580 l = *pl++;
9581 u = *pu++;
9582#endif
9583 /* Every character that appears is a word character. */
9584 if (f < 256)
9585 new_st.st_isw[f] = TRUE;
9586 if (l < 256)
9587 new_st.st_isw[l] = TRUE;
9588 if (u < 256)
9589 new_st.st_isw[u] = TRUE;
9590
9591 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9592 * case-folding */
9593 if (l < 256 && l != f)
9594 {
9595 if (f >= 256)
9596 {
9597 EMSG(_(e_affrange));
9598 return FAIL;
9599 }
9600 new_st.st_fold[l] = f;
9601 }
9602
9603 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009604 * case-folding, it's upper case and the "UPP" is the upper case of
9605 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009606 if (u < 256 && u != f)
9607 {
9608 if (f >= 256)
9609 {
9610 EMSG(_(e_affrange));
9611 return FAIL;
9612 }
9613 new_st.st_fold[u] = f;
9614 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009615 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009616 }
9617 }
9618
9619 if (*pl != NUL || *pu != NUL)
9620 {
9621 EMSG(_(e_affform));
9622 return FAIL;
9623 }
9624
9625 return set_spell_finish(&new_st);
9626}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009627
9628/*
9629 * Set the spell character tables from strings in the .spl file.
9630 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009631 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009632set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009633 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009634 int cnt; /* length of "flags" */
9635 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009636{
9637 /* We build the new tables here first, so that we can compare with the
9638 * previous one. */
9639 spelltab_T new_st;
9640 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009641 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009642 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009643
9644 clear_spell_chartab(&new_st);
9645
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009646 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009647 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009648 if (i < cnt)
9649 {
9650 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9651 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9652 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009653
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009654 if (*p != NUL)
9655 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009656#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009657 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009658#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009659 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009660#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009661 new_st.st_fold[i + 128] = c;
9662 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9663 new_st.st_upper[c] = i + 128;
9664 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009665 }
9666
Bram Moolenaar5195e452005-08-19 20:32:47 +00009667 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009668}
9669
9670 static int
9671set_spell_finish(new_st)
9672 spelltab_T *new_st;
9673{
9674 int i;
9675
9676 if (did_set_spelltab)
9677 {
9678 /* check that it's the same table */
9679 for (i = 0; i < 256; ++i)
9680 {
9681 if (spelltab.st_isw[i] != new_st->st_isw[i]
9682 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009683 || spelltab.st_fold[i] != new_st->st_fold[i]
9684 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009685 {
9686 EMSG(_("E763: Word characters differ between spell files"));
9687 return FAIL;
9688 }
9689 }
9690 }
9691 else
9692 {
9693 /* copy the new spelltab into the one being used */
9694 spelltab = *new_st;
9695 did_set_spelltab = TRUE;
9696 }
9697
9698 return OK;
9699}
9700
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009701/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009702 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009703 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009704 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009705 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009706 */
9707 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009708spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009709 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009710 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009711{
Bram Moolenaarea408852005-06-25 22:49:46 +00009712#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009713 char_u *s;
9714 int l;
9715 int c;
9716
9717 if (has_mbyte)
9718 {
9719 l = MB_BYTE2LEN(*p);
9720 s = p;
9721 if (l == 1)
9722 {
9723 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009724 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009725 {
9726 s = p + 1; /* skip a mid-word character */
9727 l = MB_BYTE2LEN(*s);
9728 }
9729 }
9730 else
9731 {
9732 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009733 if (c < 256 ? buf->b_spell_ismw[c]
9734 : (buf->b_spell_ismw_mb != NULL
9735 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009736 {
9737 s = p + l;
9738 l = MB_BYTE2LEN(*s);
9739 }
9740 }
9741
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009742 c = mb_ptr2char(s);
9743 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009744 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009745 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009746 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009747#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009748
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009749 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9750}
9751
9752/*
9753 * Return TRUE if "p" points to a word character.
9754 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9755 */
9756 static int
9757spell_iswordp_nmw(p)
9758 char_u *p;
9759{
9760#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009761 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009762
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009763 if (has_mbyte)
9764 {
9765 c = mb_ptr2char(p);
9766 if (c > 255)
9767 return mb_get_class(p) >= 2;
9768 return spelltab.st_isw[c];
9769 }
9770#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009771 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009772}
9773
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009774#ifdef FEAT_MBYTE
9775/*
9776 * Return TRUE if "p" points to a word character.
9777 * Wide version of spell_iswordp().
9778 */
9779 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009780spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009781 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009782 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009783{
9784 int *s;
9785
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009786 if (*p < 256 ? buf->b_spell_ismw[*p]
9787 : (buf->b_spell_ismw_mb != NULL
9788 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009789 s = p + 1;
9790 else
9791 s = p;
9792
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009793 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009794 {
9795 if (enc_utf8)
9796 return utf_class(*s) >= 2;
9797 if (enc_dbcs)
9798 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9799 return 0;
9800 }
9801 return spelltab.st_isw[*s];
9802}
9803#endif
9804
Bram Moolenaarea408852005-06-25 22:49:46 +00009805/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009806 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009807 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009808 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009809 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009810write_spell_prefcond(fd, gap)
9811 FILE *fd;
9812 garray_T *gap;
9813{
9814 int i;
9815 char_u *p;
9816 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009817 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009818
Bram Moolenaar5195e452005-08-19 20:32:47 +00009819 if (fd != NULL)
9820 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9821
9822 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009823
9824 for (i = 0; i < gap->ga_len; ++i)
9825 {
9826 /* <prefcond> : <condlen> <condstr> */
9827 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009828 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009829 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009830 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009831 if (fd != NULL)
9832 {
9833 fputc(len, fd);
9834 fwrite(p, (size_t)len, (size_t)1, fd);
9835 }
9836 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009837 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009838 else if (fd != NULL)
9839 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009840 }
9841
Bram Moolenaar5195e452005-08-19 20:32:47 +00009842 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009843}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009844
9845/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009846 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9847 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009848 * When using a multi-byte 'encoding' the length may change!
9849 * Returns FAIL when something wrong.
9850 */
9851 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009852spell_casefold(str, len, buf, buflen)
9853 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009854 int len;
9855 char_u *buf;
9856 int buflen;
9857{
9858 int i;
9859
9860 if (len >= buflen)
9861 {
9862 buf[0] = NUL;
9863 return FAIL; /* result will not fit */
9864 }
9865
9866#ifdef FEAT_MBYTE
9867 if (has_mbyte)
9868 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009869 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009870 char_u *p;
9871 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009872
9873 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009874 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009875 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009876 if (outi + MB_MAXBYTES > buflen)
9877 {
9878 buf[outi] = NUL;
9879 return FAIL;
9880 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009881 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009882 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009883 }
9884 buf[outi] = NUL;
9885 }
9886 else
9887#endif
9888 {
9889 /* Be quick for non-multibyte encodings. */
9890 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009891 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009892 buf[i] = NUL;
9893 }
9894
9895 return OK;
9896}
9897
Bram Moolenaar4770d092006-01-12 23:22:24 +00009898/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009899#define SPS_BEST 1
9900#define SPS_FAST 2
9901#define SPS_DOUBLE 4
9902
Bram Moolenaar4770d092006-01-12 23:22:24 +00009903static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9904static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009905
9906/*
9907 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009908 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009909 */
9910 int
9911spell_check_sps()
9912{
9913 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009914 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009915 char_u buf[MAXPATHL];
9916 int f;
9917
9918 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009919 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009920
9921 for (p = p_sps; *p != NUL; )
9922 {
9923 copy_option_part(&p, buf, MAXPATHL, ",");
9924
9925 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009926 if (VIM_ISDIGIT(*buf))
9927 {
9928 s = buf;
9929 sps_limit = getdigits(&s);
9930 if (*s != NUL && !VIM_ISDIGIT(*s))
9931 f = -1;
9932 }
9933 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009934 f = SPS_BEST;
9935 else if (STRCMP(buf, "fast") == 0)
9936 f = SPS_FAST;
9937 else if (STRCMP(buf, "double") == 0)
9938 f = SPS_DOUBLE;
9939 else if (STRNCMP(buf, "expr:", 5) != 0
9940 && STRNCMP(buf, "file:", 5) != 0)
9941 f = -1;
9942
9943 if (f == -1 || (sps_flags != 0 && f != 0))
9944 {
9945 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009946 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009947 return FAIL;
9948 }
9949 if (f != 0)
9950 sps_flags = f;
9951 }
9952
9953 if (sps_flags == 0)
9954 sps_flags = SPS_BEST;
9955
9956 return OK;
9957}
9958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009959/*
9960 * "z?": Find badly spelled word under or after the cursor.
9961 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009962 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009963 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964 */
9965 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009966spell_suggest(count)
9967 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968{
9969 char_u *line;
9970 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009971 char_u wcopy[MAXWLEN + 2];
9972 char_u *p;
9973 int i;
9974 int c;
9975 suginfo_T sug;
9976 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009977 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009978 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009979 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009980 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009981 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009983 if (no_spell_checking(curwin))
9984 return;
9985
9986#ifdef FEAT_VISUAL
9987 if (VIsual_active)
9988 {
9989 /* Use the Visually selected text as the bad word. But reject
9990 * a multi-line selection. */
9991 if (curwin->w_cursor.lnum != VIsual.lnum)
9992 {
9993 vim_beep();
9994 return;
9995 }
9996 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9997 if (badlen < 0)
9998 badlen = -badlen;
9999 else
10000 curwin->w_cursor.col = VIsual.col;
10001 ++badlen;
10002 end_visual_mode();
10003 }
10004 else
10005#endif
10006 /* Find the start of the badly spelled word. */
10007 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010008 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010010 /* No bad word or it starts after the cursor: use the word under the
10011 * cursor. */
10012 curwin->w_cursor = prev_cursor;
10013 line = ml_get_curline();
10014 p = line + curwin->w_cursor.col;
10015 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010016 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010017 mb_ptr_back(line, p);
10018 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010019 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010020 mb_ptr_adv(p);
10021
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010022 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010023 {
10024 beep_flush();
10025 return;
10026 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010027 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010028 }
10029
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010030 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010031
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010032 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010033 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010034
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010035 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010036
Bram Moolenaar5195e452005-08-19 20:32:47 +000010037 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10038 * 'spellsuggest', whatever is smaller. */
10039 if (sps_limit > (int)Rows - 2)
10040 limit = (int)Rows - 2;
10041 else
10042 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010043 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010044 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045
10046 if (sug.su_ga.ga_len == 0)
10047 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010048 else if (count > 0)
10049 {
10050 if (count > sug.su_ga.ga_len)
10051 smsg((char_u *)_("Sorry, only %ld suggestions"),
10052 (long)sug.su_ga.ga_len);
10053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010054 else
10055 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010056 vim_free(repl_from);
10057 repl_from = NULL;
10058 vim_free(repl_to);
10059 repl_to = NULL;
10060
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010061#ifdef FEAT_RIGHTLEFT
10062 /* When 'rightleft' is set the list is drawn right-left. */
10063 cmdmsg_rl = curwin->w_p_rl;
10064 if (cmdmsg_rl)
10065 msg_col = Columns - 1;
10066#endif
10067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 /* List the suggestions. */
10069 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010070 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010071 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10072 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010073#ifdef FEAT_RIGHTLEFT
10074 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10075 {
10076 /* And now the rabbit from the high hat: Avoid showing the
10077 * untranslated message rightleft. */
10078 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10079 sug.su_badlen, sug.su_badptr);
10080 }
10081#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 msg_puts(IObuff);
10083 msg_clr_eos();
10084 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 msg_scroll = TRUE;
10087 for (i = 0; i < sug.su_ga.ga_len; ++i)
10088 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010089 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090
10091 /* The suggested word may replace only part of the bad word, add
10092 * the not replaced part. */
10093 STRCPY(wcopy, stp->st_word);
10094 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010095 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 sug.su_badptr + stp->st_orglen,
10097 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010098 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10099#ifdef FEAT_RIGHTLEFT
10100 if (cmdmsg_rl)
10101 rl_mirror(IObuff);
10102#endif
10103 msg_puts(IObuff);
10104
10105 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010106 msg_puts(IObuff);
10107
10108 /* The word may replace more than "su_badlen". */
10109 if (sug.su_badlen < stp->st_orglen)
10110 {
10111 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10112 stp->st_orglen, sug.su_badptr);
10113 msg_puts(IObuff);
10114 }
10115
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010116 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010117 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010118 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010119 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010120 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010121 stp->st_salscore ? "s " : "",
10122 stp->st_score, stp->st_altscore);
10123 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010124 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010125 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010126#ifdef FEAT_RIGHTLEFT
10127 if (cmdmsg_rl)
10128 /* Mirror the numbers, but keep the leading space. */
10129 rl_mirror(IObuff + 1);
10130#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010131 msg_advance(30);
10132 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010133 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 msg_putchar('\n');
10135 }
10136
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010137#ifdef FEAT_RIGHTLEFT
10138 cmdmsg_rl = FALSE;
10139 msg_col = 0;
10140#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010142 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010143 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010144 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010145 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010146 }
10147
Bram Moolenaard12a1322005-08-21 22:08:24 +000010148 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10149 {
10150 /* Save the from and to text for :spellrepall. */
10151 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010152 if (sug.su_badlen > stp->st_orglen)
10153 {
10154 /* Replacing less than "su_badlen", append the remainder to
10155 * repl_to. */
10156 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10157 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10158 sug.su_badlen - stp->st_orglen,
10159 sug.su_badptr + stp->st_orglen);
10160 repl_to = vim_strsave(IObuff);
10161 }
10162 else
10163 {
10164 /* Replacing su_badlen or more, use the whole word. */
10165 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10166 repl_to = vim_strsave(stp->st_word);
10167 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010168
10169 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010170 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010171 if (p != NULL)
10172 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010173 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010174 mch_memmove(p, line, c);
10175 STRCPY(p + c, stp->st_word);
10176 STRCAT(p, sug.su_badptr + stp->st_orglen);
10177 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10178 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010179
10180 /* For redo we use a change-word command. */
10181 ResetRedobuff();
10182 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010183 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010184 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010185 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010186
10187 /* After this "p" may be invalid. */
10188 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010189 }
10190 }
10191 else
10192 curwin->w_cursor = prev_cursor;
10193
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010194 spell_find_cleanup(&sug);
10195}
10196
10197/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010198 * Check if the word at line "lnum" column "col" is required to start with a
10199 * capital. This uses 'spellcapcheck' of the current buffer.
10200 */
10201 static int
10202check_need_cap(lnum, col)
10203 linenr_T lnum;
10204 colnr_T col;
10205{
10206 int need_cap = FALSE;
10207 char_u *line;
10208 char_u *line_copy = NULL;
10209 char_u *p;
10210 colnr_T endcol;
10211 regmatch_T regmatch;
10212
10213 if (curbuf->b_cap_prog == NULL)
10214 return FALSE;
10215
10216 line = ml_get_curline();
10217 endcol = 0;
10218 if ((int)(skipwhite(line) - line) >= (int)col)
10219 {
10220 /* At start of line, check if previous line is empty or sentence
10221 * ends there. */
10222 if (lnum == 1)
10223 need_cap = TRUE;
10224 else
10225 {
10226 line = ml_get(lnum - 1);
10227 if (*skipwhite(line) == NUL)
10228 need_cap = TRUE;
10229 else
10230 {
10231 /* Append a space in place of the line break. */
10232 line_copy = concat_str(line, (char_u *)" ");
10233 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010234 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010235 }
10236 }
10237 }
10238 else
10239 endcol = col;
10240
10241 if (endcol > 0)
10242 {
10243 /* Check if sentence ends before the bad word. */
10244 regmatch.regprog = curbuf->b_cap_prog;
10245 regmatch.rm_ic = FALSE;
10246 p = line + endcol;
10247 for (;;)
10248 {
10249 mb_ptr_back(line, p);
10250 if (p == line || spell_iswordp_nmw(p))
10251 break;
10252 if (vim_regexec(&regmatch, p, 0)
10253 && regmatch.endp[0] == line + endcol)
10254 {
10255 need_cap = TRUE;
10256 break;
10257 }
10258 }
10259 }
10260
10261 vim_free(line_copy);
10262
10263 return need_cap;
10264}
10265
10266
10267/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010268 * ":spellrepall"
10269 */
10270/*ARGSUSED*/
10271 void
10272ex_spellrepall(eap)
10273 exarg_T *eap;
10274{
10275 pos_T pos = curwin->w_cursor;
10276 char_u *frompat;
10277 int addlen;
10278 char_u *line;
10279 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010280 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010281 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010282
10283 if (repl_from == NULL || repl_to == NULL)
10284 {
10285 EMSG(_("E752: No previous spell replacement"));
10286 return;
10287 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010288 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010290 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010291 if (frompat == NULL)
10292 return;
10293 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10294 p_ws = FALSE;
10295
Bram Moolenaar5195e452005-08-19 20:32:47 +000010296 sub_nsubs = 0;
10297 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010298 curwin->w_cursor.lnum = 0;
10299 while (!got_int)
10300 {
10301 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10302 || u_save_cursor() == FAIL)
10303 break;
10304
10305 /* Only replace when the right word isn't there yet. This happens
10306 * when changing "etc" to "etc.". */
10307 line = ml_get_curline();
10308 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10309 repl_to, STRLEN(repl_to)) != 0)
10310 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010311 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010312 if (p == NULL)
10313 break;
10314 mch_memmove(p, line, curwin->w_cursor.col);
10315 STRCPY(p + curwin->w_cursor.col, repl_to);
10316 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10317 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10318 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010319
10320 if (curwin->w_cursor.lnum != prev_lnum)
10321 {
10322 ++sub_nlines;
10323 prev_lnum = curwin->w_cursor.lnum;
10324 }
10325 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010326 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010327 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010328 }
10329
10330 p_ws = save_ws;
10331 curwin->w_cursor = pos;
10332 vim_free(frompat);
10333
Bram Moolenaar5195e452005-08-19 20:32:47 +000010334 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010335 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010336 else
10337 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010338}
10339
10340/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010341 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10342 * a list of allocated strings.
10343 */
10344 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010345spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010346 garray_T *gap;
10347 char_u *word;
10348 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010349 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010350 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010351{
10352 suginfo_T sug;
10353 int i;
10354 suggest_T *stp;
10355 char_u *wcopy;
10356
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010357 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010358
10359 /* Make room in "gap". */
10360 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010361 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010362 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010363 for (i = 0; i < sug.su_ga.ga_len; ++i)
10364 {
10365 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010366
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010367 /* The suggested word may replace only part of "word", add the not
10368 * replaced part. */
10369 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010370 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010371 if (wcopy == NULL)
10372 break;
10373 STRCPY(wcopy, stp->st_word);
10374 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10375 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10376 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010377 }
10378
10379 spell_find_cleanup(&sug);
10380}
10381
10382/*
10383 * Find spell suggestions for the word at the start of "badptr".
10384 * Return the suggestions in "su->su_ga".
10385 * The maximum number of suggestions is "maxcount".
10386 * Note: does use info for the current window.
10387 * This is based on the mechanisms of Aspell, but completely reimplemented.
10388 */
10389 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010390spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010391 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010392 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010393 suginfo_T *su;
10394 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010395 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010396 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010397 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010398{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010399 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010400 char_u buf[MAXPATHL];
10401 char_u *p;
10402 int do_combine = FALSE;
10403 char_u *sps_copy;
10404#ifdef FEAT_EVAL
10405 static int expr_busy = FALSE;
10406#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010407 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010408 int i;
10409 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010410
10411 /*
10412 * Set the info in "*su".
10413 */
10414 vim_memset(su, 0, sizeof(suginfo_T));
10415 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10416 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010417 if (*badptr == NUL)
10418 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010419 hash_init(&su->su_banned);
10420
10421 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010422 if (badlen != 0)
10423 su->su_badlen = badlen;
10424 else
10425 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010426 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010427 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010428
10429 if (su->su_badlen >= MAXWLEN)
10430 su->su_badlen = MAXWLEN - 1; /* just in case */
10431 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10432 (void)spell_casefold(su->su_badptr, su->su_badlen,
10433 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010434 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010435 su->su_badflags = badword_captype(su->su_badptr,
10436 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010437 if (need_cap)
10438 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010439
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010440 /* Find the default language for sound folding. We simply use the first
10441 * one in 'spelllang' that supports sound folding. That's good for when
10442 * using multiple files for one language, it's not that bad when mixing
10443 * languages (e.g., "pl,en"). */
10444 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10445 {
10446 lp = LANGP_ENTRY(curbuf->b_langp, i);
10447 if (lp->lp_sallang != NULL)
10448 {
10449 su->su_sallang = lp->lp_sallang;
10450 break;
10451 }
10452 }
10453
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010454 /* Soundfold the bad word with the default sound folding, so that we don't
10455 * have to do this many times. */
10456 if (su->su_sallang != NULL)
10457 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10458 su->su_sal_badword);
10459
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010460 /* If the word is not capitalised and spell_check() doesn't consider the
10461 * word to be bad then it might need to be capitalised. Add a suggestion
10462 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010463 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010464 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010465 {
10466 make_case_word(su->su_badword, buf, WF_ONECAP);
10467 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010468 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010469 }
10470
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010471 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010472 if (banbadword)
10473 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010474
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010475 /* Make a copy of 'spellsuggest', because the expression may change it. */
10476 sps_copy = vim_strsave(p_sps);
10477 if (sps_copy == NULL)
10478 return;
10479
10480 /* Loop over the items in 'spellsuggest'. */
10481 for (p = sps_copy; *p != NUL; )
10482 {
10483 copy_option_part(&p, buf, MAXPATHL, ",");
10484
10485 if (STRNCMP(buf, "expr:", 5) == 0)
10486 {
10487#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010488 /* Evaluate an expression. Skip this when called recursively,
10489 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010490 if (!expr_busy)
10491 {
10492 expr_busy = TRUE;
10493 spell_suggest_expr(su, buf + 5);
10494 expr_busy = FALSE;
10495 }
10496#endif
10497 }
10498 else if (STRNCMP(buf, "file:", 5) == 0)
10499 /* Use list of suggestions in a file. */
10500 spell_suggest_file(su, buf + 5);
10501 else
10502 {
10503 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010504 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010505 if (sps_flags & SPS_DOUBLE)
10506 do_combine = TRUE;
10507 }
10508 }
10509
10510 vim_free(sps_copy);
10511
10512 if (do_combine)
10513 /* Combine the two list of suggestions. This must be done last,
10514 * because sorting changes the order again. */
10515 score_combine(su);
10516}
10517
10518#ifdef FEAT_EVAL
10519/*
10520 * Find suggestions by evaluating expression "expr".
10521 */
10522 static void
10523spell_suggest_expr(su, expr)
10524 suginfo_T *su;
10525 char_u *expr;
10526{
10527 list_T *list;
10528 listitem_T *li;
10529 int score;
10530 char_u *p;
10531
10532 /* The work is split up in a few parts to avoid having to export
10533 * suginfo_T.
10534 * First evaluate the expression and get the resulting list. */
10535 list = eval_spell_expr(su->su_badword, expr);
10536 if (list != NULL)
10537 {
10538 /* Loop over the items in the list. */
10539 for (li = list->lv_first; li != NULL; li = li->li_next)
10540 if (li->li_tv.v_type == VAR_LIST)
10541 {
10542 /* Get the word and the score from the items. */
10543 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010544 if (score >= 0 && score <= su->su_maxscore)
10545 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10546 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010547 }
10548 list_unref(list);
10549 }
10550
Bram Moolenaar4770d092006-01-12 23:22:24 +000010551 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10552 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010553 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10554}
10555#endif
10556
10557/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010558 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010559 */
10560 static void
10561spell_suggest_file(su, fname)
10562 suginfo_T *su;
10563 char_u *fname;
10564{
10565 FILE *fd;
10566 char_u line[MAXWLEN * 2];
10567 char_u *p;
10568 int len;
10569 char_u cword[MAXWLEN];
10570
10571 /* Open the file. */
10572 fd = mch_fopen((char *)fname, "r");
10573 if (fd == NULL)
10574 {
10575 EMSG2(_(e_notopen), fname);
10576 return;
10577 }
10578
10579 /* Read it line by line. */
10580 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10581 {
10582 line_breakcheck();
10583
10584 p = vim_strchr(line, '/');
10585 if (p == NULL)
10586 continue; /* No Tab found, just skip the line. */
10587 *p++ = NUL;
10588 if (STRICMP(su->su_badword, line) == 0)
10589 {
10590 /* Match! Isolate the good word, until CR or NL. */
10591 for (len = 0; p[len] >= ' '; ++len)
10592 ;
10593 p[len] = NUL;
10594
10595 /* If the suggestion doesn't have specific case duplicate the case
10596 * of the bad word. */
10597 if (captype(p, NULL) == 0)
10598 {
10599 make_case_word(p, cword, su->su_badflags);
10600 p = cword;
10601 }
10602
10603 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010604 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010605 }
10606 }
10607
10608 fclose(fd);
10609
Bram Moolenaar4770d092006-01-12 23:22:24 +000010610 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10611 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010612 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10613}
10614
10615/*
10616 * Find suggestions for the internal method indicated by "sps_flags".
10617 */
10618 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010619spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010620 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010621 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010622{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010623 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010624 * Load the .sug file(s) that are available and not done yet.
10625 */
10626 suggest_load_files();
10627
10628 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010629 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010630 *
10631 * Set a maximum score to limit the combination of operations that is
10632 * tried.
10633 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010634 suggest_try_special(su);
10635
10636 /*
10637 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10638 * from the .aff file and inserting a space (split the word).
10639 */
10640 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010641
10642 /* For the resulting top-scorers compute the sound-a-like score. */
10643 if (sps_flags & SPS_DOUBLE)
10644 score_comp_sal(su);
10645
10646 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010647 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010648 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010649 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010650 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010651 if (sps_flags & SPS_BEST)
10652 /* Adjust the word score for the suggestions found so far for how
10653 * they sounds like. */
10654 rescore_suggestions(su);
10655
10656 /*
10657 * While going throught the soundfold tree "su_maxscore" is the score
10658 * for the soundfold word, limits the changes that are being tried,
10659 * and "su_sfmaxscore" the rescored score, which is set by
10660 * cleanup_suggestions().
10661 * First find words with a small edit distance, because this is much
10662 * faster and often already finds the top-N suggestions. If we didn't
10663 * find many suggestions try again with a higher edit distance.
10664 * "sl_sounddone" is used to avoid doing the same word twice.
10665 */
10666 suggest_try_soundalike_prep();
10667 su->su_maxscore = SCORE_SFMAX1;
10668 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010669 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010670 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10671 {
10672 /* We didn't find enough matches, try again, allowing more
10673 * changes to the soundfold word. */
10674 su->su_maxscore = SCORE_SFMAX2;
10675 suggest_try_soundalike(su);
10676 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10677 {
10678 /* Still didn't find enough matches, try again, allowing even
10679 * more changes to the soundfold word. */
10680 su->su_maxscore = SCORE_SFMAX3;
10681 suggest_try_soundalike(su);
10682 }
10683 }
10684 su->su_maxscore = su->su_sfmaxscore;
10685 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010686 }
10687
Bram Moolenaar4770d092006-01-12 23:22:24 +000010688 /* When CTRL-C was hit while searching do show the results. Only clear
10689 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010690 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010691 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010692 {
10693 (void)vgetc();
10694 got_int = FALSE;
10695 }
10696
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010697 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010698 {
10699 if (sps_flags & SPS_BEST)
10700 /* Adjust the word score for how it sounds like. */
10701 rescore_suggestions(su);
10702
Bram Moolenaar4770d092006-01-12 23:22:24 +000010703 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10704 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010705 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010706 }
10707}
10708
10709/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010710 * Load the .sug files for languages that have one and weren't loaded yet.
10711 */
10712 static void
10713suggest_load_files()
10714{
10715 langp_T *lp;
10716 int lpi;
10717 slang_T *slang;
10718 char_u *dotp;
10719 FILE *fd;
10720 char_u buf[MAXWLEN];
10721 int i;
10722 time_t timestamp;
10723 int wcount;
10724 int wordnr;
10725 garray_T ga;
10726 int c;
10727
10728 /* Do this for all languages that support sound folding. */
10729 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10730 {
10731 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10732 slang = lp->lp_slang;
10733 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10734 {
10735 /* Change ".spl" to ".sug" and open the file. When the file isn't
10736 * found silently skip it. Do set "sl_sugloaded" so that we
10737 * don't try again and again. */
10738 slang->sl_sugloaded = TRUE;
10739
10740 dotp = vim_strrchr(slang->sl_fname, '.');
10741 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10742 continue;
10743 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010744 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010745 if (fd == NULL)
10746 goto nextone;
10747
10748 /*
10749 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10750 */
10751 for (i = 0; i < VIMSUGMAGICL; ++i)
10752 buf[i] = getc(fd); /* <fileID> */
10753 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10754 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010755 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010756 slang->sl_fname);
10757 goto nextone;
10758 }
10759 c = getc(fd); /* <versionnr> */
10760 if (c < VIMSUGVERSION)
10761 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010762 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010763 slang->sl_fname);
10764 goto nextone;
10765 }
10766 else if (c > VIMSUGVERSION)
10767 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010768 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010769 slang->sl_fname);
10770 goto nextone;
10771 }
10772
10773 /* Check the timestamp, it must be exactly the same as the one in
10774 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010775 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010776 if (timestamp != slang->sl_sugtime)
10777 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010778 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010779 slang->sl_fname);
10780 goto nextone;
10781 }
10782
10783 /*
10784 * <SUGWORDTREE>: <wordtree>
10785 * Read the trie with the soundfolded words.
10786 */
10787 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10788 FALSE, 0) != 0)
10789 {
10790someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010791 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010792 slang->sl_fname);
10793 slang_clear_sug(slang);
10794 goto nextone;
10795 }
10796
10797 /*
10798 * <SUGTABLE>: <sugwcount> <sugline> ...
10799 *
10800 * Read the table with word numbers. We use a file buffer for
10801 * this, because it's so much like a file with lines. Makes it
10802 * possible to swap the info and save on memory use.
10803 */
10804 slang->sl_sugbuf = open_spellbuf();
10805 if (slang->sl_sugbuf == NULL)
10806 goto someerror;
10807 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010808 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010809 if (wcount < 0)
10810 goto someerror;
10811
10812 /* Read all the wordnr lists into the buffer, one NUL terminated
10813 * list per line. */
10814 ga_init2(&ga, 1, 100);
10815 for (wordnr = 0; wordnr < wcount; ++wordnr)
10816 {
10817 ga.ga_len = 0;
10818 for (;;)
10819 {
10820 c = getc(fd); /* <sugline> */
10821 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10822 goto someerror;
10823 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10824 if (c == NUL)
10825 break;
10826 }
10827 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10828 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10829 goto someerror;
10830 }
10831 ga_clear(&ga);
10832
10833 /*
10834 * Need to put word counts in the word tries, so that we can find
10835 * a word by its number.
10836 */
10837 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10838 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10839
10840nextone:
10841 if (fd != NULL)
10842 fclose(fd);
10843 STRCPY(dotp, ".spl");
10844 }
10845 }
10846}
10847
10848
10849/*
10850 * Fill in the wordcount fields for a trie.
10851 * Returns the total number of words.
10852 */
10853 static void
10854tree_count_words(byts, idxs)
10855 char_u *byts;
10856 idx_T *idxs;
10857{
10858 int depth;
10859 idx_T arridx[MAXWLEN];
10860 int curi[MAXWLEN];
10861 int c;
10862 idx_T n;
10863 int wordcount[MAXWLEN];
10864
10865 arridx[0] = 0;
10866 curi[0] = 1;
10867 wordcount[0] = 0;
10868 depth = 0;
10869 while (depth >= 0 && !got_int)
10870 {
10871 if (curi[depth] > byts[arridx[depth]])
10872 {
10873 /* Done all bytes at this node, go up one level. */
10874 idxs[arridx[depth]] = wordcount[depth];
10875 if (depth > 0)
10876 wordcount[depth - 1] += wordcount[depth];
10877
10878 --depth;
10879 fast_breakcheck();
10880 }
10881 else
10882 {
10883 /* Do one more byte at this node. */
10884 n = arridx[depth] + curi[depth];
10885 ++curi[depth];
10886
10887 c = byts[n];
10888 if (c == 0)
10889 {
10890 /* End of word, count it. */
10891 ++wordcount[depth];
10892
10893 /* Skip over any other NUL bytes (same word with different
10894 * flags). */
10895 while (byts[n + 1] == 0)
10896 {
10897 ++n;
10898 ++curi[depth];
10899 }
10900 }
10901 else
10902 {
10903 /* Normal char, go one level deeper to count the words. */
10904 ++depth;
10905 arridx[depth] = idxs[n];
10906 curi[depth] = 1;
10907 wordcount[depth] = 0;
10908 }
10909 }
10910 }
10911}
10912
10913/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010914 * Free the info put in "*su" by spell_find_suggest().
10915 */
10916 static void
10917spell_find_cleanup(su)
10918 suginfo_T *su;
10919{
10920 int i;
10921
10922 /* Free the suggestions. */
10923 for (i = 0; i < su->su_ga.ga_len; ++i)
10924 vim_free(SUG(su->su_ga, i).st_word);
10925 ga_clear(&su->su_ga);
10926 for (i = 0; i < su->su_sga.ga_len; ++i)
10927 vim_free(SUG(su->su_sga, i).st_word);
10928 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929
10930 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010931 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932}
10933
10934/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010935 * Make a copy of "word", with the first letter upper or lower cased, to
10936 * "wcopy[MAXWLEN]". "word" must not be empty.
10937 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 */
10939 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010940onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 char_u *wcopy;
10943 int upper; /* TRUE: first letter made upper case */
10944{
10945 char_u *p;
10946 int c;
10947 int l;
10948
10949 p = word;
10950#ifdef FEAT_MBYTE
10951 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010952 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 else
10954#endif
10955 c = *p++;
10956 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010957 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010959 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960#ifdef FEAT_MBYTE
10961 if (has_mbyte)
10962 l = mb_char2bytes(c, wcopy);
10963 else
10964#endif
10965 {
10966 l = 1;
10967 wcopy[0] = c;
10968 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010969 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970}
10971
10972/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010973 * Make a copy of "word" with all the letters upper cased into
10974 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 */
10976 static void
10977allcap_copy(word, wcopy)
10978 char_u *word;
10979 char_u *wcopy;
10980{
10981 char_u *s;
10982 char_u *d;
10983 int c;
10984
10985 d = wcopy;
10986 for (s = word; *s != NUL; )
10987 {
10988#ifdef FEAT_MBYTE
10989 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010990 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 else
10992#endif
10993 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010994
10995#ifdef FEAT_MBYTE
10996 /* We only change ß to SS when we are certain latin1 is used. It
10997 * would cause weird errors in other 8-bit encodings. */
10998 if (enc_latin1like && c == 0xdf)
10999 {
11000 c = 'S';
11001 if (d - wcopy >= MAXWLEN - 1)
11002 break;
11003 *d++ = c;
11004 }
11005 else
11006#endif
11007 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008
11009#ifdef FEAT_MBYTE
11010 if (has_mbyte)
11011 {
11012 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11013 break;
11014 d += mb_char2bytes(c, d);
11015 }
11016 else
11017#endif
11018 {
11019 if (d - wcopy >= MAXWLEN - 1)
11020 break;
11021 *d++ = c;
11022 }
11023 }
11024 *d = NUL;
11025}
11026
11027/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011028 * Try finding suggestions by recognizing specific situations.
11029 */
11030 static void
11031suggest_try_special(su)
11032 suginfo_T *su;
11033{
11034 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011035 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011036 int c;
11037 char_u word[MAXWLEN];
11038
11039 /*
11040 * Recognize a word that is repeated: "the the".
11041 */
11042 p = skiptowhite(su->su_fbadword);
11043 len = p - su->su_fbadword;
11044 p = skipwhite(p);
11045 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11046 {
11047 /* Include badflags: if the badword is onecap or allcap
11048 * use that for the goodword too: "The the" -> "The". */
11049 c = su->su_fbadword[len];
11050 su->su_fbadword[len] = NUL;
11051 make_case_word(su->su_fbadword, word, su->su_badflags);
11052 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011053
11054 /* Give a soundalike score of 0, compute the score as if deleting one
11055 * character. */
11056 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011057 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011058 }
11059}
11060
11061/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011062 * Try finding suggestions by adding/removing/swapping letters.
11063 */
11064 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011065suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 suginfo_T *su;
11067{
11068 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011069 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011071 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011072 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073
11074 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011075 * to find matches (esp. REP items). Append some more text, changing
11076 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011078 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011079 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011080 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011082 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011084 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011085
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011086 /* If reloading a spell file fails it's still in the list but
11087 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011088 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011089 continue;
11090
Bram Moolenaar4770d092006-01-12 23:22:24 +000011091 /* Try it for this language. Will add possible suggestions. */
11092 suggest_trie_walk(su, lp, fword, FALSE);
11093 }
11094}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095
Bram Moolenaar4770d092006-01-12 23:22:24 +000011096/* Check the maximum score, if we go over it we won't try this change. */
11097#define TRY_DEEPER(su, stack, depth, add) \
11098 (stack[depth].ts_score + (add) < su->su_maxscore)
11099
11100/*
11101 * Try finding suggestions by adding/removing/swapping letters.
11102 *
11103 * This uses a state machine. At each node in the tree we try various
11104 * operations. When trying if an operation works "depth" is increased and the
11105 * stack[] is used to store info. This allows combinations, thus insert one
11106 * character, replace one and delete another. The number of changes is
11107 * limited by su->su_maxscore.
11108 *
11109 * After implementing this I noticed an article by Kemal Oflazer that
11110 * describes something similar: "Error-tolerant Finite State Recognition with
11111 * Applications to Morphological Analysis and Spelling Correction" (1996).
11112 * The implementation in the article is simplified and requires a stack of
11113 * unknown depth. The implementation here only needs a stack depth equal to
11114 * the length of the word.
11115 *
11116 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11117 * The mechanism is the same, but we find a match with a sound-folded word
11118 * that comes from one or more original words. Each of these words may be
11119 * added, this is done by add_sound_suggest().
11120 * Don't use:
11121 * the prefix tree or the keep-case tree
11122 * "su->su_badlen"
11123 * anything to do with upper and lower case
11124 * anything to do with word or non-word characters ("spell_iswordp()")
11125 * banned words
11126 * word flags (rare, region, compounding)
11127 * word splitting for now
11128 * "similar_chars()"
11129 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11130 */
11131 static void
11132suggest_trie_walk(su, lp, fword, soundfold)
11133 suginfo_T *su;
11134 langp_T *lp;
11135 char_u *fword;
11136 int soundfold;
11137{
11138 char_u tword[MAXWLEN]; /* good word collected so far */
11139 trystate_T stack[MAXWLEN];
11140 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11141 * concatanation of prefix compound
11142 * words and split word. NUL terminated
11143 * when going deeper but not when coming
11144 * back. */
11145 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11146 trystate_T *sp;
11147 int newscore;
11148 int score;
11149 char_u *byts, *fbyts, *pbyts;
11150 idx_T *idxs, *fidxs, *pidxs;
11151 int depth;
11152 int c, c2, c3;
11153 int n = 0;
11154 int flags;
11155 garray_T *gap;
11156 idx_T arridx;
11157 int len;
11158 char_u *p;
11159 fromto_T *ftp;
11160 int fl = 0, tl;
11161 int repextra = 0; /* extra bytes in fword[] from REP item */
11162 slang_T *slang = lp->lp_slang;
11163 int fword_ends;
11164 int goodword_ends;
11165#ifdef DEBUG_TRIEWALK
11166 /* Stores the name of the change made at each level. */
11167 char_u changename[MAXWLEN][80];
11168#endif
11169 int breakcheckcount = 1000;
11170 int compound_ok;
11171
11172 /*
11173 * Go through the whole case-fold tree, try changes at each node.
11174 * "tword[]" contains the word collected from nodes in the tree.
11175 * "fword[]" the word we are trying to match with (initially the bad
11176 * word).
11177 */
11178 depth = 0;
11179 sp = &stack[0];
11180 vim_memset(sp, 0, sizeof(trystate_T));
11181 sp->ts_curi = 1;
11182
11183 if (soundfold)
11184 {
11185 /* Going through the soundfold tree. */
11186 byts = fbyts = slang->sl_sbyts;
11187 idxs = fidxs = slang->sl_sidxs;
11188 pbyts = NULL;
11189 pidxs = NULL;
11190 sp->ts_prefixdepth = PFD_NOPREFIX;
11191 sp->ts_state = STATE_START;
11192 }
11193 else
11194 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011195 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011196 * When there are postponed prefixes we need to use these first. At
11197 * the end of the prefix we continue in the case-fold tree.
11198 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011199 fbyts = slang->sl_fbyts;
11200 fidxs = slang->sl_fidxs;
11201 pbyts = slang->sl_pbyts;
11202 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011203 if (pbyts != NULL)
11204 {
11205 byts = pbyts;
11206 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011207 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011208 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11209 }
11210 else
11211 {
11212 byts = fbyts;
11213 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011214 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011215 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011216 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011217 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011218
Bram Moolenaar4770d092006-01-12 23:22:24 +000011219 /*
11220 * Loop to find all suggestions. At each round we either:
11221 * - For the current state try one operation, advance "ts_curi",
11222 * increase "depth".
11223 * - When a state is done go to the next, set "ts_state".
11224 * - When all states are tried decrease "depth".
11225 */
11226 while (depth >= 0 && !got_int)
11227 {
11228 sp = &stack[depth];
11229 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011231 case STATE_START:
11232 case STATE_NOPREFIX:
11233 /*
11234 * Start of node: Deal with NUL bytes, which means
11235 * tword[] may end here.
11236 */
11237 arridx = sp->ts_arridx; /* current node in the tree */
11238 len = byts[arridx]; /* bytes in this node */
11239 arridx += sp->ts_curi; /* index of current byte */
11240
11241 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011243 /* Skip over the NUL bytes, we use them later. */
11244 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11245 ;
11246 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247
Bram Moolenaar4770d092006-01-12 23:22:24 +000011248 /* Always past NUL bytes now. */
11249 n = (int)sp->ts_state;
11250 sp->ts_state = STATE_ENDNUL;
11251 sp->ts_save_badflags = su->su_badflags;
11252
11253 /* At end of a prefix or at start of prefixtree: check for
11254 * following word. */
11255 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011256 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011257 /* Set su->su_badflags to the caps type at this position.
11258 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011259#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011260 if (has_mbyte)
11261 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11262 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011263#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011264 n = sp->ts_fidx;
11265 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11266 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011267 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011268#ifdef DEBUG_TRIEWALK
11269 sprintf(changename[depth], "prefix");
11270#endif
11271 go_deeper(stack, depth, 0);
11272 ++depth;
11273 sp = &stack[depth];
11274 sp->ts_prefixdepth = depth - 1;
11275 byts = fbyts;
11276 idxs = fidxs;
11277 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011278
Bram Moolenaar4770d092006-01-12 23:22:24 +000011279 /* Move the prefix to preword[] with the right case
11280 * and make find_keepcap_word() works. */
11281 tword[sp->ts_twordlen] = NUL;
11282 make_case_word(tword + sp->ts_splitoff,
11283 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011284 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011286 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011287 break;
11288 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011289
Bram Moolenaar4770d092006-01-12 23:22:24 +000011290 if (sp->ts_curi > len || byts[arridx] != 0)
11291 {
11292 /* Past bytes in node and/or past NUL bytes. */
11293 sp->ts_state = STATE_ENDNUL;
11294 sp->ts_save_badflags = su->su_badflags;
11295 break;
11296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297
Bram Moolenaar4770d092006-01-12 23:22:24 +000011298 /*
11299 * End of word in tree.
11300 */
11301 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302
Bram Moolenaar4770d092006-01-12 23:22:24 +000011303 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011304
11305 /* Skip words with the NOSUGGEST flag. */
11306 if (flags & WF_NOSUGGEST)
11307 break;
11308
Bram Moolenaar4770d092006-01-12 23:22:24 +000011309 fword_ends = (fword[sp->ts_fidx] == NUL
11310 || (soundfold
11311 ? vim_iswhite(fword[sp->ts_fidx])
11312 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11313 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011314
Bram Moolenaar4770d092006-01-12 23:22:24 +000011315 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011316 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011317 {
11318 /* There was a prefix before the word. Check that the prefix
11319 * can be used with this word. */
11320 /* Count the length of the NULs in the prefix. If there are
11321 * none this must be the first try without a prefix. */
11322 n = stack[sp->ts_prefixdepth].ts_arridx;
11323 len = pbyts[n++];
11324 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11325 ;
11326 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011327 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011328 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011329 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011330 if (c == 0)
11331 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011332
Bram Moolenaar4770d092006-01-12 23:22:24 +000011333 /* Use the WF_RARE flag for a rare prefix. */
11334 if (c & WF_RAREPFX)
11335 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011336
Bram Moolenaar4770d092006-01-12 23:22:24 +000011337 /* Tricky: when checking for both prefix and compounding
11338 * we run into the prefix flag first.
11339 * Remember that it's OK, so that we accept the prefix
11340 * when arriving at a compound flag. */
11341 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011342 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011343 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011344
Bram Moolenaar4770d092006-01-12 23:22:24 +000011345 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11346 * appending another compound word below. */
11347 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011348 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011349 goodword_ends = FALSE;
11350 else
11351 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011352
Bram Moolenaar4770d092006-01-12 23:22:24 +000011353 p = NULL;
11354 compound_ok = TRUE;
11355 if (sp->ts_complen > sp->ts_compsplit)
11356 {
11357 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011358 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011359 /* There was a word before this word. When there was no
11360 * change in this word (it was correct) add the first word
11361 * as a suggestion. If this word was corrected too, we
11362 * need to check if a correct word follows. */
11363 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011364 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011365 && STRNCMP(fword + sp->ts_splitfidx,
11366 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011367 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011368 {
11369 preword[sp->ts_prewordlen] = NUL;
11370 newscore = score_wordcount_adj(slang, sp->ts_score,
11371 preword + sp->ts_prewordlen,
11372 sp->ts_prewordlen > 0);
11373 /* Add the suggestion if the score isn't too bad. */
11374 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011375 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011376 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011377 newscore, 0, FALSE,
11378 lp->lp_sallang, FALSE);
11379 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011380 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011381 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011382 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011383 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011384 /* There was a compound word before this word. If this
11385 * word does not support compounding then give up
11386 * (splitting is tried for the word without compound
11387 * flag). */
11388 if (((unsigned)flags >> 24) == 0
11389 || sp->ts_twordlen - sp->ts_splitoff
11390 < slang->sl_compminlen)
11391 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011392#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011393 /* For multi-byte chars check character length against
11394 * COMPOUNDMIN. */
11395 if (has_mbyte
11396 && slang->sl_compminlen > 0
11397 && mb_charlen(tword + sp->ts_splitoff)
11398 < slang->sl_compminlen)
11399 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011400#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011401
Bram Moolenaar4770d092006-01-12 23:22:24 +000011402 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11403 compflags[sp->ts_complen + 1] = NUL;
11404 vim_strncpy(preword + sp->ts_prewordlen,
11405 tword + sp->ts_splitoff,
11406 sp->ts_twordlen - sp->ts_splitoff);
11407 p = preword;
11408 while (*skiptowhite(p) != NUL)
11409 p = skipwhite(skiptowhite(p));
11410 if (fword_ends && !can_compound(slang, p,
11411 compflags + sp->ts_compsplit))
11412 /* Compound is not allowed. But it may still be
11413 * possible if we add another (short) word. */
11414 compound_ok = FALSE;
11415
11416 /* Get pointer to last char of previous word. */
11417 p = preword + sp->ts_prewordlen;
11418 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011419 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421
Bram Moolenaar4770d092006-01-12 23:22:24 +000011422 /*
11423 * Form the word with proper case in preword.
11424 * If there is a word from a previous split, append.
11425 * For the soundfold tree don't change the case, simply append.
11426 */
11427 if (soundfold)
11428 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11429 else if (flags & WF_KEEPCAP)
11430 /* Must find the word in the keep-case tree. */
11431 find_keepcap_word(slang, tword + sp->ts_splitoff,
11432 preword + sp->ts_prewordlen);
11433 else
11434 {
11435 /* Include badflags: If the badword is onecap or allcap
11436 * use that for the goodword too. But if the badword is
11437 * allcap and it's only one char long use onecap. */
11438 c = su->su_badflags;
11439 if ((c & WF_ALLCAP)
11440#ifdef FEAT_MBYTE
11441 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11442#else
11443 && su->su_badlen == 1
11444#endif
11445 )
11446 c = WF_ONECAP;
11447 c |= flags;
11448
11449 /* When appending a compound word after a word character don't
11450 * use Onecap. */
11451 if (p != NULL && spell_iswordp_nmw(p))
11452 c &= ~WF_ONECAP;
11453 make_case_word(tword + sp->ts_splitoff,
11454 preword + sp->ts_prewordlen, c);
11455 }
11456
11457 if (!soundfold)
11458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 /* Don't use a banned word. It may appear again as a good
11460 * word, thus remember it. */
11461 if (flags & WF_BANNED)
11462 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011463 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 break;
11465 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011466 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011467 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11468 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011469 {
11470 if (slang->sl_compprog == NULL)
11471 break;
11472 /* the word so far was banned but we may try compounding */
11473 goodword_ends = FALSE;
11474 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476
Bram Moolenaar4770d092006-01-12 23:22:24 +000011477 newscore = 0;
11478 if (!soundfold) /* soundfold words don't have flags */
11479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011481 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482 newscore += SCORE_REGION;
11483 if (flags & WF_RARE)
11484 newscore += SCORE_RARE;
11485
Bram Moolenaar0c405862005-06-22 22:26:26 +000011486 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011487 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490
Bram Moolenaar4770d092006-01-12 23:22:24 +000011491 /* TODO: how about splitting in the soundfold tree? */
11492 if (fword_ends
11493 && goodword_ends
11494 && sp->ts_fidx >= sp->ts_fidxtry
11495 && compound_ok)
11496 {
11497 /* The badword also ends: add suggestions. */
11498#ifdef DEBUG_TRIEWALK
11499 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011501 int j;
11502
11503 /* print the stack of changes that brought us here */
11504 smsg("------ %s -------", fword);
11505 for (j = 0; j < depth; ++j)
11506 smsg("%s", changename[j]);
11507 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011508#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011509 if (soundfold)
11510 {
11511 /* For soundfolded words we need to find the original
11512 * words, the edit distrance and then add them. */
11513 add_sound_suggest(su, preword, sp->ts_score, lp);
11514 }
11515 else
11516 {
11517 /* Give a penalty when changing non-word char to word
11518 * char, e.g., "thes," -> "these". */
11519 p = fword + sp->ts_fidx;
11520 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011521 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011522 {
11523 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011524 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011525 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011526 newscore += SCORE_NONWORD;
11527 }
11528
Bram Moolenaar4770d092006-01-12 23:22:24 +000011529 /* Give a bonus to words seen before. */
11530 score = score_wordcount_adj(slang,
11531 sp->ts_score + newscore,
11532 preword + sp->ts_prewordlen,
11533 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011534
Bram Moolenaar4770d092006-01-12 23:22:24 +000011535 /* Add the suggestion if the score isn't too bad. */
11536 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011537 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011538 add_suggestion(su, &su->su_ga, preword,
11539 sp->ts_fidx - repextra,
11540 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011541
11542 if (su->su_badflags & WF_MIXCAP)
11543 {
11544 /* We really don't know if the word should be
11545 * upper or lower case, add both. */
11546 c = captype(preword, NULL);
11547 if (c == 0 || c == WF_ALLCAP)
11548 {
11549 make_case_word(tword + sp->ts_splitoff,
11550 preword + sp->ts_prewordlen,
11551 c == 0 ? WF_ALLCAP : 0);
11552
11553 add_suggestion(su, &su->su_ga, preword,
11554 sp->ts_fidx - repextra,
11555 score + SCORE_ICASE, 0, FALSE,
11556 lp->lp_sallang, FALSE);
11557 }
11558 }
11559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011560 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011561 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011562
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 /*
11564 * Try word split and/or compounding.
11565 */
11566 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011567#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011568 /* Don't split halfway a character. */
11569 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011570#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011571 )
11572 {
11573 int try_compound;
11574 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011575
Bram Moolenaar4770d092006-01-12 23:22:24 +000011576 /* If past the end of the bad word don't try a split.
11577 * Otherwise try changing the next word. E.g., find
11578 * suggestions for "the the" where the second "the" is
11579 * different. It's done like a split.
11580 * TODO: word split for soundfold words */
11581 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11582 && !soundfold;
11583
11584 /* Get here in several situations:
11585 * 1. The word in the tree ends:
11586 * If the word allows compounding try that. Otherwise try
11587 * a split by inserting a space. For both check that a
11588 * valid words starts at fword[sp->ts_fidx].
11589 * For NOBREAK do like compounding to be able to check if
11590 * the next word is valid.
11591 * 2. The badword does end, but it was due to a change (e.g.,
11592 * a swap). No need to split, but do check that the
11593 * following word is valid.
11594 * 3. The badword and the word in the tree end. It may still
11595 * be possible to compound another (short) word.
11596 */
11597 try_compound = FALSE;
11598 if (!soundfold
11599 && slang->sl_compprog != NULL
11600 && ((unsigned)flags >> 24) != 0
11601 && sp->ts_twordlen - sp->ts_splitoff
11602 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011603#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011604 && (!has_mbyte
11605 || slang->sl_compminlen == 0
11606 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011607 >= slang->sl_compminlen)
11608#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011609 && (slang->sl_compsylmax < MAXWLEN
11610 || sp->ts_complen + 1 - sp->ts_compsplit
11611 < slang->sl_compmax)
11612 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11613 ? slang->sl_compstartflags
11614 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011615 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011616 {
11617 try_compound = TRUE;
11618 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11619 compflags[sp->ts_complen + 1] = NUL;
11620 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011621
Bram Moolenaar4770d092006-01-12 23:22:24 +000011622 /* For NOBREAK we never try splitting, it won't make any word
11623 * valid. */
11624 if (slang->sl_nobreak)
11625 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011626
Bram Moolenaar4770d092006-01-12 23:22:24 +000011627 /* If we could add a compound word, and it's also possible to
11628 * split at this point, do the split first and set
11629 * TSF_DIDSPLIT to avoid doing it again. */
11630 else if (!fword_ends
11631 && try_compound
11632 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11633 {
11634 try_compound = FALSE;
11635 sp->ts_flags |= TSF_DIDSPLIT;
11636 --sp->ts_curi; /* do the same NUL again */
11637 compflags[sp->ts_complen] = NUL;
11638 }
11639 else
11640 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011641
Bram Moolenaar4770d092006-01-12 23:22:24 +000011642 if (try_split || try_compound)
11643 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011644 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011645 {
11646 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011647 * words so far are valid for compounding. If there
11648 * is only one word it must not have the NEEDCOMPOUND
11649 * flag. */
11650 if (sp->ts_complen == sp->ts_compsplit
11651 && (flags & WF_NEEDCOMP))
11652 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011653 p = preword;
11654 while (*skiptowhite(p) != NUL)
11655 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011656 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011657 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011658 compflags + sp->ts_compsplit))
11659 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011660
11661 if (slang->sl_nosplitsugs)
11662 newscore += SCORE_SPLIT_NO;
11663 else
11664 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011665
11666 /* Give a bonus to words seen before. */
11667 newscore = score_wordcount_adj(slang, newscore,
11668 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011669 }
11670
Bram Moolenaar4770d092006-01-12 23:22:24 +000011671 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011673 go_deeper(stack, depth, newscore);
11674#ifdef DEBUG_TRIEWALK
11675 if (!try_compound && !fword_ends)
11676 sprintf(changename[depth], "%.*s-%s: split",
11677 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11678 else
11679 sprintf(changename[depth], "%.*s-%s: compound",
11680 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11681#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011683 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011684 sp->ts_state = STATE_SPLITUNDO;
11685
11686 ++depth;
11687 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011689 /* Append a space to preword when splitting. */
11690 if (!try_compound && !fword_ends)
11691 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011692 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011693 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011694 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011695
11696 /* If the badword has a non-word character at this
11697 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011698 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011699 * character when the word ends. But only when the
11700 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011701 if (((!try_compound && !spell_iswordp_nmw(fword
11702 + sp->ts_fidx))
11703 || fword_ends)
11704 && fword[sp->ts_fidx] != NUL
11705 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011706 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011707 int l;
11708
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011709#ifdef FEAT_MBYTE
11710 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011711 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011712 else
11713#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011714 l = 1;
11715 if (fword_ends)
11716 {
11717 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011718 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011719 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011720 sp->ts_prewordlen += l;
11721 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011722 }
11723 else
11724 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11725 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011726 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011727
Bram Moolenaard12a1322005-08-21 22:08:24 +000011728 /* When compounding include compound flag in
11729 * compflags[] (already set above). When splitting we
11730 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011731 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011732 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011733 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011734 sp->ts_compsplit = sp->ts_complen;
11735 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011736
Bram Moolenaar53805d12005-08-01 07:08:33 +000011737 /* set su->su_badflags to the caps type at this
11738 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011739#ifdef FEAT_MBYTE
11740 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011741 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011742 else
11743#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011744 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011745 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011746 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011748 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011749 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011750
11751 /* If there are postponed prefixes, try these too. */
11752 if (pbyts != NULL)
11753 {
11754 byts = pbyts;
11755 idxs = pidxs;
11756 sp->ts_prefixdepth = PFD_PREFIXTREE;
11757 sp->ts_state = STATE_NOPREFIX;
11758 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759 }
11760 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011761 }
11762 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763
Bram Moolenaar4770d092006-01-12 23:22:24 +000011764 case STATE_SPLITUNDO:
11765 /* Undo the changes done for word split or compound word. */
11766 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011767
Bram Moolenaar4770d092006-01-12 23:22:24 +000011768 /* Continue looking for NUL bytes. */
11769 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011770
Bram Moolenaar4770d092006-01-12 23:22:24 +000011771 /* In case we went into the prefix tree. */
11772 byts = fbyts;
11773 idxs = fidxs;
11774 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775
Bram Moolenaar4770d092006-01-12 23:22:24 +000011776 case STATE_ENDNUL:
11777 /* Past the NUL bytes in the node. */
11778 su->su_badflags = sp->ts_save_badflags;
11779 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011780#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011781 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011782#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011783 )
11784 {
11785 /* The badword ends, can't use STATE_PLAIN. */
11786 sp->ts_state = STATE_DEL;
11787 break;
11788 }
11789 sp->ts_state = STATE_PLAIN;
11790 /*FALLTHROUGH*/
11791
11792 case STATE_PLAIN:
11793 /*
11794 * Go over all possible bytes at this node, add each to tword[]
11795 * and use child node. "ts_curi" is the index.
11796 */
11797 arridx = sp->ts_arridx;
11798 if (sp->ts_curi > byts[arridx])
11799 {
11800 /* Done all bytes at this node, do next state. When still at
11801 * already changed bytes skip the other tricks. */
11802 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011805 sp->ts_state = STATE_FINAL;
11806 }
11807 else
11808 {
11809 arridx += sp->ts_curi++;
11810 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811
Bram Moolenaar4770d092006-01-12 23:22:24 +000011812 /* Normal byte, go one level deeper. If it's not equal to the
11813 * byte in the bad word adjust the score. But don't even try
11814 * when the byte was already changed. And don't try when we
11815 * just deleted this byte, accepting it is always cheaper then
11816 * delete + substitute. */
11817 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011818#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011819 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011820#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011821 )
11822 newscore = 0;
11823 else
11824 newscore = SCORE_SUBST;
11825 if ((newscore == 0
11826 || (sp->ts_fidx >= sp->ts_fidxtry
11827 && ((sp->ts_flags & TSF_DIDDEL) == 0
11828 || c != fword[sp->ts_delidx])))
11829 && TRY_DEEPER(su, stack, depth, newscore))
11830 {
11831 go_deeper(stack, depth, newscore);
11832#ifdef DEBUG_TRIEWALK
11833 if (newscore > 0)
11834 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11835 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11836 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011837 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011838 sprintf(changename[depth], "%.*s-%s: accept %c",
11839 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11840 fword[sp->ts_fidx]);
11841#endif
11842 ++depth;
11843 sp = &stack[depth];
11844 ++sp->ts_fidx;
11845 tword[sp->ts_twordlen++] = c;
11846 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011847#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011848 if (newscore == SCORE_SUBST)
11849 sp->ts_isdiff = DIFF_YES;
11850 if (has_mbyte)
11851 {
11852 /* Multi-byte characters are a bit complicated to
11853 * handle: They differ when any of the bytes differ
11854 * and then their length may also differ. */
11855 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011856 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011857 /* First byte. */
11858 sp->ts_tcharidx = 0;
11859 sp->ts_tcharlen = MB_BYTE2LEN(c);
11860 sp->ts_fcharstart = sp->ts_fidx - 1;
11861 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011862 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011863 }
11864 else if (sp->ts_isdiff == DIFF_INSERT)
11865 /* When inserting trail bytes don't advance in the
11866 * bad word. */
11867 --sp->ts_fidx;
11868 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11869 {
11870 /* Last byte of character. */
11871 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011872 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011873 /* Correct ts_fidx for the byte length of the
11874 * character (we didn't check that before). */
11875 sp->ts_fidx = sp->ts_fcharstart
11876 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011877 fword[sp->ts_fcharstart]);
11878
Bram Moolenaar4770d092006-01-12 23:22:24 +000011879 /* For changing a composing character adjust
11880 * the score from SCORE_SUBST to
11881 * SCORE_SUBCOMP. */
11882 if (enc_utf8
11883 && utf_iscomposing(
11884 mb_ptr2char(tword
11885 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011886 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011887 && utf_iscomposing(
11888 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011889 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011890 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011891 SCORE_SUBST - SCORE_SUBCOMP;
11892
Bram Moolenaar4770d092006-01-12 23:22:24 +000011893 /* For a similar character adjust score from
11894 * SCORE_SUBST to SCORE_SIMILAR. */
11895 else if (!soundfold
11896 && slang->sl_has_map
11897 && similar_chars(slang,
11898 mb_ptr2char(tword
11899 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011900 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011901 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011902 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011903 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011904 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011905 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011906 else if (sp->ts_isdiff == DIFF_INSERT
11907 && sp->ts_twordlen > sp->ts_tcharlen)
11908 {
11909 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11910 c = mb_ptr2char(p);
11911 if (enc_utf8 && utf_iscomposing(c))
11912 {
11913 /* Inserting a composing char doesn't
11914 * count that much. */
11915 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11916 }
11917 else
11918 {
11919 /* If the previous character was the same,
11920 * thus doubling a character, give a bonus
11921 * to the score. Also for the soundfold
11922 * tree (might seem illogical but does
11923 * give better scores). */
11924 mb_ptr_back(tword, p);
11925 if (c == mb_ptr2char(p))
11926 sp->ts_score -= SCORE_INS
11927 - SCORE_INSDUP;
11928 }
11929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930
Bram Moolenaar4770d092006-01-12 23:22:24 +000011931 /* Starting a new char, reset the length. */
11932 sp->ts_tcharlen = 0;
11933 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011934 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011935 else
11936#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011937 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011938 /* If we found a similar char adjust the score.
11939 * We do this after calling go_deeper() because
11940 * it's slow. */
11941 if (newscore != 0
11942 && !soundfold
11943 && slang->sl_has_map
11944 && similar_chars(slang,
11945 c, fword[sp->ts_fidx - 1]))
11946 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011947 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011948 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011949 }
11950 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951
Bram Moolenaar4770d092006-01-12 23:22:24 +000011952 case STATE_DEL:
11953#ifdef FEAT_MBYTE
11954 /* When past the first byte of a multi-byte char don't try
11955 * delete/insert/swap a character. */
11956 if (has_mbyte && sp->ts_tcharlen > 0)
11957 {
11958 sp->ts_state = STATE_FINAL;
11959 break;
11960 }
11961#endif
11962 /*
11963 * Try skipping one character in the bad word (delete it).
11964 */
11965 sp->ts_state = STATE_INS_PREP;
11966 sp->ts_curi = 1;
11967 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11968 /* Deleting a vowel at the start of a word counts less, see
11969 * soundalike_score(). */
11970 newscore = 2 * SCORE_DEL / 3;
11971 else
11972 newscore = SCORE_DEL;
11973 if (fword[sp->ts_fidx] != NUL
11974 && TRY_DEEPER(su, stack, depth, newscore))
11975 {
11976 go_deeper(stack, depth, newscore);
11977#ifdef DEBUG_TRIEWALK
11978 sprintf(changename[depth], "%.*s-%s: delete %c",
11979 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11980 fword[sp->ts_fidx]);
11981#endif
11982 ++depth;
11983
11984 /* Remember what character we deleted, so that we can avoid
11985 * inserting it again. */
11986 stack[depth].ts_flags |= TSF_DIDDEL;
11987 stack[depth].ts_delidx = sp->ts_fidx;
11988
11989 /* Advance over the character in fword[]. Give a bonus to the
11990 * score if the same character is following "nn" -> "n". It's
11991 * a bit illogical for soundfold tree but it does give better
11992 * results. */
11993#ifdef FEAT_MBYTE
11994 if (has_mbyte)
11995 {
11996 c = mb_ptr2char(fword + sp->ts_fidx);
11997 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11998 if (enc_utf8 && utf_iscomposing(c))
11999 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12000 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12001 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12002 }
12003 else
12004#endif
12005 {
12006 ++stack[depth].ts_fidx;
12007 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12008 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12009 }
12010 break;
12011 }
12012 /*FALLTHROUGH*/
12013
12014 case STATE_INS_PREP:
12015 if (sp->ts_flags & TSF_DIDDEL)
12016 {
12017 /* If we just deleted a byte then inserting won't make sense,
12018 * a substitute is always cheaper. */
12019 sp->ts_state = STATE_SWAP;
12020 break;
12021 }
12022
12023 /* skip over NUL bytes */
12024 n = sp->ts_arridx;
12025 for (;;)
12026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 if (sp->ts_curi > byts[n])
12028 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012029 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012031 break;
12032 }
12033 if (byts[n + sp->ts_curi] != NUL)
12034 {
12035 /* Found a byte to insert. */
12036 sp->ts_state = STATE_INS;
12037 break;
12038 }
12039 ++sp->ts_curi;
12040 }
12041 break;
12042
12043 /*FALLTHROUGH*/
12044
12045 case STATE_INS:
12046 /* Insert one byte. Repeat this for each possible byte at this
12047 * node. */
12048 n = sp->ts_arridx;
12049 if (sp->ts_curi > byts[n])
12050 {
12051 /* Done all bytes at this node, go to next state. */
12052 sp->ts_state = STATE_SWAP;
12053 break;
12054 }
12055
12056 /* Do one more byte at this node, but:
12057 * - Skip NUL bytes.
12058 * - Skip the byte if it's equal to the byte in the word,
12059 * accepting that byte is always better.
12060 */
12061 n += sp->ts_curi++;
12062 c = byts[n];
12063 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12064 /* Inserting a vowel at the start of a word counts less,
12065 * see soundalike_score(). */
12066 newscore = 2 * SCORE_INS / 3;
12067 else
12068 newscore = SCORE_INS;
12069 if (c != fword[sp->ts_fidx]
12070 && TRY_DEEPER(su, stack, depth, newscore))
12071 {
12072 go_deeper(stack, depth, newscore);
12073#ifdef DEBUG_TRIEWALK
12074 sprintf(changename[depth], "%.*s-%s: insert %c",
12075 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12076 c);
12077#endif
12078 ++depth;
12079 sp = &stack[depth];
12080 tword[sp->ts_twordlen++] = c;
12081 sp->ts_arridx = idxs[n];
12082#ifdef FEAT_MBYTE
12083 if (has_mbyte)
12084 {
12085 fl = MB_BYTE2LEN(c);
12086 if (fl > 1)
12087 {
12088 /* There are following bytes for the same character.
12089 * We must find all bytes before trying
12090 * delete/insert/swap/etc. */
12091 sp->ts_tcharlen = fl;
12092 sp->ts_tcharidx = 1;
12093 sp->ts_isdiff = DIFF_INSERT;
12094 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095 }
12096 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012097 fl = 1;
12098 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012099#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012100 {
12101 /* If the previous character was the same, thus doubling a
12102 * character, give a bonus to the score. Also for
12103 * soundfold words (illogical but does give a better
12104 * score). */
12105 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012106 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012107 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012109 }
12110 break;
12111
12112 case STATE_SWAP:
12113 /*
12114 * Swap two bytes in the bad word: "12" -> "21".
12115 * We change "fword" here, it's changed back afterwards at
12116 * STATE_UNSWAP.
12117 */
12118 p = fword + sp->ts_fidx;
12119 c = *p;
12120 if (c == NUL)
12121 {
12122 /* End of word, can't swap or replace. */
12123 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012124 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012126
Bram Moolenaar4770d092006-01-12 23:22:24 +000012127 /* Don't swap if the first character is not a word character.
12128 * SWAP3 etc. also don't make sense then. */
12129 if (!soundfold && !spell_iswordp(p, curbuf))
12130 {
12131 sp->ts_state = STATE_REP_INI;
12132 break;
12133 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012134
Bram Moolenaar4770d092006-01-12 23:22:24 +000012135#ifdef FEAT_MBYTE
12136 if (has_mbyte)
12137 {
12138 n = mb_cptr2len(p);
12139 c = mb_ptr2char(p);
12140 if (!soundfold && !spell_iswordp(p + n, curbuf))
12141 c2 = c; /* don't swap non-word char */
12142 else
12143 c2 = mb_ptr2char(p + n);
12144 }
12145 else
12146#endif
12147 {
12148 if (!soundfold && !spell_iswordp(p + 1, curbuf))
12149 c2 = c; /* don't swap non-word char */
12150 else
12151 c2 = p[1];
12152 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012153
Bram Moolenaar4770d092006-01-12 23:22:24 +000012154 /* When characters are identical, swap won't do anything.
12155 * Also get here if the second char is not a word character. */
12156 if (c == c2)
12157 {
12158 sp->ts_state = STATE_SWAP3;
12159 break;
12160 }
12161 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12162 {
12163 go_deeper(stack, depth, SCORE_SWAP);
12164#ifdef DEBUG_TRIEWALK
12165 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12166 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12167 c, c2);
12168#endif
12169 sp->ts_state = STATE_UNSWAP;
12170 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012171#ifdef FEAT_MBYTE
12172 if (has_mbyte)
12173 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012174 fl = mb_char2len(c2);
12175 mch_memmove(p, p + n, fl);
12176 mb_char2bytes(c, p + fl);
12177 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012178 }
12179 else
12180#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012181 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012182 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012183 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012184 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012185 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012186 }
12187 else
12188 /* If this swap doesn't work then SWAP3 won't either. */
12189 sp->ts_state = STATE_REP_INI;
12190 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012191
Bram Moolenaar4770d092006-01-12 23:22:24 +000012192 case STATE_UNSWAP:
12193 /* Undo the STATE_SWAP swap: "21" -> "12". */
12194 p = fword + sp->ts_fidx;
12195#ifdef FEAT_MBYTE
12196 if (has_mbyte)
12197 {
12198 n = MB_BYTE2LEN(*p);
12199 c = mb_ptr2char(p + n);
12200 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12201 mb_char2bytes(c, p);
12202 }
12203 else
12204#endif
12205 {
12206 c = *p;
12207 *p = p[1];
12208 p[1] = c;
12209 }
12210 /*FALLTHROUGH*/
12211
12212 case STATE_SWAP3:
12213 /* Swap two bytes, skipping one: "123" -> "321". We change
12214 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12215 p = fword + sp->ts_fidx;
12216#ifdef FEAT_MBYTE
12217 if (has_mbyte)
12218 {
12219 n = mb_cptr2len(p);
12220 c = mb_ptr2char(p);
12221 fl = mb_cptr2len(p + n);
12222 c2 = mb_ptr2char(p + n);
12223 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12224 c3 = c; /* don't swap non-word char */
12225 else
12226 c3 = mb_ptr2char(p + n + fl);
12227 }
12228 else
12229#endif
12230 {
12231 c = *p;
12232 c2 = p[1];
12233 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12234 c3 = c; /* don't swap non-word char */
12235 else
12236 c3 = p[2];
12237 }
12238
12239 /* When characters are identical: "121" then SWAP3 result is
12240 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12241 * same as SWAP on next char: "112". Thus skip all swapping.
12242 * Also skip when c3 is NUL.
12243 * Also get here when the third character is not a word character.
12244 * Second character may any char: "a.b" -> "b.a" */
12245 if (c == c3 || c3 == NUL)
12246 {
12247 sp->ts_state = STATE_REP_INI;
12248 break;
12249 }
12250 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12251 {
12252 go_deeper(stack, depth, SCORE_SWAP3);
12253#ifdef DEBUG_TRIEWALK
12254 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12255 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12256 c, c3);
12257#endif
12258 sp->ts_state = STATE_UNSWAP3;
12259 ++depth;
12260#ifdef FEAT_MBYTE
12261 if (has_mbyte)
12262 {
12263 tl = mb_char2len(c3);
12264 mch_memmove(p, p + n + fl, tl);
12265 mb_char2bytes(c2, p + tl);
12266 mb_char2bytes(c, p + fl + tl);
12267 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12268 }
12269 else
12270#endif
12271 {
12272 p[0] = p[2];
12273 p[2] = c;
12274 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12275 }
12276 }
12277 else
12278 sp->ts_state = STATE_REP_INI;
12279 break;
12280
12281 case STATE_UNSWAP3:
12282 /* Undo STATE_SWAP3: "321" -> "123" */
12283 p = fword + sp->ts_fidx;
12284#ifdef FEAT_MBYTE
12285 if (has_mbyte)
12286 {
12287 n = MB_BYTE2LEN(*p);
12288 c2 = mb_ptr2char(p + n);
12289 fl = MB_BYTE2LEN(p[n]);
12290 c = mb_ptr2char(p + n + fl);
12291 tl = MB_BYTE2LEN(p[n + fl]);
12292 mch_memmove(p + fl + tl, p, n);
12293 mb_char2bytes(c, p);
12294 mb_char2bytes(c2, p + tl);
12295 p = p + tl;
12296 }
12297 else
12298#endif
12299 {
12300 c = *p;
12301 *p = p[2];
12302 p[2] = c;
12303 ++p;
12304 }
12305
12306 if (!soundfold && !spell_iswordp(p, curbuf))
12307 {
12308 /* Middle char is not a word char, skip the rotate. First and
12309 * third char were already checked at swap and swap3. */
12310 sp->ts_state = STATE_REP_INI;
12311 break;
12312 }
12313
12314 /* Rotate three characters left: "123" -> "231". We change
12315 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12316 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12317 {
12318 go_deeper(stack, depth, SCORE_SWAP3);
12319#ifdef DEBUG_TRIEWALK
12320 p = fword + sp->ts_fidx;
12321 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12322 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12323 p[0], p[1], p[2]);
12324#endif
12325 sp->ts_state = STATE_UNROT3L;
12326 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012327 p = fword + sp->ts_fidx;
12328#ifdef FEAT_MBYTE
12329 if (has_mbyte)
12330 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012331 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012332 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012333 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012334 fl += mb_cptr2len(p + n + fl);
12335 mch_memmove(p, p + n, fl);
12336 mb_char2bytes(c, p + fl);
12337 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012338 }
12339 else
12340#endif
12341 {
12342 c = *p;
12343 *p = p[1];
12344 p[1] = p[2];
12345 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012346 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012347 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012348 }
12349 else
12350 sp->ts_state = STATE_REP_INI;
12351 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352
Bram Moolenaar4770d092006-01-12 23:22:24 +000012353 case STATE_UNROT3L:
12354 /* Undo ROT3L: "231" -> "123" */
12355 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012356#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012357 if (has_mbyte)
12358 {
12359 n = MB_BYTE2LEN(*p);
12360 n += MB_BYTE2LEN(p[n]);
12361 c = mb_ptr2char(p + n);
12362 tl = MB_BYTE2LEN(p[n]);
12363 mch_memmove(p + tl, p, n);
12364 mb_char2bytes(c, p);
12365 }
12366 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012367#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012368 {
12369 c = p[2];
12370 p[2] = p[1];
12371 p[1] = *p;
12372 *p = c;
12373 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012374
Bram Moolenaar4770d092006-01-12 23:22:24 +000012375 /* Rotate three bytes right: "123" -> "312". We change "fword"
12376 * here, it's changed back afterwards at STATE_UNROT3R. */
12377 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12378 {
12379 go_deeper(stack, depth, SCORE_SWAP3);
12380#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012381 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012382 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12383 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12384 p[0], p[1], p[2]);
12385#endif
12386 sp->ts_state = STATE_UNROT3R;
12387 ++depth;
12388 p = fword + sp->ts_fidx;
12389#ifdef FEAT_MBYTE
12390 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012391 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012392 n = mb_cptr2len(p);
12393 n += mb_cptr2len(p + n);
12394 c = mb_ptr2char(p + n);
12395 tl = mb_cptr2len(p + n);
12396 mch_memmove(p + tl, p, n);
12397 mb_char2bytes(c, p);
12398 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012399 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012400 else
12401#endif
12402 {
12403 c = p[2];
12404 p[2] = p[1];
12405 p[1] = *p;
12406 *p = c;
12407 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12408 }
12409 }
12410 else
12411 sp->ts_state = STATE_REP_INI;
12412 break;
12413
12414 case STATE_UNROT3R:
12415 /* Undo ROT3R: "312" -> "123" */
12416 p = fword + sp->ts_fidx;
12417#ifdef FEAT_MBYTE
12418 if (has_mbyte)
12419 {
12420 c = mb_ptr2char(p);
12421 tl = MB_BYTE2LEN(*p);
12422 n = MB_BYTE2LEN(p[tl]);
12423 n += MB_BYTE2LEN(p[tl + n]);
12424 mch_memmove(p, p + tl, n);
12425 mb_char2bytes(c, p + n);
12426 }
12427 else
12428#endif
12429 {
12430 c = *p;
12431 *p = p[1];
12432 p[1] = p[2];
12433 p[2] = c;
12434 }
12435 /*FALLTHROUGH*/
12436
12437 case STATE_REP_INI:
12438 /* Check if matching with REP items from the .aff file would work.
12439 * Quickly skip if:
12440 * - there are no REP items and we are not in the soundfold trie
12441 * - the score is going to be too high anyway
12442 * - already applied a REP item or swapped here */
12443 if ((lp->lp_replang == NULL && !soundfold)
12444 || sp->ts_score + SCORE_REP >= su->su_maxscore
12445 || sp->ts_fidx < sp->ts_fidxtry)
12446 {
12447 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012448 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450
Bram Moolenaar4770d092006-01-12 23:22:24 +000012451 /* Use the first byte to quickly find the first entry that may
12452 * match. If the index is -1 there is none. */
12453 if (soundfold)
12454 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12455 else
12456 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457
Bram Moolenaar4770d092006-01-12 23:22:24 +000012458 if (sp->ts_curi < 0)
12459 {
12460 sp->ts_state = STATE_FINAL;
12461 break;
12462 }
12463
12464 sp->ts_state = STATE_REP;
12465 /*FALLTHROUGH*/
12466
12467 case STATE_REP:
12468 /* Try matching with REP items from the .aff file. For each match
12469 * replace the characters and check if the resulting word is
12470 * valid. */
12471 p = fword + sp->ts_fidx;
12472
12473 if (soundfold)
12474 gap = &slang->sl_repsal;
12475 else
12476 gap = &lp->lp_replang->sl_rep;
12477 while (sp->ts_curi < gap->ga_len)
12478 {
12479 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12480 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012481 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012482 /* past possible matching entries */
12483 sp->ts_curi = gap->ga_len;
12484 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012485 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012486 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12487 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12488 {
12489 go_deeper(stack, depth, SCORE_REP);
12490#ifdef DEBUG_TRIEWALK
12491 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12492 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12493 ftp->ft_from, ftp->ft_to);
12494#endif
12495 /* Need to undo this afterwards. */
12496 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012497
Bram Moolenaar4770d092006-01-12 23:22:24 +000012498 /* Change the "from" to the "to" string. */
12499 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012500 fl = (int)STRLEN(ftp->ft_from);
12501 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012502 if (fl != tl)
12503 {
12504 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12505 repextra += tl - fl;
12506 }
12507 mch_memmove(p, ftp->ft_to, tl);
12508 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12509#ifdef FEAT_MBYTE
12510 stack[depth].ts_tcharlen = 0;
12511#endif
12512 break;
12513 }
12514 }
12515
12516 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12517 /* No (more) matches. */
12518 sp->ts_state = STATE_FINAL;
12519
12520 break;
12521
12522 case STATE_REP_UNDO:
12523 /* Undo a REP replacement and continue with the next one. */
12524 if (soundfold)
12525 gap = &slang->sl_repsal;
12526 else
12527 gap = &lp->lp_replang->sl_rep;
12528 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012529 fl = (int)STRLEN(ftp->ft_from);
12530 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012531 p = fword + sp->ts_fidx;
12532 if (fl != tl)
12533 {
12534 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12535 repextra -= tl - fl;
12536 }
12537 mch_memmove(p, ftp->ft_from, fl);
12538 sp->ts_state = STATE_REP;
12539 break;
12540
12541 default:
12542 /* Did all possible states at this level, go up one level. */
12543 --depth;
12544
12545 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12546 {
12547 /* Continue in or go back to the prefix tree. */
12548 byts = pbyts;
12549 idxs = pidxs;
12550 }
12551
12552 /* Don't check for CTRL-C too often, it takes time. */
12553 if (--breakcheckcount == 0)
12554 {
12555 ui_breakcheck();
12556 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012557 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 }
12559 }
12560}
12561
Bram Moolenaar4770d092006-01-12 23:22:24 +000012562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012564 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012565 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012566 static void
12567go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568 trystate_T *stack;
12569 int depth;
12570 int score_add;
12571{
Bram Moolenaarea424162005-06-16 21:51:00 +000012572 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012573 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012574 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012575 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012576 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012577}
12578
Bram Moolenaar53805d12005-08-01 07:08:33 +000012579#ifdef FEAT_MBYTE
12580/*
12581 * Case-folding may change the number of bytes: Count nr of chars in
12582 * fword[flen] and return the byte length of that many chars in "word".
12583 */
12584 static int
12585nofold_len(fword, flen, word)
12586 char_u *fword;
12587 int flen;
12588 char_u *word;
12589{
12590 char_u *p;
12591 int i = 0;
12592
12593 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12594 ++i;
12595 for (p = word; i > 0; mb_ptr_adv(p))
12596 --i;
12597 return (int)(p - word);
12598}
12599#endif
12600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601/*
12602 * "fword" is a good word with case folded. Find the matching keep-case
12603 * words and put it in "kword".
12604 * Theoretically there could be several keep-case words that result in the
12605 * same case-folded word, but we only find one...
12606 */
12607 static void
12608find_keepcap_word(slang, fword, kword)
12609 slang_T *slang;
12610 char_u *fword;
12611 char_u *kword;
12612{
12613 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12614 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012615 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012616
12617 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012618 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 int round[MAXWLEN];
12620 int fwordidx[MAXWLEN];
12621 int uwordidx[MAXWLEN];
12622 int kwordlen[MAXWLEN];
12623
12624 int flen, ulen;
12625 int l;
12626 int len;
12627 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012628 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 char_u *p;
12630 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012631 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012632
12633 if (byts == NULL)
12634 {
12635 /* array is empty: "cannot happen" */
12636 *kword = NUL;
12637 return;
12638 }
12639
12640 /* Make an all-cap version of "fword". */
12641 allcap_copy(fword, uword);
12642
12643 /*
12644 * Each character needs to be tried both case-folded and upper-case.
12645 * All this gets very complicated if we keep in mind that changing case
12646 * may change the byte length of a multi-byte character...
12647 */
12648 depth = 0;
12649 arridx[0] = 0;
12650 round[0] = 0;
12651 fwordidx[0] = 0;
12652 uwordidx[0] = 0;
12653 kwordlen[0] = 0;
12654 while (depth >= 0)
12655 {
12656 if (fword[fwordidx[depth]] == NUL)
12657 {
12658 /* We are at the end of "fword". If the tree allows a word to end
12659 * here we have found a match. */
12660 if (byts[arridx[depth] + 1] == 0)
12661 {
12662 kword[kwordlen[depth]] = NUL;
12663 return;
12664 }
12665
12666 /* kword is getting too long, continue one level up */
12667 --depth;
12668 }
12669 else if (++round[depth] > 2)
12670 {
12671 /* tried both fold-case and upper-case character, continue one
12672 * level up */
12673 --depth;
12674 }
12675 else
12676 {
12677 /*
12678 * round[depth] == 1: Try using the folded-case character.
12679 * round[depth] == 2: Try using the upper-case character.
12680 */
12681#ifdef FEAT_MBYTE
12682 if (has_mbyte)
12683 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012684 flen = mb_cptr2len(fword + fwordidx[depth]);
12685 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012686 }
12687 else
12688#endif
12689 ulen = flen = 1;
12690 if (round[depth] == 1)
12691 {
12692 p = fword + fwordidx[depth];
12693 l = flen;
12694 }
12695 else
12696 {
12697 p = uword + uwordidx[depth];
12698 l = ulen;
12699 }
12700
12701 for (tryidx = arridx[depth]; l > 0; --l)
12702 {
12703 /* Perform a binary search in the list of accepted bytes. */
12704 len = byts[tryidx++];
12705 c = *p++;
12706 lo = tryidx;
12707 hi = tryidx + len - 1;
12708 while (lo < hi)
12709 {
12710 m = (lo + hi) / 2;
12711 if (byts[m] > c)
12712 hi = m - 1;
12713 else if (byts[m] < c)
12714 lo = m + 1;
12715 else
12716 {
12717 lo = hi = m;
12718 break;
12719 }
12720 }
12721
12722 /* Stop if there is no matching byte. */
12723 if (hi < lo || byts[lo] != c)
12724 break;
12725
12726 /* Continue at the child (if there is one). */
12727 tryidx = idxs[lo];
12728 }
12729
12730 if (l == 0)
12731 {
12732 /*
12733 * Found the matching char. Copy it to "kword" and go a
12734 * level deeper.
12735 */
12736 if (round[depth] == 1)
12737 {
12738 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12739 flen);
12740 kwordlen[depth + 1] = kwordlen[depth] + flen;
12741 }
12742 else
12743 {
12744 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12745 ulen);
12746 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12747 }
12748 fwordidx[depth + 1] = fwordidx[depth] + flen;
12749 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12750
12751 ++depth;
12752 arridx[depth] = tryidx;
12753 round[depth] = 0;
12754 }
12755 }
12756 }
12757
12758 /* Didn't find it: "cannot happen". */
12759 *kword = NUL;
12760}
12761
12762/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012763 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12764 * su->su_sga.
12765 */
12766 static void
12767score_comp_sal(su)
12768 suginfo_T *su;
12769{
12770 langp_T *lp;
12771 char_u badsound[MAXWLEN];
12772 int i;
12773 suggest_T *stp;
12774 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012775 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012776 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012777
12778 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12779 return;
12780
12781 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012782 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012783 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012784 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012785 if (lp->lp_slang->sl_sal.ga_len > 0)
12786 {
12787 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012788 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012789
12790 for (i = 0; i < su->su_ga.ga_len; ++i)
12791 {
12792 stp = &SUG(su->su_ga, i);
12793
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012794 /* Case-fold the suggested word, sound-fold it and compute the
12795 * sound-a-like score. */
12796 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012797 if (score < SCORE_MAXMAX)
12798 {
12799 /* Add the suggestion. */
12800 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12801 sstp->st_word = vim_strsave(stp->st_word);
12802 if (sstp->st_word != NULL)
12803 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012804 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012805 sstp->st_score = score;
12806 sstp->st_altscore = 0;
12807 sstp->st_orglen = stp->st_orglen;
12808 ++su->su_sga.ga_len;
12809 }
12810 }
12811 }
12812 break;
12813 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012814 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012815}
12816
12817/*
12818 * Combine the list of suggestions in su->su_ga and su->su_sga.
12819 * They are intwined.
12820 */
12821 static void
12822score_combine(su)
12823 suginfo_T *su;
12824{
12825 int i;
12826 int j;
12827 garray_T ga;
12828 garray_T *gap;
12829 langp_T *lp;
12830 suggest_T *stp;
12831 char_u *p;
12832 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012833 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012834 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012835 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012836
12837 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012838 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012839 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012840 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012841 if (lp->lp_slang->sl_sal.ga_len > 0)
12842 {
12843 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012844 slang = lp->lp_slang;
12845 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012846
12847 for (i = 0; i < su->su_ga.ga_len; ++i)
12848 {
12849 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012850 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012851 if (stp->st_altscore == SCORE_MAXMAX)
12852 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12853 else
12854 stp->st_score = (stp->st_score * 3
12855 + stp->st_altscore) / 4;
12856 stp->st_salscore = FALSE;
12857 }
12858 break;
12859 }
12860 }
12861
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012862 if (slang == NULL) /* Using "double" without sound folding. */
12863 {
12864 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12865 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012866 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012867 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012868
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012869 /* Add the alternate score to su_sga. */
12870 for (i = 0; i < su->su_sga.ga_len; ++i)
12871 {
12872 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012873 stp->st_altscore = spell_edit_score(slang,
12874 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012875 if (stp->st_score == SCORE_MAXMAX)
12876 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12877 else
12878 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12879 stp->st_salscore = TRUE;
12880 }
12881
Bram Moolenaar4770d092006-01-12 23:22:24 +000012882 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12883 * for both lists. */
12884 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012885 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012886 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012887 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12888
12889 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12890 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12891 return;
12892
12893 stp = &SUG(ga, 0);
12894 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12895 {
12896 /* round 1: get a suggestion from su_ga
12897 * round 2: get a suggestion from su_sga */
12898 for (round = 1; round <= 2; ++round)
12899 {
12900 gap = round == 1 ? &su->su_ga : &su->su_sga;
12901 if (i < gap->ga_len)
12902 {
12903 /* Don't add a word if it's already there. */
12904 p = SUG(*gap, i).st_word;
12905 for (j = 0; j < ga.ga_len; ++j)
12906 if (STRCMP(stp[j].st_word, p) == 0)
12907 break;
12908 if (j == ga.ga_len)
12909 stp[ga.ga_len++] = SUG(*gap, i);
12910 else
12911 vim_free(p);
12912 }
12913 }
12914 }
12915
12916 ga_clear(&su->su_ga);
12917 ga_clear(&su->su_sga);
12918
12919 /* Truncate the list to the number of suggestions that will be displayed. */
12920 if (ga.ga_len > su->su_maxcount)
12921 {
12922 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12923 vim_free(stp[i].st_word);
12924 ga.ga_len = su->su_maxcount;
12925 }
12926
12927 su->su_ga = ga;
12928}
12929
12930/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012931 * For the goodword in "stp" compute the soundalike score compared to the
12932 * badword.
12933 */
12934 static int
12935stp_sal_score(stp, su, slang, badsound)
12936 suggest_T *stp;
12937 suginfo_T *su;
12938 slang_T *slang;
12939 char_u *badsound; /* sound-folded badword */
12940{
12941 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012942 char_u *pbad;
12943 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012944 char_u badsound2[MAXWLEN];
12945 char_u fword[MAXWLEN];
12946 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012947 char_u goodword[MAXWLEN];
12948 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012949
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012950 lendiff = (int)(su->su_badlen - stp->st_orglen);
12951 if (lendiff >= 0)
12952 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012953 else
12954 {
12955 /* soundfold the bad word with more characters following */
12956 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12957
12958 /* When joining two words the sound often changes a lot. E.g., "t he"
12959 * sounds like "t h" while "the" sounds like "@". Avoid that by
12960 * removing the space. Don't do it when the good word also contains a
12961 * space. */
12962 if (vim_iswhite(su->su_badptr[su->su_badlen])
12963 && *skiptowhite(stp->st_word) == NUL)
12964 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12965 mch_memmove(p, p + 1, STRLEN(p));
12966
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012967 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012968 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012969 }
12970
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012971 if (lendiff > 0)
12972 {
12973 /* Add part of the bad word to the good word, so that we soundfold
12974 * what replaces the bad word. */
12975 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012976 vim_strncpy(goodword + stp->st_wordlen,
12977 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012978 pgood = goodword;
12979 }
12980 else
12981 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012982
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012983 /* Sound-fold the word and compute the score for the difference. */
12984 spell_soundfold(slang, pgood, FALSE, goodsound);
12985
12986 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012987}
12988
Bram Moolenaar4770d092006-01-12 23:22:24 +000012989/* structure used to store soundfolded words that add_sound_suggest() has
12990 * handled already. */
12991typedef struct
12992{
12993 short sft_score; /* lowest score used */
12994 char_u sft_word[1]; /* soundfolded word, actually longer */
12995} sftword_T;
12996
12997static sftword_T dumsft;
12998#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12999#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13000
13001/*
13002 * Prepare for calling suggest_try_soundalike().
13003 */
13004 static void
13005suggest_try_soundalike_prep()
13006{
13007 langp_T *lp;
13008 int lpi;
13009 slang_T *slang;
13010
13011 /* Do this for all languages that support sound folding and for which a
13012 * .sug file has been loaded. */
13013 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13014 {
13015 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13016 slang = lp->lp_slang;
13017 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13018 /* prepare the hashtable used by add_sound_suggest() */
13019 hash_init(&slang->sl_sounddone);
13020 }
13021}
13022
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013023/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013025 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 */
13027 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013028suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 suginfo_T *su;
13030{
13031 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013033 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013034 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035
Bram Moolenaar4770d092006-01-12 23:22:24 +000013036 /* Do this for all languages that support sound folding and for which a
13037 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013038 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013039 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013040 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13041 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013042 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 {
13044 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013045 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046
Bram Moolenaar4770d092006-01-12 23:22:24 +000013047 /* try all kinds of inserts/deletes/swaps/etc. */
13048 /* TODO: also soundfold the next words, so that we can try joining
13049 * and splitting */
13050 suggest_trie_walk(su, lp, salword, TRUE);
13051 }
13052 }
13053}
13054
13055/*
13056 * Finish up after calling suggest_try_soundalike().
13057 */
13058 static void
13059suggest_try_soundalike_finish()
13060{
13061 langp_T *lp;
13062 int lpi;
13063 slang_T *slang;
13064 int todo;
13065 hashitem_T *hi;
13066
13067 /* Do this for all languages that support sound folding and for which a
13068 * .sug file has been loaded. */
13069 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13070 {
13071 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13072 slang = lp->lp_slang;
13073 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13074 {
13075 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013076 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013077 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13078 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013079 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013080 vim_free(HI2SFT(hi));
13081 --todo;
13082 }
13083 hash_clear(&slang->sl_sounddone);
13084 }
13085 }
13086}
13087
13088/*
13089 * A match with a soundfolded word is found. Add the good word(s) that
13090 * produce this soundfolded word.
13091 */
13092 static void
13093add_sound_suggest(su, goodword, score, lp)
13094 suginfo_T *su;
13095 char_u *goodword;
13096 int score; /* soundfold score */
13097 langp_T *lp;
13098{
13099 slang_T *slang = lp->lp_slang; /* language for sound folding */
13100 int sfwordnr;
13101 char_u *nrline;
13102 int orgnr;
13103 char_u theword[MAXWLEN];
13104 int i;
13105 int wlen;
13106 char_u *byts;
13107 idx_T *idxs;
13108 int n;
13109 int wordcount;
13110 int wc;
13111 int goodscore;
13112 hash_T hash;
13113 hashitem_T *hi;
13114 sftword_T *sft;
13115 int bc, gc;
13116 int limit;
13117
13118 /*
13119 * It's very well possible that the same soundfold word is found several
13120 * times with different scores. Since the following is quite slow only do
13121 * the words that have a better score than before. Use a hashtable to
13122 * remember the words that have been done.
13123 */
13124 hash = hash_hash(goodword);
13125 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13126 if (HASHITEM_EMPTY(hi))
13127 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013128 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13129 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013130 if (sft != NULL)
13131 {
13132 sft->sft_score = score;
13133 STRCPY(sft->sft_word, goodword);
13134 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13135 }
13136 }
13137 else
13138 {
13139 sft = HI2SFT(hi);
13140 if (score >= sft->sft_score)
13141 return;
13142 sft->sft_score = score;
13143 }
13144
13145 /*
13146 * Find the word nr in the soundfold tree.
13147 */
13148 sfwordnr = soundfold_find(slang, goodword);
13149 if (sfwordnr < 0)
13150 {
13151 EMSG2(_(e_intern2), "add_sound_suggest()");
13152 return;
13153 }
13154
13155 /*
13156 * go over the list of good words that produce this soundfold word
13157 */
13158 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13159 orgnr = 0;
13160 while (*nrline != NUL)
13161 {
13162 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13163 * previous wordnr. */
13164 orgnr += bytes2offset(&nrline);
13165
13166 byts = slang->sl_fbyts;
13167 idxs = slang->sl_fidxs;
13168
13169 /* Lookup the word "orgnr" one of the two tries. */
13170 n = 0;
13171 wlen = 0;
13172 wordcount = 0;
13173 for (;;)
13174 {
13175 i = 1;
13176 if (wordcount == orgnr && byts[n + 1] == NUL)
13177 break; /* found end of word */
13178
13179 if (byts[n + 1] == NUL)
13180 ++wordcount;
13181
13182 /* skip over the NUL bytes */
13183 for ( ; byts[n + i] == NUL; ++i)
13184 if (i > byts[n]) /* safety check */
13185 {
13186 STRCPY(theword + wlen, "BAD");
13187 goto badword;
13188 }
13189
13190 /* One of the siblings must have the word. */
13191 for ( ; i < byts[n]; ++i)
13192 {
13193 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13194 if (wordcount + wc > orgnr)
13195 break;
13196 wordcount += wc;
13197 }
13198
13199 theword[wlen++] = byts[n + i];
13200 n = idxs[n + i];
13201 }
13202badword:
13203 theword[wlen] = NUL;
13204
13205 /* Go over the possible flags and regions. */
13206 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13207 {
13208 char_u cword[MAXWLEN];
13209 char_u *p;
13210 int flags = (int)idxs[n + i];
13211
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013212 /* Skip words with the NOSUGGEST flag */
13213 if (flags & WF_NOSUGGEST)
13214 continue;
13215
Bram Moolenaar4770d092006-01-12 23:22:24 +000013216 if (flags & WF_KEEPCAP)
13217 {
13218 /* Must find the word in the keep-case tree. */
13219 find_keepcap_word(slang, theword, cword);
13220 p = cword;
13221 }
13222 else
13223 {
13224 flags |= su->su_badflags;
13225 if ((flags & WF_CAPMASK) != 0)
13226 {
13227 /* Need to fix case according to "flags". */
13228 make_case_word(theword, cword, flags);
13229 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 }
13231 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013232 p = theword;
13233 }
13234
13235 /* Add the suggestion. */
13236 if (sps_flags & SPS_DOUBLE)
13237 {
13238 /* Add the suggestion if the score isn't too bad. */
13239 if (score <= su->su_maxscore)
13240 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13241 score, 0, FALSE, slang, FALSE);
13242 }
13243 else
13244 {
13245 /* Add a penalty for words in another region. */
13246 if ((flags & WF_REGION)
13247 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13248 goodscore = SCORE_REGION;
13249 else
13250 goodscore = 0;
13251
13252 /* Add a small penalty for changing the first letter from
13253 * lower to upper case. Helps for "tath" -> "Kath", which is
13254 * less common thatn "tath" -> "path". Don't do it when the
13255 * letter is the same, that has already been counted. */
13256 gc = PTR2CHAR(p);
13257 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013259 bc = PTR2CHAR(su->su_badword);
13260 if (!SPELL_ISUPPER(bc)
13261 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13262 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 }
13264
Bram Moolenaar4770d092006-01-12 23:22:24 +000013265 /* Compute the score for the good word. This only does letter
13266 * insert/delete/swap/replace. REP items are not considered,
13267 * which may make the score a bit higher.
13268 * Use a limit for the score to make it work faster. Use
13269 * MAXSCORE(), because RESCORE() will change the score.
13270 * If the limit is very high then the iterative method is
13271 * inefficient, using an array is quicker. */
13272 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13273 if (limit > SCORE_LIMITMAX)
13274 goodscore += spell_edit_score(slang, su->su_badword, p);
13275 else
13276 goodscore += spell_edit_score_limit(slang, su->su_badword,
13277 p, limit);
13278
13279 /* When going over the limit don't bother to do the rest. */
13280 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013282 /* Give a bonus to words seen before. */
13283 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284
Bram Moolenaar4770d092006-01-12 23:22:24 +000013285 /* Add the suggestion if the score isn't too bad. */
13286 goodscore = RESCORE(goodscore, score);
13287 if (goodscore <= su->su_sfmaxscore)
13288 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13289 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 }
13292 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013293 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 }
13295}
13296
13297/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013298 * Find word "word" in fold-case tree for "slang" and return the word number.
13299 */
13300 static int
13301soundfold_find(slang, word)
13302 slang_T *slang;
13303 char_u *word;
13304{
13305 idx_T arridx = 0;
13306 int len;
13307 int wlen = 0;
13308 int c;
13309 char_u *ptr = word;
13310 char_u *byts;
13311 idx_T *idxs;
13312 int wordnr = 0;
13313
13314 byts = slang->sl_sbyts;
13315 idxs = slang->sl_sidxs;
13316
13317 for (;;)
13318 {
13319 /* First byte is the number of possible bytes. */
13320 len = byts[arridx++];
13321
13322 /* If the first possible byte is a zero the word could end here.
13323 * If the word ends we found the word. If not skip the NUL bytes. */
13324 c = ptr[wlen];
13325 if (byts[arridx] == NUL)
13326 {
13327 if (c == NUL)
13328 break;
13329
13330 /* Skip over the zeros, there can be several. */
13331 while (len > 0 && byts[arridx] == NUL)
13332 {
13333 ++arridx;
13334 --len;
13335 }
13336 if (len == 0)
13337 return -1; /* no children, word should have ended here */
13338 ++wordnr;
13339 }
13340
13341 /* If the word ends we didn't find it. */
13342 if (c == NUL)
13343 return -1;
13344
13345 /* Perform a binary search in the list of accepted bytes. */
13346 if (c == TAB) /* <Tab> is handled like <Space> */
13347 c = ' ';
13348 while (byts[arridx] < c)
13349 {
13350 /* The word count is in the first idxs[] entry of the child. */
13351 wordnr += idxs[idxs[arridx]];
13352 ++arridx;
13353 if (--len == 0) /* end of the bytes, didn't find it */
13354 return -1;
13355 }
13356 if (byts[arridx] != c) /* didn't find the byte */
13357 return -1;
13358
13359 /* Continue at the child (if there is one). */
13360 arridx = idxs[arridx];
13361 ++wlen;
13362
13363 /* One space in the good word may stand for several spaces in the
13364 * checked word. */
13365 if (c == ' ')
13366 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13367 ++wlen;
13368 }
13369
13370 return wordnr;
13371}
13372
13373/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013374 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375 */
13376 static void
13377make_case_word(fword, cword, flags)
13378 char_u *fword;
13379 char_u *cword;
13380 int flags;
13381{
13382 if (flags & WF_ALLCAP)
13383 /* Make it all upper-case */
13384 allcap_copy(fword, cword);
13385 else if (flags & WF_ONECAP)
13386 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013387 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 else
13389 /* Use goodword as-is. */
13390 STRCPY(cword, fword);
13391}
13392
Bram Moolenaarea424162005-06-16 21:51:00 +000013393/*
13394 * Use map string "map" for languages "lp".
13395 */
13396 static void
13397set_map_str(lp, map)
13398 slang_T *lp;
13399 char_u *map;
13400{
13401 char_u *p;
13402 int headc = 0;
13403 int c;
13404 int i;
13405
13406 if (*map == NUL)
13407 {
13408 lp->sl_has_map = FALSE;
13409 return;
13410 }
13411 lp->sl_has_map = TRUE;
13412
Bram Moolenaar4770d092006-01-12 23:22:24 +000013413 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013414 for (i = 0; i < 256; ++i)
13415 lp->sl_map_array[i] = 0;
13416#ifdef FEAT_MBYTE
13417 hash_init(&lp->sl_map_hash);
13418#endif
13419
13420 /*
13421 * The similar characters are stored separated with slashes:
13422 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13423 * before the same slash. For characters above 255 sl_map_hash is used.
13424 */
13425 for (p = map; *p != NUL; )
13426 {
13427#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013428 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013429#else
13430 c = *p++;
13431#endif
13432 if (c == '/')
13433 headc = 0;
13434 else
13435 {
13436 if (headc == 0)
13437 headc = c;
13438
13439#ifdef FEAT_MBYTE
13440 /* Characters above 255 don't fit in sl_map_array[], put them in
13441 * the hash table. Each entry is the char, a NUL the headchar and
13442 * a NUL. */
13443 if (c >= 256)
13444 {
13445 int cl = mb_char2len(c);
13446 int headcl = mb_char2len(headc);
13447 char_u *b;
13448 hash_T hash;
13449 hashitem_T *hi;
13450
13451 b = alloc((unsigned)(cl + headcl + 2));
13452 if (b == NULL)
13453 return;
13454 mb_char2bytes(c, b);
13455 b[cl] = NUL;
13456 mb_char2bytes(headc, b + cl + 1);
13457 b[cl + 1 + headcl] = NUL;
13458 hash = hash_hash(b);
13459 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13460 if (HASHITEM_EMPTY(hi))
13461 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13462 else
13463 {
13464 /* This should have been checked when generating the .spl
13465 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013466 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013467 vim_free(b);
13468 }
13469 }
13470 else
13471#endif
13472 lp->sl_map_array[c] = headc;
13473 }
13474 }
13475}
13476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477/*
13478 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13479 * lines in the .aff file.
13480 */
13481 static int
13482similar_chars(slang, c1, c2)
13483 slang_T *slang;
13484 int c1;
13485 int c2;
13486{
Bram Moolenaarea424162005-06-16 21:51:00 +000013487 int m1, m2;
13488#ifdef FEAT_MBYTE
13489 char_u buf[MB_MAXBYTES];
13490 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013491
Bram Moolenaarea424162005-06-16 21:51:00 +000013492 if (c1 >= 256)
13493 {
13494 buf[mb_char2bytes(c1, buf)] = 0;
13495 hi = hash_find(&slang->sl_map_hash, buf);
13496 if (HASHITEM_EMPTY(hi))
13497 m1 = 0;
13498 else
13499 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13500 }
13501 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013502#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013503 m1 = slang->sl_map_array[c1];
13504 if (m1 == 0)
13505 return FALSE;
13506
13507
13508#ifdef FEAT_MBYTE
13509 if (c2 >= 256)
13510 {
13511 buf[mb_char2bytes(c2, buf)] = 0;
13512 hi = hash_find(&slang->sl_map_hash, buf);
13513 if (HASHITEM_EMPTY(hi))
13514 m2 = 0;
13515 else
13516 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13517 }
13518 else
13519#endif
13520 m2 = slang->sl_map_array[c2];
13521
13522 return m1 == m2;
13523}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524
13525/*
13526 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013527 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 */
13529 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013530add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13531 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013533 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013534 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013535 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013536 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013537 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013538 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013539 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013540 int maxsf; /* su_maxscore applies to soundfold score,
13541 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013542{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013543 int goodlen; /* len of goodword changed */
13544 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013546 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013547 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013548 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013550 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13551 * "thee the" is added next to changing the first "the" the "thee". */
13552 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013553 pbad = su->su_badptr + badlenarg;
13554 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013555 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013556 goodlen = (int)(pgood - goodword);
13557 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013558 if (goodlen <= 0 || badlen <= 0)
13559 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013560 mb_ptr_back(goodword, pgood);
13561 mb_ptr_back(su->su_badptr, pbad);
13562#ifdef FEAT_MBYTE
13563 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013564 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013565 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13566 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013567 }
13568 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013569#endif
13570 if (*pgood != *pbad)
13571 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013572 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013573
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013574 if (badlen == 0 && goodlen == 0)
13575 /* goodword doesn't change anything; may happen for "the the" changing
13576 * the first "the" to itself. */
13577 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013578
Bram Moolenaar4770d092006-01-12 23:22:24 +000013579 /* Check if the word is already there. Also check the length that is
13580 * being replaced "thes," -> "these" is a different suggestion from
13581 * "thes" -> "these". */
13582 stp = &SUG(*gap, 0);
13583 for (i = gap->ga_len; --i >= 0; ++stp)
13584 if (stp->st_wordlen == goodlen
13585 && stp->st_orglen == badlen
13586 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13587 {
13588 /*
13589 * Found it. Remember the word with the lowest score.
13590 */
13591 if (stp->st_slang == NULL)
13592 stp->st_slang = slang;
13593
13594 new_sug.st_score = score;
13595 new_sug.st_altscore = altscore;
13596 new_sug.st_had_bonus = had_bonus;
13597
13598 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013599 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013600 /* Only one of the two had the soundalike score computed.
13601 * Need to do that for the other one now, otherwise the
13602 * scores can't be compared. This happens because
13603 * suggest_try_change() doesn't compute the soundalike
13604 * word to keep it fast, while some special methods set
13605 * the soundalike score to zero. */
13606 if (had_bonus)
13607 rescore_one(su, stp);
13608 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013609 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013610 new_sug.st_word = stp->st_word;
13611 new_sug.st_wordlen = stp->st_wordlen;
13612 new_sug.st_slang = stp->st_slang;
13613 new_sug.st_orglen = badlen;
13614 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013616 }
13617
Bram Moolenaar4770d092006-01-12 23:22:24 +000013618 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013619 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013620 stp->st_score = new_sug.st_score;
13621 stp->st_altscore = new_sug.st_altscore;
13622 stp->st_had_bonus = new_sug.st_had_bonus;
13623 }
13624 break;
13625 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626
Bram Moolenaar4770d092006-01-12 23:22:24 +000013627 if (i < 0 && ga_grow(gap, 1) == OK)
13628 {
13629 /* Add a suggestion. */
13630 stp = &SUG(*gap, gap->ga_len);
13631 stp->st_word = vim_strnsave(goodword, goodlen);
13632 if (stp->st_word != NULL)
13633 {
13634 stp->st_wordlen = goodlen;
13635 stp->st_score = score;
13636 stp->st_altscore = altscore;
13637 stp->st_had_bonus = had_bonus;
13638 stp->st_orglen = badlen;
13639 stp->st_slang = slang;
13640 ++gap->ga_len;
13641
13642 /* If we have too many suggestions now, sort the list and keep
13643 * the best suggestions. */
13644 if (gap->ga_len > SUG_MAX_COUNT(su))
13645 {
13646 if (maxsf)
13647 su->su_sfmaxscore = cleanup_suggestions(gap,
13648 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13649 else
13650 {
13651 i = su->su_maxscore;
13652 su->su_maxscore = cleanup_suggestions(gap,
13653 su->su_maxscore, SUG_CLEAN_COUNT(su));
13654 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 }
13656 }
13657 }
13658}
13659
13660/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013661 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13662 * for split words, such as "the the". Remove these from the list here.
13663 */
13664 static void
13665check_suggestions(su, gap)
13666 suginfo_T *su;
13667 garray_T *gap; /* either su_ga or su_sga */
13668{
13669 suggest_T *stp;
13670 int i;
13671 char_u longword[MAXWLEN + 1];
13672 int len;
13673 hlf_T attr;
13674
13675 stp = &SUG(*gap, 0);
13676 for (i = gap->ga_len - 1; i >= 0; --i)
13677 {
13678 /* Need to append what follows to check for "the the". */
13679 STRCPY(longword, stp[i].st_word);
13680 len = stp[i].st_wordlen;
13681 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13682 MAXWLEN - len);
13683 attr = HLF_COUNT;
13684 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13685 if (attr != HLF_COUNT)
13686 {
13687 /* Remove this entry. */
13688 vim_free(stp[i].st_word);
13689 --gap->ga_len;
13690 if (i < gap->ga_len)
13691 mch_memmove(stp + i, stp + i + 1,
13692 sizeof(suggest_T) * (gap->ga_len - i));
13693 }
13694 }
13695}
13696
13697
13698/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699 * Add a word to be banned.
13700 */
13701 static void
13702add_banned(su, word)
13703 suginfo_T *su;
13704 char_u *word;
13705{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013706 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 hash_T hash;
13708 hashitem_T *hi;
13709
Bram Moolenaar4770d092006-01-12 23:22:24 +000013710 hash = hash_hash(word);
13711 hi = hash_lookup(&su->su_banned, word, hash);
13712 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013713 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013714 s = vim_strsave(word);
13715 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 hash_add_item(&su->su_banned, hi, s, hash);
13717 }
13718}
13719
13720/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013721 * Recompute the score for all suggestions if sound-folding is possible. This
13722 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013723 */
13724 static void
13725rescore_suggestions(su)
13726 suginfo_T *su;
13727{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013728 int i;
13729
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013730 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013731 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013732 rescore_one(su, &SUG(su->su_ga, i));
13733}
13734
13735/*
13736 * Recompute the score for one suggestion if sound-folding is possible.
13737 */
13738 static void
13739rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013740 suginfo_T *su;
13741 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013742{
13743 slang_T *slang = stp->st_slang;
13744 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013745 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013746
13747 /* Only rescore suggestions that have no sal score yet and do have a
13748 * language. */
13749 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13750 {
13751 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013752 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013753 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013754 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013755 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013756 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013757 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013758
13759 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013760 if (stp->st_altscore == SCORE_MAXMAX)
13761 stp->st_altscore = SCORE_BIG;
13762 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13763 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013764 }
13765}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767static int
13768#ifdef __BORLANDC__
13769_RTLENTRYF
13770#endif
13771sug_compare __ARGS((const void *s1, const void *s2));
13772
13773/*
13774 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013775 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013776 */
13777 static int
13778#ifdef __BORLANDC__
13779_RTLENTRYF
13780#endif
13781sug_compare(s1, s2)
13782 const void *s1;
13783 const void *s2;
13784{
13785 suggest_T *p1 = (suggest_T *)s1;
13786 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013787 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013789 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013790 {
13791 n = p1->st_altscore - p2->st_altscore;
13792 if (n == 0)
13793 n = STRICMP(p1->st_word, p2->st_word);
13794 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013795 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013796}
13797
13798/*
13799 * Cleanup the suggestions:
13800 * - Sort on score.
13801 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013802 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013804 static int
13805cleanup_suggestions(gap, maxscore, keep)
13806 garray_T *gap;
13807 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013808 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013809{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013810 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811 int i;
13812
13813 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013814 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013815
13816 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013817 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013818 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013819 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013821 gap->ga_len = keep;
13822 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013824 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825}
13826
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013827#if defined(FEAT_EVAL) || defined(PROTO)
13828/*
13829 * Soundfold a string, for soundfold().
13830 * Result is in allocated memory, NULL for an error.
13831 */
13832 char_u *
13833eval_soundfold(word)
13834 char_u *word;
13835{
13836 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013837 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013838 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013839
13840 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13841 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013842 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013843 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013844 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013845 if (lp->lp_slang->sl_sal.ga_len > 0)
13846 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013847 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013848 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013849 return vim_strsave(sound);
13850 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013851 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013852
13853 /* No language with sound folding, return word as-is. */
13854 return vim_strsave(word);
13855}
13856#endif
13857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013858/*
13859 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013860 *
13861 * There are many ways to turn a word into a sound-a-like representation. The
13862 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13863 * swedish name matching - survey and test of different algorithms" by Klas
13864 * Erikson.
13865 *
13866 * We support two methods:
13867 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13868 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 */
13870 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013871spell_soundfold(slang, inword, folded, res)
13872 slang_T *slang;
13873 char_u *inword;
13874 int folded; /* "inword" is already case-folded */
13875 char_u *res;
13876{
13877 char_u fword[MAXWLEN];
13878 char_u *word;
13879
13880 if (slang->sl_sofo)
13881 /* SOFOFROM and SOFOTO used */
13882 spell_soundfold_sofo(slang, inword, res);
13883 else
13884 {
13885 /* SAL items used. Requires the word to be case-folded. */
13886 if (folded)
13887 word = inword;
13888 else
13889 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013890 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013891 word = fword;
13892 }
13893
13894#ifdef FEAT_MBYTE
13895 if (has_mbyte)
13896 spell_soundfold_wsal(slang, word, res);
13897 else
13898#endif
13899 spell_soundfold_sal(slang, word, res);
13900 }
13901}
13902
13903/*
13904 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13905 * SOFOTO lines.
13906 */
13907 static void
13908spell_soundfold_sofo(slang, inword, res)
13909 slang_T *slang;
13910 char_u *inword;
13911 char_u *res;
13912{
13913 char_u *s;
13914 int ri = 0;
13915 int c;
13916
13917#ifdef FEAT_MBYTE
13918 if (has_mbyte)
13919 {
13920 int prevc = 0;
13921 int *ip;
13922
13923 /* The sl_sal_first[] table contains the translation for chars up to
13924 * 255, sl_sal the rest. */
13925 for (s = inword; *s != NUL; )
13926 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013927 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013928 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13929 c = ' ';
13930 else if (c < 256)
13931 c = slang->sl_sal_first[c];
13932 else
13933 {
13934 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13935 if (ip == NULL) /* empty list, can't match */
13936 c = NUL;
13937 else
13938 for (;;) /* find "c" in the list */
13939 {
13940 if (*ip == 0) /* not found */
13941 {
13942 c = NUL;
13943 break;
13944 }
13945 if (*ip == c) /* match! */
13946 {
13947 c = ip[1];
13948 break;
13949 }
13950 ip += 2;
13951 }
13952 }
13953
13954 if (c != NUL && c != prevc)
13955 {
13956 ri += mb_char2bytes(c, res + ri);
13957 if (ri + MB_MAXBYTES > MAXWLEN)
13958 break;
13959 prevc = c;
13960 }
13961 }
13962 }
13963 else
13964#endif
13965 {
13966 /* The sl_sal_first[] table contains the translation. */
13967 for (s = inword; (c = *s) != NUL; ++s)
13968 {
13969 if (vim_iswhite(c))
13970 c = ' ';
13971 else
13972 c = slang->sl_sal_first[c];
13973 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13974 res[ri++] = c;
13975 }
13976 }
13977
13978 res[ri] = NUL;
13979}
13980
13981 static void
13982spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013983 slang_T *slang;
13984 char_u *inword;
13985 char_u *res;
13986{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013987 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013989 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013991 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013993 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 int n, k = 0;
13995 int z0;
13996 int k0;
13997 int n0;
13998 int c;
13999 int pri;
14000 int p0 = -333;
14001 int c0;
14002
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014003 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014004 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 if (slang->sl_rem_accents)
14006 {
14007 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014008 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014010 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014011 {
14012 *t++ = ' ';
14013 s = skipwhite(s);
14014 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014015 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014017 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 *t++ = *s;
14019 ++s;
14020 }
14021 }
14022 *t = NUL;
14023 }
14024 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014025 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014027 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014028
14029 /*
14030 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014031 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014033 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014034 while ((c = word[i]) != NUL)
14035 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014036 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 n = slang->sl_sal_first[c];
14038 z0 = 0;
14039
14040 if (n >= 0)
14041 {
14042 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014043 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014045 /* Quickly skip entries that don't match the word. Most
14046 * entries are less then three chars, optimize for that. */
14047 k = smp[n].sm_leadlen;
14048 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014050 if (word[i + 1] != s[1])
14051 continue;
14052 if (k > 2)
14053 {
14054 for (j = 2; j < k; ++j)
14055 if (word[i + j] != s[j])
14056 break;
14057 if (j < k)
14058 continue;
14059 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 }
14061
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014062 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014064 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014065 while (*pf != NUL && *pf != word[i + k])
14066 ++pf;
14067 if (*pf == NUL)
14068 continue;
14069 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014071 s = smp[n].sm_rules;
14072 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073
14074 p0 = *s;
14075 k0 = k;
14076 while (*s == '-' && k > 1)
14077 {
14078 k--;
14079 s++;
14080 }
14081 if (*s == '<')
14082 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014083 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 {
14085 /* determine priority */
14086 pri = *s - '0';
14087 s++;
14088 }
14089 if (*s == '^' && *(s + 1) == '^')
14090 s++;
14091
14092 if (*s == NUL
14093 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014094 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014095 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014097 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014099 && spell_iswordp(word + i - 1, curbuf)
14100 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 {
14102 /* search for followup rules, if: */
14103 /* followup and k > 1 and NO '-' in searchstring */
14104 c0 = word[i + k - 1];
14105 n0 = slang->sl_sal_first[c0];
14106
14107 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014108 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 {
14110 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014111 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014113 /* Quickly skip entries that don't match the word.
14114 * */
14115 k0 = smp[n0].sm_leadlen;
14116 if (k0 > 1)
14117 {
14118 if (word[i + k] != s[1])
14119 continue;
14120 if (k0 > 2)
14121 {
14122 pf = word + i + k + 1;
14123 for (j = 2; j < k0; ++j)
14124 if (*pf++ != s[j])
14125 break;
14126 if (j < k0)
14127 continue;
14128 }
14129 }
14130 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014132 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014133 {
14134 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014135 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014136 while (*pf != NUL && *pf != word[i + k0])
14137 ++pf;
14138 if (*pf == NUL)
14139 continue;
14140 ++k0;
14141 }
14142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014144 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 while (*s == '-')
14146 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014147 /* "k0" gets NOT reduced because
14148 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 s++;
14150 }
14151 if (*s == '<')
14152 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014153 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 {
14155 p0 = *s - '0';
14156 s++;
14157 }
14158
14159 if (*s == NUL
14160 /* *s == '^' cuts */
14161 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014162 && !spell_iswordp(word + i + k0,
14163 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 {
14165 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168
14169 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 /* rule fits; stop search */
14173 break;
14174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 }
14176
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014177 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 }
14180
14181 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014182 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014183 if (s == NULL)
14184 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014185 pf = smp[n].sm_rules;
14186 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 if (p0 == 1 && z == 0)
14188 {
14189 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014190 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14191 || res[reslen - 1] == *s))
14192 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 z0 = 1;
14194 z = 1;
14195 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014196 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 {
14198 word[i + k0] = *s;
14199 k0++;
14200 s++;
14201 }
14202 if (k > k0)
14203 mch_memmove(word + i + k0, word + i + k,
14204 STRLEN(word + i + k) + 1);
14205
14206 /* new "actual letter" */
14207 c = word[i];
14208 }
14209 else
14210 {
14211 /* no '<' rule used */
14212 i += k - 1;
14213 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014214 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014216 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014217 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 s++;
14219 }
14220 /* new "actual letter" */
14221 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014222 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 {
14224 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014225 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 mch_memmove(word, word + i + 1,
14227 STRLEN(word + i + 1) + 1);
14228 i = 0;
14229 z0 = 1;
14230 }
14231 }
14232 break;
14233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 }
14235 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014236 else if (vim_iswhite(c))
14237 {
14238 c = ' ';
14239 k = 1;
14240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241
14242 if (z0 == 0)
14243 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014244 if (k && !p0 && reslen < MAXWLEN && c != NUL
14245 && (!slang->sl_collapse || reslen == 0
14246 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014248 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249
14250 i++;
14251 z = 0;
14252 k = 0;
14253 }
14254 }
14255
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014256 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257}
14258
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014259#ifdef FEAT_MBYTE
14260/*
14261 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14262 * Multi-byte version of spell_soundfold().
14263 */
14264 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014265spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014266 slang_T *slang;
14267 char_u *inword;
14268 char_u *res;
14269{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014270 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014271 int word[MAXWLEN];
14272 int wres[MAXWLEN];
14273 int l;
14274 char_u *s;
14275 int *ws;
14276 char_u *t;
14277 int *pf;
14278 int i, j, z;
14279 int reslen;
14280 int n, k = 0;
14281 int z0;
14282 int k0;
14283 int n0;
14284 int c;
14285 int pri;
14286 int p0 = -333;
14287 int c0;
14288 int did_white = FALSE;
14289
14290 /*
14291 * Convert the multi-byte string to a wide-character string.
14292 * Remove accents, if wanted. We actually remove all non-word characters.
14293 * But keep white space.
14294 */
14295 n = 0;
14296 for (s = inword; *s != NUL; )
14297 {
14298 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014299 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014300 if (slang->sl_rem_accents)
14301 {
14302 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14303 {
14304 if (did_white)
14305 continue;
14306 c = ' ';
14307 did_white = TRUE;
14308 }
14309 else
14310 {
14311 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014312 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014313 continue;
14314 }
14315 }
14316 word[n++] = c;
14317 }
14318 word[n] = NUL;
14319
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014320 /*
14321 * This comes from Aspell phonet.cpp.
14322 * Converted from C++ to C. Added support for multi-byte chars.
14323 * Changed to keep spaces.
14324 */
14325 i = reslen = z = 0;
14326 while ((c = word[i]) != NUL)
14327 {
14328 /* Start with the first rule that has the character in the word. */
14329 n = slang->sl_sal_first[c & 0xff];
14330 z0 = 0;
14331
14332 if (n >= 0)
14333 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014334 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014335 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14336 {
14337 /* Quickly skip entries that don't match the word. Most
14338 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014339 if (c != ws[0])
14340 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014341 k = smp[n].sm_leadlen;
14342 if (k > 1)
14343 {
14344 if (word[i + 1] != ws[1])
14345 continue;
14346 if (k > 2)
14347 {
14348 for (j = 2; j < k; ++j)
14349 if (word[i + j] != ws[j])
14350 break;
14351 if (j < k)
14352 continue;
14353 }
14354 }
14355
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014356 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014357 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014358 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014359 while (*pf != NUL && *pf != word[i + k])
14360 ++pf;
14361 if (*pf == NUL)
14362 continue;
14363 ++k;
14364 }
14365 s = smp[n].sm_rules;
14366 pri = 5; /* default priority */
14367
14368 p0 = *s;
14369 k0 = k;
14370 while (*s == '-' && k > 1)
14371 {
14372 k--;
14373 s++;
14374 }
14375 if (*s == '<')
14376 s++;
14377 if (VIM_ISDIGIT(*s))
14378 {
14379 /* determine priority */
14380 pri = *s - '0';
14381 s++;
14382 }
14383 if (*s == '^' && *(s + 1) == '^')
14384 s++;
14385
14386 if (*s == NUL
14387 || (*s == '^'
14388 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014389 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014390 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014391 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014392 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014393 && spell_iswordp_w(word + i - 1, curbuf)
14394 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014395 {
14396 /* search for followup rules, if: */
14397 /* followup and k > 1 and NO '-' in searchstring */
14398 c0 = word[i + k - 1];
14399 n0 = slang->sl_sal_first[c0 & 0xff];
14400
14401 if (slang->sl_followup && k > 1 && n0 >= 0
14402 && p0 != '-' && word[i + k] != NUL)
14403 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014404 /* Test follow-up rule for "word[i + k]"; loop over
14405 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014406 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14407 == (c0 & 0xff); ++n0)
14408 {
14409 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014410 */
14411 if (c0 != ws[0])
14412 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014413 k0 = smp[n0].sm_leadlen;
14414 if (k0 > 1)
14415 {
14416 if (word[i + k] != ws[1])
14417 continue;
14418 if (k0 > 2)
14419 {
14420 pf = word + i + k + 1;
14421 for (j = 2; j < k0; ++j)
14422 if (*pf++ != ws[j])
14423 break;
14424 if (j < k0)
14425 continue;
14426 }
14427 }
14428 k0 += k - 1;
14429
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014430 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014431 {
14432 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014433 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014434 while (*pf != NUL && *pf != word[i + k0])
14435 ++pf;
14436 if (*pf == NUL)
14437 continue;
14438 ++k0;
14439 }
14440
14441 p0 = 5;
14442 s = smp[n0].sm_rules;
14443 while (*s == '-')
14444 {
14445 /* "k0" gets NOT reduced because
14446 * "if (k0 == k)" */
14447 s++;
14448 }
14449 if (*s == '<')
14450 s++;
14451 if (VIM_ISDIGIT(*s))
14452 {
14453 p0 = *s - '0';
14454 s++;
14455 }
14456
14457 if (*s == NUL
14458 /* *s == '^' cuts */
14459 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014460 && !spell_iswordp_w(word + i + k0,
14461 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014462 {
14463 if (k0 == k)
14464 /* this is just a piece of the string */
14465 continue;
14466
14467 if (p0 < pri)
14468 /* priority too low */
14469 continue;
14470 /* rule fits; stop search */
14471 break;
14472 }
14473 }
14474
14475 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14476 == (c0 & 0xff))
14477 continue;
14478 }
14479
14480 /* replace string */
14481 ws = smp[n].sm_to_w;
14482 s = smp[n].sm_rules;
14483 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14484 if (p0 == 1 && z == 0)
14485 {
14486 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014487 if (reslen > 0 && ws != NULL && *ws != NUL
14488 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014489 || wres[reslen - 1] == *ws))
14490 reslen--;
14491 z0 = 1;
14492 z = 1;
14493 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014494 if (ws != NULL)
14495 while (*ws != NUL && word[i + k0] != NUL)
14496 {
14497 word[i + k0] = *ws;
14498 k0++;
14499 ws++;
14500 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014501 if (k > k0)
14502 mch_memmove(word + i + k0, word + i + k,
14503 sizeof(int) * (STRLEN(word + i + k) + 1));
14504
14505 /* new "actual letter" */
14506 c = word[i];
14507 }
14508 else
14509 {
14510 /* no '<' rule used */
14511 i += k - 1;
14512 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014513 if (ws != NULL)
14514 while (*ws != NUL && ws[1] != NUL
14515 && reslen < MAXWLEN)
14516 {
14517 if (reslen == 0 || wres[reslen - 1] != *ws)
14518 wres[reslen++] = *ws;
14519 ws++;
14520 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014521 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014522 if (ws == NULL)
14523 c = NUL;
14524 else
14525 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014526 if (strstr((char *)s, "^^") != NULL)
14527 {
14528 if (c != NUL)
14529 wres[reslen++] = c;
14530 mch_memmove(word, word + i + 1,
14531 sizeof(int) * (STRLEN(word + i + 1) + 1));
14532 i = 0;
14533 z0 = 1;
14534 }
14535 }
14536 break;
14537 }
14538 }
14539 }
14540 else if (vim_iswhite(c))
14541 {
14542 c = ' ';
14543 k = 1;
14544 }
14545
14546 if (z0 == 0)
14547 {
14548 if (k && !p0 && reslen < MAXWLEN && c != NUL
14549 && (!slang->sl_collapse || reslen == 0
14550 || wres[reslen - 1] != c))
14551 /* condense only double letters */
14552 wres[reslen++] = c;
14553
14554 i++;
14555 z = 0;
14556 k = 0;
14557 }
14558 }
14559
14560 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14561 l = 0;
14562 for (n = 0; n < reslen; ++n)
14563 {
14564 l += mb_char2bytes(wres[n], res + l);
14565 if (l + MB_MAXBYTES > MAXWLEN)
14566 break;
14567 }
14568 res[l] = NUL;
14569}
14570#endif
14571
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014572/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014573 * Compute a score for two sound-a-like words.
14574 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14575 * Instead of a generic loop we write out the code. That keeps it fast by
14576 * avoiding checks that will not be possible.
14577 */
14578 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014579soundalike_score(goodstart, badstart)
14580 char_u *goodstart; /* sound-folded good word */
14581 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014582{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014583 char_u *goodsound = goodstart;
14584 char_u *badsound = badstart;
14585 int goodlen;
14586 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014587 int n;
14588 char_u *pl, *ps;
14589 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014590 int score = 0;
14591
14592 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14593 * counted so much, vowels halfway the word aren't counted at all. */
14594 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14595 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014596 if (badsound[1] == goodsound[1]
14597 || (badsound[1] != NUL
14598 && goodsound[1] != NUL
14599 && badsound[2] == goodsound[2]))
14600 {
14601 /* handle like a substitute */
14602 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014603 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014604 {
14605 score = 2 * SCORE_DEL / 3;
14606 if (*badsound == '*')
14607 ++badsound;
14608 else
14609 ++goodsound;
14610 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014611 }
14612
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014613 goodlen = (int)STRLEN(goodsound);
14614 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014615
14616 /* Return quickly if the lenghts are too different to be fixed by two
14617 * changes. */
14618 n = goodlen - badlen;
14619 if (n < -2 || n > 2)
14620 return SCORE_MAXMAX;
14621
14622 if (n > 0)
14623 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014624 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014625 ps = badsound;
14626 }
14627 else
14628 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014629 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014630 ps = goodsound;
14631 }
14632
14633 /* Skip over the identical part. */
14634 while (*pl == *ps && *pl != NUL)
14635 {
14636 ++pl;
14637 ++ps;
14638 }
14639
14640 switch (n)
14641 {
14642 case -2:
14643 case 2:
14644 /*
14645 * Must delete two characters from "pl".
14646 */
14647 ++pl; /* first delete */
14648 while (*pl == *ps)
14649 {
14650 ++pl;
14651 ++ps;
14652 }
14653 /* strings must be equal after second delete */
14654 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014655 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014656
14657 /* Failed to compare. */
14658 break;
14659
14660 case -1:
14661 case 1:
14662 /*
14663 * Minimal one delete from "pl" required.
14664 */
14665
14666 /* 1: delete */
14667 pl2 = pl + 1;
14668 ps2 = ps;
14669 while (*pl2 == *ps2)
14670 {
14671 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014672 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014673 ++pl2;
14674 ++ps2;
14675 }
14676
14677 /* 2: delete then swap, then rest must be equal */
14678 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14679 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014680 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014681
14682 /* 3: delete then substitute, then the rest must be equal */
14683 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014684 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014685
14686 /* 4: first swap then delete */
14687 if (pl[0] == ps[1] && pl[1] == ps[0])
14688 {
14689 pl2 = pl + 2; /* swap, skip two chars */
14690 ps2 = ps + 2;
14691 while (*pl2 == *ps2)
14692 {
14693 ++pl2;
14694 ++ps2;
14695 }
14696 /* delete a char and then strings must be equal */
14697 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014698 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014699 }
14700
14701 /* 5: first substitute then delete */
14702 pl2 = pl + 1; /* substitute, skip one char */
14703 ps2 = ps + 1;
14704 while (*pl2 == *ps2)
14705 {
14706 ++pl2;
14707 ++ps2;
14708 }
14709 /* delete a char and then strings must be equal */
14710 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014711 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014712
14713 /* Failed to compare. */
14714 break;
14715
14716 case 0:
14717 /*
14718 * Lenghts are equal, thus changes must result in same length: An
14719 * insert is only possible in combination with a delete.
14720 * 1: check if for identical strings
14721 */
14722 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014723 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014724
14725 /* 2: swap */
14726 if (pl[0] == ps[1] && pl[1] == ps[0])
14727 {
14728 pl2 = pl + 2; /* swap, skip two chars */
14729 ps2 = ps + 2;
14730 while (*pl2 == *ps2)
14731 {
14732 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014733 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014734 ++pl2;
14735 ++ps2;
14736 }
14737 /* 3: swap and swap again */
14738 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14739 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014740 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014741
14742 /* 4: swap and substitute */
14743 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014744 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014745 }
14746
14747 /* 5: substitute */
14748 pl2 = pl + 1;
14749 ps2 = ps + 1;
14750 while (*pl2 == *ps2)
14751 {
14752 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014753 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014754 ++pl2;
14755 ++ps2;
14756 }
14757
14758 /* 6: substitute and swap */
14759 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14760 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014761 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014762
14763 /* 7: substitute and substitute */
14764 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014765 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014766
14767 /* 8: insert then delete */
14768 pl2 = pl;
14769 ps2 = ps + 1;
14770 while (*pl2 == *ps2)
14771 {
14772 ++pl2;
14773 ++ps2;
14774 }
14775 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014776 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014777
14778 /* 9: delete then insert */
14779 pl2 = pl + 1;
14780 ps2 = ps;
14781 while (*pl2 == *ps2)
14782 {
14783 ++pl2;
14784 ++ps2;
14785 }
14786 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014787 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014788
14789 /* Failed to compare. */
14790 break;
14791 }
14792
14793 return SCORE_MAXMAX;
14794}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796/*
14797 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014798 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014799 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014800 * The algorithm is described by Du and Chang, 1992.
14801 * The implementation of the algorithm comes from Aspell editdist.cpp,
14802 * edit_distance(). It has been converted from C++ to C and modified to
14803 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 */
14805 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014806spell_edit_score(slang, badword, goodword)
14807 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014808 char_u *badword;
14809 char_u *goodword;
14810{
14811 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014812 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014813 int j, i;
14814 int t;
14815 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014816 int pbc, pgc;
14817#ifdef FEAT_MBYTE
14818 char_u *p;
14819 int wbadword[MAXWLEN];
14820 int wgoodword[MAXWLEN];
14821
14822 if (has_mbyte)
14823 {
14824 /* Get the characters from the multi-byte strings and put them in an
14825 * int array for easy access. */
14826 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014827 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014828 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014829 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014830 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014831 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014832 }
14833 else
14834#endif
14835 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014836 badlen = (int)STRLEN(badword) + 1;
14837 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014838 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839
14840 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14841#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014842 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14843 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014844 if (cnt == NULL)
14845 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014846
14847 CNT(0, 0) = 0;
14848 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014849 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850
14851 for (i = 1; i <= badlen; ++i)
14852 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014853 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 for (j = 1; j <= goodlen; ++j)
14855 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014856#ifdef FEAT_MBYTE
14857 if (has_mbyte)
14858 {
14859 bc = wbadword[i - 1];
14860 gc = wgoodword[j - 1];
14861 }
14862 else
14863#endif
14864 {
14865 bc = badword[i - 1];
14866 gc = goodword[j - 1];
14867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 if (bc == gc)
14869 CNT(i, j) = CNT(i - 1, j - 1);
14870 else
14871 {
14872 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014873 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14875 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014876 {
14877 /* For a similar character use SCORE_SIMILAR. */
14878 if (slang != NULL
14879 && slang->sl_has_map
14880 && similar_chars(slang, gc, bc))
14881 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14882 else
14883 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14884 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014886 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014888#ifdef FEAT_MBYTE
14889 if (has_mbyte)
14890 {
14891 pbc = wbadword[i - 2];
14892 pgc = wgoodword[j - 2];
14893 }
14894 else
14895#endif
14896 {
14897 pbc = badword[i - 2];
14898 pgc = goodword[j - 2];
14899 }
14900 if (bc == pgc && pbc == gc)
14901 {
14902 t = SCORE_SWAP + CNT(i - 2, j - 2);
14903 if (t < CNT(i, j))
14904 CNT(i, j) = t;
14905 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014906 }
14907 t = SCORE_DEL + CNT(i - 1, j);
14908 if (t < CNT(i, j))
14909 CNT(i, j) = t;
14910 t = SCORE_INS + CNT(i, j - 1);
14911 if (t < CNT(i, j))
14912 CNT(i, j) = t;
14913 }
14914 }
14915 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014916
14917 i = CNT(badlen - 1, goodlen - 1);
14918 vim_free(cnt);
14919 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014920}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014921
Bram Moolenaar4770d092006-01-12 23:22:24 +000014922typedef struct
14923{
14924 int badi;
14925 int goodi;
14926 int score;
14927} limitscore_T;
14928
14929/*
14930 * Like spell_edit_score(), but with a limit on the score to make it faster.
14931 * May return SCORE_MAXMAX when the score is higher than "limit".
14932 *
14933 * This uses a stack for the edits still to be tried.
14934 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14935 * for multi-byte characters.
14936 */
14937 static int
14938spell_edit_score_limit(slang, badword, goodword, limit)
14939 slang_T *slang;
14940 char_u *badword;
14941 char_u *goodword;
14942 int limit;
14943{
14944 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14945 int stackidx;
14946 int bi, gi;
14947 int bi2, gi2;
14948 int bc, gc;
14949 int score;
14950 int score_off;
14951 int minscore;
14952 int round;
14953
14954#ifdef FEAT_MBYTE
14955 /* Multi-byte characters require a bit more work, use a different function
14956 * to avoid testing "has_mbyte" quite often. */
14957 if (has_mbyte)
14958 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14959#endif
14960
14961 /*
14962 * The idea is to go from start to end over the words. So long as
14963 * characters are equal just continue, this always gives the lowest score.
14964 * When there is a difference try several alternatives. Each alternative
14965 * increases "score" for the edit distance. Some of the alternatives are
14966 * pushed unto a stack and tried later, some are tried right away. At the
14967 * end of the word the score for one alternative is known. The lowest
14968 * possible score is stored in "minscore".
14969 */
14970 stackidx = 0;
14971 bi = 0;
14972 gi = 0;
14973 score = 0;
14974 minscore = limit + 1;
14975
14976 for (;;)
14977 {
14978 /* Skip over an equal part, score remains the same. */
14979 for (;;)
14980 {
14981 bc = badword[bi];
14982 gc = goodword[gi];
14983 if (bc != gc) /* stop at a char that's different */
14984 break;
14985 if (bc == NUL) /* both words end */
14986 {
14987 if (score < minscore)
14988 minscore = score;
14989 goto pop; /* do next alternative */
14990 }
14991 ++bi;
14992 ++gi;
14993 }
14994
14995 if (gc == NUL) /* goodword ends, delete badword chars */
14996 {
14997 do
14998 {
14999 if ((score += SCORE_DEL) >= minscore)
15000 goto pop; /* do next alternative */
15001 } while (badword[++bi] != NUL);
15002 minscore = score;
15003 }
15004 else if (bc == NUL) /* badword ends, insert badword chars */
15005 {
15006 do
15007 {
15008 if ((score += SCORE_INS) >= minscore)
15009 goto pop; /* do next alternative */
15010 } while (goodword[++gi] != NUL);
15011 minscore = score;
15012 }
15013 else /* both words continue */
15014 {
15015 /* If not close to the limit, perform a change. Only try changes
15016 * that may lead to a lower score than "minscore".
15017 * round 0: try deleting a char from badword
15018 * round 1: try inserting a char in badword */
15019 for (round = 0; round <= 1; ++round)
15020 {
15021 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15022 if (score_off < minscore)
15023 {
15024 if (score_off + SCORE_EDIT_MIN >= minscore)
15025 {
15026 /* Near the limit, rest of the words must match. We
15027 * can check that right now, no need to push an item
15028 * onto the stack. */
15029 bi2 = bi + 1 - round;
15030 gi2 = gi + round;
15031 while (goodword[gi2] == badword[bi2])
15032 {
15033 if (goodword[gi2] == NUL)
15034 {
15035 minscore = score_off;
15036 break;
15037 }
15038 ++bi2;
15039 ++gi2;
15040 }
15041 }
15042 else
15043 {
15044 /* try deleting/inserting a character later */
15045 stack[stackidx].badi = bi + 1 - round;
15046 stack[stackidx].goodi = gi + round;
15047 stack[stackidx].score = score_off;
15048 ++stackidx;
15049 }
15050 }
15051 }
15052
15053 if (score + SCORE_SWAP < minscore)
15054 {
15055 /* If swapping two characters makes a match then the
15056 * substitution is more expensive, thus there is no need to
15057 * try both. */
15058 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15059 {
15060 /* Swap two characters, that is: skip them. */
15061 gi += 2;
15062 bi += 2;
15063 score += SCORE_SWAP;
15064 continue;
15065 }
15066 }
15067
15068 /* Substitute one character for another which is the same
15069 * thing as deleting a character from both goodword and badword.
15070 * Use a better score when there is only a case difference. */
15071 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15072 score += SCORE_ICASE;
15073 else
15074 {
15075 /* For a similar character use SCORE_SIMILAR. */
15076 if (slang != NULL
15077 && slang->sl_has_map
15078 && similar_chars(slang, gc, bc))
15079 score += SCORE_SIMILAR;
15080 else
15081 score += SCORE_SUBST;
15082 }
15083
15084 if (score < minscore)
15085 {
15086 /* Do the substitution. */
15087 ++gi;
15088 ++bi;
15089 continue;
15090 }
15091 }
15092pop:
15093 /*
15094 * Get here to try the next alternative, pop it from the stack.
15095 */
15096 if (stackidx == 0) /* stack is empty, finished */
15097 break;
15098
15099 /* pop an item from the stack */
15100 --stackidx;
15101 gi = stack[stackidx].goodi;
15102 bi = stack[stackidx].badi;
15103 score = stack[stackidx].score;
15104 }
15105
15106 /* When the score goes over "limit" it may actually be much higher.
15107 * Return a very large number to avoid going below the limit when giving a
15108 * bonus. */
15109 if (minscore > limit)
15110 return SCORE_MAXMAX;
15111 return minscore;
15112}
15113
15114#ifdef FEAT_MBYTE
15115/*
15116 * Multi-byte version of spell_edit_score_limit().
15117 * Keep it in sync with the above!
15118 */
15119 static int
15120spell_edit_score_limit_w(slang, badword, goodword, limit)
15121 slang_T *slang;
15122 char_u *badword;
15123 char_u *goodword;
15124 int limit;
15125{
15126 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15127 int stackidx;
15128 int bi, gi;
15129 int bi2, gi2;
15130 int bc, gc;
15131 int score;
15132 int score_off;
15133 int minscore;
15134 int round;
15135 char_u *p;
15136 int wbadword[MAXWLEN];
15137 int wgoodword[MAXWLEN];
15138
15139 /* Get the characters from the multi-byte strings and put them in an
15140 * int array for easy access. */
15141 bi = 0;
15142 for (p = badword; *p != NUL; )
15143 wbadword[bi++] = mb_cptr2char_adv(&p);
15144 wbadword[bi++] = 0;
15145 gi = 0;
15146 for (p = goodword; *p != NUL; )
15147 wgoodword[gi++] = mb_cptr2char_adv(&p);
15148 wgoodword[gi++] = 0;
15149
15150 /*
15151 * The idea is to go from start to end over the words. So long as
15152 * characters are equal just continue, this always gives the lowest score.
15153 * When there is a difference try several alternatives. Each alternative
15154 * increases "score" for the edit distance. Some of the alternatives are
15155 * pushed unto a stack and tried later, some are tried right away. At the
15156 * end of the word the score for one alternative is known. The lowest
15157 * possible score is stored in "minscore".
15158 */
15159 stackidx = 0;
15160 bi = 0;
15161 gi = 0;
15162 score = 0;
15163 minscore = limit + 1;
15164
15165 for (;;)
15166 {
15167 /* Skip over an equal part, score remains the same. */
15168 for (;;)
15169 {
15170 bc = wbadword[bi];
15171 gc = wgoodword[gi];
15172
15173 if (bc != gc) /* stop at a char that's different */
15174 break;
15175 if (bc == NUL) /* both words end */
15176 {
15177 if (score < minscore)
15178 minscore = score;
15179 goto pop; /* do next alternative */
15180 }
15181 ++bi;
15182 ++gi;
15183 }
15184
15185 if (gc == NUL) /* goodword ends, delete badword chars */
15186 {
15187 do
15188 {
15189 if ((score += SCORE_DEL) >= minscore)
15190 goto pop; /* do next alternative */
15191 } while (wbadword[++bi] != NUL);
15192 minscore = score;
15193 }
15194 else if (bc == NUL) /* badword ends, insert badword chars */
15195 {
15196 do
15197 {
15198 if ((score += SCORE_INS) >= minscore)
15199 goto pop; /* do next alternative */
15200 } while (wgoodword[++gi] != NUL);
15201 minscore = score;
15202 }
15203 else /* both words continue */
15204 {
15205 /* If not close to the limit, perform a change. Only try changes
15206 * that may lead to a lower score than "minscore".
15207 * round 0: try deleting a char from badword
15208 * round 1: try inserting a char in badword */
15209 for (round = 0; round <= 1; ++round)
15210 {
15211 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15212 if (score_off < minscore)
15213 {
15214 if (score_off + SCORE_EDIT_MIN >= minscore)
15215 {
15216 /* Near the limit, rest of the words must match. We
15217 * can check that right now, no need to push an item
15218 * onto the stack. */
15219 bi2 = bi + 1 - round;
15220 gi2 = gi + round;
15221 while (wgoodword[gi2] == wbadword[bi2])
15222 {
15223 if (wgoodword[gi2] == NUL)
15224 {
15225 minscore = score_off;
15226 break;
15227 }
15228 ++bi2;
15229 ++gi2;
15230 }
15231 }
15232 else
15233 {
15234 /* try deleting a character from badword later */
15235 stack[stackidx].badi = bi + 1 - round;
15236 stack[stackidx].goodi = gi + round;
15237 stack[stackidx].score = score_off;
15238 ++stackidx;
15239 }
15240 }
15241 }
15242
15243 if (score + SCORE_SWAP < minscore)
15244 {
15245 /* If swapping two characters makes a match then the
15246 * substitution is more expensive, thus there is no need to
15247 * try both. */
15248 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15249 {
15250 /* Swap two characters, that is: skip them. */
15251 gi += 2;
15252 bi += 2;
15253 score += SCORE_SWAP;
15254 continue;
15255 }
15256 }
15257
15258 /* Substitute one character for another which is the same
15259 * thing as deleting a character from both goodword and badword.
15260 * Use a better score when there is only a case difference. */
15261 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15262 score += SCORE_ICASE;
15263 else
15264 {
15265 /* For a similar character use SCORE_SIMILAR. */
15266 if (slang != NULL
15267 && slang->sl_has_map
15268 && similar_chars(slang, gc, bc))
15269 score += SCORE_SIMILAR;
15270 else
15271 score += SCORE_SUBST;
15272 }
15273
15274 if (score < minscore)
15275 {
15276 /* Do the substitution. */
15277 ++gi;
15278 ++bi;
15279 continue;
15280 }
15281 }
15282pop:
15283 /*
15284 * Get here to try the next alternative, pop it from the stack.
15285 */
15286 if (stackidx == 0) /* stack is empty, finished */
15287 break;
15288
15289 /* pop an item from the stack */
15290 --stackidx;
15291 gi = stack[stackidx].goodi;
15292 bi = stack[stackidx].badi;
15293 score = stack[stackidx].score;
15294 }
15295
15296 /* When the score goes over "limit" it may actually be much higher.
15297 * Return a very large number to avoid going below the limit when giving a
15298 * bonus. */
15299 if (minscore > limit)
15300 return SCORE_MAXMAX;
15301 return minscore;
15302}
15303#endif
15304
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015305/*
15306 * ":spellinfo"
15307 */
15308/*ARGSUSED*/
15309 void
15310ex_spellinfo(eap)
15311 exarg_T *eap;
15312{
15313 int lpi;
15314 langp_T *lp;
15315 char_u *p;
15316
15317 if (no_spell_checking(curwin))
15318 return;
15319
15320 msg_start();
15321 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15322 {
15323 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15324 msg_puts((char_u *)"file: ");
15325 msg_puts(lp->lp_slang->sl_fname);
15326 msg_putchar('\n');
15327 p = lp->lp_slang->sl_info;
15328 if (p != NULL)
15329 {
15330 msg_puts(p);
15331 msg_putchar('\n');
15332 }
15333 }
15334 msg_end();
15335}
15336
Bram Moolenaar4770d092006-01-12 23:22:24 +000015337#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15338#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015339#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015340#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15341#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015342
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015343/*
15344 * ":spelldump"
15345 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015346 void
15347ex_spelldump(eap)
15348 exarg_T *eap;
15349{
15350 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015351
15352 if (no_spell_checking(curwin))
15353 return;
15354
15355 /* Create a new empty buffer by splitting the window. */
15356 do_cmdline_cmd((char_u *)"new");
15357 if (!bufempty() || !buf_valid(buf))
15358 return;
15359
15360 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15361
15362 /* Delete the empty line that we started with. */
15363 if (curbuf->b_ml.ml_line_count > 1)
15364 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15365
15366 redraw_later(NOT_VALID);
15367}
15368
15369/*
15370 * Go through all possible words and:
15371 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15372 * "ic" and "dir" are not used.
15373 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15374 */
15375 void
15376spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15377 buf_T *buf; /* buffer with spell checking */
15378 char_u *pat; /* leading part of the word */
15379 int ic; /* ignore case */
15380 int *dir; /* direction for adding matches */
15381 int dumpflags_arg; /* DUMPFLAG_* */
15382{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015383 langp_T *lp;
15384 slang_T *slang;
15385 idx_T arridx[MAXWLEN];
15386 int curi[MAXWLEN];
15387 char_u word[MAXWLEN];
15388 int c;
15389 char_u *byts;
15390 idx_T *idxs;
15391 linenr_T lnum = 0;
15392 int round;
15393 int depth;
15394 int n;
15395 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015396 char_u *region_names = NULL; /* region names being used */
15397 int do_region = TRUE; /* dump region names and numbers */
15398 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015399 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015400 int dumpflags = dumpflags_arg;
15401 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015402
Bram Moolenaard0131a82006-03-04 21:46:13 +000015403 /* When ignoring case or when the pattern starts with capital pass this on
15404 * to dump_word(). */
15405 if (pat != NULL)
15406 {
15407 if (ic)
15408 dumpflags |= DUMPFLAG_ICASE;
15409 else
15410 {
15411 n = captype(pat, NULL);
15412 if (n == WF_ONECAP)
15413 dumpflags |= DUMPFLAG_ONECAP;
15414 else if (n == WF_ALLCAP
15415#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015416 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015417#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015418 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015419#endif
15420 )
15421 dumpflags |= DUMPFLAG_ALLCAP;
15422 }
15423 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015424
Bram Moolenaar7887d882005-07-01 22:33:52 +000015425 /* Find out if we can support regions: All languages must support the same
15426 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015427 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015428 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015429 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015430 p = lp->lp_slang->sl_regions;
15431 if (p[0] != 0)
15432 {
15433 if (region_names == NULL) /* first language with regions */
15434 region_names = p;
15435 else if (STRCMP(region_names, p) != 0)
15436 {
15437 do_region = FALSE; /* region names are different */
15438 break;
15439 }
15440 }
15441 }
15442
15443 if (do_region && region_names != NULL)
15444 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015445 if (pat == NULL)
15446 {
15447 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15448 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15449 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015450 }
15451 else
15452 do_region = FALSE;
15453
15454 /*
15455 * Loop over all files loaded for the entries in 'spelllang'.
15456 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015457 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015458 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015459 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015460 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015461 if (slang->sl_fbyts == NULL) /* reloading failed */
15462 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015463
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015464 if (pat == NULL)
15465 {
15466 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15467 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15468 }
15469
15470 /* When matching with a pattern and there are no prefixes only use
15471 * parts of the tree that match "pat". */
15472 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015473 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015474 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015475 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015476
15477 /* round 1: case-folded tree
15478 * round 2: keep-case tree */
15479 for (round = 1; round <= 2; ++round)
15480 {
15481 if (round == 1)
15482 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015483 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015484 byts = slang->sl_fbyts;
15485 idxs = slang->sl_fidxs;
15486 }
15487 else
15488 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015489 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015490 byts = slang->sl_kbyts;
15491 idxs = slang->sl_kidxs;
15492 }
15493 if (byts == NULL)
15494 continue; /* array is empty */
15495
15496 depth = 0;
15497 arridx[0] = 0;
15498 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015499 while (depth >= 0 && !got_int
15500 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015501 {
15502 if (curi[depth] > byts[arridx[depth]])
15503 {
15504 /* Done all bytes at this node, go up one level. */
15505 --depth;
15506 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015507 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015508 }
15509 else
15510 {
15511 /* Do one more byte at this node. */
15512 n = arridx[depth] + curi[depth];
15513 ++curi[depth];
15514 c = byts[n];
15515 if (c == 0)
15516 {
15517 /* End of word, deal with the word.
15518 * Don't use keep-case words in the fold-case tree,
15519 * they will appear in the keep-case tree.
15520 * Only use the word when the region matches. */
15521 flags = (int)idxs[n];
15522 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015523 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015524 && (do_region
15525 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015526 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015527 & lp->lp_region) != 0))
15528 {
15529 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015530 if (!do_region)
15531 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015532
15533 /* Dump the basic word if there is no prefix or
15534 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015535 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015536 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015537 {
15538 dump_word(slang, word, pat, dir,
15539 dumpflags, flags, lnum);
15540 if (pat == NULL)
15541 ++lnum;
15542 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015543
15544 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015545 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015546 lnum = dump_prefixes(slang, word, pat, dir,
15547 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548 }
15549 }
15550 else
15551 {
15552 /* Normal char, go one level deeper. */
15553 word[depth++] = c;
15554 arridx[depth] = idxs[n];
15555 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015556
15557 /* Check if this characters matches with the pattern.
15558 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015559 * Always ignore case here, dump_word() will check
15560 * proper case later. This isn't exactly right when
15561 * length changes for multi-byte characters with
15562 * ignore case... */
15563 if (depth <= patlen
15564 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015565 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015566 }
15567 }
15568 }
15569 }
15570 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015571}
15572
15573/*
15574 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015575 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015576 */
15577 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015578dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015579 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015580 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015581 char_u *pat;
15582 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015583 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015584 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015585 linenr_T lnum;
15586{
15587 int keepcap = FALSE;
15588 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015589 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015590 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015591 char_u badword[MAXWLEN + 10];
15592 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015593 int flags = wordflags;
15594
15595 if (dumpflags & DUMPFLAG_ONECAP)
15596 flags |= WF_ONECAP;
15597 if (dumpflags & DUMPFLAG_ALLCAP)
15598 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015599
Bram Moolenaar4770d092006-01-12 23:22:24 +000015600 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015601 {
15602 /* Need to fix case according to "flags". */
15603 make_case_word(word, cword, flags);
15604 p = cword;
15605 }
15606 else
15607 {
15608 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015609 if ((dumpflags & DUMPFLAG_KEEPCASE)
15610 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015611 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015612 keepcap = TRUE;
15613 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015614 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015615
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015616 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015617 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015618 /* Add flags and regions after a slash. */
15619 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015620 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015621 STRCPY(badword, p);
15622 STRCAT(badword, "/");
15623 if (keepcap)
15624 STRCAT(badword, "=");
15625 if (flags & WF_BANNED)
15626 STRCAT(badword, "!");
15627 else if (flags & WF_RARE)
15628 STRCAT(badword, "?");
15629 if (flags & WF_REGION)
15630 for (i = 0; i < 7; ++i)
15631 if (flags & (0x10000 << i))
15632 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15633 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015634 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015635
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015636 if (dumpflags & DUMPFLAG_COUNT)
15637 {
15638 hashitem_T *hi;
15639
15640 /* Include the word count for ":spelldump!". */
15641 hi = hash_find(&slang->sl_wordcount, tw);
15642 if (!HASHITEM_EMPTY(hi))
15643 {
15644 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15645 tw, HI2WC(hi)->wc_count);
15646 p = IObuff;
15647 }
15648 }
15649
15650 ml_append(lnum, p, (colnr_T)0, FALSE);
15651 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015652 else if (((dumpflags & DUMPFLAG_ICASE)
15653 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15654 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015655 && ins_compl_add_infercase(p, (int)STRLEN(p),
15656 dumpflags & DUMPFLAG_ICASE,
15657 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015658 /* if dir was BACKWARD then honor it just once */
15659 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015660}
15661
15662/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015663 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15664 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015665 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015666 * Return the updated line number.
15667 */
15668 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015669dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015670 slang_T *slang;
15671 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015672 char_u *pat;
15673 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015674 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015675 int flags; /* flags with prefix ID */
15676 linenr_T startlnum;
15677{
15678 idx_T arridx[MAXWLEN];
15679 int curi[MAXWLEN];
15680 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015681 char_u word_up[MAXWLEN];
15682 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015683 int c;
15684 char_u *byts;
15685 idx_T *idxs;
15686 linenr_T lnum = startlnum;
15687 int depth;
15688 int n;
15689 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015690 int i;
15691
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015692 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015693 * upper-case letter in word_up[]. */
15694 c = PTR2CHAR(word);
15695 if (SPELL_TOUPPER(c) != c)
15696 {
15697 onecap_copy(word, word_up, TRUE);
15698 has_word_up = TRUE;
15699 }
15700
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015701 byts = slang->sl_pbyts;
15702 idxs = slang->sl_pidxs;
15703 if (byts != NULL) /* array not is empty */
15704 {
15705 /*
15706 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015707 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015708 */
15709 depth = 0;
15710 arridx[0] = 0;
15711 curi[0] = 1;
15712 while (depth >= 0 && !got_int)
15713 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015714 n = arridx[depth];
15715 len = byts[n];
15716 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015717 {
15718 /* Done all bytes at this node, go up one level. */
15719 --depth;
15720 line_breakcheck();
15721 }
15722 else
15723 {
15724 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015725 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015726 ++curi[depth];
15727 c = byts[n];
15728 if (c == 0)
15729 {
15730 /* End of prefix, find out how many IDs there are. */
15731 for (i = 1; i < len; ++i)
15732 if (byts[n + i] != 0)
15733 break;
15734 curi[depth] += i - 1;
15735
Bram Moolenaar53805d12005-08-01 07:08:33 +000015736 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15737 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015738 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015739 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015740 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015741 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015742 : flags, lnum);
15743 if (lnum != 0)
15744 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015745 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015746
15747 /* Check for prefix that matches the word when the
15748 * first letter is upper-case, but only if the prefix has
15749 * a condition. */
15750 if (has_word_up)
15751 {
15752 c = valid_word_prefix(i, n, flags, word_up, slang,
15753 TRUE);
15754 if (c != 0)
15755 {
15756 vim_strncpy(prefix + depth, word_up,
15757 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015758 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015759 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015760 : flags, lnum);
15761 if (lnum != 0)
15762 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015763 }
15764 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015765 }
15766 else
15767 {
15768 /* Normal char, go one level deeper. */
15769 prefix[depth++] = c;
15770 arridx[depth] = idxs[n];
15771 curi[depth] = 1;
15772 }
15773 }
15774 }
15775 }
15776
15777 return lnum;
15778}
15779
Bram Moolenaar95529562005-08-25 21:21:38 +000015780/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015781 * Move "p" to the end of word "start".
15782 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015783 */
15784 char_u *
15785spell_to_word_end(start, buf)
15786 char_u *start;
15787 buf_T *buf;
15788{
15789 char_u *p = start;
15790
15791 while (*p != NUL && spell_iswordp(p, buf))
15792 mb_ptr_adv(p);
15793 return p;
15794}
15795
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015796#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015797/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015798 * For Insert mode completion CTRL-X s:
15799 * Find start of the word in front of column "startcol".
15800 * We don't check if it is badly spelled, with completion we can only change
15801 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015802 * Returns the column number of the word.
15803 */
15804 int
15805spell_word_start(startcol)
15806 int startcol;
15807{
15808 char_u *line;
15809 char_u *p;
15810 int col = 0;
15811
Bram Moolenaar95529562005-08-25 21:21:38 +000015812 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015813 return startcol;
15814
15815 /* Find a word character before "startcol". */
15816 line = ml_get_curline();
15817 for (p = line + startcol; p > line; )
15818 {
15819 mb_ptr_back(line, p);
15820 if (spell_iswordp_nmw(p))
15821 break;
15822 }
15823
15824 /* Go back to start of the word. */
15825 while (p > line)
15826 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015827 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015828 mb_ptr_back(line, p);
15829 if (!spell_iswordp(p, curbuf))
15830 break;
15831 col = 0;
15832 }
15833
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015834 return col;
15835}
15836
15837/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015838 * Need to check for 'spellcapcheck' now, the word is removed before
15839 * expand_spelling() is called. Therefore the ugly global variable.
15840 */
15841static int spell_expand_need_cap;
15842
15843 void
15844spell_expand_check_cap(col)
15845 colnr_T col;
15846{
15847 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15848}
15849
15850/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015851 * Get list of spelling suggestions.
15852 * Used for Insert mode completion CTRL-X ?.
15853 * Returns the number of matches. The matches are in "matchp[]", array of
15854 * allocated strings.
15855 */
15856/*ARGSUSED*/
15857 int
15858expand_spelling(lnum, col, pat, matchp)
15859 linenr_T lnum;
15860 int col;
15861 char_u *pat;
15862 char_u ***matchp;
15863{
15864 garray_T ga;
15865
Bram Moolenaar4770d092006-01-12 23:22:24 +000015866 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015867 *matchp = ga.ga_data;
15868 return ga.ga_len;
15869}
15870#endif
15871
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015872#endif /* FEAT_SPELL */